diff --git a/kv/cache_snapshot.go b/kv/cache_snapshot.go index 9537c34727..63764800a6 100644 --- a/kv/cache_snapshot.go +++ b/kv/cache_snapshot.go @@ -74,9 +74,9 @@ func (c *cacheSnapshot) BatchGet(keys []Key) (map[string][]byte, error) { return m, nil } -// Scan scans values from snapshot and saves them in cache. -func (c *cacheSnapshot) Scan(start, end Key, limit int) (map[string][]byte, error) { - values, err := c.snapshot.Scan(start, end, limit) +// RangeGet gets values from snapshot and saves them in cache. +func (c *cacheSnapshot) RangeGet(start, end Key, limit int) (map[string][]byte, error) { + values, err := c.snapshot.RangeGet(start, end, limit) if err != nil { return nil, errors.Trace(err) } diff --git a/kv/cache_snapshot_test.go b/kv/cache_snapshot_test.go index 26c6efb7f2..044ee4a31b 100644 --- a/kv/cache_snapshot_test.go +++ b/kv/cache_snapshot_test.go @@ -14,8 +14,6 @@ package kv import ( - "bytes" - "github.com/juju/errors" . "github.com/pingcap/check" ) @@ -53,17 +51,17 @@ func (s *testCacheSnapshotSuite) TestGet(c *C) { v, err := s.cache.Get(s.k1) c.Assert(err, IsNil) - c.Assert(bytes.Compare(v, s.v1), Equals, 0) + c.Assert(v, BytesEquals, s.v1) s.store.Set(s.k1, s.v3) v, err = s.cache.Get(s.k1) c.Assert(err, IsNil) - c.Assert(bytes.Compare(v, s.v1), Equals, 0) + c.Assert(v, BytesEquals, s.v1) s.store.Set(s.k2, s.v4) v, err = s.cache.Get(s.k2) c.Assert(err, IsNil) - c.Assert(bytes.Compare(v, s.v4), Equals, 0) + c.Assert(v, BytesEquals, s.v4) } func (s *testCacheSnapshotSuite) TestBatchGet(c *C) { @@ -72,8 +70,8 @@ func (s *testCacheSnapshotSuite) TestBatchGet(c *C) { m, err := s.cache.BatchGet([]Key{s.k1, s.k2, s.k3}) c.Assert(err, IsNil) - c.Assert(bytes.Compare(m[string(s.k1)], s.v1), Equals, 0) - c.Assert(bytes.Compare(m[string(s.k2)], s.v2), Equals, 0) + c.Assert(m[string(s.k1)], BytesEquals, s.v1) + c.Assert(m[string(s.k2)], BytesEquals, s.v2) _, exist := m[string(s.k3)] c.Assert(exist, IsFalse) @@ -81,25 +79,25 @@ func (s *testCacheSnapshotSuite) TestBatchGet(c *C) { s.store.Set(s.k1, s.v4) v, err := s.cache.Get(s.k1) c.Assert(err, IsNil) - c.Assert(bytes.Compare(v, s.v1), Equals, 0) + c.Assert(v, BytesEquals, s.v1) } -func (s *testCacheSnapshotSuite) TestScan(c *C) { +func (s *testCacheSnapshotSuite) TestRangeGet(c *C) { s.store.Set(s.k1, s.v1) s.store.Set(s.k2, s.v2) s.store.Set(s.k3, s.v3) - m, err := s.cache.Scan(s.k1, s.k2, 100) + m, err := s.cache.RangeGet(s.k1, s.k2, 100) c.Assert(err, IsNil) c.Assert(len(m), Equals, 2) - c.Assert(bytes.Compare(m[string(s.k1)], s.v1), Equals, 0) - c.Assert(bytes.Compare(m[string(s.k2)], s.v2), Equals, 0) + c.Assert(m[string(s.k1)], BytesEquals, s.v1) + c.Assert(m[string(s.k2)], BytesEquals, s.v2) // result should be saved in cache s.store.Set(s.k1, s.v4) v, err := s.cache.Get(s.k1) c.Assert(err, IsNil) - c.Assert(bytes.Compare(v, s.v1), Equals, 0) + c.Assert(v, BytesEquals, s.v1) } type mockSnapshot struct { @@ -125,7 +123,7 @@ func (s *mockSnapshot) BatchGet(keys []Key) (map[string][]byte, error) { return m, nil } -func (s *mockSnapshot) Scan(start, end Key, limit int) (map[string][]byte, error) { +func (s *mockSnapshot) RangeGet(start, end Key, limit int) (map[string][]byte, error) { m := make(map[string][]byte) it := s.NewIterator([]byte(start)) defer it.Close() diff --git a/kv/kv.go b/kv/kv.go index da6054fe5c..02a03277f5 100644 --- a/kv/kv.go +++ b/kv/kv.go @@ -103,8 +103,9 @@ type Transaction interface { Get(k Key) ([]byte, error) // BatchGet gets a batch of values from KV store. BatchGet(keys []Key) (map[string][]byte, error) - // Scan gets values in specific range from KV store. - Scan(start, end Key, limit int) (map[string][]byte, error) + // RangeGet gets values in the range [start, end] from KV store. Maximum + // number of values is up to limit. + RangeGet(start, end Key, limit int) (map[string][]byte, error) // Set sets the value for key k as v into KV store. Set(k Key, v []byte) error // Seek searches for the entry with key k in KV store. @@ -146,8 +147,9 @@ type Snapshot interface { Get(k Key) ([]byte, error) // BatchGet gets a batch of values from snapshot. BatchGet(keys []Key) (map[string][]byte, error) - // Scan gets values in specific range from snapshot. - Scan(start, end Key, limit int) (map[string][]byte, error) + // RangeGet gets values in the range [start, end] from snapshot. Maximum + // number of values is up to limit. + RangeGet(start, end Key, limit int) (map[string][]byte, error) // NewIterator gets a new iterator on the snapshot. NewIterator(param interface{}) Iterator // Release releases the snapshot to store. @@ -185,7 +187,6 @@ type Storage interface { Close() error // Storage's unique ID UUID() string - // CurrentVersion returns current max committed version. CurrentVersion() (Version, error) } diff --git a/kv/union_store_test.go b/kv/union_store_test.go index 0eb4a2da24..94910520e0 100644 --- a/kv/union_store_test.go +++ b/kv/union_store_test.go @@ -13,11 +13,7 @@ package kv -import ( - "bytes" - - . "github.com/pingcap/check" -) +import . "github.com/pingcap/check" var _ = Suite(&testUnionStoreSuite{}) @@ -51,11 +47,11 @@ func (s *testUnionStoreSuite) TestGetSet(c *C) { s.store.Set(s.k1, s.v1) v, err := s.us.Get(s.k1) c.Assert(err, IsNil) - c.Assert(bytes.Compare(v, s.v1), Equals, 0) + c.Assert(v, BytesEquals, s.v1) s.us.Set(s.k1, s.v2) v, err = s.us.Get(s.k1) c.Assert(err, IsNil) - c.Assert(bytes.Compare(v, s.v2), Equals, 0) + c.Assert(v, BytesEquals, s.v2) } func (s *testUnionStoreSuite) TestDelete(c *C) { @@ -68,7 +64,7 @@ func (s *testUnionStoreSuite) TestDelete(c *C) { s.us.Set(s.k1, s.v2) v, err := s.us.Get(s.k1) c.Assert(err, IsNil) - c.Assert(bytes.Compare(v, s.v2), Equals, 0) + c.Assert(v, BytesEquals, s.v2) } func (s *testUnionStoreSuite) TestSeek(c *C) { @@ -102,7 +98,7 @@ func checkIterator(c *C, iter Iterator, keys [][]byte, values [][]byte) { v := values[i] c.Assert(iter.Valid(), IsTrue) c.Assert(iter.Key(), Equals, string(k)) - c.Assert(bytes.Compare(iter.Value(), v), Equals, 0) + c.Assert(iter.Value(), BytesEquals, v) c.Assert(iter.Next(), IsNil) } c.Assert(iter.Valid(), IsFalse) diff --git a/store/hbase/snapshot.go b/store/hbase/snapshot.go index a611e3ffc1..e5a840a484 100644 --- a/store/hbase/snapshot.go +++ b/store/hbase/snapshot.go @@ -75,9 +75,10 @@ func (s *hbaseSnapshot) Get(k kv.Key) ([]byte, error) { // BatchGet implements kv.Snapshot.BatchGet(). func (s *hbaseSnapshot) BatchGet(keys []kv.Key) (map[string][]byte, error) { gets := make([]*hbase.Get, len(keys)) + bColFamily, bQualifier := []byte(ColFamily), []byte(Qualifier) for i, key := range keys { g := hbase.NewGet(key) - g.AddColumn([]byte(ColFamily), []byte(Qualifier)) + g.AddColumn(bColFamily, bQualifier) gets[i] = g } rows, err := s.txn.BatchGet(s.storeName, gets) @@ -87,16 +88,16 @@ func (s *hbaseSnapshot) BatchGet(keys []kv.Key) (map[string][]byte, error) { m := make(map[string][]byte) for _, r := range rows { - k := r.Row + k := string(r.Row) v := r.Columns[FmlAndQual].Value - m[string(k)] = v - s.cache[string(k)] = v + m[k] = v + s.cache[k] = v } return m, nil } -// Scan implements kv.Snapshot.Scan(). -func (s *hbaseSnapshot) Scan(start, end kv.Key, limit int) (map[string][]byte, error) { +// RangeGet implements kv.Snapshot.RangeGet(). +func (s *hbaseSnapshot) RangeGet(start, end kv.Key, limit int) (map[string][]byte, error) { scanner := s.txn.GetScanner([]byte(s.storeName), start, end, limit) defer scanner.Close() diff --git a/store/hbase/txn.go b/store/hbase/txn.go index d2593f2456..930982722d 100644 --- a/store/hbase/txn.go +++ b/store/hbase/txn.go @@ -111,8 +111,8 @@ func (txn *hbaseTxn) BatchGet(keys []kv.Key) (map[string][]byte, error) { return txn.UnionStore.Snapshot.BatchGet(encodedKeys) } -func (txn *hbaseTxn) Scan(start, end kv.Key, limit int) (map[string][]byte, error) { - return txn.UnionStore.Snapshot.Scan(kv.EncodeKey(start), kv.EncodeKey(end), limit) +func (txn *hbaseTxn) RangeGet(start, end kv.Key, limit int) (map[string][]byte, error) { + return txn.UnionStore.Snapshot.RangeGet(kv.EncodeKey(start), kv.EncodeKey(end), limit) } // GetInt64 get int64 which created by Inc method. diff --git a/store/localstore/snapshot.go b/store/localstore/snapshot.go index 06a8864f20..fea4efb64f 100644 --- a/store/localstore/snapshot.go +++ b/store/localstore/snapshot.go @@ -120,7 +120,7 @@ func (s *dbSnapshot) BatchGet(keys []kv.Key) (map[string][]byte, error) { return m, nil } -func (s *dbSnapshot) Scan(start, end kv.Key, limit int) (map[string][]byte, error) { +func (s *dbSnapshot) RangeGet(start, end kv.Key, limit int) (map[string][]byte, error) { m := make(map[string][]byte) it := s.NewIterator(start) defer it.Close() diff --git a/store/localstore/txn.go b/store/localstore/txn.go index 6ffaf67866..57b549c37a 100644 --- a/store/localstore/txn.go +++ b/store/localstore/txn.go @@ -129,8 +129,8 @@ func (txn *dbTxn) BatchGet(keys []kv.Key) (map[string][]byte, error) { return txn.UnionStore.Snapshot.BatchGet(encodedKeys) } -func (txn *dbTxn) Scan(start, end kv.Key, limit int) (map[string][]byte, error) { - return txn.UnionStore.Snapshot.Scan(kv.EncodeKey(start), kv.EncodeKey(end), limit) +func (txn *dbTxn) RangeGet(start, end kv.Key, limit int) (map[string][]byte, error) { + return txn.UnionStore.Snapshot.RangeGet(kv.EncodeKey(start), kv.EncodeKey(end), limit) } func (txn *dbTxn) Set(k kv.Key, data []byte) error {