rocksdb_store_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/pb"
  12. "github.com/seaweedfs/seaweedfs/weed/util"
  13. )
  14. func TestCreateAndFind(t *testing.T) {
  15. testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", 0, "", "", "", nil)
  16. dir := t.TempDir()
  17. store := &RocksDBStore{}
  18. store.initialize(dir)
  19. testFiler.SetStore(store)
  20. fullpath := util.FullPath("/home/chris/this/is/one/file1.jpg")
  21. ctx := context.Background()
  22. entry1 := &filer.Entry{
  23. FullPath: fullpath,
  24. Attr: filer.Attr{
  25. Mode: 0440,
  26. Uid: 1234,
  27. Gid: 5678,
  28. },
  29. }
  30. if err := testFiler.CreateEntry(ctx, entry1, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
  31. t.Errorf("create entry %v: %v", entry1.FullPath, err)
  32. return
  33. }
  34. entry, err := testFiler.FindEntry(ctx, fullpath)
  35. if err != nil {
  36. t.Errorf("find entry: %v", err)
  37. return
  38. }
  39. if entry.FullPath != entry1.FullPath {
  40. t.Errorf("find wrong entry: %v", entry.FullPath)
  41. return
  42. }
  43. // checking one upper directory
  44. entries, _, _ := testFiler.ListDirectoryEntries(ctx, util.FullPath("/home/chris/this/is/one"), "", false, 100, "", "", "")
  45. if len(entries) != 1 {
  46. t.Errorf("list entries count: %v", len(entries))
  47. return
  48. }
  49. // checking one upper directory
  50. entries, _, _ = testFiler.ListDirectoryEntries(ctx, util.FullPath("/"), "", false, 100, "", "", "")
  51. if len(entries) != 1 {
  52. t.Errorf("list entries count: %v", len(entries))
  53. return
  54. }
  55. }
  56. func TestEmptyRoot(t *testing.T) {
  57. testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", 0, "", "", "", nil)
  58. dir := t.TempDir()
  59. store := &RocksDBStore{}
  60. store.initialize(dir)
  61. testFiler.SetStore(store)
  62. ctx := context.Background()
  63. // checking one upper directory
  64. entries, _, err := testFiler.ListDirectoryEntries(ctx, util.FullPath("/"), "", false, 100, "", "", "")
  65. if err != nil {
  66. t.Errorf("list entries: %v", err)
  67. return
  68. }
  69. if len(entries) != 0 {
  70. t.Errorf("list entries count: %v", len(entries))
  71. return
  72. }
  73. }
  74. func BenchmarkInsertEntry(b *testing.B) {
  75. testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", 0, "", "", "", nil)
  76. dir := b.TempDir()
  77. store := &RocksDBStore{}
  78. store.initialize(dir)
  79. testFiler.SetStore(store)
  80. ctx := context.Background()
  81. b.ReportAllocs()
  82. for i := 0; i < b.N; i++ {
  83. entry := &filer.Entry{
  84. FullPath: util.FullPath(fmt.Sprintf("/file%d.txt", i)),
  85. Attr: filer.Attr{
  86. Crtime: time.Now(),
  87. Mtime: time.Now(),
  88. Mode: os.FileMode(0644),
  89. },
  90. }
  91. store.InsertEntry(ctx, entry)
  92. }
  93. }
  94. func TestListDirectoryWithPrefix(t *testing.T) {
  95. testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
  96. dir := t.TempDir()
  97. store := &RocksDBStore{}
  98. store.initialize(dir)
  99. testFiler.SetStore(store)
  100. ctx := context.Background()
  101. files := []string{
  102. "/bucket1/test-prefix1/file1.txt",
  103. "/bucket1/test-prefix1/file2.txt",
  104. "/bucket1/test-prefix1-extra.txt",
  105. }
  106. expected1 := []string{
  107. "/bucket1/test-prefix1",
  108. "/bucket1/test-prefix1-extra.txt",
  109. }
  110. expected2 := []string{
  111. "/bucket1/test-prefix1/file1.txt",
  112. "/bucket1/test-prefix1/file2.txt",
  113. }
  114. for _, file := range files {
  115. fullpath := util.FullPath(file)
  116. entry := &filer.Entry{
  117. FullPath: fullpath,
  118. Attr: filer.Attr{
  119. Mode: 0644,
  120. Uid: 1,
  121. Gid: 1,
  122. },
  123. }
  124. if err := testFiler.CreateEntry(ctx, entry, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
  125. t.Fatalf("Failed to create entry %s: %v", fullpath, err)
  126. }
  127. }
  128. prefix1 := "test-prefix1"
  129. entries1, _, err := testFiler.ListDirectoryEntries(ctx, util.FullPath("/bucket1"), "", false, 100, prefix1, "", "")
  130. if err != nil {
  131. t.Fatalf("Failed to list entries with prefix %s: %v", prefix1, err)
  132. }
  133. if len(entries1) != 2 {
  134. t.Errorf("Expected 2 entries with prefix %s, got %d", prefix1, len(entries1))
  135. } else {
  136. t.Logf("Found %d entries with prefix %s", len(entries1), prefix1)
  137. }
  138. for i, entry := range entries1 {
  139. if string(entry.FullPath) != expected1[i] {
  140. t.Errorf("Expected entry %s, got %s", expected1[i], entry.FullPath)
  141. }
  142. }
  143. entries2, _, err := testFiler.ListDirectoryEntries(ctx, util.FullPath("/bucket1/test-prefix1"), "", false, 100, "", "", "")
  144. if err != nil {
  145. t.Fatalf("Failed to list entries with prefix %s: %v", prefix1, err)
  146. }
  147. if len(entries2) != 2 {
  148. t.Errorf("Expected 2 entries with prefix %s, got %d", prefix1, len(entries1))
  149. }
  150. for i, entry := range entries2 {
  151. if string(entry.FullPath) != expected2[i] {
  152. t.Errorf("Expected entry %s, got %s", expected2[i], entry.FullPath)
  153. }
  154. }
  155. }