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.

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