You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.1 KiB

3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/filer"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. "io"
  8. "sort"
  9. "sync"
  10. )
  11. type FileHandleId uint64
  12. type FileHandle struct {
  13. fh FileHandleId
  14. counter int64
  15. entry *filer_pb.Entry
  16. chunkAddLock sync.Mutex
  17. inode uint64
  18. wfs *WFS
  19. // cache file has been written to
  20. dirtyMetadata bool
  21. dirtyPages *PageWriter
  22. entryViewCache []filer.VisibleInterval
  23. reader io.ReaderAt
  24. contentType string
  25. handle uint64
  26. sync.Mutex
  27. isDeleted bool
  28. }
  29. func newFileHandle(wfs *WFS, handleId FileHandleId, inode uint64, entry *filer_pb.Entry) *FileHandle {
  30. fh := &FileHandle{
  31. fh: handleId,
  32. counter: 1,
  33. inode: inode,
  34. wfs: wfs,
  35. }
  36. // dirtyPages: newContinuousDirtyPages(file, writeOnly),
  37. fh.dirtyPages = newPageWriter(fh, wfs.option.ChunkSizeLimit)
  38. if entry != nil {
  39. entry.Attributes.FileSize = filer.FileSize(entry)
  40. }
  41. return fh
  42. }
  43. func (fh *FileHandle) FullPath() util.FullPath {
  44. return fh.wfs.inodeToPath.GetPath(fh.inode)
  45. }
  46. func (fh *FileHandle) addChunks(chunks []*filer_pb.FileChunk) {
  47. // find the earliest incoming chunk
  48. newChunks := chunks
  49. earliestChunk := newChunks[0]
  50. for i := 1; i < len(newChunks); i++ {
  51. if lessThan(earliestChunk, newChunks[i]) {
  52. earliestChunk = newChunks[i]
  53. }
  54. }
  55. if fh.entry == nil {
  56. return
  57. }
  58. // pick out-of-order chunks from existing chunks
  59. for _, chunk := range fh.entry.Chunks {
  60. if lessThan(earliestChunk, chunk) {
  61. chunks = append(chunks, chunk)
  62. }
  63. }
  64. // sort incoming chunks
  65. sort.Slice(chunks, func(i, j int) bool {
  66. return lessThan(chunks[i], chunks[j])
  67. })
  68. glog.V(4).Infof("%s existing %d chunks adds %d more", fh.FullPath(), len(fh.entry.Chunks), len(chunks))
  69. fh.chunkAddLock.Lock()
  70. fh.entry.Chunks = append(fh.entry.Chunks, newChunks...)
  71. fh.entryViewCache = nil
  72. fh.chunkAddLock.Unlock()
  73. }
  74. func lessThan(a, b *filer_pb.FileChunk) bool {
  75. if a.Mtime == b.Mtime {
  76. return a.Fid.FileKey < b.Fid.FileKey
  77. }
  78. return a.Mtime < b.Mtime
  79. }