compact_map_test.go 3.7 KB

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