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.

89 lines
2.3 KiB

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