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.

93 lines
2.2 KiB

5 years ago
  1. package meta_cache
  2. import (
  3. "context"
  4. "os"
  5. "sync"
  6. "github.com/chrislusf/seaweedfs/weed/filer2"
  7. "github.com/chrislusf/seaweedfs/weed/filer2/leveldb"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. type MetaCache struct {
  12. actualStore filer2.FilerStore
  13. sync.RWMutex
  14. }
  15. func NewMetaCache(dbFolder string) *MetaCache {
  16. return &MetaCache{
  17. actualStore: openMetaStore(dbFolder),
  18. }
  19. }
  20. func openMetaStore(dbFolder string) filer2.FilerStore {
  21. os.RemoveAll(dbFolder)
  22. os.MkdirAll(dbFolder, 0755)
  23. store := &leveldb.LevelDBStore{}
  24. config := &cacheConfig{
  25. dir: dbFolder,
  26. }
  27. if err := store.Initialize(config, ""); err != nil {
  28. glog.Fatalf("Failed to initialize metadata cache store for %s: %+v", store.GetName(), err)
  29. }
  30. return store
  31. }
  32. func (mc *MetaCache) InsertEntry(ctx context.Context, entry *filer2.Entry) error {
  33. mc.Lock()
  34. defer mc.Unlock()
  35. return mc.actualStore.InsertEntry(ctx, entry)
  36. }
  37. func (mc *MetaCache) AtomicUpdateEntry(ctx context.Context, oldPath util.FullPath, newEntry *filer2.Entry) error {
  38. mc.Lock()
  39. defer mc.Unlock()
  40. if oldPath != "" {
  41. if err := mc.actualStore.DeleteEntry(ctx, oldPath); err != nil {
  42. return err
  43. }
  44. }
  45. if newEntry != nil {
  46. if err := mc.actualStore.InsertEntry(ctx, newEntry); err != nil {
  47. return err
  48. }
  49. }
  50. return nil
  51. }
  52. func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer2.Entry) error {
  53. mc.Lock()
  54. defer mc.Unlock()
  55. return mc.actualStore.UpdateEntry(ctx, entry)
  56. }
  57. func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer2.Entry, err error) {
  58. mc.RLock()
  59. defer mc.RUnlock()
  60. return mc.actualStore.FindEntry(ctx, fp)
  61. }
  62. func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  63. mc.Lock()
  64. defer mc.Unlock()
  65. return mc.actualStore.DeleteEntry(ctx, fp)
  66. }
  67. func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int) ([]*filer2.Entry, error) {
  68. mc.RLock()
  69. defer mc.RUnlock()
  70. return mc.actualStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit)
  71. }
  72. func (mc *MetaCache) Shutdown() {
  73. mc.Lock()
  74. defer mc.Unlock()
  75. mc.actualStore.Shutdown()
  76. }