diff --git a/weed/filer2/filechunks.go b/weed/filer2/filechunks.go index 0a53e4ecc..6c3157e6c 100644 --- a/weed/filer2/filechunks.go +++ b/weed/filer2/filechunks.go @@ -118,7 +118,7 @@ var bufPool = sync.Pool{ }, } -func mergeIntoVisibles(visibles, newVisibles []VisibleInterval, chunk *filer_pb.FileChunk) []VisibleInterval { +func MergeIntoVisibles(visibles, newVisibles []VisibleInterval, chunk *filer_pb.FileChunk) []VisibleInterval { newV := newVisibleInterval( chunk.Offset, @@ -185,12 +185,12 @@ func NonOverlappingVisibleIntervals(chunks []*filer_pb.FileChunk) (visibles []Vi return chunks[i].Mtime < chunks[j].Mtime }) - var newVislbles []VisibleInterval + var newVisibles []VisibleInterval for _, chunk := range chunks { - newVislbles = mergeIntoVisibles(visibles, newVislbles, chunk) + newVisibles = MergeIntoVisibles(visibles, newVisibles, chunk) t := visibles[:0] - visibles = newVislbles - newVislbles = t + visibles = newVisibles + newVisibles = t logPrintf("add", visibles) diff --git a/weed/filesys/file.go b/weed/filesys/file.go index 6c96ba4ed..4bb169a33 100644 --- a/weed/filesys/file.go +++ b/weed/filesys/file.go @@ -2,14 +2,16 @@ package filesys import ( "context" + "os" + "path/filepath" + "sort" + "time" + "github.com/chrislusf/seaweedfs/weed/filer2" "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/fuse" "github.com/seaweedfs/fuse/fs" - "os" - "path/filepath" - "time" ) const blockSize = 512 @@ -179,12 +181,20 @@ func (file *File) addChunk(chunk *filer_pb.FileChunk) { } func (file *File) addChunks(chunks []*filer_pb.FileChunk) { + + sort.Slice(chunks, func(i, j int) bool { + return chunks[i].Mtime < chunks[j].Mtime + }) + + var newVisibles []filer2.VisibleInterval for _, chunk := range chunks { - file.entry.Chunks = append(file.entry.Chunks, chunk) - file.entryViewCache = nil - glog.V(4).Infof("uploaded %s/%s to %s [%d,%d)", file.dir.Path, file.Name, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size)) + newVisibles = filer2.MergeIntoVisibles(file.entryViewCache, newVisibles, chunk) + t := file.entryViewCache[:0] + file.entryViewCache = newVisibles + newVisibles = t } - file.entryViewCache = filer2.NonOverlappingVisibleIntervals(file.entry.Chunks) + + file.entry.Chunks = append(file.entry.Chunks, chunks...) } func (file *File) setEntry(entry *filer_pb.Entry) {