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.

137 lines
3.5 KiB

4 years ago
5 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
4 years ago
  1. package meta_cache
  2. import (
  3. "context"
  4. "os"
  5. "sync"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/filer/leveldb"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "github.com/chrislusf/seaweedfs/weed/util/bounded_tree"
  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. }
  20. func NewMetaCache(dbFolder string, uidGidMapper *UidGidMapper) *MetaCache {
  21. return &MetaCache{
  22. localStore: openMetaStore(dbFolder),
  23. visitedBoundary: bounded_tree.NewBoundedTree(),
  24. uidGidMapper: uidGidMapper,
  25. }
  26. }
  27. func openMetaStore(dbFolder string) filer.VirtualFilerStore {
  28. os.RemoveAll(dbFolder)
  29. os.MkdirAll(dbFolder, 0755)
  30. store := &leveldb.LevelDBStore{}
  31. config := &cacheConfig{
  32. dir: dbFolder,
  33. }
  34. if err := store.Initialize(config, ""); err != nil {
  35. glog.Fatalf("Failed to initialize metadata cache store for %s: %+v", store.GetName(), err)
  36. }
  37. return filer.NewFilerStoreWrapper(store)
  38. }
  39. func (mc *MetaCache) InsertEntry(ctx context.Context, entry *filer.Entry) error {
  40. mc.Lock()
  41. defer mc.Unlock()
  42. return mc.doInsertEntry(ctx, entry)
  43. }
  44. func (mc *MetaCache) doInsertEntry(ctx context.Context, entry *filer.Entry) error {
  45. return mc.localStore.InsertEntry(ctx, entry)
  46. }
  47. func (mc *MetaCache) AtomicUpdateEntryFromFiler(ctx context.Context, oldPath util.FullPath, newEntry *filer.Entry) error {
  48. mc.Lock()
  49. defer mc.Unlock()
  50. oldDir, _ := oldPath.DirAndName()
  51. if mc.visitedBoundary.HasVisited(util.FullPath(oldDir)) {
  52. if oldPath != "" {
  53. if newEntry != nil && oldPath == newEntry.FullPath {
  54. // skip the unnecessary deletion
  55. // leave the update to the following InsertEntry operation
  56. } else {
  57. if err := mc.localStore.DeleteEntry(ctx, oldPath); err != nil {
  58. return err
  59. }
  60. }
  61. }
  62. } else {
  63. // println("unknown old directory:", oldDir)
  64. }
  65. if newEntry != nil {
  66. newDir, _ := newEntry.DirAndName()
  67. if mc.visitedBoundary.HasVisited(util.FullPath(newDir)) {
  68. if err := mc.localStore.InsertEntry(ctx, newEntry); err != nil {
  69. return err
  70. }
  71. }
  72. }
  73. return nil
  74. }
  75. func (mc *MetaCache) UpdateEntry(ctx context.Context, entry *filer.Entry) error {
  76. mc.Lock()
  77. defer mc.Unlock()
  78. return mc.localStore.UpdateEntry(ctx, entry)
  79. }
  80. func (mc *MetaCache) FindEntry(ctx context.Context, fp util.FullPath) (entry *filer.Entry, err error) {
  81. mc.RLock()
  82. defer mc.RUnlock()
  83. entry, err = mc.localStore.FindEntry(ctx, fp)
  84. if err != nil {
  85. return nil, err
  86. }
  87. mc.mapIdFromFilerToLocal(entry)
  88. return
  89. }
  90. func (mc *MetaCache) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  91. mc.Lock()
  92. defer mc.Unlock()
  93. return mc.localStore.DeleteEntry(ctx, fp)
  94. }
  95. func (mc *MetaCache) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int) ([]*filer.Entry, error) {
  96. mc.RLock()
  97. defer mc.RUnlock()
  98. entries, err := mc.localStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit)
  99. if err != nil {
  100. return nil, err
  101. }
  102. for _, entry := range entries {
  103. mc.mapIdFromFilerToLocal(entry)
  104. }
  105. return entries, err
  106. }
  107. func (mc *MetaCache) Shutdown() {
  108. mc.Lock()
  109. defer mc.Unlock()
  110. mc.localStore.Shutdown()
  111. }
  112. func (mc *MetaCache) mapIdFromFilerToLocal(entry *filer.Entry) {
  113. entry.Attr.Uid, entry.Attr.Gid = mc.uidGidMapper.FilerToLocal(entry.Attr.Uid, entry.Attr.Gid)
  114. }