mirror of https://github.com/tikv/client-go.git
region_cache: rename LocateBucketV2 to LocateBucket and let the old LocateBucket be the internal function of the new LocateBucket (#496)
Signed-off-by: SpadeA-Tang <u6748471@anu.edu.au>
This commit is contained in:
parent
ff5e35ac28
commit
2be4a58dbc
|
|
@ -755,13 +755,13 @@ func (l *KeyLocation) GetBucketVersion() uint64 {
|
||||||
return l.Buckets.GetVersion()
|
return l.Buckets.GetVersion()
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocateBucketV2 will not return nil if the key is in the region.
|
// LocateBucket calls locateBucket and check the result.
|
||||||
// LocateBucketV2 is similar with LocateBucket. The difference is that when the key is in [KeyLocation.StartKey, first Bucket key)
|
// When the key is in [KeyLocation.StartKey, first Bucket key), the result returned by locateBucket will be nil
|
||||||
// it will return Bucket{KeyLocation.StartKey, first Bucket key} rather than nil --- it's reasonable to assume that
|
// as there's no bucket containing this key. LocateBucket will return Bucket{KeyLocation.StartKey, first Bucket key}
|
||||||
// Bucket{KeyLocation.StartKey, first Bucket key} is a bucket belonging to the region. Key in [last Bucket key, KeyLocation.EndKey)
|
// --- it's reasonable to assume that Bucket{KeyLocation.StartKey, first Bucket key} is a bucket belonging to the region.
|
||||||
// is handled similarly.
|
// Key in [last Bucket key, KeyLocation.EndKey) is handled similarly.
|
||||||
func (l *KeyLocation) LocateBucketV2(key []byte) *Bucket {
|
func (l *KeyLocation) LocateBucket(key []byte) *Bucket {
|
||||||
bucket := l.LocateBucket(key)
|
bucket := l.locateBucket(key)
|
||||||
if bucket != nil || !l.Contains(key) {
|
if bucket != nil || !l.Contains(key) {
|
||||||
return bucket
|
return bucket
|
||||||
}
|
}
|
||||||
|
|
@ -789,8 +789,8 @@ func (l *KeyLocation) LocateBucketV2(key []byte) *Bucket {
|
||||||
return bucket
|
return bucket
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocateBucket returns the bucket the key is located.
|
// locateBucket returns the bucket the key is located.
|
||||||
func (l *KeyLocation) LocateBucket(key []byte) *Bucket {
|
func (l *KeyLocation) locateBucket(key []byte) *Bucket {
|
||||||
keys := l.Buckets.GetKeys()
|
keys := l.Buckets.GetKeys()
|
||||||
searchLen := len(keys) - 1
|
searchLen := len(keys) - 1
|
||||||
i := sort.Search(searchLen, func(i int) bool {
|
i := sort.Search(searchLen, func(i int) bool {
|
||||||
|
|
|
||||||
|
|
@ -1411,14 +1411,14 @@ func (s *testRegionCacheSuite) TestBuckets() {
|
||||||
buckets := cachedRegion.getStore().buckets
|
buckets := cachedRegion.getStore().buckets
|
||||||
s.Equal(defaultBuckets, buckets)
|
s.Equal(defaultBuckets, buckets)
|
||||||
|
|
||||||
// test LocateBucket
|
// test locateBucket
|
||||||
loc, err := s.cache.LocateKey(s.bo, []byte("a"))
|
loc, err := s.cache.LocateKey(s.bo, []byte("a"))
|
||||||
s.NotNil(loc)
|
s.NotNil(loc)
|
||||||
s.Nil(err)
|
s.Nil(err)
|
||||||
s.Equal(buckets, loc.Buckets)
|
s.Equal(buckets, loc.Buckets)
|
||||||
s.Equal(buckets.GetVersion(), loc.GetBucketVersion())
|
s.Equal(buckets.GetVersion(), loc.GetBucketVersion())
|
||||||
for _, key := range [][]byte{{}, {'a' - 1}, []byte("a"), []byte("a0"), []byte("b"), []byte("c")} {
|
for _, key := range [][]byte{{}, {'a' - 1}, []byte("a"), []byte("a0"), []byte("b"), []byte("c")} {
|
||||||
b := loc.LocateBucket(key)
|
b := loc.locateBucket(key)
|
||||||
s.NotNil(b)
|
s.NotNil(b)
|
||||||
s.True(b.Contains(key))
|
s.True(b.Contains(key))
|
||||||
}
|
}
|
||||||
|
|
@ -1426,7 +1426,7 @@ func (s *testRegionCacheSuite) TestBuckets() {
|
||||||
loc.Buckets = proto.Clone(loc.Buckets).(*metapb.Buckets)
|
loc.Buckets = proto.Clone(loc.Buckets).(*metapb.Buckets)
|
||||||
loc.Buckets.Keys = [][]byte{[]byte("b"), []byte("c"), []byte("d")}
|
loc.Buckets.Keys = [][]byte{[]byte("b"), []byte("c"), []byte("d")}
|
||||||
for _, key := range [][]byte{[]byte("a"), []byte("d"), []byte("e")} {
|
for _, key := range [][]byte{[]byte("a"), []byte("d"), []byte("e")} {
|
||||||
b := loc.LocateBucket(key)
|
b := loc.locateBucket(key)
|
||||||
s.Nil(b)
|
s.Nil(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1506,7 +1506,7 @@ func (s *testRegionCacheSuite) TestBuckets() {
|
||||||
waitUpdateBuckets(newBuckets, []byte("a"))
|
waitUpdateBuckets(newBuckets, []byte("a"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *testRegionCacheSuite) TestLocateBucketV2() {
|
func (s *testRegionCacheSuite) TestLocateBucket() {
|
||||||
// proto.Clone clones []byte{} to nil and [][]byte{nil or []byte{}} to [][]byte{[]byte{}}.
|
// proto.Clone clones []byte{} to nil and [][]byte{nil or []byte{}} to [][]byte{[]byte{}}.
|
||||||
// nilToEmtpyBytes unifies it for tests.
|
// nilToEmtpyBytes unifies it for tests.
|
||||||
nilToEmtpyBytes := func(s []byte) []byte {
|
nilToEmtpyBytes := func(s []byte) []byte {
|
||||||
|
|
@ -1525,7 +1525,7 @@ func (s *testRegionCacheSuite) TestLocateBucketV2() {
|
||||||
s.NotNil(loc)
|
s.NotNil(loc)
|
||||||
s.Nil(err)
|
s.Nil(err)
|
||||||
for _, key := range [][]byte{{}, {'a' - 1}, []byte("a"), []byte("a0"), []byte("b"), []byte("c")} {
|
for _, key := range [][]byte{{}, {'a' - 1}, []byte("a"), []byte("a0"), []byte("b"), []byte("c")} {
|
||||||
b := loc.LocateBucket(key)
|
b := loc.locateBucket(key)
|
||||||
s.NotNil(b)
|
s.NotNil(b)
|
||||||
s.True(b.Contains(key))
|
s.True(b.Contains(key))
|
||||||
}
|
}
|
||||||
|
|
@ -1542,9 +1542,9 @@ func (s *testRegionCacheSuite) TestLocateBucketV2() {
|
||||||
s.NotNil(loc)
|
s.NotNil(loc)
|
||||||
s.Nil(err)
|
s.Nil(err)
|
||||||
for _, key := range [][]byte{{'a' - 1}, []byte("c")} {
|
for _, key := range [][]byte{{'a' - 1}, []byte("c")} {
|
||||||
b := loc.LocateBucket(key)
|
b := loc.locateBucket(key)
|
||||||
s.Nil(b)
|
s.Nil(b)
|
||||||
b = loc.LocateBucketV2(key)
|
b = loc.LocateBucket(key)
|
||||||
s.NotNil(b)
|
s.NotNil(b)
|
||||||
s.True(b.Contains(key))
|
s.True(b.Contains(key))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue