update also delete old chunks if different from latest chunks

This commit is contained in:
Chris Lu 2018-07-21 17:40:00 -07:00
parent 7e2031b18f
commit 6f30a78a6c

View file

@ -4,12 +4,13 @@ import (
"fmt" "fmt"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/operation"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/karlseguin/ccache" "github.com/karlseguin/ccache"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/chrislusf/seaweedfs/weed/operation"
) )
type Filer struct { type Filer struct {
@ -112,7 +113,7 @@ func (f *Filer) CreateEntry(entry *Entry) error {
return fmt.Errorf("insert entry %s: %v", entry.FullPath, err) return fmt.Errorf("insert entry %s: %v", entry.FullPath, err)
} }
f.deleteChunks(oldEntry) f.deleteChunksIfNotNew(oldEntry, entry)
return nil return nil
} }
@ -149,7 +150,7 @@ func (f *Filer) DeleteEntryMetaAndData(p FullPath, isRecursive bool, shouldDelet
} }
if shouldDeleteChunks { if shouldDeleteChunks {
f.deleteChunks(entry) f.deleteChunks(entry.Chunks)
} }
return f.store.DeleteEntry(p) return f.store.DeleteEntry(p)
@ -157,7 +158,7 @@ func (f *Filer) DeleteEntryMetaAndData(p FullPath, isRecursive bool, shouldDelet
func (f *Filer) ListDirectoryEntries(p FullPath, startFileName string, inclusive bool, limit int) ([]*Entry, error) { func (f *Filer) ListDirectoryEntries(p FullPath, startFileName string, inclusive bool, limit int) ([]*Entry, error) {
if strings.HasSuffix(string(p), "/") && len(p) > 1 { if strings.HasSuffix(string(p), "/") && len(p) > 1 {
p = p[0: len(p)-1] p = p[0 : len(p)-1]
} }
return f.store.ListDirectoryEntries(p, startFileName, inclusive, limit) return f.store.ListDirectoryEntries(p, startFileName, inclusive, limit)
} }
@ -195,14 +196,36 @@ func (f *Filer) cacheSetDirectory(dirpath string, dirEntry *Entry, level int) {
f.directoryCache.Set(dirpath, dirEntry, time.Duration(minutes)*time.Minute) f.directoryCache.Set(dirpath, dirEntry, time.Duration(minutes)*time.Minute)
} }
func (f *Filer) deleteChunks(entry *Entry) { func (f *Filer) deleteChunks(chunks []*filer_pb.FileChunk) {
for _, chunk := range chunks {
if entry == nil {
return
}
for _, chunk := range entry.Chunks {
if err := operation.DeleteFile(f.GetMaster(), chunk.FileId, ""); err != nil { if err := operation.DeleteFile(f.GetMaster(), chunk.FileId, ""); err != nil {
glog.V(0).Infof("deleting file %s: %v", chunk.FileId, err) glog.V(0).Infof("deleting file %s: %v", chunk.FileId, err)
} }
} }
} }
func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
if oldEntry == nil {
return
}
if newEntry == nil {
f.deleteChunks(oldEntry.Chunks)
}
var toDelete []*filer_pb.FileChunk
for _, oldChunk := range oldEntry.Chunks {
found := false
for _, newChunk := range newEntry.Chunks {
if oldChunk.FileId == newChunk.FileId {
found = true
break
}
}
if !found {
toDelete = append(toDelete, oldChunk)
}
}
f.deleteChunks(toDelete)
}