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.

158 lines
3.7 KiB

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