kvstore_test.go 18 KB

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