compact_map_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package needle_map
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/sequence"
  5. . "github.com/seaweedfs/seaweedfs/weed/storage/types"
  6. "testing"
  7. )
  8. func TestSnowflakeSequencer(t *testing.T) {
  9. m := NewCompactMap()
  10. seq, _ := sequence.NewSnowflakeSequencer("for_test", 1)
  11. for i := 0; i < 200000; i++ {
  12. id := seq.NextFileId(1)
  13. oldOffset, oldSize := m.Set(NeedleId(id), ToOffset(8), 3000073)
  14. if oldSize != 0 {
  15. t.Errorf("id %d oldOffset %v oldSize %d", id, oldOffset, oldSize)
  16. }
  17. }
  18. }
  19. func TestOverflow2(t *testing.T) {
  20. m := NewCompactMap()
  21. _, oldSize := m.Set(NeedleId(150088), ToOffset(8), 3000073)
  22. if oldSize != 0 {
  23. t.Fatalf("expecting no previous data")
  24. }
  25. _, oldSize = m.Set(NeedleId(150088), ToOffset(8), 3000073)
  26. if oldSize != 3000073 {
  27. t.Fatalf("expecting previous data size is %d, not %d", 3000073, oldSize)
  28. }
  29. m.Set(NeedleId(150073), ToOffset(8), 3000073)
  30. m.Set(NeedleId(150089), ToOffset(8), 3000073)
  31. m.Set(NeedleId(150076), ToOffset(8), 3000073)
  32. m.Set(NeedleId(150124), ToOffset(8), 3000073)
  33. m.Set(NeedleId(150137), ToOffset(8), 3000073)
  34. m.Set(NeedleId(150147), ToOffset(8), 3000073)
  35. m.Set(NeedleId(150145), ToOffset(8), 3000073)
  36. m.Set(NeedleId(150158), ToOffset(8), 3000073)
  37. m.Set(NeedleId(150162), ToOffset(8), 3000073)
  38. m.AscendingVisit(func(value NeedleValue) error {
  39. println("needle key:", value.Key)
  40. return nil
  41. })
  42. }
  43. func TestIssue52(t *testing.T) {
  44. m := NewCompactMap()
  45. m.Set(NeedleId(10002), ToOffset(10002), 10002)
  46. if element, ok := m.Get(NeedleId(10002)); ok {
  47. fmt.Printf("key %d ok %v %d, %v, %d\n", 10002, ok, element.Key, element.Offset, element.Size)
  48. }
  49. m.Set(NeedleId(10001), ToOffset(10001), 10001)
  50. if element, ok := m.Get(NeedleId(10002)); ok {
  51. fmt.Printf("key %d ok %v %d, %v, %d\n", 10002, ok, element.Key, element.Offset, element.Size)
  52. } else {
  53. t.Fatal("key 10002 missing after setting 10001")
  54. }
  55. }
  56. func TestCompactMap(t *testing.T) {
  57. m := NewCompactMap()
  58. for i := uint32(0); i < 100*batch; i += 2 {
  59. m.Set(NeedleId(i), ToOffset(int64(i)), Size(i))
  60. }
  61. for i := uint32(0); i < 100*batch; i += 37 {
  62. m.Delete(NeedleId(i))
  63. }
  64. for i := uint32(0); i < 10*batch; i += 3 {
  65. m.Set(NeedleId(i), ToOffset(int64(i+11)), Size(i+5))
  66. }
  67. // for i := uint32(0); i < 100; i++ {
  68. // if v := m.Get(Key(i)); v != nil {
  69. // glog.V(4).Infoln(i, "=", v.Key, v.Offset, v.Size)
  70. // }
  71. // }
  72. for i := uint32(0); i < 10*batch; i++ {
  73. v, ok := m.Get(NeedleId(i))
  74. if i%3 == 0 {
  75. if !ok {
  76. t.Fatal("key", i, "missing!")
  77. }
  78. if v.Size != Size(i+5) {
  79. t.Fatal("key", i, "size", v.Size)
  80. }
  81. } else if i%37 == 0 {
  82. if ok && v.Size.IsValid() {
  83. t.Fatal("key", i, "should have been deleted needle value", v)
  84. }
  85. } else if i%2 == 0 {
  86. if v.Size != Size(i) {
  87. t.Fatal("key", i, "size", v.Size)
  88. }
  89. }
  90. }
  91. for i := uint32(10 * batch); i < 100*batch; i++ {
  92. v, ok := m.Get(NeedleId(i))
  93. if i%37 == 0 {
  94. if ok && v.Size.IsValid() {
  95. t.Fatal("key", i, "should have been deleted needle value", v)
  96. }
  97. } else if i%2 == 0 {
  98. if v == nil {
  99. t.Fatal("key", i, "missing")
  100. }
  101. if v.Size != Size(i) {
  102. t.Fatal("key", i, "size", v.Size)
  103. }
  104. }
  105. }
  106. }
  107. func TestOverflow(t *testing.T) {
  108. cs := NewCompactSection(1)
  109. cs.setOverflowEntry(1, ToOffset(12), 12)
  110. cs.setOverflowEntry(2, ToOffset(12), 12)
  111. cs.setOverflowEntry(3, ToOffset(12), 12)
  112. cs.setOverflowEntry(4, ToOffset(12), 12)
  113. cs.setOverflowEntry(5, ToOffset(12), 12)
  114. if cs.overflow[2].Key != 3 {
  115. t.Fatalf("expecting o[2] has key 3: %+v", cs.overflow[2].Key)
  116. }
  117. cs.setOverflowEntry(3, ToOffset(24), 24)
  118. if cs.overflow[2].Key != 3 {
  119. t.Fatalf("expecting o[2] has key 3: %+v", cs.overflow[2].Key)
  120. }
  121. if cs.overflow[2].Size != 24 {
  122. t.Fatalf("expecting o[2] has size 24: %+v", cs.overflow[2].Size)
  123. }
  124. cs.deleteOverflowEntry(4)
  125. if len(cs.overflow) != 5 {
  126. t.Fatalf("expecting 5 entries now: %+v", cs.overflow)
  127. }
  128. _, x, _ := cs.findOverflowEntry(5)
  129. if x.Key != 5 {
  130. t.Fatalf("expecting entry 5 now: %+v", x)
  131. }
  132. for i, x := range cs.overflow {
  133. println("overflow[", i, "]:", x.Key)
  134. }
  135. println()
  136. cs.deleteOverflowEntry(1)
  137. for i, x := range cs.overflow {
  138. println("overflow[", i, "]:", x.Key, "size", x.Size)
  139. }
  140. println()
  141. cs.setOverflowEntry(4, ToOffset(44), 44)
  142. for i, x := range cs.overflow {
  143. println("overflow[", i, "]:", x.Key)
  144. }
  145. println()
  146. cs.setOverflowEntry(1, ToOffset(11), 11)
  147. for i, x := range cs.overflow {
  148. println("overflow[", i, "]:", x.Key)
  149. }
  150. println()
  151. }