Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store/mockstore: Add raw delete range to mock store #6156

Closed
74 changes: 66 additions & 8 deletions store/mockstore/mocktikv/mock_tikv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package mocktikv

import (
"bytes"
"strings"
"testing"

Expand All @@ -33,24 +34,81 @@ type testMockTiKVSuite struct {

type testMarshal struct{}

// testMvccStore is used to test MvccStore implementation.
type testMvccStore struct {
testMockTiKVSuite
}

// testMVCCLevelDB is used to test MVCCLevelDB implementation.
type testMVCCLevelDB struct {
testMockTiKVSuite
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Friendly remind: avoid to move code if it's not necessary.
Because it would make the code reviewer's work easier.


var (
_ = Suite(&testMvccStore{})
_ = Suite(&testMVCCLevelDB{})
_ = Suite(testMarshal{})
)

// testMvccStore is used to test MvccStore implementation.
type testMvccStore struct {
testMockTiKVSuite
}

func (s *testMvccStore) SetUpTest(c *C) {
s.store = NewMvccStore()
}

// testMVCCLevelDB is used to test MVCCLevelDB implementation.
type testMVCCLevelDB struct {
testMockTiKVSuite
func (s *testMvccStore) mustRawGet(c *C, key []byte, value []byte) {
rawkv := s.store.(RawKV)
actualValue := rawkv.RawGet(key)
c.Assert(actualValue, DeepEquals, value)
}

func (s *testMvccStore) rawPut(c *C, key []byte, value []byte) {
rawkv := s.store.(RawKV)
rawkv.RawPut(key, value)
}

func (s *testMvccStore) checkRawData(c *C, expected map[string]string) {
rawkv := s.store.(RawKV)
pairs := rawkv.RawScan([]byte(""), []byte("zzzzzzzz"), len(expected)+1)
c.Assert(len(pairs), Equals, len(expected))

actual := map[string]string{}
for _, pair := range pairs {
actual[string(pair.Key)] = string(pair.Value)
}

c.Assert(actual, DeepEquals, expected)
}

func (s *testMvccStore) mustRawDeleteRange(c *C, startKey, endKey []byte, expectedData map[string]string) {
rawkv := s.store.(RawKV)
rawkv.RawDeleteRange(startKey, endKey)

for strKey := range expectedData {
key := []byte(strKey)
if bytes.Compare(startKey, key) <= 0 && bytes.Compare(key, endKey) < 0 {
delete(expectedData, strKey)
}
}

s.checkRawData(c, expectedData)
}

func (s *testMvccStore) TestRawDeleteRange(c *C) {
testData := map[string]string{}

for i := byte('a'); i <= byte('g'); i++ {
key := []byte{i}
value := []byte{'v', i}
s.rawPut(c, key, value)
testData[string(key)] = string(value)
}

s.checkRawData(c, testData)

s.mustRawDeleteRange(c, []byte("b"), []byte("b"), testData)
s.mustRawDeleteRange(c, []byte("d"), []byte("f"), testData)
s.mustRawDeleteRange(c, []byte("b"), []byte("c"), testData)
s.mustRawDeleteRange(c, []byte("c11"), []byte("c99"), testData)
s.mustRawDeleteRange(c, []byte("a"), []byte("z"), testData)
}

func (s *testMockTiKVSuite) SetUpTest(c *C) {
Expand Down
18 changes: 18 additions & 0 deletions store/mockstore/mocktikv/mvcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ type RawKV interface {
RawScan(startKey, endKey []byte, limit int) []Pair
RawPut(key, value []byte)
RawDelete(key []byte)
RawDeleteRange(startKey, endKey []byte)
}

// MVCCDebugger is for debugging.
Expand Down Expand Up @@ -780,6 +781,23 @@ func (s *MvccStore) RawDelete(key []byte) {
s.rawkv.Delete(newRawEntry(key))
}

// RawDeleteRange deletes all key-value pairs in a given range
func (s *MvccStore) RawDeleteRange(startKey, endKey []byte) {
s.Lock()
defer s.Unlock()

var entriesToDelete []*rawEntry
iterator := func(item btree.Item) bool {
entriesToDelete = append(entriesToDelete, item.(*rawEntry))
return true
}
s.rawkv.AscendRange(newRawEntry(startKey), newRawEntry(endKey), iterator)

for _, entry := range entriesToDelete {
s.rawkv.Delete(entry)
}
}

// RawScan reads up to a limited number of rawkv Pairs.
func (s *MvccStore) RawScan(startKey, endKey []byte, limit int) []Pair {
s.RLock()
Expand Down