index_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mvcc
  15. import (
  16. "reflect"
  17. "testing"
  18. "github.com/google/btree"
  19. "go.uber.org/zap"
  20. )
  21. func TestIndexGet(t *testing.T) {
  22. ti := newTreeIndex(zap.NewExample())
  23. ti.Put([]byte("foo"), revision{main: 2})
  24. ti.Put([]byte("foo"), revision{main: 4})
  25. ti.Tombstone([]byte("foo"), revision{main: 6})
  26. tests := []struct {
  27. rev int64
  28. wrev revision
  29. wcreated revision
  30. wver int64
  31. werr error
  32. }{
  33. {0, revision{}, revision{}, 0, ErrRevisionNotFound},
  34. {1, revision{}, revision{}, 0, ErrRevisionNotFound},
  35. {2, revision{main: 2}, revision{main: 2}, 1, nil},
  36. {3, revision{main: 2}, revision{main: 2}, 1, nil},
  37. {4, revision{main: 4}, revision{main: 2}, 2, nil},
  38. {5, revision{main: 4}, revision{main: 2}, 2, nil},
  39. {6, revision{}, revision{}, 0, ErrRevisionNotFound},
  40. }
  41. for i, tt := range tests {
  42. rev, created, ver, err := ti.Get([]byte("foo"), tt.rev)
  43. if err != tt.werr {
  44. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  45. }
  46. if rev != tt.wrev {
  47. t.Errorf("#%d: rev = %+v, want %+v", i, rev, tt.wrev)
  48. }
  49. if created != tt.wcreated {
  50. t.Errorf("#%d: created = %+v, want %+v", i, created, tt.wcreated)
  51. }
  52. if ver != tt.wver {
  53. t.Errorf("#%d: ver = %d, want %d", i, ver, tt.wver)
  54. }
  55. }
  56. }
  57. func TestIndexRange(t *testing.T) {
  58. allKeys := [][]byte{[]byte("foo"), []byte("foo1"), []byte("foo2")}
  59. allRevs := []revision{{main: 1}, {main: 2}, {main: 3}}
  60. ti := newTreeIndex(zap.NewExample())
  61. for i := range allKeys {
  62. ti.Put(allKeys[i], allRevs[i])
  63. }
  64. atRev := int64(3)
  65. tests := []struct {
  66. key, end []byte
  67. wkeys [][]byte
  68. wrevs []revision
  69. }{
  70. // single key that not found
  71. {
  72. []byte("bar"), nil, nil, nil,
  73. },
  74. // single key that found
  75. {
  76. []byte("foo"), nil, allKeys[:1], allRevs[:1],
  77. },
  78. // range keys, return first member
  79. {
  80. []byte("foo"), []byte("foo1"), allKeys[:1], allRevs[:1],
  81. },
  82. // range keys, return first two members
  83. {
  84. []byte("foo"), []byte("foo2"), allKeys[:2], allRevs[:2],
  85. },
  86. // range keys, return all members
  87. {
  88. []byte("foo"), []byte("fop"), allKeys, allRevs,
  89. },
  90. // range keys, return last two members
  91. {
  92. []byte("foo1"), []byte("fop"), allKeys[1:], allRevs[1:],
  93. },
  94. // range keys, return last member
  95. {
  96. []byte("foo2"), []byte("fop"), allKeys[2:], allRevs[2:],
  97. },
  98. // range keys, return nothing
  99. {
  100. []byte("foo3"), []byte("fop"), nil, nil,
  101. },
  102. }
  103. for i, tt := range tests {
  104. keys, revs := ti.Range(tt.key, tt.end, atRev)
  105. if !reflect.DeepEqual(keys, tt.wkeys) {
  106. t.Errorf("#%d: keys = %+v, want %+v", i, keys, tt.wkeys)
  107. }
  108. if !reflect.DeepEqual(revs, tt.wrevs) {
  109. t.Errorf("#%d: revs = %+v, want %+v", i, revs, tt.wrevs)
  110. }
  111. }
  112. }
  113. func TestIndexTombstone(t *testing.T) {
  114. ti := newTreeIndex(zap.NewExample())
  115. ti.Put([]byte("foo"), revision{main: 1})
  116. err := ti.Tombstone([]byte("foo"), revision{main: 2})
  117. if err != nil {
  118. t.Errorf("tombstone error = %v, want nil", err)
  119. }
  120. _, _, _, err = ti.Get([]byte("foo"), 2)
  121. if err != ErrRevisionNotFound {
  122. t.Errorf("get error = %v, want nil", err)
  123. }
  124. err = ti.Tombstone([]byte("foo"), revision{main: 3})
  125. if err != ErrRevisionNotFound {
  126. t.Errorf("tombstone error = %v, want %v", err, ErrRevisionNotFound)
  127. }
  128. }
  129. func TestIndexRangeSince(t *testing.T) {
  130. allKeys := [][]byte{[]byte("foo"), []byte("foo1"), []byte("foo2"), []byte("foo2"), []byte("foo1"), []byte("foo")}
  131. allRevs := []revision{{main: 1}, {main: 2}, {main: 3}, {main: 4}, {main: 5}, {main: 6}}
  132. ti := newTreeIndex(zap.NewExample())
  133. for i := range allKeys {
  134. ti.Put(allKeys[i], allRevs[i])
  135. }
  136. atRev := int64(1)
  137. tests := []struct {
  138. key, end []byte
  139. wrevs []revision
  140. }{
  141. // single key that not found
  142. {
  143. []byte("bar"), nil, nil,
  144. },
  145. // single key that found
  146. {
  147. []byte("foo"), nil, []revision{{main: 1}, {main: 6}},
  148. },
  149. // range keys, return first member
  150. {
  151. []byte("foo"), []byte("foo1"), []revision{{main: 1}, {main: 6}},
  152. },
  153. // range keys, return first two members
  154. {
  155. []byte("foo"), []byte("foo2"), []revision{{main: 1}, {main: 2}, {main: 5}, {main: 6}},
  156. },
  157. // range keys, return all members
  158. {
  159. []byte("foo"), []byte("fop"), allRevs,
  160. },
  161. // range keys, return last two members
  162. {
  163. []byte("foo1"), []byte("fop"), []revision{{main: 2}, {main: 3}, {main: 4}, {main: 5}},
  164. },
  165. // range keys, return last member
  166. {
  167. []byte("foo2"), []byte("fop"), []revision{{main: 3}, {main: 4}},
  168. },
  169. // range keys, return nothing
  170. {
  171. []byte("foo3"), []byte("fop"), nil,
  172. },
  173. }
  174. for i, tt := range tests {
  175. revs := ti.RangeSince(tt.key, tt.end, atRev)
  176. if !reflect.DeepEqual(revs, tt.wrevs) {
  177. t.Errorf("#%d: revs = %+v, want %+v", i, revs, tt.wrevs)
  178. }
  179. }
  180. }
  181. func TestIndexCompactAndKeep(t *testing.T) {
  182. maxRev := int64(20)
  183. tests := []struct {
  184. key []byte
  185. remove bool
  186. rev revision
  187. created revision
  188. ver int64
  189. }{
  190. {[]byte("foo"), false, revision{main: 1}, revision{main: 1}, 1},
  191. {[]byte("foo1"), false, revision{main: 2}, revision{main: 2}, 1},
  192. {[]byte("foo2"), false, revision{main: 3}, revision{main: 3}, 1},
  193. {[]byte("foo2"), false, revision{main: 4}, revision{main: 3}, 2},
  194. {[]byte("foo"), false, revision{main: 5}, revision{main: 1}, 2},
  195. {[]byte("foo1"), false, revision{main: 6}, revision{main: 2}, 2},
  196. {[]byte("foo1"), true, revision{main: 7}, revision{}, 0},
  197. {[]byte("foo2"), true, revision{main: 8}, revision{}, 0},
  198. {[]byte("foo"), true, revision{main: 9}, revision{}, 0},
  199. {[]byte("foo"), false, revision{10, 0}, revision{10, 0}, 1},
  200. {[]byte("foo1"), false, revision{10, 1}, revision{10, 1}, 1},
  201. }
  202. // Continuous Compact and Keep
  203. ti := newTreeIndex(zap.NewExample())
  204. for _, tt := range tests {
  205. if tt.remove {
  206. ti.Tombstone(tt.key, tt.rev)
  207. } else {
  208. ti.Put(tt.key, tt.rev)
  209. }
  210. }
  211. for i := int64(1); i < maxRev; i++ {
  212. am := ti.Compact(i)
  213. keep := ti.Keep(i)
  214. if !(reflect.DeepEqual(am, keep)) {
  215. t.Errorf("#%d: compact keep %v != Keep keep %v", i, am, keep)
  216. }
  217. wti := &treeIndex{tree: btree.New(32)}
  218. for _, tt := range tests {
  219. if _, ok := am[tt.rev]; ok || tt.rev.GreaterThan(revision{main: i}) {
  220. if tt.remove {
  221. wti.Tombstone(tt.key, tt.rev)
  222. } else {
  223. restore(wti, tt.key, tt.created, tt.rev, tt.ver)
  224. }
  225. }
  226. }
  227. if !ti.Equal(wti) {
  228. t.Errorf("#%d: not equal ti", i)
  229. }
  230. }
  231. // Once Compact and Keep
  232. for i := int64(1); i < maxRev; i++ {
  233. ti := newTreeIndex(zap.NewExample())
  234. for _, tt := range tests {
  235. if tt.remove {
  236. ti.Tombstone(tt.key, tt.rev)
  237. } else {
  238. ti.Put(tt.key, tt.rev)
  239. }
  240. }
  241. am := ti.Compact(i)
  242. keep := ti.Keep(i)
  243. if !(reflect.DeepEqual(am, keep)) {
  244. t.Errorf("#%d: compact keep %v != Keep keep %v", i, am, keep)
  245. }
  246. wti := &treeIndex{tree: btree.New(32)}
  247. for _, tt := range tests {
  248. if _, ok := am[tt.rev]; ok || tt.rev.GreaterThan(revision{main: i}) {
  249. if tt.remove {
  250. wti.Tombstone(tt.key, tt.rev)
  251. } else {
  252. restore(wti, tt.key, tt.created, tt.rev, tt.ver)
  253. }
  254. }
  255. }
  256. if !ti.Equal(wti) {
  257. t.Errorf("#%d: not equal ti", i)
  258. }
  259. }
  260. }
  261. func restore(ti *treeIndex, key []byte, created, modified revision, ver int64) {
  262. keyi := &keyIndex{key: key}
  263. ti.Lock()
  264. defer ti.Unlock()
  265. item := ti.tree.Get(keyi)
  266. if item == nil {
  267. keyi.restore(ti.lg, created, modified, ver)
  268. ti.tree.ReplaceOrInsert(keyi)
  269. return
  270. }
  271. okeyi := item.(*keyIndex)
  272. okeyi.put(ti.lg, modified.main, modified.sub)
  273. }