index_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 TestIndexRange(t *testing.T) {
  28. atRev := int64(3)
  29. allKeys := [][]byte{[]byte("foo"), []byte("foo1"), []byte("foo2")}
  30. allRevs := []reversion{{main: 1}, {main: 2}, {main: 3}}
  31. tests := []struct {
  32. key, end []byte
  33. wkeys [][]byte
  34. wrevs []reversion
  35. }{
  36. // single key that not found
  37. {
  38. []byte("bar"), nil, nil, nil,
  39. },
  40. // single key that found
  41. {
  42. []byte("foo"), nil, allKeys[:1], allRevs[:1],
  43. },
  44. // range keys, return first member
  45. {
  46. []byte("foo"), []byte("foo1"), allKeys[:1], allRevs[:1],
  47. },
  48. // range keys, return first two members
  49. {
  50. []byte("foo"), []byte("foo2"), allKeys[:2], allRevs[:2],
  51. },
  52. // range keys, return all members
  53. {
  54. []byte("foo"), []byte("fop"), allKeys, allRevs,
  55. },
  56. // range keys, return last two members
  57. {
  58. []byte("foo1"), []byte("fop"), allKeys[1:], allRevs[1:],
  59. },
  60. // range keys, return last member
  61. {
  62. []byte("foo2"), []byte("fop"), allKeys[2:], allRevs[2:],
  63. },
  64. // range keys, return nothing
  65. {
  66. []byte("foo3"), []byte("fop"), nil, nil,
  67. },
  68. }
  69. for i, tt := range tests {
  70. index := newTestTreeIndex()
  71. keys, revs := index.Range(tt.key, tt.end, atRev)
  72. if !reflect.DeepEqual(keys, tt.wkeys) {
  73. t.Errorf("#%d: keys = %+v, want %+v", i, keys, tt.wkeys)
  74. }
  75. if !reflect.DeepEqual(revs, tt.wrevs) {
  76. t.Errorf("#%d: revs = %+v, want %+v", i, revs, tt.wrevs)
  77. }
  78. }
  79. }
  80. func TestIndexTombstone(t *testing.T) {
  81. index := newTestTreeIndex()
  82. err := index.Tombstone([]byte("foo"), reversion{main: 7})
  83. if err != nil {
  84. t.Errorf("tombstone error = %v, want nil", err)
  85. }
  86. rev, _, _, err := index.Get([]byte("foo"), 7)
  87. if err != nil {
  88. t.Errorf("get error = %v, want nil", err)
  89. }
  90. w := reversion{main: 7}
  91. if !reflect.DeepEqual(rev, w) {
  92. t.Errorf("get reversion = %+v, want %+v", rev, w)
  93. }
  94. }
  95. func TestContinuousCompact(t *testing.T) {
  96. index := newTestTreeIndex()
  97. tests := []T{
  98. {[]byte("foo"), 0, ErrReversionNotFound, 0},
  99. {[]byte("foo"), 1, nil, 1},
  100. {[]byte("foo"), 3, nil, 1},
  101. {[]byte("foo"), 5, nil, 5},
  102. {[]byte("foo"), 6, nil, 5},
  103. {[]byte("foo1"), 0, ErrReversionNotFound, 0},
  104. {[]byte("foo1"), 1, ErrReversionNotFound, 0},
  105. {[]byte("foo1"), 2, nil, 2},
  106. {[]byte("foo1"), 5, nil, 2},
  107. {[]byte("foo1"), 6, nil, 6},
  108. {[]byte("foo2"), 0, ErrReversionNotFound, 0},
  109. {[]byte("foo2"), 1, ErrReversionNotFound, 0},
  110. {[]byte("foo2"), 3, nil, 3},
  111. {[]byte("foo2"), 4, nil, 4},
  112. {[]byte("foo2"), 6, nil, 4},
  113. }
  114. wa := map[reversion]struct{}{
  115. reversion{main: 1}: struct{}{},
  116. }
  117. ga := index.Compact(1)
  118. if !reflect.DeepEqual(ga, wa) {
  119. t.Errorf("a = %v, want %v", ga, wa)
  120. }
  121. verify(t, index, tests)
  122. wa = map[reversion]struct{}{
  123. reversion{main: 1}: struct{}{},
  124. reversion{main: 2}: struct{}{},
  125. }
  126. ga = index.Compact(2)
  127. if !reflect.DeepEqual(ga, wa) {
  128. t.Errorf("a = %v, want %v", ga, wa)
  129. }
  130. verify(t, index, tests)
  131. wa = map[reversion]struct{}{
  132. reversion{main: 1}: struct{}{},
  133. reversion{main: 2}: struct{}{},
  134. reversion{main: 3}: struct{}{},
  135. }
  136. ga = index.Compact(3)
  137. if !reflect.DeepEqual(ga, wa) {
  138. t.Errorf("a = %v, want %v", ga, wa)
  139. }
  140. verify(t, index, tests)
  141. wa = map[reversion]struct{}{
  142. reversion{main: 1}: struct{}{},
  143. reversion{main: 2}: struct{}{},
  144. reversion{main: 4}: struct{}{},
  145. }
  146. ga = index.Compact(4)
  147. delete(wa, reversion{main: 3})
  148. tests[12] = T{[]byte("foo2"), 3, ErrReversionNotFound, 0}
  149. if !reflect.DeepEqual(wa, ga) {
  150. t.Errorf("a = %v, want %v", ga, wa)
  151. }
  152. verify(t, index, tests)
  153. wa = map[reversion]struct{}{
  154. reversion{main: 2}: struct{}{},
  155. reversion{main: 4}: struct{}{},
  156. reversion{main: 5}: struct{}{},
  157. }
  158. ga = index.Compact(5)
  159. delete(wa, reversion{main: 1})
  160. if !reflect.DeepEqual(ga, wa) {
  161. t.Errorf("a = %v, want %v", ga, wa)
  162. }
  163. tests[1] = T{[]byte("foo"), 1, ErrReversionNotFound, 0}
  164. tests[2] = T{[]byte("foo"), 3, ErrReversionNotFound, 0}
  165. verify(t, index, tests)
  166. wa = map[reversion]struct{}{
  167. reversion{main: 4}: struct{}{},
  168. reversion{main: 5}: struct{}{},
  169. reversion{main: 6}: struct{}{},
  170. }
  171. ga = index.Compact(6)
  172. delete(wa, reversion{main: 2})
  173. if !reflect.DeepEqual(ga, wa) {
  174. t.Errorf("a = %v, want %v", ga, wa)
  175. }
  176. tests[7] = T{[]byte("foo1"), 2, ErrReversionNotFound, 0}
  177. tests[8] = T{[]byte("foo1"), 5, ErrReversionNotFound, 0}
  178. verify(t, index, tests)
  179. }
  180. func verify(t *testing.T, index index, tests []T) {
  181. for i, tt := range tests {
  182. h, _, _, err := index.Get(tt.key, tt.rev)
  183. if err != tt.werr {
  184. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  185. }
  186. if h.main != tt.wrev {
  187. t.Errorf("#%d: rev = %d, want %d", i, h.main, tt.wrev)
  188. }
  189. }
  190. }
  191. type T struct {
  192. key []byte
  193. rev int64
  194. werr error
  195. wrev int64
  196. }
  197. func newTestTreeIndex() index {
  198. index := newTreeIndex()
  199. index.Put([]byte("foo"), reversion{main: 1})
  200. index.Put([]byte("foo1"), reversion{main: 2})
  201. index.Put([]byte("foo2"), reversion{main: 3})
  202. index.Put([]byte("foo2"), reversion{main: 4})
  203. index.Put([]byte("foo"), reversion{main: 5})
  204. index.Put([]byte("foo1"), reversion{main: 6})
  205. return index
  206. }