kvstore_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. "crypto/rand"
  17. "encoding/binary"
  18. "math"
  19. "os"
  20. "reflect"
  21. "testing"
  22. "time"
  23. "github.com/coreos/etcd/lease"
  24. "github.com/coreos/etcd/mvcc/backend"
  25. "github.com/coreos/etcd/mvcc/mvccpb"
  26. "github.com/coreos/etcd/pkg/schedule"
  27. "github.com/coreos/etcd/pkg/testutil"
  28. )
  29. func TestStoreRev(t *testing.T) {
  30. b, tmpPath := backend.NewDefaultTmpBackend()
  31. s := NewStore(b, &lease.FakeLessor{}, nil)
  32. defer s.Close()
  33. defer os.Remove(tmpPath)
  34. for i := 1; i <= 3; i++ {
  35. s.Put([]byte("foo"), []byte("bar"), lease.NoLease)
  36. if r := s.Rev(); r != int64(i+1) {
  37. t.Errorf("#%d: rev = %d, want %d", i, r, i+1)
  38. }
  39. }
  40. }
  41. func TestStorePut(t *testing.T) {
  42. kv := mvccpb.KeyValue{
  43. Key: []byte("foo"),
  44. Value: []byte("bar"),
  45. CreateRevision: 1,
  46. ModRevision: 2,
  47. Version: 1,
  48. }
  49. kvb, err := kv.Marshal()
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. tests := []struct {
  54. rev revision
  55. r indexGetResp
  56. rr *rangeResp
  57. wrev revision
  58. wkey []byte
  59. wkv mvccpb.KeyValue
  60. wputrev revision
  61. }{
  62. {
  63. revision{1, 0},
  64. indexGetResp{revision{}, revision{}, 0, ErrRevisionNotFound},
  65. nil,
  66. revision{1, 1},
  67. newTestKeyBytes(revision{2, 0}, false),
  68. mvccpb.KeyValue{
  69. Key: []byte("foo"),
  70. Value: []byte("bar"),
  71. CreateRevision: 2,
  72. ModRevision: 2,
  73. Version: 1,
  74. Lease: 1,
  75. },
  76. revision{2, 0},
  77. },
  78. {
  79. revision{1, 1},
  80. indexGetResp{revision{2, 0}, revision{2, 0}, 1, nil},
  81. &rangeResp{[][]byte{newTestKeyBytes(revision{2, 1}, false)}, [][]byte{kvb}},
  82. revision{1, 2},
  83. newTestKeyBytes(revision{2, 1}, false),
  84. mvccpb.KeyValue{
  85. Key: []byte("foo"),
  86. Value: []byte("bar"),
  87. CreateRevision: 2,
  88. ModRevision: 2,
  89. Version: 2,
  90. Lease: 2,
  91. },
  92. revision{2, 1},
  93. },
  94. {
  95. revision{2, 0},
  96. indexGetResp{revision{2, 1}, revision{2, 0}, 2, nil},
  97. &rangeResp{[][]byte{newTestKeyBytes(revision{2, 1}, false)}, [][]byte{kvb}},
  98. revision{2, 1},
  99. newTestKeyBytes(revision{3, 0}, false),
  100. mvccpb.KeyValue{
  101. Key: []byte("foo"),
  102. Value: []byte("bar"),
  103. CreateRevision: 2,
  104. ModRevision: 3,
  105. Version: 3,
  106. Lease: 3,
  107. },
  108. revision{3, 0},
  109. },
  110. }
  111. for i, tt := range tests {
  112. s := newFakeStore()
  113. b := s.b.(*fakeBackend)
  114. fi := s.kvindex.(*fakeIndex)
  115. s.currentRev = tt.rev
  116. s.tx = b.BatchTx()
  117. fi.indexGetRespc <- tt.r
  118. if tt.rr != nil {
  119. b.tx.rangeRespc <- *tt.rr
  120. }
  121. s.put([]byte("foo"), []byte("bar"), lease.LeaseID(i+1))
  122. data, err := tt.wkv.Marshal()
  123. if err != nil {
  124. t.Errorf("#%d: marshal err = %v, want nil", i, err)
  125. }
  126. wact := []testutil.Action{
  127. {"seqput", []interface{}{keyBucketName, tt.wkey, data}},
  128. }
  129. if tt.rr != nil {
  130. wact = []testutil.Action{
  131. {"seqput", []interface{}{keyBucketName, tt.wkey, data}},
  132. }
  133. }
  134. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  135. t.Errorf("#%d: tx action = %+v, want %+v", i, g, wact)
  136. }
  137. wact = []testutil.Action{
  138. {"get", []interface{}{[]byte("foo"), tt.wputrev.main}},
  139. {"put", []interface{}{[]byte("foo"), tt.wputrev}},
  140. }
  141. if g := fi.Action(); !reflect.DeepEqual(g, wact) {
  142. t.Errorf("#%d: index action = %+v, want %+v", i, g, wact)
  143. }
  144. if s.currentRev != tt.wrev {
  145. t.Errorf("#%d: rev = %+v, want %+v", i, s.currentRev, tt.wrev)
  146. }
  147. s.Close()
  148. }
  149. }
  150. func TestStoreRange(t *testing.T) {
  151. key := newTestKeyBytes(revision{2, 0}, false)
  152. kv := mvccpb.KeyValue{
  153. Key: []byte("foo"),
  154. Value: []byte("bar"),
  155. CreateRevision: 1,
  156. ModRevision: 2,
  157. Version: 1,
  158. }
  159. kvb, err := kv.Marshal()
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. currev := revision{1, 1}
  164. wrev := int64(2)
  165. tests := []struct {
  166. idxr indexRangeResp
  167. r rangeResp
  168. }{
  169. {
  170. indexRangeResp{[][]byte{[]byte("foo")}, []revision{{2, 0}}},
  171. rangeResp{[][]byte{key}, [][]byte{kvb}},
  172. },
  173. {
  174. indexRangeResp{[][]byte{[]byte("foo"), []byte("foo1")}, []revision{{2, 0}, {3, 0}}},
  175. rangeResp{[][]byte{key}, [][]byte{kvb}},
  176. },
  177. }
  178. for i, tt := range tests {
  179. s := newFakeStore()
  180. b := s.b.(*fakeBackend)
  181. fi := s.kvindex.(*fakeIndex)
  182. s.currentRev = currev
  183. s.tx = b.BatchTx()
  184. b.tx.rangeRespc <- tt.r
  185. fi.indexRangeRespc <- tt.idxr
  186. kvs, _, rev, err := s.rangeKeys([]byte("foo"), []byte("goo"), 1, 0, false)
  187. if err != nil {
  188. t.Errorf("#%d: err = %v, want nil", i, err)
  189. }
  190. if w := []mvccpb.KeyValue{kv}; !reflect.DeepEqual(kvs, w) {
  191. t.Errorf("#%d: kvs = %+v, want %+v", i, kvs, w)
  192. }
  193. if rev != wrev {
  194. t.Errorf("#%d: rev = %d, want %d", i, rev, wrev)
  195. }
  196. wstart, wend := revBytesRange(tt.idxr.revs[0])
  197. wact := []testutil.Action{
  198. {"range", []interface{}{keyBucketName, wstart, wend, int64(0)}},
  199. }
  200. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  201. t.Errorf("#%d: tx action = %+v, want %+v", i, g, wact)
  202. }
  203. wact = []testutil.Action{
  204. {"range", []interface{}{[]byte("foo"), []byte("goo"), wrev}},
  205. }
  206. if g := fi.Action(); !reflect.DeepEqual(g, wact) {
  207. t.Errorf("#%d: index action = %+v, want %+v", i, g, wact)
  208. }
  209. if s.currentRev != currev {
  210. t.Errorf("#%d: current rev = %+v, want %+v", i, s.currentRev, currev)
  211. }
  212. s.Close()
  213. }
  214. }
  215. func TestStoreDeleteRange(t *testing.T) {
  216. key := newTestKeyBytes(revision{2, 0}, false)
  217. kv := mvccpb.KeyValue{
  218. Key: []byte("foo"),
  219. Value: []byte("bar"),
  220. CreateRevision: 1,
  221. ModRevision: 2,
  222. Version: 1,
  223. }
  224. kvb, err := kv.Marshal()
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. tests := []struct {
  229. rev revision
  230. r indexRangeResp
  231. rr rangeResp
  232. wkey []byte
  233. wrev revision
  234. wrrev int64
  235. wdelrev revision
  236. }{
  237. {
  238. revision{2, 0},
  239. indexRangeResp{[][]byte{[]byte("foo")}, []revision{{2, 0}}},
  240. rangeResp{[][]byte{key}, [][]byte{kvb}},
  241. newTestKeyBytes(revision{3, 0}, true),
  242. revision{2, 1},
  243. 2,
  244. revision{3, 0},
  245. },
  246. {
  247. revision{2, 1},
  248. indexRangeResp{[][]byte{[]byte("foo")}, []revision{{2, 0}}},
  249. rangeResp{[][]byte{key}, [][]byte{kvb}},
  250. newTestKeyBytes(revision{3, 1}, true),
  251. revision{2, 2},
  252. 3,
  253. revision{3, 1},
  254. },
  255. }
  256. for i, tt := range tests {
  257. s := newFakeStore()
  258. b := s.b.(*fakeBackend)
  259. fi := s.kvindex.(*fakeIndex)
  260. s.currentRev = tt.rev
  261. s.tx = b.BatchTx()
  262. fi.indexRangeRespc <- tt.r
  263. b.tx.rangeRespc <- tt.rr
  264. n := s.deleteRange([]byte("foo"), []byte("goo"))
  265. if n != 1 {
  266. t.Errorf("#%d: n = %d, want 1", i, n)
  267. }
  268. data, err := (&mvccpb.KeyValue{
  269. Key: []byte("foo"),
  270. }).Marshal()
  271. if err != nil {
  272. t.Errorf("#%d: marshal err = %v, want nil", i, err)
  273. }
  274. wact := []testutil.Action{
  275. {"seqput", []interface{}{keyBucketName, tt.wkey, data}},
  276. }
  277. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  278. t.Errorf("#%d: tx action = %+v, want %+v", i, g, wact)
  279. }
  280. wact = []testutil.Action{
  281. {"range", []interface{}{[]byte("foo"), []byte("goo"), tt.wrrev}},
  282. {"tombstone", []interface{}{[]byte("foo"), tt.wdelrev}},
  283. }
  284. if g := fi.Action(); !reflect.DeepEqual(g, wact) {
  285. t.Errorf("#%d: index action = %+v, want %+v", i, g, wact)
  286. }
  287. if s.currentRev != tt.wrev {
  288. t.Errorf("#%d: rev = %+v, want %+v", i, s.currentRev, tt.wrev)
  289. }
  290. }
  291. }
  292. func TestStoreCompact(t *testing.T) {
  293. s := newFakeStore()
  294. defer s.Close()
  295. b := s.b.(*fakeBackend)
  296. fi := s.kvindex.(*fakeIndex)
  297. s.currentRev = revision{3, 0}
  298. fi.indexCompactRespc <- map[revision]struct{}{{1, 0}: {}}
  299. key1 := newTestKeyBytes(revision{1, 0}, false)
  300. key2 := newTestKeyBytes(revision{2, 0}, false)
  301. b.tx.rangeRespc <- rangeResp{[][]byte{key1, key2}, nil}
  302. s.Compact(3)
  303. s.fifoSched.WaitFinish(1)
  304. if s.compactMainRev != 3 {
  305. t.Errorf("compact main rev = %d, want 3", s.compactMainRev)
  306. }
  307. end := make([]byte, 8)
  308. binary.BigEndian.PutUint64(end, uint64(4))
  309. wact := []testutil.Action{
  310. {"put", []interface{}{metaBucketName, scheduledCompactKeyName, newTestRevBytes(revision{3, 0})}},
  311. {"range", []interface{}{keyBucketName, make([]byte, 17), end, int64(10000)}},
  312. {"delete", []interface{}{keyBucketName, key2}},
  313. {"put", []interface{}{metaBucketName, finishedCompactKeyName, newTestRevBytes(revision{3, 0})}},
  314. }
  315. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  316. t.Errorf("tx actions = %+v, want %+v", g, wact)
  317. }
  318. wact = []testutil.Action{
  319. {"compact", []interface{}{int64(3)}},
  320. }
  321. if g := fi.Action(); !reflect.DeepEqual(g, wact) {
  322. t.Errorf("index action = %+v, want %+v", g, wact)
  323. }
  324. }
  325. func TestStoreRestore(t *testing.T) {
  326. s := newFakeStore()
  327. b := s.b.(*fakeBackend)
  328. fi := s.kvindex.(*fakeIndex)
  329. putkey := newTestKeyBytes(revision{3, 0}, false)
  330. putkv := mvccpb.KeyValue{
  331. Key: []byte("foo"),
  332. Value: []byte("bar"),
  333. CreateRevision: 4,
  334. ModRevision: 4,
  335. Version: 1,
  336. }
  337. putkvb, err := putkv.Marshal()
  338. if err != nil {
  339. t.Fatal(err)
  340. }
  341. delkey := newTestKeyBytes(revision{5, 0}, true)
  342. delkv := mvccpb.KeyValue{
  343. Key: []byte("foo"),
  344. }
  345. delkvb, err := delkv.Marshal()
  346. if err != nil {
  347. t.Fatal(err)
  348. }
  349. b.tx.rangeRespc <- rangeResp{[][]byte{finishedCompactKeyName}, [][]byte{newTestRevBytes(revision{3, 0})}}
  350. b.tx.rangeRespc <- rangeResp{[][]byte{putkey, delkey}, [][]byte{putkvb, delkvb}}
  351. b.tx.rangeRespc <- rangeResp{[][]byte{scheduledCompactKeyName}, [][]byte{newTestRevBytes(revision{3, 0})}}
  352. s.restore()
  353. if s.compactMainRev != 3 {
  354. t.Errorf("compact rev = %d, want 5", s.compactMainRev)
  355. }
  356. wrev := revision{5, 0}
  357. if !reflect.DeepEqual(s.currentRev, wrev) {
  358. t.Errorf("current rev = %v, want %v", s.currentRev, wrev)
  359. }
  360. wact := []testutil.Action{
  361. {"range", []interface{}{metaBucketName, finishedCompactKeyName, []byte(nil), int64(0)}},
  362. {"range", []interface{}{keyBucketName, newTestRevBytes(revision{1, 0}), newTestRevBytes(revision{math.MaxInt64, math.MaxInt64}), int64(0)}},
  363. {"range", []interface{}{metaBucketName, scheduledCompactKeyName, []byte(nil), int64(0)}},
  364. }
  365. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  366. t.Errorf("tx actions = %+v, want %+v", g, wact)
  367. }
  368. gens := []generation{
  369. {created: revision{4, 0}, ver: 2, revs: []revision{{3, 0}, {5, 0}}},
  370. {created: revision{0, 0}, ver: 0, revs: nil},
  371. }
  372. ki := &keyIndex{key: []byte("foo"), modified: revision{5, 0}, generations: gens}
  373. wact = []testutil.Action{
  374. {"insert", []interface{}{ki}},
  375. }
  376. if g := fi.Action(); !reflect.DeepEqual(g, wact) {
  377. t.Errorf("index action = %+v, want %+v", g, wact)
  378. }
  379. }
  380. func TestRestoreContinueUnfinishedCompaction(t *testing.T) {
  381. b, tmpPath := backend.NewDefaultTmpBackend()
  382. s0 := NewStore(b, &lease.FakeLessor{}, nil)
  383. defer os.Remove(tmpPath)
  384. s0.Put([]byte("foo"), []byte("bar"), lease.NoLease)
  385. s0.Put([]byte("foo"), []byte("bar1"), lease.NoLease)
  386. s0.Put([]byte("foo"), []byte("bar2"), lease.NoLease)
  387. // write scheduled compaction, but not do compaction
  388. rbytes := newRevBytes()
  389. revToBytes(revision{main: 2}, rbytes)
  390. tx := s0.b.BatchTx()
  391. tx.Lock()
  392. tx.UnsafePut(metaBucketName, scheduledCompactKeyName, rbytes)
  393. tx.Unlock()
  394. s0.Close()
  395. s1 := NewStore(b, &lease.FakeLessor{}, nil)
  396. // wait for scheduled compaction to be finished
  397. time.Sleep(100 * time.Millisecond)
  398. if _, err := s1.Range([]byte("foo"), nil, RangeOptions{Rev: 1}); err != ErrCompacted {
  399. t.Errorf("range on compacted rev error = %v, want %v", err, ErrCompacted)
  400. }
  401. // check the key in backend is deleted
  402. revbytes := newRevBytes()
  403. revToBytes(revision{main: 1}, revbytes)
  404. // The disk compaction is done asynchronously and requires more time on slow disk.
  405. // try 5 times for CI with slow IO.
  406. for i := 0; i < 5; i++ {
  407. tx = s1.b.BatchTx()
  408. tx.Lock()
  409. ks, _ := tx.UnsafeRange(keyBucketName, revbytes, nil, 0)
  410. tx.Unlock()
  411. if len(ks) != 0 {
  412. time.Sleep(100 * time.Millisecond)
  413. continue
  414. }
  415. return
  416. }
  417. t.Errorf("key for rev %+v still exists, want deleted", bytesToRev(revbytes))
  418. }
  419. func TestTxnPut(t *testing.T) {
  420. // assign arbitrary size
  421. bytesN := 30
  422. sliceN := 100
  423. keys := createBytesSlice(bytesN, sliceN)
  424. vals := createBytesSlice(bytesN, sliceN)
  425. b, tmpPath := backend.NewDefaultTmpBackend()
  426. s := NewStore(b, &lease.FakeLessor{}, nil)
  427. defer cleanup(s, b, tmpPath)
  428. for i := 0; i < sliceN; i++ {
  429. id := s.TxnBegin()
  430. base := int64(i + 2)
  431. rev, err := s.TxnPut(id, keys[i], vals[i], lease.NoLease)
  432. if err != nil {
  433. t.Error("txn put error")
  434. }
  435. if rev != base {
  436. t.Errorf("#%d: rev = %d, want %d", i, rev, base)
  437. }
  438. s.TxnEnd(id)
  439. }
  440. }
  441. func TestTxnBlockBackendForceCommit(t *testing.T) {
  442. b, tmpPath := backend.NewDefaultTmpBackend()
  443. s := NewStore(b, &lease.FakeLessor{}, nil)
  444. defer os.Remove(tmpPath)
  445. id := s.TxnBegin()
  446. done := make(chan struct{})
  447. go func() {
  448. s.b.ForceCommit()
  449. done <- struct{}{}
  450. }()
  451. select {
  452. case <-done:
  453. t.Fatalf("failed to block ForceCommit")
  454. case <-time.After(100 * time.Millisecond):
  455. }
  456. s.TxnEnd(id)
  457. select {
  458. case <-done:
  459. case <-time.After(5 * time.Second): // wait 5 seconds for CI with slow IO
  460. testutil.FatalStack(t, "failed to execute ForceCommit")
  461. }
  462. }
  463. // TODO: test attach key to lessor
  464. func newTestRevBytes(rev revision) []byte {
  465. bytes := newRevBytes()
  466. revToBytes(rev, bytes)
  467. return bytes
  468. }
  469. func newTestKeyBytes(rev revision, tombstone bool) []byte {
  470. bytes := newRevBytes()
  471. revToBytes(rev, bytes)
  472. if tombstone {
  473. bytes = appendMarkTombstone(bytes)
  474. }
  475. return bytes
  476. }
  477. // TestStoreHashAfterForceCommit ensures that later Hash call to
  478. // closed backend with ForceCommit does not panic.
  479. func TestStoreHashAfterForceCommit(t *testing.T) {
  480. be, tmpPath := backend.NewDefaultTmpBackend()
  481. kv := NewStore(be, &lease.FakeLessor{}, nil)
  482. defer os.Remove(tmpPath)
  483. // as in EtcdServer.HardStop
  484. kv.Close()
  485. be.Close()
  486. kv.Hash()
  487. }
  488. func newFakeStore() *store {
  489. b := &fakeBackend{&fakeBatchTx{
  490. Recorder: &testutil.RecorderBuffered{},
  491. rangeRespc: make(chan rangeResp, 5)}}
  492. fi := &fakeIndex{
  493. Recorder: &testutil.RecorderBuffered{},
  494. indexGetRespc: make(chan indexGetResp, 1),
  495. indexRangeRespc: make(chan indexRangeResp, 1),
  496. indexRangeEventsRespc: make(chan indexRangeEventsResp, 1),
  497. indexCompactRespc: make(chan map[revision]struct{}, 1),
  498. }
  499. return &store{
  500. b: b,
  501. le: &lease.FakeLessor{},
  502. kvindex: fi,
  503. currentRev: revision{},
  504. compactMainRev: -1,
  505. fifoSched: schedule.NewFIFOScheduler(),
  506. stopc: make(chan struct{}),
  507. }
  508. }
  509. type rangeResp struct {
  510. keys [][]byte
  511. vals [][]byte
  512. }
  513. type fakeBatchTx struct {
  514. testutil.Recorder
  515. rangeRespc chan rangeResp
  516. }
  517. func (b *fakeBatchTx) Lock() {}
  518. func (b *fakeBatchTx) Unlock() {}
  519. func (b *fakeBatchTx) UnsafeCreateBucket(name []byte) {}
  520. func (b *fakeBatchTx) UnsafePut(bucketName []byte, key []byte, value []byte) {
  521. b.Recorder.Record(testutil.Action{Name: "put", Params: []interface{}{bucketName, key, value}})
  522. }
  523. func (b *fakeBatchTx) UnsafeSeqPut(bucketName []byte, key []byte, value []byte) {
  524. b.Recorder.Record(testutil.Action{Name: "seqput", Params: []interface{}{bucketName, key, value}})
  525. }
  526. func (b *fakeBatchTx) UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte) {
  527. b.Recorder.Record(testutil.Action{Name: "range", Params: []interface{}{bucketName, key, endKey, limit}})
  528. r := <-b.rangeRespc
  529. return r.keys, r.vals
  530. }
  531. func (b *fakeBatchTx) UnsafeDelete(bucketName []byte, key []byte) {
  532. b.Recorder.Record(testutil.Action{Name: "delete", Params: []interface{}{bucketName, key}})
  533. }
  534. func (b *fakeBatchTx) UnsafeForEach(bucketName []byte, visitor func(k, v []byte) error) error {
  535. return nil
  536. }
  537. func (b *fakeBatchTx) Commit() {}
  538. func (b *fakeBatchTx) CommitAndStop() {}
  539. type fakeBackend struct {
  540. tx *fakeBatchTx
  541. }
  542. func (b *fakeBackend) BatchTx() backend.BatchTx { return b.tx }
  543. func (b *fakeBackend) Hash(ignores map[backend.IgnoreKey]struct{}) (uint32, error) { return 0, nil }
  544. func (b *fakeBackend) Size() int64 { return 0 }
  545. func (b *fakeBackend) SizeInUse() int64 { return 0 }
  546. func (b *fakeBackend) Snapshot() backend.Snapshot { return nil }
  547. func (b *fakeBackend) ForceCommit() {}
  548. func (b *fakeBackend) Defrag() error { return nil }
  549. func (b *fakeBackend) Close() error { return nil }
  550. type indexGetResp struct {
  551. rev revision
  552. created revision
  553. ver int64
  554. err error
  555. }
  556. type indexRangeResp struct {
  557. keys [][]byte
  558. revs []revision
  559. }
  560. type indexRangeEventsResp struct {
  561. revs []revision
  562. }
  563. type fakeIndex struct {
  564. testutil.Recorder
  565. indexGetRespc chan indexGetResp
  566. indexRangeRespc chan indexRangeResp
  567. indexRangeEventsRespc chan indexRangeEventsResp
  568. indexCompactRespc chan map[revision]struct{}
  569. }
  570. func (i *fakeIndex) Get(key []byte, atRev int64) (rev, created revision, ver int64, err error) {
  571. i.Recorder.Record(testutil.Action{Name: "get", Params: []interface{}{key, atRev}})
  572. r := <-i.indexGetRespc
  573. return r.rev, r.created, r.ver, r.err
  574. }
  575. func (i *fakeIndex) Range(key, end []byte, atRev int64) ([][]byte, []revision) {
  576. i.Recorder.Record(testutil.Action{Name: "range", Params: []interface{}{key, end, atRev}})
  577. r := <-i.indexRangeRespc
  578. return r.keys, r.revs
  579. }
  580. func (i *fakeIndex) Put(key []byte, rev revision) {
  581. i.Recorder.Record(testutil.Action{Name: "put", Params: []interface{}{key, rev}})
  582. }
  583. func (i *fakeIndex) Tombstone(key []byte, rev revision) error {
  584. i.Recorder.Record(testutil.Action{Name: "tombstone", Params: []interface{}{key, rev}})
  585. return nil
  586. }
  587. func (i *fakeIndex) RangeSince(key, end []byte, rev int64) []revision {
  588. i.Recorder.Record(testutil.Action{Name: "rangeEvents", Params: []interface{}{key, end, rev}})
  589. r := <-i.indexRangeEventsRespc
  590. return r.revs
  591. }
  592. func (i *fakeIndex) Compact(rev int64) map[revision]struct{} {
  593. i.Recorder.Record(testutil.Action{Name: "compact", Params: []interface{}{rev}})
  594. return <-i.indexCompactRespc
  595. }
  596. func (i *fakeIndex) Equal(b index) bool { return false }
  597. func (i *fakeIndex) Insert(ki *keyIndex) {
  598. i.Recorder.Record(testutil.Action{Name: "insert", Params: []interface{}{ki}})
  599. }
  600. func createBytesSlice(bytesN, sliceN int) [][]byte {
  601. rs := [][]byte{}
  602. for len(rs) != sliceN {
  603. v := make([]byte, bytesN)
  604. if _, err := rand.Read(v); err != nil {
  605. panic(err)
  606. }
  607. rs = append(rs, v)
  608. }
  609. return rs
  610. }