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.

132 lines
3.4 KiB

7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
  1. package memdb
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. "github.com/google/btree"
  8. "strings"
  9. "sync"
  10. )
  11. func init() {
  12. filer2.Stores = append(filer2.Stores, &MemDbStore{})
  13. }
  14. type MemDbStore struct {
  15. tree *btree.BTree
  16. treeLock sync.Mutex
  17. }
  18. type entryItem struct {
  19. *filer2.Entry
  20. }
  21. func (a entryItem) Less(b btree.Item) bool {
  22. return strings.Compare(string(a.FullPath), string(b.(entryItem).FullPath)) < 0
  23. }
  24. func (store *MemDbStore) GetName() string {
  25. return "memory"
  26. }
  27. func (store *MemDbStore) Initialize(configuration util.Configuration) (err error) {
  28. store.tree = btree.New(8)
  29. return nil
  30. }
  31. func (store *MemDbStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  32. return ctx, nil
  33. }
  34. func (store *MemDbStore) CommitTransaction(ctx context.Context) error {
  35. return nil
  36. }
  37. func (store *MemDbStore) RollbackTransaction(ctx context.Context) error {
  38. return nil
  39. }
  40. func (store *MemDbStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  41. // println("inserting", entry.FullPath)
  42. store.treeLock.Lock()
  43. store.tree.ReplaceOrInsert(entryItem{entry})
  44. store.treeLock.Unlock()
  45. return nil
  46. }
  47. func (store *MemDbStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  48. if _, err = store.FindEntry(ctx, entry.FullPath); err != nil {
  49. return fmt.Errorf("no such file %s : %v", entry.FullPath, err)
  50. }
  51. store.treeLock.Lock()
  52. store.tree.ReplaceOrInsert(entryItem{entry})
  53. store.treeLock.Unlock()
  54. return nil
  55. }
  56. func (store *MemDbStore) FindEntry(ctx context.Context, fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
  57. item := store.tree.Get(entryItem{&filer2.Entry{FullPath: fullpath}})
  58. if item == nil {
  59. return nil, filer2.ErrNotFound
  60. }
  61. entry = item.(entryItem).Entry
  62. return entry, nil
  63. }
  64. func (store *MemDbStore) DeleteEntry(ctx context.Context, fullpath filer2.FullPath) (err error) {
  65. store.treeLock.Lock()
  66. store.tree.Delete(entryItem{&filer2.Entry{FullPath: fullpath}})
  67. store.treeLock.Unlock()
  68. return nil
  69. }
  70. func (store *MemDbStore) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer2.Entry, err error) {
  71. startFrom := string(fullpath)
  72. if startFileName != "" {
  73. startFrom = startFrom + "/" + startFileName
  74. }
  75. store.tree.AscendGreaterOrEqual(entryItem{&filer2.Entry{FullPath: filer2.FullPath(startFrom)}},
  76. func(item btree.Item) bool {
  77. if limit <= 0 {
  78. return false
  79. }
  80. entry := item.(entryItem).Entry
  81. // println("checking", entry.FullPath)
  82. if entry.FullPath == fullpath {
  83. // skipping the current directory
  84. // println("skipping the folder", entry.FullPath)
  85. return true
  86. }
  87. dir, name := entry.FullPath.DirAndName()
  88. if name == startFileName {
  89. if inclusive {
  90. limit--
  91. entries = append(entries, entry)
  92. }
  93. return true
  94. }
  95. // only iterate the same prefix
  96. if !strings.HasPrefix(string(entry.FullPath), string(fullpath)) {
  97. // println("breaking from", entry.FullPath)
  98. return false
  99. }
  100. if dir != string(fullpath) {
  101. // this could be items in deeper directories
  102. // println("skipping deeper folder", entry.FullPath)
  103. return true
  104. }
  105. // now process the directory items
  106. // println("adding entry", entry.FullPath)
  107. limit--
  108. entries = append(entries, entry)
  109. return true
  110. },
  111. )
  112. return entries, nil
  113. }