compact_map_test.go 4.0 KB

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