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.

108 lines
2.7 KiB

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