rocksdb_store_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //go:build rocksdb
  2. // +build rocksdb
  3. package rocksdb
  4. import (
  5. "context"
  6. "fmt"
  7. "os"
  8. "testing"
  9. "time"
  10. "github.com/seaweedfs/seaweedfs/weed/filer"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. )
  13. func TestCreateAndFind(t *testing.T) {
  14. testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", 0, "", "", "", nil)
  15. dir := t.TempDir()
  16. store := &RocksDBStore{}
  17. store.initialize(dir)
  18. testFiler.SetStore(store)
  19. fullpath := util.FullPath("/home/chris/this/is/one/file1.jpg")
  20. ctx := context.Background()
  21. entry1 := &filer.Entry{
  22. FullPath: fullpath,
  23. Attr: filer.Attr{
  24. Mode: 0440,
  25. Uid: 1234,
  26. Gid: 5678,
  27. },
  28. }
  29. if err := testFiler.CreateEntry(ctx, entry1, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
  30. t.Errorf("create entry %v: %v", entry1.FullPath, err)
  31. return
  32. }
  33. entry, err := testFiler.FindEntry(ctx, fullpath)
  34. if err != nil {
  35. t.Errorf("find entry: %v", err)
  36. return
  37. }
  38. if entry.FullPath != entry1.FullPath {
  39. t.Errorf("find wrong entry: %v", entry.FullPath)
  40. return
  41. }
  42. // checking one upper directory
  43. entries, _, _ := testFiler.ListDirectoryEntries(ctx, util.FullPath("/home/chris/this/is/one"), "", false, 100, "", "", "")
  44. if len(entries) != 1 {
  45. t.Errorf("list entries count: %v", len(entries))
  46. return
  47. }
  48. // checking one upper directory
  49. entries, _, _ = testFiler.ListDirectoryEntries(ctx, util.FullPath("/"), "", false, 100, "", "", "")
  50. if len(entries) != 1 {
  51. t.Errorf("list entries count: %v", len(entries))
  52. return
  53. }
  54. }
  55. func TestEmptyRoot(t *testing.T) {
  56. testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", 0, "", "", "", nil)
  57. dir := t.TempDir()
  58. store := &RocksDBStore{}
  59. store.initialize(dir)
  60. testFiler.SetStore(store)
  61. ctx := context.Background()
  62. // checking one upper directory
  63. entries, _, err := testFiler.ListDirectoryEntries(ctx, util.FullPath("/"), "", false, 100, "", "", "")
  64. if err != nil {
  65. t.Errorf("list entries: %v", err)
  66. return
  67. }
  68. if len(entries) != 0 {
  69. t.Errorf("list entries count: %v", len(entries))
  70. return
  71. }
  72. }
  73. func BenchmarkInsertEntry(b *testing.B) {
  74. testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", 0, "", "", "", nil)
  75. dir := b.TempDir()
  76. store := &RocksDBStore{}
  77. store.initialize(dir)
  78. testFiler.SetStore(store)
  79. ctx := context.Background()
  80. b.ReportAllocs()
  81. for i := 0; i < b.N; i++ {
  82. entry := &filer.Entry{
  83. FullPath: util.FullPath(fmt.Sprintf("/file%d.txt", i)),
  84. Attr: filer.Attr{
  85. Crtime: time.Now(),
  86. Mtime: time.Now(),
  87. Mode: os.FileMode(0644),
  88. },
  89. }
  90. store.InsertEntry(ctx, entry)
  91. }
  92. }