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.

111 lines
2.9 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. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. )
  10. type MemDbStore struct {
  11. tree *btree.BTree
  12. }
  13. type Entry struct {
  14. *filer2.Entry
  15. }
  16. func (a Entry) Less(b btree.Item) bool {
  17. return strings.Compare(string(a.FullPath), string(b.(Entry).FullPath)) < 0
  18. }
  19. func NewMemDbStore() (filer *MemDbStore) {
  20. filer = &MemDbStore{}
  21. filer.tree = btree.New(8)
  22. return
  23. }
  24. func (filer *MemDbStore) InsertEntry(entry *filer2.Entry) (err error) {
  25. // println("inserting", entry.FullPath)
  26. filer.tree.ReplaceOrInsert(Entry{entry})
  27. return nil
  28. }
  29. func (filer *MemDbStore) AppendFileChunk(fullpath filer2.FullPath, fileChunks []*filer_pb.FileChunk) (err error) {
  30. found, entry, err := filer.FindEntry(fullpath)
  31. if !found {
  32. return fmt.Errorf("No such file: %s", fullpath)
  33. }
  34. entry.Chunks = append(entry.Chunks, fileChunks...)
  35. entry.Mtime = time.Now()
  36. println("appending to entry", entry.Name(), len(entry.Chunks))
  37. filer.tree.ReplaceOrInsert(Entry{entry})
  38. return nil
  39. }
  40. func (filer *MemDbStore) FindEntry(fullpath filer2.FullPath) (found bool, entry *filer2.Entry, err error) {
  41. item := filer.tree.Get(Entry{&filer2.Entry{FullPath: fullpath}})
  42. if item == nil {
  43. return false, nil, nil
  44. }
  45. entry = item.(Entry).Entry
  46. return true, entry, nil
  47. }
  48. func (filer *MemDbStore) DeleteEntry(fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
  49. item := filer.tree.Delete(Entry{&filer2.Entry{FullPath: fullpath}})
  50. if item == nil {
  51. return nil, nil
  52. }
  53. entry = item.(Entry).Entry
  54. return entry, nil
  55. }
  56. func (filer *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. filer.tree.AscendGreaterOrEqual(Entry{&filer2.Entry{FullPath: filer2.FullPath(startFrom)}},
  62. func(item btree.Item) bool {
  63. if limit <= 0 {
  64. return false
  65. }
  66. entry := item.(Entry).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. if !strings.HasPrefix(dir, string(fullpath)) {
  82. // println("directory is:", dir, "fullpath:", 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. }