seaweedfs/weed/filesys/meta_cache/meta_cache.go

126 lines
3.1 KiB
Go
Raw Normal View History

package meta_cache
import (
2020-04-30 09:20:54 +08:00
"context"
"os"
2020-04-30 09:20:54 +08:00
"sync"
"github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/filer2/leveldb"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2020-04-30 09:20:54 +08:00
"github.com/chrislusf/seaweedfs/weed/util"
2020-06-20 00:45:27 +08:00
"github.com/chrislusf/seaweedfs/weed/util/bounded_tree"
)
// need to have logic similar to FilerStoreWrapper
// e.g. fill fileId field for chunks
type MetaCache struct {
2020-04-30 09:20:54 +08:00
actualStore filer2.FilerStore
sync.RWMutex
2020-06-20 00:45:27 +08:00
visitedBoundary *bounded_tree.BoundedTree
}
func NewMetaCache(dbFolder string) *MetaCache {
return &MetaCache{
2020-06-20 00:45:27 +08:00
actualStore: openMetaStore(dbFolder),
visitedBoundary: bounded_tree.NewBoundedTree(),
}
}
func openMetaStore(dbFolder string) filer2.FilerStore {
2020-04-22 13:00:34 +08:00
os.RemoveAll(dbFolder)
os.MkdirAll(dbFolder, 0755)
store := &leveldb.LevelDBStore{}
2020-04-22 13:00:34 +08:00
config := &cacheConfig{
dir: dbFolder,
}
if err := store.Initialize(config, ""); err != nil {
glog.Fatalf("Failed to initialize metadata cache store for %s: %+v", store.GetName(), err)
}
return store
2020-04-30 04:26:02 +08:00
}
2020-04-30 09:20:54 +08:00
func (mc *MetaCache) InsertEntry(ctx context.Context, entry *filer2.Entry) error {
mc.Lock()
defer mc.Unlock()
filer_pb.BeforeEntrySerialization(entry.Chunks)
2020-04-30 09:20:54 +08:00
return mc.actualStore.InsertEntry(ctx, entry)
}
func (mc *MetaCache) AtomicUpdateEntry(ctx context.Context, oldPath util.FullPath, newEntry *filer2.Entry) error {
mc.Lock()
defer mc.Unlock()
2020-06-20 00:45:27 +08:00
oldDir, _ := oldPath.DirAndName()
if mc.visitedBoundary.HasVisited(util.FullPath(oldDir)) {
if oldPath != "" {
if err := mc.actualStore.DeleteEntry(ctx, oldPath); err != nil {
return err
}
2020-04-30 09:20:54 +08:00
}
2020-07-14 22:34:16 +08:00
} else {
2020-06-20 00:45:27 +08:00
// println("unknown old directory:", oldDir)
2020-04-30 09:20:54 +08:00
}
2020-06-20 00:45:27 +08:00
2020-04-30 09:20:54 +08:00
if newEntry != nil {
2020-06-20 00:45:27 +08:00
newDir, _ := newEntry.DirAndName()
if mc.visitedBoundary.HasVisited(util.FullPath(newDir)) {
if err := mc.actualStore.InsertEntry(ctx, newEntry); err != nil {
return err
}
2020-04-30 09:20:54 +08:00
}
}
return nil
}
func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer2.Entry) error {
mc.Lock()
defer mc.Unlock()
filer_pb.BeforeEntrySerialization(entry.Chunks)
2020-04-30 09:20:54 +08:00
return mc.actualStore.UpdateEntry(ctx, entry)
}
func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer2.Entry, err error) {
mc.RLock()
defer mc.RUnlock()
entry, err = mc.actualStore.FindEntry(ctx, fp)
if err != nil {
return nil, err
}
filer_pb.AfterEntryDeserialization(entry.Chunks)
return
2020-04-30 09:20:54 +08:00
}
func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
mc.Lock()
defer mc.Unlock()
return mc.actualStore.DeleteEntry(ctx, fp)
}
func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int) ([]*filer2.Entry, error) {
mc.RLock()
defer mc.RUnlock()
entries, err := mc.actualStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit)
if err != nil {
return nil, err
}
for _, entry := range entries {
filer_pb.AfterEntryDeserialization(entry.Chunks)
}
return entries, err
2020-04-30 09:20:54 +08:00
}
func (mc *MetaCache) Shutdown() {
mc.Lock()
defer mc.Unlock()
mc.actualStore.Shutdown()
}