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.

139 lines
3.2 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 TestIssue52(t *testing.T) {
  7. m := NewCompactMap()
  8. m.Set(NeedleId(10002), 10002, 10002)
  9. if element, ok := m.Get(NeedleId(10002)); ok {
  10. println("key", 10002, "ok", ok, element.Key, element.Offset, element.Size)
  11. }
  12. m.Set(NeedleId(10001), 10001, 10001)
  13. if element, ok := m.Get(NeedleId(10002)); ok {
  14. println("key", 10002, "ok", ok, element.Key, element.Offset, element.Size)
  15. } else {
  16. t.Fatal("key 10002 missing after setting 10001")
  17. }
  18. }
  19. func TestCompactMap(t *testing.T) {
  20. m := NewCompactMap()
  21. for i := uint32(0); i < 100*batch; i += 2 {
  22. m.Set(NeedleId(i), Offset(i), i)
  23. }
  24. for i := uint32(0); i < 100*batch; i += 37 {
  25. m.Delete(NeedleId(i))
  26. }
  27. for i := uint32(0); i < 10*batch; i += 3 {
  28. m.Set(NeedleId(i), Offset(i+11), i+5)
  29. }
  30. // for i := uint32(0); i < 100; i++ {
  31. // if v := m.Get(Key(i)); v != nil {
  32. // glog.V(4).Infoln(i, "=", v.Key, v.Offset, v.Size)
  33. // }
  34. // }
  35. for i := uint32(0); i < 10*batch; i++ {
  36. v, ok := m.Get(NeedleId(i))
  37. if i%3 == 0 {
  38. if !ok {
  39. t.Fatal("key", i, "missing!")
  40. }
  41. if v.Size != i+5 {
  42. t.Fatal("key", i, "size", v.Size)
  43. }
  44. } else if i%37 == 0 {
  45. if ok && v.Size > 0 {
  46. t.Fatal("key", i, "should have been deleted needle value", v)
  47. }
  48. } else if i%2 == 0 {
  49. if v.Size != i {
  50. t.Fatal("key", i, "size", v.Size)
  51. }
  52. }
  53. }
  54. for i := uint32(10 * batch); i < 100*batch; i++ {
  55. v, ok := m.Get(NeedleId(i))
  56. if i%37 == 0 {
  57. if ok && v.Size > 0 {
  58. t.Fatal("key", i, "should have been deleted needle value", v)
  59. }
  60. } else if i%2 == 0 {
  61. if v == nil {
  62. t.Fatal("key", i, "missing")
  63. }
  64. if v.Size != i {
  65. t.Fatal("key", i, "size", v.Size)
  66. }
  67. }
  68. }
  69. }
  70. func TestOverflow(t *testing.T) {
  71. o := Overflow(make([]SectionalNeedleValue, 0))
  72. o = o.setOverflowEntry(SectionalNeedleValue{Key: 1, Offset: 12, Size: 12})
  73. o = o.setOverflowEntry(SectionalNeedleValue{Key: 2, Offset: 12, Size: 12})
  74. o = o.setOverflowEntry(SectionalNeedleValue{Key: 3, Offset: 12, Size: 12})
  75. o = o.setOverflowEntry(SectionalNeedleValue{Key: 4, Offset: 12, Size: 12})
  76. o = o.setOverflowEntry(SectionalNeedleValue{Key: 5, Offset: 12, Size: 12})
  77. if o[2].Key != 3 {
  78. t.Fatalf("expecting o[2] has key 3: %+v", o[2].Key)
  79. }
  80. o = o.setOverflowEntry(SectionalNeedleValue{Key: 3, Offset: 24, Size: 24})
  81. if o[2].Key != 3 {
  82. t.Fatalf("expecting o[2] has key 3: %+v", o[2].Key)
  83. }
  84. if o[2].Size != 24 {
  85. t.Fatalf("expecting o[2] has size 24: %+v", o[2].Size)
  86. }
  87. o = o.deleteOverflowEntry(4)
  88. if len(o) != 4 {
  89. t.Fatalf("expecting 4 entries now: %+v", o)
  90. }
  91. x, _ := o.findOverflowEntry(5)
  92. if x.Key != 5 {
  93. t.Fatalf("expecting entry 5 now: %+v", x)
  94. }
  95. for i, x := range o {
  96. println("overflow[", i, "]:", x.Key)
  97. }
  98. println()
  99. o = o.deleteOverflowEntry(1)
  100. for i, x := range o {
  101. println("overflow[", i, "]:", x.Key)
  102. }
  103. println()
  104. o = o.setOverflowEntry(SectionalNeedleValue{Key: 4, Offset: 44, Size: 44})
  105. for i, x := range o {
  106. println("overflow[", i, "]:", x.Key)
  107. }
  108. println()
  109. o = o.setOverflowEntry(SectionalNeedleValue{Key: 1, Offset: 11, Size: 11})
  110. for i, x := range o {
  111. println("overflow[", i, "]:", x.Key)
  112. }
  113. println()
  114. }