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.

113 lines
2.8 KiB

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