diff --git a/ddl/db_test.go b/ddl/db_test.go index 7fd9f5fafb0d4..5c01ad8aa75e2 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -1891,7 +1891,7 @@ func (s *testDBSuite) TestTruncateTable(c *C) { hasOldTableData := true for i := 0; i < waitForCleanDataRound; i++ { err = kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error { - it, err1 := txn.Seek(tablePrefix) + it, err1 := txn.Iter(tablePrefix, nil) if err1 != nil { return err1 } @@ -2817,7 +2817,7 @@ func (s *testDBSuite) TestAlterTableDropPartition(c *C) { s.tk.MustExec("drop table if exists tr;") s.tk.MustExec(` create table tr( - id int, name varchar(50), + id int, name varchar(50), purchased date ) partition by range( year(purchased) ) ( @@ -2907,7 +2907,7 @@ func checkPartitionDelRangeDone(c *C, s *testDBSuite, partitionPrefix kv.Key) bo hasOldPartitionData := true for i := 0; i < waitForCleanDataRound; i++ { err := kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error { - it, err := txn.Seek(partitionPrefix) + it, err := txn.Iter(partitionPrefix, nil) if err != nil { return err } @@ -2957,7 +2957,7 @@ func (s *testDBSuite) TestTruncatePartitionAndDropTable(c *C) { // Test truncate table partition. s.tk.MustExec("drop table if exists t3;") s.tk.MustExec(`create table t3( - id int, name varchar(50), + id int, name varchar(50), purchased date ) partition by range( year(purchased) ) ( @@ -2995,7 +2995,7 @@ func (s *testDBSuite) TestTruncatePartitionAndDropTable(c *C) { // Test drop table partition. s.tk.MustExec("drop table if exists t4;") s.tk.MustExec(`create table t4( - id int, name varchar(50), + id int, name varchar(50), purchased date ) partition by range( year(purchased) ) ( diff --git a/ddl/delete_range.go b/ddl/delete_range.go index afe30274ee33a..d0e242662fd48 100644 --- a/ddl/delete_range.go +++ b/ddl/delete_range.go @@ -154,7 +154,7 @@ func (dr *delRange) doTask(ctx sessionctx.Context, r util.DelRangeTask) error { finish := true dr.keys = dr.keys[:0] err := kv.RunInNewTxn(dr.store, false, func(txn kv.Transaction) error { - iter, err := txn.Seek(oldStartKey) + iter, err := txn.Iter(oldStartKey, nil) if err != nil { return errors.Trace(err) } diff --git a/ddl/index.go b/ddl/index.go index b8454dff94e9e..bcd690383490c 100644 --- a/ddl/index.go +++ b/ddl/index.go @@ -1216,7 +1216,7 @@ func iterateSnapshotRows(store kv.Storage, priority int, t table.Table, version return errors.Trace(err) } firstKey := t.RecordKey(seekHandle) - it, err := snap.Seek(firstKey) + it, err := snap.Iter(firstKey, nil) if err != nil { return errors.Trace(err) } diff --git a/go.mod b/go.mod index b9bc33169b9c9..8ce29d723c96e 100644 --- a/go.mod +++ b/go.mod @@ -68,7 +68,7 @@ require ( github.com/pingcap/errcode v0.0.0-20180921232412-a1a7271709d9 // indirect github.com/pingcap/errors v0.11.0 github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e - github.com/pingcap/kvproto v0.0.0-20181028030329-855d2192cdc7 + github.com/pingcap/kvproto v0.0.0-20181105061835-1b5d69cd1d26 github.com/pingcap/parser v0.0.0-20181102070703-4acd198f5092 github.com/pingcap/pd v2.1.0-rc.4+incompatible github.com/pingcap/tidb-tools v0.0.0-20181101090416-cfac1096162e diff --git a/go.sum b/go.sum index 7e6637029312b..f97032e4215d2 100644 --- a/go.sum +++ b/go.sum @@ -189,6 +189,8 @@ github.com/pingcap/kvproto v0.0.0-20180930052200-fae11119f066 h1:ulo0ph8sxCzY3GY github.com/pingcap/kvproto v0.0.0-20180930052200-fae11119f066/go.mod h1:0gwbe1F2iBIjuQ9AH0DbQhL+Dpr5GofU8fgYyXk+ykk= github.com/pingcap/kvproto v0.0.0-20181028030329-855d2192cdc7 h1:CYssSnPvf90ZSbFdZpsZGSI7y+drG1EfKxqTOnKnHb0= github.com/pingcap/kvproto v0.0.0-20181028030329-855d2192cdc7/go.mod h1:0gwbe1F2iBIjuQ9AH0DbQhL+Dpr5GofU8fgYyXk+ykk= +github.com/pingcap/kvproto v0.0.0-20181105061835-1b5d69cd1d26 h1:JK4VLNYbSn36QSbCnqALi2ySXdH0DfcMssT/zmLf4Ls= +github.com/pingcap/kvproto v0.0.0-20181105061835-1b5d69cd1d26/go.mod h1:0gwbe1F2iBIjuQ9AH0DbQhL+Dpr5GofU8fgYyXk+ykk= github.com/pingcap/parser v0.0.0-20181024082006-53ac409ed043 h1:P9Osi8lei5j2fiRgsBi2Wch7qe4a3yWUOsS5vSan/JU= github.com/pingcap/parser v0.0.0-20181024082006-53ac409ed043/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= github.com/pingcap/parser v0.0.0-20181102070703-4acd198f5092 h1:vGjjf7fhuaO9udn6QEFzvsNJDwVxFmdJvIJhCdCNe/E= diff --git a/kv/buffer_store.go b/kv/buffer_store.go index 4854fe0a6eab7..950fd04b4c79f 100644 --- a/kv/buffer_store.go +++ b/kv/buffer_store.go @@ -74,26 +74,26 @@ func (s *BufferStore) Get(k Key) ([]byte, error) { return val, nil } -// Seek implements the Retriever interface. -func (s *BufferStore) Seek(k Key) (Iterator, error) { - bufferIt, err := s.MemBuffer.Seek(k) +// Iter implements the Retriever interface. +func (s *BufferStore) Iter(k Key, upperBound Key) (Iterator, error) { + bufferIt, err := s.MemBuffer.Iter(k, upperBound) if err != nil { return nil, errors.Trace(err) } - retrieverIt, err := s.r.Seek(k) + retrieverIt, err := s.r.Iter(k, upperBound) if err != nil { return nil, errors.Trace(err) } return NewUnionIter(bufferIt, retrieverIt, false) } -// SeekReverse implements the Retriever interface. -func (s *BufferStore) SeekReverse(k Key) (Iterator, error) { - bufferIt, err := s.MemBuffer.SeekReverse(k) +// IterReverse implements the Retriever interface. +func (s *BufferStore) IterReverse(k Key) (Iterator, error) { + bufferIt, err := s.MemBuffer.IterReverse(k) if err != nil { return nil, errors.Trace(err) } - retrieverIt, err := s.r.SeekReverse(k) + retrieverIt, err := s.r.IterReverse(k) if err != nil { return nil, errors.Trace(err) } diff --git a/kv/buffer_store_test.go b/kv/buffer_store_test.go index af84a9294cd24..1af716011c2a4 100644 --- a/kv/buffer_store_test.go +++ b/kv/buffer_store_test.go @@ -53,7 +53,7 @@ func (s testBufferStoreSuite) TestSaveTo(c *C) { err := bs.SaveTo(mutator) c.Check(err, IsNil) - iter, err := mutator.Seek(nil) + iter, err := mutator.Iter(nil, nil) c.Check(err, IsNil) for iter.Valid() { cmp := bytes.Compare(iter.Key(), iter.Value()) diff --git a/kv/kv.go b/kv/kv.go index 8c8a0830b208a..84e0ea8fd07f5 100644 --- a/kv/kv.go +++ b/kv/kv.go @@ -79,15 +79,17 @@ type Retriever interface { // Get gets the value for key k from kv store. // If corresponding kv pair does not exist, it returns nil and ErrNotExist. Get(k Key) ([]byte, error) - // Seek creates an Iterator positioned on the first entry that k <= entry's key. + // Iter creates an Iterator positioned on the first entry that k <= entry's key. // If such entry is not found, it returns an invalid Iterator with no error. + // It yields only keys that < upperBound. If upperBound is nil, it means the upperBound is unbounded. // The Iterator must be Closed after use. - Seek(k Key) (Iterator, error) + Iter(k Key, upperBound Key) (Iterator, error) - // SeekReverse creates a reversed Iterator positioned on the first entry which key is less than k. + // IterReverse creates a reversed Iterator positioned on the first entry which key is less than k. // The returned iterator will iterate from greater key to smaller key. // If k is nil, the returned iterator will be positioned at the last key. - SeekReverse(k Key) (Iterator, error) + // TODO: Add lower bound limit + IterReverse(k Key) (Iterator, error) } // Mutator is the interface wraps the basic Set and Delete methods. diff --git a/kv/mem_buffer_test.go b/kv/mem_buffer_test.go index 92807b528da2d..e7ec5a1f4f9e6 100644 --- a/kv/mem_buffer_test.go +++ b/kv/mem_buffer_test.go @@ -76,7 +76,7 @@ func valToStr(c *C, iter Iterator) string { func checkNewIterator(c *C, buffer MemBuffer) { for i := startIndex; i < testCount; i++ { val := encodeInt(i * indexStep) - iter, err := buffer.Seek(val) + iter, err := buffer.Iter(val, nil) c.Assert(err, IsNil) c.Assert([]byte(iter.Key()), BytesEquals, val) c.Assert(decodeInt([]byte(valToStr(c, iter))), Equals, i*indexStep) @@ -86,7 +86,7 @@ func checkNewIterator(c *C, buffer MemBuffer) { // Test iterator Next() for i := startIndex; i < testCount-1; i++ { val := encodeInt(i * indexStep) - iter, err := buffer.Seek(val) + iter, err := buffer.Iter(val, nil) c.Assert(err, IsNil) c.Assert([]byte(iter.Key()), BytesEquals, val) c.Assert(valToStr(c, iter), Equals, string(val)) @@ -102,7 +102,7 @@ func checkNewIterator(c *C, buffer MemBuffer) { } // Non exist and beyond maximum seek test - iter, err := buffer.Seek(encodeInt(testCount * indexStep)) + iter, err := buffer.Iter(encodeInt(testCount*indexStep), nil) c.Assert(err, IsNil) c.Assert(iter.Valid(), IsFalse) @@ -110,7 +110,7 @@ func checkNewIterator(c *C, buffer MemBuffer) { // it returns the smallest key that larger than the one we are seeking inBetween := encodeInt((testCount-1)*indexStep - 1) last := encodeInt((testCount - 1) * indexStep) - iter, err = buffer.Seek(inBetween) + iter, err = buffer.Iter(inBetween, nil) c.Assert(err, IsNil) c.Assert(iter.Valid(), IsTrue) c.Assert([]byte(iter.Key()), Not(BytesEquals), inBetween) @@ -140,7 +140,7 @@ func (s *testKVSuite) TestNewIterator(c *C) { defer testleak.AfterTest(c)() for _, buffer := range s.bs { // should be invalid - iter, err := buffer.Seek(nil) + iter, err := buffer.Iter(nil, nil) c.Assert(err, IsNil) c.Assert(iter.Valid(), IsFalse) @@ -155,7 +155,7 @@ func (s *testKVSuite) TestIterNextUntil(c *C) { buffer := NewMemDbBuffer(DefaultTxnMembufCap) insertData(c, buffer) - iter, err := buffer.Seek(nil) + iter, err := buffer.Iter(nil, nil) c.Assert(err, IsNil) err = NextUntil(iter, func(k Key) bool { @@ -168,7 +168,7 @@ func (s *testKVSuite) TestIterNextUntil(c *C) { func (s *testKVSuite) TestBasicNewIterator(c *C) { defer testleak.AfterTest(c)() for _, buffer := range s.bs { - it, err := buffer.Seek([]byte("2")) + it, err := buffer.Iter([]byte("2"), nil) c.Assert(err, IsNil) c.Assert(it.Valid(), IsFalse) } @@ -193,7 +193,7 @@ func (s *testKVSuite) TestNewIteratorMin(c *C) { } cnt := 0 - it, err := buffer.Seek(nil) + it, err := buffer.Iter(nil, nil) c.Assert(err, IsNil) for it.Valid() { cnt++ @@ -201,7 +201,7 @@ func (s *testKVSuite) TestNewIteratorMin(c *C) { } c.Assert(cnt, Equals, 6) - it, err = buffer.Seek([]byte("DATA_test_main_db_tbl_tbl_test_record__00000000000000000000")) + it, err = buffer.Iter([]byte("DATA_test_main_db_tbl_tbl_test_record__00000000000000000000"), nil) c.Assert(err, IsNil) c.Assert(string(it.Key()), Equals, "DATA_test_main_db_tbl_tbl_test_record__00000000000000000001") } @@ -294,7 +294,7 @@ func benchIterator(b *testing.B, buffer MemBuffer) { } b.ResetTimer() for i := 0; i < b.N; i++ { - iter, err := buffer.Seek(nil) + iter, err := buffer.Iter(nil, nil) if err != nil { b.Error(err) } diff --git a/kv/memdb_buffer.go b/kv/memdb_buffer.go index 3066288f4146f..8579800e236c2 100644 --- a/kv/memdb_buffer.go +++ b/kv/memdb_buffer.go @@ -50,14 +50,10 @@ func NewMemDbBuffer(cap int) MemBuffer { } } -// Seek creates an Iterator. -func (m *memDbBuffer) Seek(k Key) (Iterator, error) { - var i Iterator - if k == nil { - i = &memDbIter{iter: m.db.NewIterator(&util.Range{}), reverse: false} - } else { - i = &memDbIter{iter: m.db.NewIterator(&util.Range{Start: []byte(k)}), reverse: false} - } +// Iter creates an Iterator. +func (m *memDbBuffer) Iter(k Key, upperBound Key) (Iterator, error) { + i := &memDbIter{iter: m.db.NewIterator(&util.Range{Start: []byte(k), Limit: []byte(upperBound)}), reverse: false} + err := i.Next() if err != nil { return nil, errors.Trace(err) @@ -69,7 +65,7 @@ func (m *memDbBuffer) SetCap(cap int) { } -func (m *memDbBuffer) SeekReverse(k Key) (Iterator, error) { +func (m *memDbBuffer) IterReverse(k Key) (Iterator, error) { var i *memDbIter if k == nil { i = &memDbIter{iter: m.db.NewIterator(&util.Range{}), reverse: true} @@ -161,7 +157,7 @@ func (i *memDbIter) Close() { // WalkMemBuffer iterates all buffered kv pairs in memBuf func WalkMemBuffer(memBuf MemBuffer, f func(k Key, v []byte) error) error { - iter, err := memBuf.Seek(nil) + iter, err := memBuf.Iter(nil, nil) if err != nil { return errors.Trace(err) } diff --git a/kv/mock.go b/kv/mock.go index 4dc7fafa490e0..a84618d264957 100644 --- a/kv/mock.go +++ b/kv/mock.go @@ -68,11 +68,11 @@ func (t *mockTxn) Get(k Key) ([]byte, error) { return nil, nil } -func (t *mockTxn) Seek(k Key) (Iterator, error) { +func (t *mockTxn) Iter(k Key, upperBound Key) (Iterator, error) { return nil, nil } -func (t *mockTxn) SeekReverse(k Key) (Iterator, error) { +func (t *mockTxn) IterReverse(k Key) (Iterator, error) { return nil, nil } @@ -211,10 +211,10 @@ func (s *mockSnapshot) BatchGet(keys []Key) (map[string][]byte, error) { return m, nil } -func (s *mockSnapshot) Seek(k Key) (Iterator, error) { - return s.store.Seek(k) +func (s *mockSnapshot) Iter(k Key, upperBound Key) (Iterator, error) { + return s.store.Iter(k, upperBound) } -func (s *mockSnapshot) SeekReverse(k Key) (Iterator, error) { - return s.store.SeekReverse(k) +func (s *mockSnapshot) IterReverse(k Key) (Iterator, error) { + return s.store.IterReverse(k) } diff --git a/kv/mock_test.go b/kv/mock_test.go index 93dc59490b4a4..2c1db84cf8a97 100644 --- a/kv/mock_test.go +++ b/kv/mock_test.go @@ -46,8 +46,8 @@ func (s testMockSuite) TestInterface(c *C) { if transaction.IsReadOnly() { transaction.Get(Key("lock")) transaction.Set(Key("lock"), []byte{}) - transaction.Seek(Key("lock")) - transaction.SeekReverse(Key("lock")) + transaction.Iter(Key("lock"), nil) + transaction.IterReverse(Key("lock")) } transaction.Commit(context.Background()) diff --git a/kv/union_store.go b/kv/union_store.go index 016f9f9d9d5d2..2af127f6a40c8 100644 --- a/kv/union_store.go +++ b/kv/union_store.go @@ -127,18 +127,18 @@ func (lmb *lazyMemBuffer) Delete(k Key) error { return lmb.mb.Delete(k) } -func (lmb *lazyMemBuffer) Seek(k Key) (Iterator, error) { +func (lmb *lazyMemBuffer) Iter(k Key, upperBound Key) (Iterator, error) { if lmb.mb == nil { return invalidIterator{}, nil } - return lmb.mb.Seek(k) + return lmb.mb.Iter(k, upperBound) } -func (lmb *lazyMemBuffer) SeekReverse(k Key) (Iterator, error) { +func (lmb *lazyMemBuffer) IterReverse(k Key) (Iterator, error) { if lmb.mb == nil { return invalidIterator{}, nil } - return lmb.mb.SeekReverse(k) + return lmb.mb.IterReverse(k) } func (lmb *lazyMemBuffer) Size() int { diff --git a/kv/union_store_test.go b/kv/union_store_test.go index c4e6c400f915a..3297b14f969bc 100644 --- a/kv/union_store_test.go +++ b/kv/union_store_test.go @@ -63,46 +63,46 @@ func (s *testUnionStoreSuite) TestSeek(c *C) { s.store.Set([]byte("2"), []byte("2")) s.store.Set([]byte("3"), []byte("3")) - iter, err := s.us.Seek(nil) + iter, err := s.us.Iter(nil, nil) c.Assert(err, IsNil) checkIterator(c, iter, [][]byte{[]byte("1"), []byte("2"), []byte("3")}, [][]byte{[]byte("1"), []byte("2"), []byte("3")}) - iter, err = s.us.Seek([]byte("2")) + iter, err = s.us.Iter([]byte("2"), nil) c.Assert(err, IsNil) checkIterator(c, iter, [][]byte{[]byte("2"), []byte("3")}, [][]byte{[]byte("2"), []byte("3")}) s.us.Set([]byte("4"), []byte("4")) - iter, err = s.us.Seek([]byte("2")) + iter, err = s.us.Iter([]byte("2"), nil) c.Assert(err, IsNil) checkIterator(c, iter, [][]byte{[]byte("2"), []byte("3"), []byte("4")}, [][]byte{[]byte("2"), []byte("3"), []byte("4")}) s.us.Delete([]byte("3")) - iter, err = s.us.Seek([]byte("2")) + iter, err = s.us.Iter([]byte("2"), nil) c.Assert(err, IsNil) checkIterator(c, iter, [][]byte{[]byte("2"), []byte("4")}, [][]byte{[]byte("2"), []byte("4")}) } -func (s *testUnionStoreSuite) TestSeekReverse(c *C) { +func (s *testUnionStoreSuite) TestIterReverse(c *C) { defer testleak.AfterTest(c)() s.store.Set([]byte("1"), []byte("1")) s.store.Set([]byte("2"), []byte("2")) s.store.Set([]byte("3"), []byte("3")) - iter, err := s.us.SeekReverse(nil) + iter, err := s.us.IterReverse(nil) c.Assert(err, IsNil) checkIterator(c, iter, [][]byte{[]byte("3"), []byte("2"), []byte("1")}, [][]byte{[]byte("3"), []byte("2"), []byte("1")}) - iter, err = s.us.SeekReverse([]byte("3")) + iter, err = s.us.IterReverse([]byte("3")) c.Assert(err, IsNil) checkIterator(c, iter, [][]byte{[]byte("2"), []byte("1")}, [][]byte{[]byte("2"), []byte("1")}) s.us.Set([]byte("0"), []byte("0")) - iter, err = s.us.SeekReverse([]byte("3")) + iter, err = s.us.IterReverse([]byte("3")) c.Assert(err, IsNil) checkIterator(c, iter, [][]byte{[]byte("2"), []byte("1"), []byte("0")}, [][]byte{[]byte("2"), []byte("1"), []byte("0")}) s.us.Delete([]byte("1")) - iter, err = s.us.SeekReverse([]byte("3")) + iter, err = s.us.IterReverse([]byte("3")) c.Assert(err, IsNil) checkIterator(c, iter, [][]byte{[]byte("2"), []byte("0")}, [][]byte{[]byte("2"), []byte("0")}) } diff --git a/session/txn.go b/session/txn.go index 0dc4445003eb2..53444bcf0bc56 100644 --- a/session/txn.go +++ b/session/txn.go @@ -159,26 +159,26 @@ func (st *TxnState) Delete(k kv.Key) error { return st.buf.Delete(k) } -// Seek overrides the Transaction interface. -func (st *TxnState) Seek(k kv.Key) (kv.Iterator, error) { - bufferIt, err := st.buf.Seek(k) +// Iter overrides the Transaction interface. +func (st *TxnState) Iter(k kv.Key, upperBound kv.Key) (kv.Iterator, error) { + bufferIt, err := st.buf.Iter(k, upperBound) if err != nil { return nil, errors.Trace(err) } - retrieverIt, err := st.Transaction.Seek(k) + retrieverIt, err := st.Transaction.Iter(k, upperBound) if err != nil { return nil, errors.Trace(err) } return kv.NewUnionIter(bufferIt, retrieverIt, false) } -// SeekReverse overrides the Transaction interface. -func (st *TxnState) SeekReverse(k kv.Key) (kv.Iterator, error) { - bufferIt, err := st.buf.SeekReverse(k) +// IterReverse overrides the Transaction interface. +func (st *TxnState) IterReverse(k kv.Key) (kv.Iterator, error) { + bufferIt, err := st.buf.IterReverse(k) if err != nil { return nil, errors.Trace(err) } - retrieverIt, err := st.Transaction.SeekReverse(k) + retrieverIt, err := st.Transaction.IterReverse(k) if err != nil { return nil, errors.Trace(err) } diff --git a/store/mockstore/mocktikv/mock_tikv_test.go b/store/mockstore/mocktikv/mock_tikv_test.go index 807a4267134ba..a4af86f037a28 100644 --- a/store/mockstore/mocktikv/mock_tikv_test.go +++ b/store/mockstore/mocktikv/mock_tikv_test.go @@ -119,7 +119,11 @@ func (s *testMockTiKVSuite) mustDeleteOK(c *C, key string, startTS, commitTS uin } func (s *testMockTiKVSuite) mustScanOK(c *C, start string, limit int, ts uint64, expect ...string) { - pairs := s.store.Scan([]byte(start), nil, limit, ts, kvrpcpb.IsolationLevel_SI) + s.mustRangeScanOK(c, start, "", limit, ts, expect...) +} + +func (s *testMockTiKVSuite) mustRangeScanOK(c *C, start, end string, limit int, ts uint64, expect ...string) { + pairs := s.store.Scan([]byte(start), []byte(end), limit, ts, kvrpcpb.IsolationLevel_SI) c.Assert(len(pairs)*2, Equals, len(expect)) for i := 0; i < len(pairs); i++ { c.Assert(pairs[i].Err, IsNil) @@ -129,7 +133,11 @@ func (s *testMockTiKVSuite) mustScanOK(c *C, start string, limit int, ts uint64, } func (s *testMockTiKVSuite) mustReverseScanOK(c *C, end string, limit int, ts uint64, expect ...string) { - pairs := s.store.ReverseScan(nil, []byte(end), limit, ts, kvrpcpb.IsolationLevel_SI) + s.mustRangeReverseScanOK(c, "", end, limit, ts, expect...) +} + +func (s *testMockTiKVSuite) mustRangeReverseScanOK(c *C, start, end string, limit int, ts uint64, expect ...string) { + pairs := s.store.ReverseScan([]byte(start), []byte(end), limit, ts, kvrpcpb.IsolationLevel_SI) c.Assert(len(pairs)*2, Equals, len(expect)) for i := 0; i < len(pairs); i++ { c.Assert(pairs[i].Err, IsNil) @@ -249,6 +257,9 @@ func (s *testMockTiKVSuite) TestReverseScan(c *C) { s.mustReverseScanOK(c, "C\x00", 3, 10, "C", "C10", "A", "A10") s.mustReverseScanOK(c, "C\x00", 4, 10, "C", "C10", "A", "A10") s.mustReverseScanOK(c, "B", 1, 10, "A", "A10") + s.mustRangeReverseScanOK(c, "", "E", 5, 10, "C", "C10", "A", "A10") + s.mustRangeReverseScanOK(c, "", "C\x00", 5, 10, "C", "C10", "A", "A10") + s.mustRangeReverseScanOK(c, "A\x00", "C", 5, 10) } checkV10() @@ -260,6 +271,9 @@ func (s *testMockTiKVSuite) TestReverseScan(c *C) { s.mustReverseScanOK(c, "Z", 5, 20, "E", "E10", "D", "D20", "C", "C10", "B", "B20", "A", "A10") s.mustReverseScanOK(c, "C\x00", 5, 20, "C", "C10", "B", "B20", "A", "A10") s.mustReverseScanOK(c, "A\x00", 1, 20, "A", "A10") + s.mustRangeReverseScanOK(c, "B", "D", 5, 20, "C", "C10", "B", "B20") + s.mustRangeReverseScanOK(c, "B", "D\x00", 5, 20, "D", "D20", "C", "C10", "B", "B20") + s.mustRangeReverseScanOK(c, "B\x00", "D\x00", 5, 20, "D", "D20", "C", "C10") } checkV10() checkV20() @@ -308,6 +322,9 @@ func (s *testMockTiKVSuite) TestScan(c *C) { s.mustScanOK(c, "A\x00", 3, 10, "C", "C10", "E", "E10") s.mustScanOK(c, "C", 4, 10, "C", "C10", "E", "E10") s.mustScanOK(c, "F", 1, 10) + s.mustRangeScanOK(c, "", "E", 5, 10, "A", "A10", "C", "C10") + s.mustRangeScanOK(c, "", "C\x00", 5, 10, "A", "A10", "C", "C10") + s.mustRangeScanOK(c, "A\x00", "C", 5, 10) } checkV10() @@ -319,6 +336,9 @@ func (s *testMockTiKVSuite) TestScan(c *C) { s.mustScanOK(c, "", 5, 20, "A", "A10", "B", "B20", "C", "C10", "D", "D20", "E", "E10") s.mustScanOK(c, "C", 5, 20, "C", "C10", "D", "D20", "E", "E10") s.mustScanOK(c, "D\x00", 1, 20, "E", "E10") + s.mustRangeScanOK(c, "B", "D", 5, 20, "B", "B20", "C", "C10") + s.mustRangeScanOK(c, "B", "D\x00", 5, 20, "B", "B20", "C", "C10", "D", "D20") + s.mustRangeScanOK(c, "B\x00", "D\x00", 5, 20, "C", "C10", "D", "D20") } checkV10() checkV20() diff --git a/store/mockstore/mocktikv/rpc.go b/store/mockstore/mocktikv/rpc.go index 96458eb569aa3..7cc6009569537 100644 --- a/store/mockstore/mocktikv/rpc.go +++ b/store/mockstore/mocktikv/rpc.go @@ -229,7 +229,11 @@ func (h *rpcHandler) handleKvScan(req *kvrpcpb.ScanRequest) *kvrpcpb.ScanRespons if !h.checkKeyInRegion(req.GetStartKey()) { panic("KvScan: startKey not in region") } - pairs := h.mvccStore.Scan(req.GetStartKey(), h.endKey, int(req.GetLimit()), req.GetVersion(), h.isolationLevel) + endKey := h.endKey + if len(req.EndKey) > 0 && (len(endKey) == 0 || bytes.Compare(req.EndKey, endKey) < 0) { + endKey = req.EndKey + } + pairs := h.mvccStore.Scan(req.GetStartKey(), endKey, int(req.GetLimit()), req.GetVersion(), h.isolationLevel) return &kvrpcpb.ScanResponse{ Pairs: convertToPbPairs(pairs), } diff --git a/store/store_test.go b/store/store_test.go index bf481caeab7da..95ef4c131b4e9 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -97,7 +97,7 @@ func valToStr(c *C, iter kv.Iterator) string { func checkSeek(c *C, txn kv.Transaction) { for i := startIndex; i < testCount; i++ { val := encodeInt(i * indexStep) - iter, err := txn.Seek(val) + iter, err := txn.Iter(val, nil) c.Assert(err, IsNil) c.Assert([]byte(iter.Key()), BytesEquals, val) c.Assert(decodeInt([]byte(valToStr(c, iter))), Equals, i*indexStep) @@ -107,7 +107,7 @@ func checkSeek(c *C, txn kv.Transaction) { // Test iterator Next() for i := startIndex; i < testCount-1; i++ { val := encodeInt(i * indexStep) - iter, err := txn.Seek(val) + iter, err := txn.Iter(val, nil) c.Assert(err, IsNil) c.Assert([]byte(iter.Key()), BytesEquals, val) c.Assert(valToStr(c, iter), Equals, string(val)) @@ -123,7 +123,7 @@ func checkSeek(c *C, txn kv.Transaction) { } // Non exist and beyond maximum seek test - iter, err := txn.Seek(encodeInt(testCount * indexStep)) + iter, err := txn.Iter(encodeInt(testCount*indexStep), nil) c.Assert(err, IsNil) c.Assert(iter.Valid(), IsFalse) @@ -131,7 +131,7 @@ func checkSeek(c *C, txn kv.Transaction) { // it returns the smallest key that larger than the one we are seeking inBetween := encodeInt((testCount-1)*indexStep - 1) last := encodeInt((testCount - 1) * indexStep) - iter, err = txn.Seek(inBetween) + iter, err = txn.Iter(inBetween, nil) c.Assert(err, IsNil) c.Assert(iter.Valid(), IsTrue) c.Assert([]byte(iter.Key()), Not(BytesEquals), inBetween) @@ -278,7 +278,7 @@ func (s *testKVSuite) TestDelete2(c *C) { txn, err = s.s.Begin() c.Assert(err, IsNil) - it, err := txn.Seek([]byte("DATA_test_tbl_department_record__0000000001_0003")) + it, err := txn.Iter([]byte("DATA_test_tbl_department_record__0000000001_0003"), nil) c.Assert(err, IsNil) for it.Valid() { err = txn.Delete([]byte(it.Key())) @@ -290,7 +290,7 @@ func (s *testKVSuite) TestDelete2(c *C) { txn, err = s.s.Begin() c.Assert(err, IsNil) - it, _ = txn.Seek([]byte("DATA_test_tbl_department_record__000000000")) + it, _ = txn.Iter([]byte("DATA_test_tbl_department_record__000000000"), nil) c.Assert(it.Valid(), IsFalse) txn.Commit(context.Background()) } @@ -312,7 +312,7 @@ func (s *testKVSuite) TestBasicSeek(c *C) { c.Assert(err, IsNil) defer txn.Commit(context.Background()) - it, err := txn.Seek([]byte("2")) + it, err := txn.Iter([]byte("2"), nil) c.Assert(err, IsNil) c.Assert(it.Valid(), Equals, false) txn.Delete([]byte("1")) @@ -333,30 +333,30 @@ func (s *testKVSuite) TestBasicTable(c *C) { err = txn.Set([]byte("1"), []byte("1")) c.Assert(err, IsNil) - it, err := txn.Seek([]byte("0")) + it, err := txn.Iter([]byte("0"), nil) c.Assert(err, IsNil) c.Assert(string(it.Key()), Equals, "1") err = txn.Set([]byte("0"), []byte("0")) c.Assert(err, IsNil) - it, err = txn.Seek([]byte("0")) + it, err = txn.Iter([]byte("0"), nil) c.Assert(err, IsNil) c.Assert(string(it.Key()), Equals, "0") err = txn.Delete([]byte("0")) c.Assert(err, IsNil) txn.Delete([]byte("1")) - it, err = txn.Seek([]byte("0")) + it, err = txn.Iter([]byte("0"), nil) c.Assert(err, IsNil) c.Assert(string(it.Key()), Equals, "2") err = txn.Delete([]byte("3")) c.Assert(err, IsNil) - it, err = txn.Seek([]byte("2")) + it, err = txn.Iter([]byte("2"), nil) c.Assert(err, IsNil) c.Assert(string(it.Key()), Equals, "2") - it, err = txn.Seek([]byte("3")) + it, err = txn.Iter([]byte("3"), nil) c.Assert(err, IsNil) c.Assert(string(it.Key()), Equals, "4") err = txn.Delete([]byte("2")) @@ -411,14 +411,14 @@ func (s *testKVSuite) TestSeekMin(c *C) { txn.Set([]byte(row.key), []byte(row.value)) } - it, err := txn.Seek(nil) + it, err := txn.Iter(nil, nil) c.Assert(err, IsNil) for it.Valid() { fmt.Printf("%s, %s\n", it.Key(), it.Value()) it.Next() } - it, err = txn.Seek([]byte("DATA_test_main_db_tbl_tbl_test_record__00000000000000000000")) + it, err = txn.Iter([]byte("DATA_test_main_db_tbl_tbl_test_record__00000000000000000000"), nil) c.Assert(err, IsNil) c.Assert(string(it.Key()), Equals, "DATA_test_main_db_tbl_tbl_test_record__00000000000000000001") diff --git a/store/tikv/delete_range_test.go b/store/tikv/delete_range_test.go index 7864eb0954878..ee0b3c6c73126 100644 --- a/store/tikv/delete_range_test.go +++ b/store/tikv/delete_range_test.go @@ -50,7 +50,7 @@ func (s *testDeleteRangeSuite) TearDownTest(c *C) { func (s *testDeleteRangeSuite) checkData(c *C, expectedData map[string]string) { txn, err := s.store.Begin() c.Assert(err, IsNil) - it, err := txn.Seek([]byte("a")) + it, err := txn.Iter([]byte("a"), nil) c.Assert(err, IsNil) // Scan all data and save into a map diff --git a/store/tikv/lock_test.go b/store/tikv/lock_test.go index 20bedba474f10..31e0c4597b121 100644 --- a/store/tikv/lock_test.go +++ b/store/tikv/lock_test.go @@ -116,7 +116,7 @@ func (s *testLockSuite) TestScanLockResolveWithSeek(c *C) { txn, err := s.store.Begin() c.Assert(err, IsNil) - iter, err := txn.Seek([]byte("a")) + iter, err := txn.Iter([]byte("a"), nil) c.Assert(err, IsNil) for ch := byte('a'); ch <= byte('z'); ch++ { c.Assert(iter.Valid(), IsTrue) @@ -133,7 +133,7 @@ func (s *testLockSuite) TestScanLockResolveWithSeekKeyOnly(c *C) { txn, err := s.store.Begin() c.Assert(err, IsNil) txn.SetOption(kv.KeyOnly, true) - iter, err := txn.Seek([]byte("a")) + iter, err := txn.Iter([]byte("a"), nil) c.Assert(err, IsNil) for ch := byte('a'); ch <= byte('z'); ch++ { c.Assert(iter.Valid(), IsTrue) diff --git a/store/tikv/safepoint_test.go b/store/tikv/safepoint_test.go index 0af5fcaa2ef92..69b85d02bd7a7 100644 --- a/store/tikv/safepoint_test.go +++ b/store/tikv/safepoint_test.go @@ -100,7 +100,7 @@ func (s *testSafePointSuite) TestSafePoint(c *C) { s.waitUntilErrorPlugIn(txn3.startTS) - _, seekerr := txn3.Seek(encodeKey(s.prefix, "")) + _, seekerr := txn3.Iter(encodeKey(s.prefix, ""), nil) c.Assert(seekerr, NotNil) isFallBehind = terror.ErrorEqual(errors.Cause(geterr2), ErrGCTooEarly) isMayFallBehind = terror.ErrorEqual(errors.Cause(geterr2), ErrPDServerTimeout.GenWithStackByArgs("start timestamp may fall behind safe point")) diff --git a/store/tikv/scan.go b/store/tikv/scan.go index 2f545b8cf032c..52ce4ca23df2a 100644 --- a/store/tikv/scan.go +++ b/store/tikv/scan.go @@ -30,10 +30,11 @@ type Scanner struct { cache []*pb.KvPair idx int nextStartKey []byte + endKey []byte eof bool } -func newScanner(snapshot *tikvSnapshot, startKey []byte, batchSize int) (*Scanner, error) { +func newScanner(snapshot *tikvSnapshot, startKey []byte, endKey []byte, batchSize int) (*Scanner, error) { // It must be > 1. Otherwise scanner won't skipFirst. if batchSize <= 1 { batchSize = scanBatchSize @@ -43,6 +44,7 @@ func newScanner(snapshot *tikvSnapshot, startKey []byte, batchSize int) (*Scanne batchSize: batchSize, valid: true, nextStartKey: startKey, + endKey: endKey, } err := scanner.Next() if kv.IsErrNotFound(err) { @@ -147,6 +149,7 @@ func (s *Scanner) getData(bo *Backoffer) error { Type: tikvrpc.CmdScan, Scan: &pb.ScanRequest{ StartKey: s.nextStartKey, + EndKey: s.endKey, Limit: uint32(s.batchSize), Version: s.startTS(), KeyOnly: s.snapshot.keyOnly, @@ -199,7 +202,7 @@ func (s *Scanner) getData(bo *Backoffer) error { // No more data in current Region. Next getData() starts // from current Region's endKey. s.nextStartKey = loc.EndKey - if len(loc.EndKey) == 0 { + if len(loc.EndKey) == 0 || (len(s.endKey) > 0 && kv.Key(s.nextStartKey).Cmp(kv.Key(s.endKey)) >= 0) { // Current Region is the last one. s.eof = true } diff --git a/store/tikv/scan_mock_test.go b/store/tikv/scan_mock_test.go index af453a985ff32..94b720314f2bd 100644 --- a/store/tikv/scan_mock_test.go +++ b/store/tikv/scan_mock_test.go @@ -41,11 +41,19 @@ func (s *testScanMockSuite) TestScanMultipleRegions(c *C) { txn, err = store.Begin() c.Assert(err, IsNil) snapshot := newTiKVSnapshot(store, kv.Version{Ver: txn.StartTS()}) - scanner, err := newScanner(snapshot, []byte("a"), 10) + scanner, err := newScanner(snapshot, []byte("a"), nil, 10) c.Assert(err, IsNil) for ch := byte('a'); ch <= byte('z'); ch++ { c.Assert([]byte{ch}, BytesEquals, []byte(scanner.Key())) c.Assert(scanner.Next(), IsNil) } c.Assert(scanner.Valid(), IsFalse) + + scanner, err = newScanner(snapshot, []byte("a"), []byte("i"), 10) + c.Assert(err, IsNil) + for ch := byte('a'); ch <= byte('h'); ch++ { + c.Assert([]byte{ch}, BytesEquals, []byte(scanner.Key())) + c.Assert(scanner.Next(), IsNil) + } + c.Assert(scanner.Valid(), IsFalse) } diff --git a/store/tikv/scan_test.go b/store/tikv/scan_test.go index e4897bc2de2b7..ff9184d0c0323 100644 --- a/store/tikv/scan_test.go +++ b/store/tikv/scan_test.go @@ -40,7 +40,7 @@ func (s *testScanSuite) SetUpSuite(c *C) { func (s *testScanSuite) TearDownSuite(c *C) { txn := s.beginTxn(c) - scanner, err := txn.Seek(encodeKey(s.prefix, "")) + scanner, err := txn.Iter(encodeKey(s.prefix, ""), nil) c.Assert(err, IsNil) c.Assert(scanner, NotNil) for scanner.Valid() { @@ -62,7 +62,25 @@ func (s *testScanSuite) beginTxn(c *C) *tikvTxn { return txn.(*tikvTxn) } -func (s *testScanSuite) TestSeek(c *C) { +func (s *testScanSuite) TestScan(c *C) { + check := func(c *C, scan kv.Iterator, rowNum int, keyOnly bool) { + for i := 0; i < rowNum; i++ { + k := scan.Key() + c.Assert([]byte(k), BytesEquals, encodeKey(s.prefix, s08d("key", i))) + if !keyOnly { + v := scan.Value() + c.Assert(v, BytesEquals, valueBytes(i)) + } + // Because newScan return first item without calling scan.Next() just like go-hbase, + // for-loop count will decrease 1. + if i < rowNum-1 { + scan.Next() + } + } + scan.Next() + c.Assert(scan.Valid(), IsFalse) + } + for _, rowNum := range s.rowNums { txn := s.beginTxn(c) for i := 0; i < rowNum; i++ { @@ -76,57 +94,35 @@ func (s *testScanSuite) TestSeek(c *C) { val, err := txn2.Get(encodeKey(s.prefix, s08d("key", 0))) c.Assert(err, IsNil) c.Assert(val, BytesEquals, valueBytes(0)) - scan, err := txn2.Seek(encodeKey(s.prefix, "")) + // Test scan without upperBound + scan, err := txn2.Iter(encodeKey(s.prefix, ""), nil) c.Assert(err, IsNil) - - for i := 0; i < rowNum; i++ { - k := scan.Key() - c.Assert([]byte(k), BytesEquals, encodeKey(s.prefix, s08d("key", i))) - v := scan.Value() - c.Assert(v, BytesEquals, valueBytes(i)) - // Because newScan return first item without calling scan.Next() just like go-hbase, - // for-loop count will decrease 1. - if i < rowNum-1 { - scan.Next() - } - } - scan.Next() - c.Assert(scan.Valid(), IsFalse) + check(c, scan, rowNum, false) + // Test scan with upperBound + upperBound := rowNum / 2 + scan, err = txn2.Iter(encodeKey(s.prefix, ""), encodeKey(s.prefix, s08d("key", upperBound))) + c.Assert(err, IsNil) + check(c, scan, upperBound, false) txn3 := s.beginTxn(c) txn3.SetOption(kv.KeyOnly, true) - scan, err = txn3.Seek(encodeKey(s.prefix, "")) + // Test scan without upper bound + scan, err = txn3.Iter(encodeKey(s.prefix, ""), nil) c.Assert(err, IsNil) - - for i := 0; i < rowNum; i++ { - k := scan.Key() - c.Assert([]byte(k), BytesEquals, encodeKey(s.prefix, s08d("key", i))) - // Because newScan return first item without calling scan.Next() just like go-hbase, - // for-loop count will decrease 1. - if i < rowNum-1 { - scan.Next() - } - } - scan.Next() - c.Assert(scan.Valid(), IsFalse) + check(c, scan, rowNum, true) + // test scan with upper bound + scan, err = txn3.Iter(encodeKey(s.prefix, ""), encodeKey(s.prefix, s08d("key", upperBound))) + c.Assert(err, IsNil) + check(c, scan, upperBound, true) // Restore KeyOnly to false txn3.SetOption(kv.KeyOnly, false) - scan, err = txn3.Seek(encodeKey(s.prefix, "")) + scan, err = txn3.Iter(encodeKey(s.prefix, ""), nil) c.Assert(err, IsNil) - - for i := 0; i < rowNum; i++ { - k := scan.Key() - c.Assert([]byte(k), BytesEquals, encodeKey(s.prefix, s08d("key", i))) - v := scan.Value() - c.Assert(v, BytesEquals, valueBytes(i)) - // Because newScan return first item without calling scan.Next() just like go-hbase, - // for-loop count will decrease 1. - if i < rowNum-1 { - scan.Next() - } - } - scan.Next() - c.Assert(scan.Valid(), IsFalse) + check(c, scan, rowNum, true) + // test scan with upper bound + scan, err = txn3.Iter(encodeKey(s.prefix, ""), encodeKey(s.prefix, s08d("key", upperBound))) + c.Assert(err, IsNil) + check(c, scan, upperBound, true) } } diff --git a/store/tikv/snapshot.go b/store/tikv/snapshot.go index 39e7ed67a6e5a..c8d5ff9f42b97 100644 --- a/store/tikv/snapshot.go +++ b/store/tikv/snapshot.go @@ -280,14 +280,14 @@ func (s *tikvSnapshot) get(bo *Backoffer, k kv.Key) ([]byte, error) { } } -// Seek return a list of key-value pair after `k`. -func (s *tikvSnapshot) Seek(k kv.Key) (kv.Iterator, error) { - scanner, err := newScanner(s, k, scanBatchSize) +// Iter return a list of key-value pair after `k`. +func (s *tikvSnapshot) Iter(k kv.Key, upperBound kv.Key) (kv.Iterator, error) { + scanner, err := newScanner(s, k, upperBound, scanBatchSize) return scanner, errors.Trace(err) } -// SeekReverse creates a reversed Iterator positioned on the first entry which key is less than k. -func (s *tikvSnapshot) SeekReverse(k kv.Key) (kv.Iterator, error) { +// IterReverse creates a reversed Iterator positioned on the first entry which key is less than k. +func (s *tikvSnapshot) IterReverse(k kv.Key) (kv.Iterator, error) { return nil, kv.ErrNotImplemented } diff --git a/store/tikv/snapshot_test.go b/store/tikv/snapshot_test.go index 4ea695f224e4b..61d5da2112863 100644 --- a/store/tikv/snapshot_test.go +++ b/store/tikv/snapshot_test.go @@ -42,7 +42,7 @@ func (s *testSnapshotSuite) SetUpSuite(c *C) { func (s *testSnapshotSuite) TearDownSuite(c *C) { txn := s.beginTxn(c) - scanner, err := txn.Seek(encodeKey(s.prefix, "")) + scanner, err := txn.Iter(encodeKey(s.prefix, ""), nil) c.Assert(err, IsNil) c.Assert(scanner, NotNil) for scanner.Valid() { @@ -70,7 +70,7 @@ func (s *testSnapshotSuite) checkAll(keys []kv.Key, c *C) { m, err := snapshot.BatchGet(keys) c.Assert(err, IsNil) - scan, err := txn.Seek(encodeKey(s.prefix, "")) + scan, err := txn.Iter(encodeKey(s.prefix, ""), nil) c.Assert(err, IsNil) cnt := 0 for scan.Valid() { diff --git a/store/tikv/store_test.go b/store/tikv/store_test.go index 8e54767417063..6fef8610854a6 100644 --- a/store/tikv/store_test.go +++ b/store/tikv/store_test.go @@ -229,7 +229,7 @@ func (s *testStoreSuite) TestRequestPriority(c *C) { // Cover Seek request. client.priority = pb.CommandPri_High txn.SetOption(kv.Priority, kv.PriorityHigh) - iter, err := txn.Seek([]byte("key")) + iter, err := txn.Iter([]byte("key"), nil) c.Assert(err, IsNil) for iter.Valid() { c.Assert(iter.Next(), IsNil) diff --git a/store/tikv/ticlient_test.go b/store/tikv/ticlient_test.go index e243dc396e7d1..d336e03e2b178 100644 --- a/store/tikv/ticlient_test.go +++ b/store/tikv/ticlient_test.go @@ -61,7 +61,7 @@ func clearStorage(store kv.Storage) error { if err != nil { return errors.Trace(err) } - iter, err := txn.Seek(nil) + iter, err := txn.Iter(nil, nil) if err != nil { return errors.Trace(err) } @@ -93,7 +93,7 @@ func (s *testTiclientSuite) SetUpSuite(c *C) { func (s *testTiclientSuite) TearDownSuite(c *C) { // Clean all data, or it may pollute other data. txn := s.beginTxn(c) - scanner, err := txn.Seek(encodeKey(s.prefix, "")) + scanner, err := txn.Iter(encodeKey(s.prefix, ""), nil) c.Assert(err, IsNil) c.Assert(scanner, NotNil) for scanner.Valid() { diff --git a/store/tikv/txn.go b/store/tikv/txn.go index 9b72431a0e07c..c94b2e66ee138 100644 --- a/store/tikv/txn.go +++ b/store/tikv/txn.go @@ -115,23 +115,23 @@ func (txn *tikvTxn) String() string { return fmt.Sprintf("%d", txn.StartTS()) } -func (txn *tikvTxn) Seek(k kv.Key) (kv.Iterator, error) { +func (txn *tikvTxn) Iter(k kv.Key, upperBound kv.Key) (kv.Iterator, error) { metrics.TiKVTxnCmdCounter.WithLabelValues("seek").Inc() start := time.Now() defer func() { metrics.TiKVTxnCmdHistogram.WithLabelValues("seek").Observe(time.Since(start).Seconds()) }() - return txn.us.Seek(k) + return txn.us.Iter(k, upperBound) } -// SeekReverse creates a reversed Iterator positioned on the first entry which key is less than k. -func (txn *tikvTxn) SeekReverse(k kv.Key) (kv.Iterator, error) { +// IterReverse creates a reversed Iterator positioned on the first entry which key is less than k. +func (txn *tikvTxn) IterReverse(k kv.Key) (kv.Iterator, error) { metrics.TiKVTxnCmdCounter.WithLabelValues("seek_reverse").Inc() start := time.Now() defer func() { metrics.TiKVTxnCmdHistogram.WithLabelValues("seek_reverse").Observe(time.Since(start).Seconds()) }() - return txn.us.SeekReverse(k) + return txn.us.IterReverse(k) } func (txn *tikvTxn) Delete(k kv.Key) error { diff --git a/structure/hash.go b/structure/hash.go index ecc70b64517a2..8921bbe0db0af 100644 --- a/structure/hash.go +++ b/structure/hash.go @@ -238,7 +238,7 @@ func (t *TxStructure) HClear(key []byte) error { func (t *TxStructure) iterateHash(key []byte, fn func(k []byte, v []byte) error) error { dataPrefix := t.hashDataKeyPrefix(key) - it, err := t.reader.Seek(dataPrefix) + it, err := t.reader.Iter(dataPrefix, nil) if err != nil { return errors.Trace(err) } diff --git a/table/tables/index.go b/table/tables/index.go index 3f9d77402a2ef..ec87c3727089a 100644 --- a/table/tables/index.go +++ b/table/tables/index.go @@ -240,7 +240,7 @@ func (c *index) Delete(sc *stmtctx.StatementContext, m kv.Mutator, indexedValues // Drop removes the KV index from store. func (c *index) Drop(rm kv.RetrieverMutator) error { - it, err := rm.Seek(c.prefix) + it, err := rm.Iter(c.prefix, nil) if err != nil { return errors.Trace(err) } @@ -270,7 +270,7 @@ func (c *index) Seek(sc *stmtctx.StatementContext, r kv.Retriever, indexedValues return nil, false, errors.Trace(err) } - it, err := r.Seek(key) + it, err := r.Iter(key, nil) if err != nil { return nil, false, errors.Trace(err) } @@ -284,7 +284,7 @@ func (c *index) Seek(sc *stmtctx.StatementContext, r kv.Retriever, indexedValues // SeekFirst returns an iterator which points to the first entry of the KV index. func (c *index) SeekFirst(r kv.Retriever) (iter table.IndexIterator, err error) { - it, err := r.Seek(c.prefix) + it, err := r.Iter(c.prefix, nil) if err != nil { return nil, errors.Trace(err) } diff --git a/table/tables/tables.go b/table/tables/tables.go index 37dee1163c016..9d8c01022fd9b 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -782,7 +782,7 @@ func (t *tableCommon) buildIndexForRow(ctx sessionctx.Context, rm kv.RetrieverMu // IterRecords implements table.Table IterRecords interface. func (t *tableCommon) IterRecords(ctx sessionctx.Context, startKey kv.Key, cols []*table.Column, fn table.RecordIterFunc) error { - it, err := ctx.Txn().Seek(startKey) + it, err := ctx.Txn().Iter(startKey, nil) if err != nil { return errors.Trace(err) } @@ -912,7 +912,7 @@ func (t *tableCommon) RebaseAutoID(ctx sessionctx.Context, newBase int64, isSetS // Seek implements table.Table Seek interface. func (t *tableCommon) Seek(ctx sessionctx.Context, h int64) (int64, bool, error) { seekKey := tablecodec.EncodeRowKeyWithHandle(t.physicalTableID, h) - iter, err := ctx.Txn().Seek(seekKey) + iter, err := ctx.Txn().Iter(seekKey, nil) if !iter.Valid() || !iter.Key().HasPrefix(t.RecordPrefix()) { // No more records in the table, skip to the end. return 0, false, nil diff --git a/util/admin/admin.go b/util/admin/admin.go index e49fb2ee9f8d1..9aa38113ba13c 100644 --- a/util/admin/admin.go +++ b/util/admin/admin.go @@ -639,7 +639,7 @@ func rowWithCols(sessCtx sessionctx.Context, txn kv.Retriever, t table.Table, h // genExprs use to calculate generated column value. func iterRecords(sessCtx sessionctx.Context, retriever kv.Retriever, t table.Table, startKey kv.Key, cols []*table.Column, fn table.RecordIterFunc, genExprs map[model.TableColumnID]expression.Expression) error { - it, err := retriever.Seek(startKey) + it, err := retriever.Iter(startKey, nil) if err != nil { return errors.Trace(err) } diff --git a/util/prefix_helper.go b/util/prefix_helper.go index 3fc172b02d783..214414a9cef64 100644 --- a/util/prefix_helper.go +++ b/util/prefix_helper.go @@ -26,7 +26,7 @@ import ( // ScanMetaWithPrefix scans metadata with the prefix. func ScanMetaWithPrefix(retriever kv.Retriever, prefix kv.Key, filter func(kv.Key, []byte) bool) error { - iter, err := retriever.Seek(prefix) + iter, err := retriever.Iter(prefix, nil) if err != nil { return errors.Trace(err) } @@ -56,7 +56,7 @@ func ScanMetaWithPrefix(retriever kv.Retriever, prefix kv.Key, filter func(kv.Ke // DelKeyWithPrefix deletes keys with prefix. func DelKeyWithPrefix(rm kv.RetrieverMutator, prefix kv.Key) error { var keys []kv.Key - iter, err := rm.Seek(prefix) + iter, err := rm.Iter(prefix, nil) if err != nil { return errors.Trace(err) }