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.

220 lines
5.2 KiB

6 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. package needle_map
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/sequence"
  5. . "github.com/seaweedfs/seaweedfs/weed/storage/types"
  6. "log"
  7. "os"
  8. "testing"
  9. )
  10. func TestSnowflakeSequencer(t *testing.T) {
  11. m := NewCompactMap()
  12. seq, _ := sequence.NewSnowflakeSequencer("for_test", 1)
  13. for i := 0; i < 200000; i++ {
  14. id := seq.NextFileId(1)
  15. oldOffset, oldSize := m.Set(NeedleId(id), ToOffset(8), 3000073)
  16. if oldSize != 0 {
  17. t.Errorf("id %d oldOffset %v oldSize %d", id, oldOffset, oldSize)
  18. }
  19. }
  20. }
  21. func TestOverflow2(t *testing.T) {
  22. m := NewCompactMap()
  23. _, oldSize := m.Set(NeedleId(150088), ToOffset(8), 3000073)
  24. if oldSize != 0 {
  25. t.Fatalf("expecting no previous data")
  26. }
  27. _, oldSize = m.Set(NeedleId(150088), ToOffset(8), 3000073)
  28. if oldSize != 3000073 {
  29. t.Fatalf("expecting previous data size is %d, not %d", 3000073, oldSize)
  30. }
  31. m.Set(NeedleId(150073), ToOffset(8), 3000073)
  32. m.Set(NeedleId(150089), ToOffset(8), 3000073)
  33. m.Set(NeedleId(150076), ToOffset(8), 3000073)
  34. m.Set(NeedleId(150124), ToOffset(8), 3000073)
  35. m.Set(NeedleId(150137), ToOffset(8), 3000073)
  36. m.Set(NeedleId(150147), ToOffset(8), 3000073)
  37. m.Set(NeedleId(150145), ToOffset(8), 3000073)
  38. m.Set(NeedleId(150158), ToOffset(8), 3000073)
  39. m.Set(NeedleId(150162), ToOffset(8), 3000073)
  40. m.AscendingVisit(func(value NeedleValue) error {
  41. println("needle key:", value.Key)
  42. return nil
  43. })
  44. }
  45. func TestIssue52(t *testing.T) {
  46. m := NewCompactMap()
  47. m.Set(NeedleId(10002), ToOffset(10002), 10002)
  48. if element, ok := m.Get(NeedleId(10002)); ok {
  49. fmt.Printf("key %d ok %v %d, %v, %d\n", 10002, ok, element.Key, element.Offset, element.Size)
  50. }
  51. m.Set(NeedleId(10001), ToOffset(10001), 10001)
  52. if element, ok := m.Get(NeedleId(10002)); ok {
  53. fmt.Printf("key %d ok %v %d, %v, %d\n", 10002, ok, element.Key, element.Offset, element.Size)
  54. } else {
  55. t.Fatal("key 10002 missing after setting 10001")
  56. }
  57. }
  58. func TestCompactMap(t *testing.T) {
  59. m := NewCompactMap()
  60. for i := uint32(0); i < 100*batch; i += 2 {
  61. m.Set(NeedleId(i), ToOffset(int64(i)), Size(i))
  62. }
  63. for i := uint32(0); i < 100*batch; i += 37 {
  64. m.Delete(NeedleId(i))
  65. }
  66. for i := uint32(0); i < 10*batch; i += 3 {
  67. m.Set(NeedleId(i), ToOffset(int64(i+11)), Size(i+5))
  68. }
  69. // for i := uint32(0); i < 100; i++ {
  70. // if v := m.Get(Key(i)); v != nil {
  71. // glog.V(4).Infoln(i, "=", v.Key, v.Offset, v.Size)
  72. // }
  73. // }
  74. for i := uint32(0); i < 10*batch; i++ {
  75. v, ok := m.Get(NeedleId(i))
  76. if i%3 == 0 {
  77. if !ok {
  78. t.Fatal("key", i, "missing!")
  79. }
  80. if v.Size != Size(i+5) {
  81. t.Fatal("key", i, "size", v.Size)
  82. }
  83. } else if i%37 == 0 {
  84. if ok && v.Size.IsValid() {
  85. t.Fatal("key", i, "should have been deleted needle value", v)
  86. }
  87. } else if i%2 == 0 {
  88. if v.Size != Size(i) {
  89. t.Fatal("key", i, "size", v.Size)
  90. }
  91. }
  92. }
  93. for i := uint32(10 * batch); i < 100*batch; i++ {
  94. v, ok := m.Get(NeedleId(i))
  95. if i%37 == 0 {
  96. if ok && v.Size.IsValid() {
  97. t.Fatal("key", i, "should have been deleted needle value", v)
  98. }
  99. } else if i%2 == 0 {
  100. if v == nil {
  101. t.Fatal("key", i, "missing")
  102. }
  103. if v.Size != Size(i) {
  104. t.Fatal("key", i, "size", v.Size)
  105. }
  106. }
  107. }
  108. }
  109. func TestOverflow(t *testing.T) {
  110. cs := NewCompactSection(1)
  111. cs.setOverflowEntry(1, ToOffset(12), 12)
  112. cs.setOverflowEntry(2, ToOffset(12), 12)
  113. cs.setOverflowEntry(3, ToOffset(12), 12)
  114. cs.setOverflowEntry(4, ToOffset(12), 12)
  115. cs.setOverflowEntry(5, ToOffset(12), 12)
  116. if cs.overflow[2].Key != 3 {
  117. t.Fatalf("expecting o[2] has key 3: %+v", cs.overflow[2].Key)
  118. }
  119. cs.setOverflowEntry(3, ToOffset(24), 24)
  120. if cs.overflow[2].Key != 3 {
  121. t.Fatalf("expecting o[2] has key 3: %+v", cs.overflow[2].Key)
  122. }
  123. if cs.overflow[2].Size != 24 {
  124. t.Fatalf("expecting o[2] has size 24: %+v", cs.overflow[2].Size)
  125. }
  126. cs.deleteOverflowEntry(4)
  127. if len(cs.overflow) != 5 {
  128. t.Fatalf("expecting 5 entries now: %+v", cs.overflow)
  129. }
  130. _, x, _ := cs.findOverflowEntry(5)
  131. if x.Key != 5 {
  132. t.Fatalf("expecting entry 5 now: %+v", x)
  133. }
  134. for i, x := range cs.overflow {
  135. println("overflow[", i, "]:", x.Key)
  136. }
  137. println()
  138. cs.deleteOverflowEntry(1)
  139. for i, x := range cs.overflow {
  140. println("overflow[", i, "]:", x.Key, "size", x.Size)
  141. }
  142. println()
  143. cs.setOverflowEntry(4, ToOffset(44), 44)
  144. for i, x := range cs.overflow {
  145. println("overflow[", i, "]:", x.Key)
  146. }
  147. println()
  148. cs.setOverflowEntry(1, ToOffset(11), 11)
  149. for i, x := range cs.overflow {
  150. println("overflow[", i, "]:", x.Key)
  151. }
  152. println()
  153. }
  154. func TestCompactSection_Get(t *testing.T) {
  155. var maps []*CompactMap
  156. totalRowCount := uint64(0)
  157. indexFile, ie := os.OpenFile("../../../test/data/sample.idx",
  158. os.O_RDWR|os.O_RDONLY, 0644)
  159. defer indexFile.Close()
  160. if ie != nil {
  161. log.Fatalln(ie)
  162. }
  163. m, rowCount := loadNewNeedleMap(indexFile)
  164. maps = append(maps, m)
  165. totalRowCount += rowCount
  166. m.Set(1574318345753513987, ToOffset(10002), 10002)
  167. nv, ok := m.Get(1574318345753513987)
  168. if ok {
  169. t.Log(uint64(nv.Key))
  170. }
  171. nv1, ok := m.Get(1574318350048481283)
  172. if ok {
  173. t.Error(uint64(nv1.Key))
  174. }
  175. m.Set(1574318350048481283, ToOffset(10002), 10002)
  176. nv2, ok1 := m.Get(1574318350048481283)
  177. if ok1 {
  178. t.Log(uint64(nv2.Key))
  179. }
  180. m.Delete(nv2.Key)
  181. nv3, has := m.Get(nv2.Key)
  182. if has && nv3.Size > 0 {
  183. t.Error(uint64(nv3.Size))
  184. }
  185. }