key_index_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. )
  19. func TestKeyIndexGet(t *testing.T) {
  20. // key: "foo"
  21. // rev: 16
  22. // generations:
  23. // {empty}
  24. // {{14, 0}[1], {14, 1}[2], {16, 0}(t)[3]}
  25. // {{8, 0}[1], {10, 0}[2], {12, 0}(t)[3]}
  26. // {{2, 0}[1], {4, 0}[2], {6, 0}(t)[3]}
  27. ki := newTestKeyIndex()
  28. ki.compact(4, make(map[revision]struct{}))
  29. tests := []struct {
  30. rev int64
  31. wmod revision
  32. wcreat revision
  33. wver int64
  34. werr error
  35. }{
  36. {17, revision{}, revision{}, 0, ErrRevisionNotFound},
  37. {16, revision{}, revision{}, 0, ErrRevisionNotFound},
  38. // get on generation 3
  39. {15, revision{14, 1}, revision{14, 0}, 2, nil},
  40. {14, revision{14, 1}, revision{14, 0}, 2, nil},
  41. {13, revision{}, revision{}, 0, ErrRevisionNotFound},
  42. {12, revision{}, revision{}, 0, ErrRevisionNotFound},
  43. // get on generation 2
  44. {11, revision{10, 0}, revision{8, 0}, 2, nil},
  45. {10, revision{10, 0}, revision{8, 0}, 2, nil},
  46. {9, revision{8, 0}, revision{8, 0}, 1, nil},
  47. {8, revision{8, 0}, revision{8, 0}, 1, nil},
  48. {7, revision{}, revision{}, 0, ErrRevisionNotFound},
  49. {6, revision{}, revision{}, 0, ErrRevisionNotFound},
  50. // get on generation 1
  51. {5, revision{4, 0}, revision{2, 0}, 2, nil},
  52. {4, revision{4, 0}, revision{2, 0}, 2, nil},
  53. {3, revision{}, revision{}, 0, ErrRevisionNotFound},
  54. {2, revision{}, revision{}, 0, ErrRevisionNotFound},
  55. {1, revision{}, revision{}, 0, ErrRevisionNotFound},
  56. {0, revision{}, revision{}, 0, ErrRevisionNotFound},
  57. }
  58. for i, tt := range tests {
  59. mod, creat, ver, err := ki.get(tt.rev)
  60. if err != tt.werr {
  61. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  62. }
  63. if mod != tt.wmod {
  64. t.Errorf("#%d: modified = %+v, want %+v", i, mod, tt.wmod)
  65. }
  66. if creat != tt.wcreat {
  67. t.Errorf("#%d: created = %+v, want %+v", i, creat, tt.wcreat)
  68. }
  69. if ver != tt.wver {
  70. t.Errorf("#%d: version = %d, want %d", i, ver, tt.wver)
  71. }
  72. }
  73. }
  74. func TestKeyIndexSince(t *testing.T) {
  75. ki := newTestKeyIndex()
  76. ki.compact(4, make(map[revision]struct{}))
  77. allRevs := []revision{{4, 0}, {6, 0}, {8, 0}, {10, 0}, {12, 0}, {14, 1}, {16, 0}}
  78. tests := []struct {
  79. rev int64
  80. wrevs []revision
  81. }{
  82. {17, nil},
  83. {16, allRevs[6:]},
  84. {15, allRevs[6:]},
  85. {14, allRevs[5:]},
  86. {13, allRevs[5:]},
  87. {12, allRevs[4:]},
  88. {11, allRevs[4:]},
  89. {10, allRevs[3:]},
  90. {9, allRevs[3:]},
  91. {8, allRevs[2:]},
  92. {7, allRevs[2:]},
  93. {6, allRevs[1:]},
  94. {5, allRevs[1:]},
  95. {4, allRevs},
  96. {3, allRevs},
  97. {2, allRevs},
  98. {1, allRevs},
  99. {0, allRevs},
  100. }
  101. for i, tt := range tests {
  102. revs := ki.since(tt.rev)
  103. if !reflect.DeepEqual(revs, tt.wrevs) {
  104. t.Errorf("#%d: revs = %+v, want %+v", i, revs, tt.wrevs)
  105. }
  106. }
  107. }
  108. func TestKeyIndexPut(t *testing.T) {
  109. ki := &keyIndex{key: []byte("foo")}
  110. ki.put(5, 0)
  111. wki := &keyIndex{
  112. key: []byte("foo"),
  113. modified: revision{5, 0},
  114. generations: []generation{{created: revision{5, 0}, ver: 1, revs: []revision{{main: 5}}}},
  115. }
  116. if !reflect.DeepEqual(ki, wki) {
  117. t.Errorf("ki = %+v, want %+v", ki, wki)
  118. }
  119. ki.put(7, 0)
  120. wki = &keyIndex{
  121. key: []byte("foo"),
  122. modified: revision{7, 0},
  123. generations: []generation{{created: revision{5, 0}, ver: 2, revs: []revision{{main: 5}, {main: 7}}}},
  124. }
  125. if !reflect.DeepEqual(ki, wki) {
  126. t.Errorf("ki = %+v, want %+v", ki, wki)
  127. }
  128. }
  129. func TestKeyIndexRestore(t *testing.T) {
  130. ki := &keyIndex{key: []byte("foo")}
  131. ki.restore(revision{5, 0}, revision{7, 0}, 2)
  132. wki := &keyIndex{
  133. key: []byte("foo"),
  134. modified: revision{7, 0},
  135. generations: []generation{{created: revision{5, 0}, ver: 2, revs: []revision{{main: 7}}}},
  136. }
  137. if !reflect.DeepEqual(ki, wki) {
  138. t.Errorf("ki = %+v, want %+v", ki, wki)
  139. }
  140. }
  141. func TestKeyIndexTombstone(t *testing.T) {
  142. ki := &keyIndex{key: []byte("foo")}
  143. ki.put(5, 0)
  144. err := ki.tombstone(7, 0)
  145. if err != nil {
  146. t.Errorf("unexpected tombstone error: %v", err)
  147. }
  148. wki := &keyIndex{
  149. key: []byte("foo"),
  150. modified: revision{7, 0},
  151. generations: []generation{{created: revision{5, 0}, ver: 2, revs: []revision{{main: 5}, {main: 7}}}, {}},
  152. }
  153. if !reflect.DeepEqual(ki, wki) {
  154. t.Errorf("ki = %+v, want %+v", ki, wki)
  155. }
  156. ki.put(8, 0)
  157. ki.put(9, 0)
  158. err = ki.tombstone(15, 0)
  159. if err != nil {
  160. t.Errorf("unexpected tombstone error: %v", err)
  161. }
  162. wki = &keyIndex{
  163. key: []byte("foo"),
  164. modified: revision{15, 0},
  165. generations: []generation{
  166. {created: revision{5, 0}, ver: 2, revs: []revision{{main: 5}, {main: 7}}},
  167. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 9}, {main: 15}}},
  168. {},
  169. },
  170. }
  171. if !reflect.DeepEqual(ki, wki) {
  172. t.Errorf("ki = %+v, want %+v", ki, wki)
  173. }
  174. err = ki.tombstone(16, 0)
  175. if err != ErrRevisionNotFound {
  176. t.Errorf("tombstone error = %v, want %v", err, ErrRevisionNotFound)
  177. }
  178. }
  179. func TestKeyIndexCompactAndKeep(t *testing.T) {
  180. tests := []struct {
  181. compact int64
  182. wki *keyIndex
  183. wam map[revision]struct{}
  184. }{
  185. {
  186. 1,
  187. &keyIndex{
  188. key: []byte("foo"),
  189. modified: revision{16, 0},
  190. generations: []generation{
  191. {created: revision{2, 0}, ver: 3, revs: []revision{{main: 2}, {main: 4}, {main: 6}}},
  192. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
  193. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  194. {},
  195. },
  196. },
  197. map[revision]struct{}{},
  198. },
  199. {
  200. 2,
  201. &keyIndex{
  202. key: []byte("foo"),
  203. modified: revision{16, 0},
  204. generations: []generation{
  205. {created: revision{2, 0}, ver: 3, revs: []revision{{main: 2}, {main: 4}, {main: 6}}},
  206. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
  207. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  208. {},
  209. },
  210. },
  211. map[revision]struct{}{
  212. {main: 2}: {},
  213. },
  214. },
  215. {
  216. 3,
  217. &keyIndex{
  218. key: []byte("foo"),
  219. modified: revision{16, 0},
  220. generations: []generation{
  221. {created: revision{2, 0}, ver: 3, revs: []revision{{main: 2}, {main: 4}, {main: 6}}},
  222. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
  223. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  224. {},
  225. },
  226. },
  227. map[revision]struct{}{
  228. {main: 2}: {},
  229. },
  230. },
  231. {
  232. 4,
  233. &keyIndex{
  234. key: []byte("foo"),
  235. modified: revision{16, 0},
  236. generations: []generation{
  237. {created: revision{2, 0}, ver: 3, revs: []revision{{main: 4}, {main: 6}}},
  238. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
  239. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  240. {},
  241. },
  242. },
  243. map[revision]struct{}{
  244. {main: 4}: {},
  245. },
  246. },
  247. {
  248. 5,
  249. &keyIndex{
  250. key: []byte("foo"),
  251. modified: revision{16, 0},
  252. generations: []generation{
  253. {created: revision{2, 0}, ver: 3, revs: []revision{{main: 4}, {main: 6}}},
  254. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
  255. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  256. {},
  257. },
  258. },
  259. map[revision]struct{}{
  260. {main: 4}: {},
  261. },
  262. },
  263. {
  264. 6,
  265. &keyIndex{
  266. key: []byte("foo"),
  267. modified: revision{16, 0},
  268. generations: []generation{
  269. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
  270. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  271. {},
  272. },
  273. },
  274. map[revision]struct{}{},
  275. },
  276. {
  277. 7,
  278. &keyIndex{
  279. key: []byte("foo"),
  280. modified: revision{16, 0},
  281. generations: []generation{
  282. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
  283. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  284. {},
  285. },
  286. },
  287. map[revision]struct{}{},
  288. },
  289. {
  290. 8,
  291. &keyIndex{
  292. key: []byte("foo"),
  293. modified: revision{16, 0},
  294. generations: []generation{
  295. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
  296. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  297. {},
  298. },
  299. },
  300. map[revision]struct{}{
  301. {main: 8}: {},
  302. },
  303. },
  304. {
  305. 9,
  306. &keyIndex{
  307. key: []byte("foo"),
  308. modified: revision{16, 0},
  309. generations: []generation{
  310. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
  311. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  312. {},
  313. },
  314. },
  315. map[revision]struct{}{
  316. {main: 8}: {},
  317. },
  318. },
  319. {
  320. 10,
  321. &keyIndex{
  322. key: []byte("foo"),
  323. modified: revision{16, 0},
  324. generations: []generation{
  325. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 10}, {main: 12}}},
  326. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  327. {},
  328. },
  329. },
  330. map[revision]struct{}{
  331. {main: 10}: {},
  332. },
  333. },
  334. {
  335. 11,
  336. &keyIndex{
  337. key: []byte("foo"),
  338. modified: revision{16, 0},
  339. generations: []generation{
  340. {created: revision{8, 0}, ver: 3, revs: []revision{{main: 10}, {main: 12}}},
  341. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  342. {},
  343. },
  344. },
  345. map[revision]struct{}{
  346. {main: 10}: {},
  347. },
  348. },
  349. {
  350. 12,
  351. &keyIndex{
  352. key: []byte("foo"),
  353. modified: revision{16, 0},
  354. generations: []generation{
  355. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  356. {},
  357. },
  358. },
  359. map[revision]struct{}{},
  360. },
  361. {
  362. 13,
  363. &keyIndex{
  364. key: []byte("foo"),
  365. modified: revision{16, 0},
  366. generations: []generation{
  367. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
  368. {},
  369. },
  370. },
  371. map[revision]struct{}{},
  372. },
  373. {
  374. 14,
  375. &keyIndex{
  376. key: []byte("foo"),
  377. modified: revision{16, 0},
  378. generations: []generation{
  379. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14, sub: 1}, {main: 16}}},
  380. {},
  381. },
  382. },
  383. map[revision]struct{}{
  384. {main: 14, sub: 1}: {},
  385. },
  386. },
  387. {
  388. 15,
  389. &keyIndex{
  390. key: []byte("foo"),
  391. modified: revision{16, 0},
  392. generations: []generation{
  393. {created: revision{14, 0}, ver: 3, revs: []revision{{main: 14, sub: 1}, {main: 16}}},
  394. {},
  395. },
  396. },
  397. map[revision]struct{}{
  398. {main: 14, sub: 1}: {},
  399. },
  400. },
  401. {
  402. 16,
  403. &keyIndex{
  404. key: []byte("foo"),
  405. modified: revision{16, 0},
  406. generations: []generation{
  407. {},
  408. },
  409. },
  410. map[revision]struct{}{},
  411. },
  412. }
  413. // Continuous Compaction and finding Keep
  414. ki := newTestKeyIndex()
  415. for i, tt := range tests {
  416. am := make(map[revision]struct{})
  417. kiclone := cloneKeyIndex(ki)
  418. ki.keep(tt.compact, am)
  419. if !reflect.DeepEqual(ki, kiclone) {
  420. t.Errorf("#%d: ki = %+v, want %+v", i, ki, kiclone)
  421. }
  422. if !reflect.DeepEqual(am, tt.wam) {
  423. t.Errorf("#%d: am = %+v, want %+v", i, am, tt.wam)
  424. }
  425. am = make(map[revision]struct{})
  426. ki.compact(tt.compact, am)
  427. if !reflect.DeepEqual(ki, tt.wki) {
  428. t.Errorf("#%d: ki = %+v, want %+v", i, ki, tt.wki)
  429. }
  430. if !reflect.DeepEqual(am, tt.wam) {
  431. t.Errorf("#%d: am = %+v, want %+v", i, am, tt.wam)
  432. }
  433. }
  434. // Jump Compaction and finding Keep
  435. ki = newTestKeyIndex()
  436. for i, tt := range tests {
  437. if (i%2 == 0 && i < 6) || (i%2 == 1 && i > 6) {
  438. am := make(map[revision]struct{})
  439. kiclone := cloneKeyIndex(ki)
  440. ki.keep(tt.compact, am)
  441. if !reflect.DeepEqual(ki, kiclone) {
  442. t.Errorf("#%d: ki = %+v, want %+v", i, ki, kiclone)
  443. }
  444. if !reflect.DeepEqual(am, tt.wam) {
  445. t.Errorf("#%d: am = %+v, want %+v", i, am, tt.wam)
  446. }
  447. am = make(map[revision]struct{})
  448. ki.compact(tt.compact, am)
  449. if !reflect.DeepEqual(ki, tt.wki) {
  450. t.Errorf("#%d: ki = %+v, want %+v", i, ki, tt.wki)
  451. }
  452. if !reflect.DeepEqual(am, tt.wam) {
  453. t.Errorf("#%d: am = %+v, want %+v", i, am, tt.wam)
  454. }
  455. }
  456. }
  457. kiClone := newTestKeyIndex()
  458. // Once Compaction and finding Keep
  459. for i, tt := range tests {
  460. ki := newTestKeyIndex()
  461. am := make(map[revision]struct{})
  462. ki.keep(tt.compact, am)
  463. if !reflect.DeepEqual(ki, kiClone) {
  464. t.Errorf("#%d: ki = %+v, want %+v", i, ki, kiClone)
  465. }
  466. if !reflect.DeepEqual(am, tt.wam) {
  467. t.Errorf("#%d: am = %+v, want %+v", i, am, tt.wam)
  468. }
  469. am = make(map[revision]struct{})
  470. ki.compact(tt.compact, am)
  471. if !reflect.DeepEqual(ki, tt.wki) {
  472. t.Errorf("#%d: ki = %+v, want %+v", i, ki, tt.wki)
  473. }
  474. if !reflect.DeepEqual(am, tt.wam) {
  475. t.Errorf("#%d: am = %+v, want %+v", i, am, tt.wam)
  476. }
  477. }
  478. }
  479. func cloneKeyIndex(ki *keyIndex) *keyIndex {
  480. generations := make([]generation, len(ki.generations))
  481. for i, gen := range ki.generations {
  482. generations[i] = *cloneGeneration(&gen)
  483. }
  484. return &keyIndex{ki.key, ki.modified, generations}
  485. }
  486. func cloneGeneration(g *generation) *generation {
  487. if g.revs == nil {
  488. return &generation{g.ver, g.created, nil}
  489. }
  490. tmp := make([]revision, len(g.revs))
  491. copy(tmp, g.revs)
  492. return &generation{g.ver, g.created, tmp}
  493. }
  494. // test that compact on version that higher than last modified version works well
  495. func TestKeyIndexCompactOnFurtherRev(t *testing.T) {
  496. ki := &keyIndex{key: []byte("foo")}
  497. ki.put(1, 0)
  498. ki.put(2, 0)
  499. am := make(map[revision]struct{})
  500. ki.compact(3, am)
  501. wki := &keyIndex{
  502. key: []byte("foo"),
  503. modified: revision{2, 0},
  504. generations: []generation{
  505. {created: revision{1, 0}, ver: 2, revs: []revision{{main: 2}}},
  506. },
  507. }
  508. wam := map[revision]struct{}{
  509. {main: 2}: {},
  510. }
  511. if !reflect.DeepEqual(ki, wki) {
  512. t.Errorf("ki = %+v, want %+v", ki, wki)
  513. }
  514. if !reflect.DeepEqual(am, wam) {
  515. t.Errorf("am = %+v, want %+v", am, wam)
  516. }
  517. }
  518. func TestKeyIndexIsEmpty(t *testing.T) {
  519. tests := []struct {
  520. ki *keyIndex
  521. w bool
  522. }{
  523. {
  524. &keyIndex{
  525. key: []byte("foo"),
  526. generations: []generation{{}},
  527. },
  528. true,
  529. },
  530. {
  531. &keyIndex{
  532. key: []byte("foo"),
  533. modified: revision{2, 0},
  534. generations: []generation{
  535. {created: revision{1, 0}, ver: 2, revs: []revision{{main: 2}}},
  536. },
  537. },
  538. false,
  539. },
  540. }
  541. for i, tt := range tests {
  542. g := tt.ki.isEmpty()
  543. if g != tt.w {
  544. t.Errorf("#%d: isEmpty = %v, want %v", i, g, tt.w)
  545. }
  546. }
  547. }
  548. func TestKeyIndexFindGeneration(t *testing.T) {
  549. ki := newTestKeyIndex()
  550. tests := []struct {
  551. rev int64
  552. wg *generation
  553. }{
  554. {0, nil},
  555. {1, nil},
  556. {2, &ki.generations[0]},
  557. {3, &ki.generations[0]},
  558. {4, &ki.generations[0]},
  559. {5, &ki.generations[0]},
  560. {6, nil},
  561. {7, nil},
  562. {8, &ki.generations[1]},
  563. {9, &ki.generations[1]},
  564. {10, &ki.generations[1]},
  565. {11, &ki.generations[1]},
  566. {12, nil},
  567. {13, nil},
  568. }
  569. for i, tt := range tests {
  570. g := ki.findGeneration(tt.rev)
  571. if g != tt.wg {
  572. t.Errorf("#%d: generation = %+v, want %+v", i, g, tt.wg)
  573. }
  574. }
  575. }
  576. func TestKeyIndexLess(t *testing.T) {
  577. ki := &keyIndex{key: []byte("foo")}
  578. tests := []struct {
  579. ki *keyIndex
  580. w bool
  581. }{
  582. {&keyIndex{key: []byte("doo")}, false},
  583. {&keyIndex{key: []byte("foo")}, false},
  584. {&keyIndex{key: []byte("goo")}, true},
  585. }
  586. for i, tt := range tests {
  587. g := ki.Less(tt.ki)
  588. if g != tt.w {
  589. t.Errorf("#%d: Less = %v, want %v", i, g, tt.w)
  590. }
  591. }
  592. }
  593. func TestGenerationIsEmpty(t *testing.T) {
  594. tests := []struct {
  595. g *generation
  596. w bool
  597. }{
  598. {nil, true},
  599. {&generation{}, true},
  600. {&generation{revs: []revision{{main: 1}}}, false},
  601. }
  602. for i, tt := range tests {
  603. g := tt.g.isEmpty()
  604. if g != tt.w {
  605. t.Errorf("#%d: isEmpty = %v, want %v", i, g, tt.w)
  606. }
  607. }
  608. }
  609. func TestGenerationWalk(t *testing.T) {
  610. g := &generation{
  611. ver: 3,
  612. created: revision{2, 0},
  613. revs: []revision{{main: 2}, {main: 4}, {main: 6}},
  614. }
  615. tests := []struct {
  616. f func(rev revision) bool
  617. wi int
  618. }{
  619. {func(rev revision) bool { return rev.main >= 7 }, 2},
  620. {func(rev revision) bool { return rev.main >= 6 }, 1},
  621. {func(rev revision) bool { return rev.main >= 5 }, 1},
  622. {func(rev revision) bool { return rev.main >= 4 }, 0},
  623. {func(rev revision) bool { return rev.main >= 3 }, 0},
  624. {func(rev revision) bool { return rev.main >= 2 }, -1},
  625. }
  626. for i, tt := range tests {
  627. idx := g.walk(tt.f)
  628. if idx != tt.wi {
  629. t.Errorf("#%d: index = %d, want %d", i, idx, tt.wi)
  630. }
  631. }
  632. }
  633. func newTestKeyIndex() *keyIndex {
  634. // key: "foo"
  635. // rev: 16
  636. // generations:
  637. // {empty}
  638. // {{14, 0}[1], {14, 1}[2], {16, 0}(t)[3]}
  639. // {{8, 0}[1], {10, 0}[2], {12, 0}(t)[3]}
  640. // {{2, 0}[1], {4, 0}[2], {6, 0}(t)[3]}
  641. ki := &keyIndex{key: []byte("foo")}
  642. ki.put(2, 0)
  643. ki.put(4, 0)
  644. ki.tombstone(6, 0)
  645. ki.put(8, 0)
  646. ki.put(10, 0)
  647. ki.tombstone(12, 0)
  648. ki.put(14, 0)
  649. ki.put(14, 1)
  650. ki.tombstone(16, 0)
  651. return ki
  652. }