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.

117 lines
2.8 KiB

7 years ago
7 years ago
  1. package memdb
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/filer2"
  5. "github.com/google/btree"
  6. "github.com/spf13/viper"
  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(viper *viper.Viper) (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, nil
  44. }
  45. entry = item.(entryItem).Entry
  46. return entry, nil
  47. }
  48. func (store *MemDbStore) DeleteEntry(fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
  49. item := store.tree.Delete(entryItem{&filer2.Entry{FullPath: fullpath}})
  50. if item == nil {
  51. return nil, nil
  52. }
  53. entry = item.(entryItem).Entry
  54. return entry, nil
  55. }
  56. func (store *MemDbStore) ListDirectoryEntries(fullpath filer2.FullPath, startFileName string, inclusive bool, limit int) (entries []*filer2.Entry, err error) {
  57. startFrom := string(fullpath)
  58. if startFileName != "" {
  59. startFrom = startFrom + "/" + startFileName
  60. }
  61. store.tree.AscendGreaterOrEqual(entryItem{&filer2.Entry{FullPath: filer2.FullPath(startFrom)}},
  62. func(item btree.Item) bool {
  63. if limit <= 0 {
  64. return false
  65. }
  66. entry := item.(entryItem).Entry
  67. // println("checking", entry.FullPath)
  68. if entry.FullPath == fullpath {
  69. // skipping the current directory
  70. // println("skipping the folder", entry.FullPath)
  71. return true
  72. }
  73. dir, name := entry.FullPath.DirAndName()
  74. if name == startFileName {
  75. if inclusive {
  76. limit--
  77. entries = append(entries, entry)
  78. }
  79. return true
  80. }
  81. // only iterate the same prefix
  82. if !strings.HasPrefix(string(entry.FullPath), string(fullpath)) {
  83. // println("breaking from", entry.FullPath)
  84. return false
  85. }
  86. if dir != string(fullpath) {
  87. // this could be items in deeper directories
  88. // println("skipping deeper folder", entry.FullPath)
  89. return true
  90. }
  91. // now process the directory items
  92. // println("adding entry", entry.FullPath)
  93. limit--
  94. entries = append(entries, entry)
  95. return true
  96. },
  97. )
  98. return entries, nil
  99. }