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.

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