index_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package storage
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestIndexPutAndGet(t *testing.T) {
  7. index := newTestTreeIndex()
  8. tests := []T{
  9. {[]byte("foo"), 0, ErrReversionNotFound, 0},
  10. {[]byte("foo"), 1, nil, 1},
  11. {[]byte("foo"), 3, nil, 1},
  12. {[]byte("foo"), 5, nil, 5},
  13. {[]byte("foo"), 6, nil, 5},
  14. {[]byte("foo1"), 0, ErrReversionNotFound, 0},
  15. {[]byte("foo1"), 1, ErrReversionNotFound, 0},
  16. {[]byte("foo1"), 2, nil, 2},
  17. {[]byte("foo1"), 5, nil, 2},
  18. {[]byte("foo1"), 6, nil, 6},
  19. {[]byte("foo2"), 0, ErrReversionNotFound, 0},
  20. {[]byte("foo2"), 1, ErrReversionNotFound, 0},
  21. {[]byte("foo2"), 3, nil, 3},
  22. {[]byte("foo2"), 4, nil, 4},
  23. {[]byte("foo2"), 6, nil, 4},
  24. }
  25. verify(t, index, tests)
  26. }
  27. func TestContinuousCompact(t *testing.T) {
  28. index := newTestTreeIndex()
  29. tests := []T{
  30. {[]byte("foo"), 0, ErrReversionNotFound, 0},
  31. {[]byte("foo"), 1, nil, 1},
  32. {[]byte("foo"), 3, nil, 1},
  33. {[]byte("foo"), 5, nil, 5},
  34. {[]byte("foo"), 6, nil, 5},
  35. {[]byte("foo1"), 0, ErrReversionNotFound, 0},
  36. {[]byte("foo1"), 1, ErrReversionNotFound, 0},
  37. {[]byte("foo1"), 2, nil, 2},
  38. {[]byte("foo1"), 5, nil, 2},
  39. {[]byte("foo1"), 6, nil, 6},
  40. {[]byte("foo2"), 0, ErrReversionNotFound, 0},
  41. {[]byte("foo2"), 1, ErrReversionNotFound, 0},
  42. {[]byte("foo2"), 3, nil, 3},
  43. {[]byte("foo2"), 4, nil, 4},
  44. {[]byte("foo2"), 6, nil, 4},
  45. }
  46. wa := map[reversion]struct{}{
  47. reversion{main: 1}: struct{}{},
  48. }
  49. ga := index.Compact(1)
  50. if !reflect.DeepEqual(ga, wa) {
  51. t.Errorf("a = %v, want %v", ga, wa)
  52. }
  53. verify(t, index, tests)
  54. wa = map[reversion]struct{}{
  55. reversion{main: 1}: struct{}{},
  56. reversion{main: 2}: struct{}{},
  57. }
  58. ga = index.Compact(2)
  59. if !reflect.DeepEqual(ga, wa) {
  60. t.Errorf("a = %v, want %v", ga, wa)
  61. }
  62. verify(t, index, tests)
  63. wa = map[reversion]struct{}{
  64. reversion{main: 1}: struct{}{},
  65. reversion{main: 2}: struct{}{},
  66. reversion{main: 3}: struct{}{},
  67. }
  68. ga = index.Compact(3)
  69. if !reflect.DeepEqual(ga, wa) {
  70. t.Errorf("a = %v, want %v", ga, wa)
  71. }
  72. verify(t, index, tests)
  73. wa = map[reversion]struct{}{
  74. reversion{main: 1}: struct{}{},
  75. reversion{main: 2}: struct{}{},
  76. reversion{main: 4}: struct{}{},
  77. }
  78. ga = index.Compact(4)
  79. delete(wa, reversion{main: 3})
  80. tests[12] = T{[]byte("foo2"), 3, ErrReversionNotFound, 0}
  81. if !reflect.DeepEqual(wa, ga) {
  82. t.Errorf("a = %v, want %v", ga, wa)
  83. }
  84. verify(t, index, tests)
  85. wa = map[reversion]struct{}{
  86. reversion{main: 2}: struct{}{},
  87. reversion{main: 4}: struct{}{},
  88. reversion{main: 5}: struct{}{},
  89. }
  90. ga = index.Compact(5)
  91. delete(wa, reversion{main: 1})
  92. if !reflect.DeepEqual(ga, wa) {
  93. t.Errorf("a = %v, want %v", ga, wa)
  94. }
  95. tests[1] = T{[]byte("foo"), 1, ErrReversionNotFound, 0}
  96. tests[2] = T{[]byte("foo"), 3, ErrReversionNotFound, 0}
  97. verify(t, index, tests)
  98. wa = map[reversion]struct{}{
  99. reversion{main: 4}: struct{}{},
  100. reversion{main: 5}: struct{}{},
  101. reversion{main: 6}: struct{}{},
  102. }
  103. ga = index.Compact(6)
  104. delete(wa, reversion{main: 2})
  105. if !reflect.DeepEqual(ga, wa) {
  106. t.Errorf("a = %v, want %v", ga, wa)
  107. }
  108. tests[7] = T{[]byte("foo1"), 2, ErrReversionNotFound, 0}
  109. tests[8] = T{[]byte("foo1"), 5, ErrReversionNotFound, 0}
  110. verify(t, index, tests)
  111. }
  112. func verify(t *testing.T, index index, tests []T) {
  113. for i, tt := range tests {
  114. h, err := index.Get(tt.key, tt.rev)
  115. if err != tt.werr {
  116. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  117. }
  118. if h.main != tt.wrev {
  119. t.Errorf("#%d: rev = %d, want %d", i, h.main, tt.wrev)
  120. }
  121. }
  122. }
  123. type T struct {
  124. key []byte
  125. rev int64
  126. werr error
  127. wrev int64
  128. }
  129. func newTestTreeIndex() index {
  130. index := newTreeIndex()
  131. index.Put([]byte("foo"), reversion{main: 1})
  132. index.Put([]byte("foo1"), reversion{main: 2})
  133. index.Put([]byte("foo2"), reversion{main: 3})
  134. index.Put([]byte("foo2"), reversion{main: 4})
  135. index.Put([]byte("foo"), reversion{main: 5})
  136. index.Put([]byte("foo1"), reversion{main: 6})
  137. return index
  138. }