index_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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, ErrRevisionNotFound, 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, ErrRevisionNotFound, 0},
  15. {[]byte("foo1"), 1, ErrRevisionNotFound, 0},
  16. {[]byte("foo1"), 2, nil, 2},
  17. {[]byte("foo1"), 5, nil, 2},
  18. {[]byte("foo1"), 6, nil, 6},
  19. {[]byte("foo2"), 0, ErrRevisionNotFound, 0},
  20. {[]byte("foo2"), 1, ErrRevisionNotFound, 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 := []revision{{main: 1}, {main: 2}, {main: 3}}
  31. tests := []struct {
  32. key, end []byte
  33. wkeys [][]byte
  34. wrevs []revision
  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"), revision{main: 7})
  83. if err != nil {
  84. t.Errorf("tombstone error = %v, want nil", err)
  85. }
  86. _, _, _, err = index.Get([]byte("foo"), 7)
  87. if err != ErrRevisionNotFound {
  88. t.Errorf("get error = %v, want nil", err)
  89. }
  90. }
  91. func TestContinuousCompact(t *testing.T) {
  92. index := newTestTreeIndex()
  93. tests := []T{
  94. {[]byte("foo"), 0, ErrRevisionNotFound, 0},
  95. {[]byte("foo"), 1, nil, 1},
  96. {[]byte("foo"), 3, nil, 1},
  97. {[]byte("foo"), 5, nil, 5},
  98. {[]byte("foo"), 6, nil, 5},
  99. {[]byte("foo1"), 0, ErrRevisionNotFound, 0},
  100. {[]byte("foo1"), 1, ErrRevisionNotFound, 0},
  101. {[]byte("foo1"), 2, nil, 2},
  102. {[]byte("foo1"), 5, nil, 2},
  103. {[]byte("foo1"), 6, nil, 6},
  104. {[]byte("foo2"), 0, ErrRevisionNotFound, 0},
  105. {[]byte("foo2"), 1, ErrRevisionNotFound, 0},
  106. {[]byte("foo2"), 3, nil, 3},
  107. {[]byte("foo2"), 4, nil, 4},
  108. {[]byte("foo2"), 6, nil, 4},
  109. }
  110. wa := map[revision]struct{}{
  111. revision{main: 1}: {},
  112. }
  113. ga := index.Compact(1)
  114. if !reflect.DeepEqual(ga, wa) {
  115. t.Errorf("a = %v, want %v", ga, wa)
  116. }
  117. verify(t, index, tests)
  118. wa = map[revision]struct{}{
  119. revision{main: 1}: {},
  120. revision{main: 2}: {},
  121. }
  122. ga = index.Compact(2)
  123. if !reflect.DeepEqual(ga, wa) {
  124. t.Errorf("a = %v, want %v", ga, wa)
  125. }
  126. verify(t, index, tests)
  127. wa = map[revision]struct{}{
  128. revision{main: 1}: {},
  129. revision{main: 2}: {},
  130. revision{main: 3}: {},
  131. }
  132. ga = index.Compact(3)
  133. if !reflect.DeepEqual(ga, wa) {
  134. t.Errorf("a = %v, want %v", ga, wa)
  135. }
  136. verify(t, index, tests)
  137. wa = map[revision]struct{}{
  138. revision{main: 1}: {},
  139. revision{main: 2}: {},
  140. revision{main: 4}: {},
  141. }
  142. ga = index.Compact(4)
  143. delete(wa, revision{main: 3})
  144. tests[12] = T{[]byte("foo2"), 3, ErrRevisionNotFound, 0}
  145. if !reflect.DeepEqual(wa, ga) {
  146. t.Errorf("a = %v, want %v", ga, wa)
  147. }
  148. verify(t, index, tests)
  149. wa = map[revision]struct{}{
  150. revision{main: 2}: {},
  151. revision{main: 4}: {},
  152. revision{main: 5}: {},
  153. }
  154. ga = index.Compact(5)
  155. delete(wa, revision{main: 1})
  156. if !reflect.DeepEqual(ga, wa) {
  157. t.Errorf("a = %v, want %v", ga, wa)
  158. }
  159. tests[1] = T{[]byte("foo"), 1, ErrRevisionNotFound, 0}
  160. tests[2] = T{[]byte("foo"), 3, ErrRevisionNotFound, 0}
  161. verify(t, index, tests)
  162. wa = map[revision]struct{}{
  163. revision{main: 4}: {},
  164. revision{main: 5}: {},
  165. revision{main: 6}: {},
  166. }
  167. ga = index.Compact(6)
  168. delete(wa, revision{main: 2})
  169. if !reflect.DeepEqual(ga, wa) {
  170. t.Errorf("a = %v, want %v", ga, wa)
  171. }
  172. tests[7] = T{[]byte("foo1"), 2, ErrRevisionNotFound, 0}
  173. tests[8] = T{[]byte("foo1"), 5, ErrRevisionNotFound, 0}
  174. verify(t, index, tests)
  175. }
  176. func verify(t *testing.T, index index, tests []T) {
  177. for i, tt := range tests {
  178. h, _, _, err := index.Get(tt.key, tt.rev)
  179. if err != tt.werr {
  180. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  181. }
  182. if h.main != tt.wrev {
  183. t.Errorf("#%d: rev = %d, want %d", i, h.main, tt.wrev)
  184. }
  185. }
  186. }
  187. type T struct {
  188. key []byte
  189. rev int64
  190. werr error
  191. wrev int64
  192. }
  193. func newTestTreeIndex() index {
  194. index := newTreeIndex()
  195. index.Put([]byte("foo"), revision{main: 1})
  196. index.Put([]byte("foo1"), revision{main: 2})
  197. index.Put([]byte("foo2"), revision{main: 3})
  198. index.Put([]byte("foo2"), revision{main: 4})
  199. index.Put([]byte("foo"), revision{main: 5})
  200. index.Put([]byte("foo1"), revision{main: 6})
  201. return index
  202. }