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.

146 lines
3.9 KiB

4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package meta_cache
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/chrislusf/seaweedfs/weed/filer/leveldb"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/chrislusf/seaweedfs/weed/util/bounded_tree"
  10. "os"
  11. )
  12. // need to have logic similar to FilerStoreWrapper
  13. // e.g. fill fileId field for chunks
  14. type MetaCache struct {
  15. localStore filer.VirtualFilerStore
  16. // sync.RWMutex
  17. visitedBoundary *bounded_tree.BoundedTree
  18. uidGidMapper *UidGidMapper
  19. invalidateFunc func(util.FullPath)
  20. }
  21. func NewMetaCache(dbFolder string, baseDir util.FullPath, uidGidMapper *UidGidMapper, invalidateFunc func(util.FullPath)) *MetaCache {
  22. return &MetaCache{
  23. localStore: openMetaStore(dbFolder),
  24. visitedBoundary: bounded_tree.NewBoundedTree(baseDir),
  25. uidGidMapper: uidGidMapper,
  26. invalidateFunc: func(fullpath util.FullPath) {
  27. invalidateFunc(fullpath)
  28. },
  29. }
  30. }
  31. func openMetaStore(dbFolder string) filer.VirtualFilerStore {
  32. os.RemoveAll(dbFolder)
  33. os.MkdirAll(dbFolder, 0755)
  34. store := &leveldb.LevelDBStore{}
  35. config := &cacheConfig{
  36. dir: dbFolder,
  37. }
  38. if err := store.Initialize(config, ""); err != nil {
  39. glog.Fatalf("Failed to initialize metadata cache store for %s: %+v", store.GetName(), err)
  40. }
  41. return filer.NewFilerStoreWrapper(store)
  42. }
  43. func (mc *MetaCache) InsertEntry(ctx context.Context, entry *filer.Entry) error {
  44. //mc.Lock()
  45. //defer mc.Unlock()
  46. return mc.doInsertEntry(ctx, entry)
  47. }
  48. func (mc *MetaCache) doInsertEntry(ctx context.Context, entry *filer.Entry) error {
  49. return mc.localStore.InsertEntry(ctx, entry)
  50. }
  51. func (mc *MetaCache) AtomicUpdateEntryFromFiler(ctx context.Context, oldPath util.FullPath, newEntry *filer.Entry) error {
  52. //mc.Lock()
  53. //defer mc.Unlock()
  54. oldDir, _ := oldPath.DirAndName()
  55. if mc.visitedBoundary.HasVisited(util.FullPath(oldDir)) {
  56. if oldPath != "" {
  57. if newEntry != nil && oldPath == newEntry.FullPath {
  58. // skip the unnecessary deletion
  59. // leave the update to the following InsertEntry operation
  60. } else {
  61. glog.V(3).Infof("DeleteEntry %s", oldPath)
  62. if err := mc.localStore.DeleteEntry(ctx, oldPath); err != nil {
  63. return err
  64. }
  65. }
  66. }
  67. } else {
  68. // println("unknown old directory:", oldDir)
  69. }
  70. if newEntry != nil {
  71. newDir, _ := newEntry.DirAndName()
  72. if mc.visitedBoundary.HasVisited(util.FullPath(newDir)) {
  73. glog.V(3).Infof("InsertEntry %s/%s", newDir, newEntry.Name())
  74. if err := mc.localStore.InsertEntry(ctx, newEntry); err != nil {
  75. return err
  76. }
  77. }
  78. }
  79. return nil
  80. }
  81. func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer.Entry) error {
  82. //mc.Lock()
  83. //defer mc.Unlock()
  84. return mc.localStore.UpdateEntry(ctx, entry)
  85. }
  86. func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer.Entry, err error) {
  87. //mc.RLock()
  88. //defer mc.RUnlock()
  89. entry, err = mc.localStore.FindEntry(ctx, fp)
  90. if err != nil {
  91. return nil, err
  92. }
  93. mc.mapIdFromFilerToLocal(entry)
  94. return
  95. }
  96. func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  97. //mc.Lock()
  98. //defer mc.Unlock()
  99. return mc.localStore.DeleteEntry(ctx, fp)
  100. }
  101. func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) error {
  102. //mc.RLock()
  103. //defer mc.RUnlock()
  104. if !mc.visitedBoundary.HasVisited(dirPath) {
  105. return fmt.Errorf("unsynchronized dir: %v", dirPath)
  106. }
  107. _, err := mc.localStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, func(entry *filer.Entry) bool {
  108. mc.mapIdFromFilerToLocal(entry)
  109. return eachEntryFunc(entry)
  110. })
  111. if err != nil {
  112. return err
  113. }
  114. return err
  115. }
  116. func (mc *MetaCache) Shutdown() {
  117. //mc.Lock()
  118. //defer mc.Unlock()
  119. mc.localStore.Shutdown()
  120. }
  121. func (mc *MetaCache) mapIdFromFilerToLocal(entry *filer.Entry) {
  122. entry.Attr.Uid, entry.Attr.Gid = mc.uidGidMapper.FilerToLocal(entry.Attr.Uid, entry.Attr.Gid)
  123. }