Skip to content

Commit

Permalink
txnkv: fix the issue that deleteRange cannot use nil as endkey (#429)
Browse files Browse the repository at this point in the history
Signed-off-by: disksing <[email protected]>
  • Loading branch information
disksing authored Feb 24, 2022
1 parent f43cc3a commit e5f932a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion integration_tests/delete_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (s *testDeleteRangeSuite) deleteRange(startKey []byte, endKey []byte) int {
func deleteRangeFromMap(m map[string]string, startKey []byte, endKey []byte) {
for keyStr := range m {
key := []byte(keyStr)
if bytes.Compare(startKey, key) <= 0 && bytes.Compare(key, endKey) < 0 {
if bytes.Compare(startKey, key) <= 0 && (len(endKey) == 0 || bytes.Compare(key, endKey) < 0) {
delete(m, keyStr)
}
}
Expand Down Expand Up @@ -160,4 +160,5 @@ func (s *testDeleteRangeSuite) TestDeleteRange() {
s.mustDeleteRange([]byte("d0\x00"), []byte("d1\x00"), testData, 1)
s.mustDeleteRange([]byte("c5"), []byte("d5"), testData, 2)
s.mustDeleteRange([]byte("a"), []byte("z"), testData, 4)
s.mustDeleteRange(nil, nil, testData, 4)
}
6 changes: 5 additions & 1 deletion internal/mockstore/mocktikv/mvcc_leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,11 @@ func (mvcc *MVCCLevelDB) GC(startKey, endKey []byte, safePoint uint64) error {

// DeleteRange implements the MVCCStore interface.
func (mvcc *MVCCLevelDB) DeleteRange(startKey, endKey []byte) error {
return mvcc.doRawDeleteRange(codec.EncodeBytes(nil, startKey), codec.EncodeBytes(nil, endKey))
var end []byte
if len(endKey) > 0 {
end = codec.EncodeBytes(nil, endKey)
}
return mvcc.doRawDeleteRange(codec.EncodeBytes(nil, startKey), end)
}

// Close calls leveldb's Close to free resources.
Expand Down
8 changes: 6 additions & 2 deletions txnkv/rangetask/delete_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (t *DeleteRangeTask) sendReqOnRange(ctx context.Context, r kv.KeyRange) (Ta
default:
}

if bytes.Compare(startKey, rangeEndKey) >= 0 {
if len(rangeEndKey) > 0 && bytes.Compare(startKey, rangeEndKey) >= 0 {
break
}

Expand All @@ -135,8 +135,9 @@ func (t *DeleteRangeTask) sendReqOnRange(ctx context.Context, r kv.KeyRange) (Ta

// Delete to the end of the region, except if it's the last region overlapping the range
endKey := loc.EndKey
isLast := len(endKey) == 0 || (len(rangeEndKey) > 0 && bytes.Compare(endKey, rangeEndKey) >= 0)
// If it is the last region
if loc.Contains(rangeEndKey) {
if isLast {
endKey = rangeEndKey
}

Expand Down Expand Up @@ -169,6 +170,9 @@ func (t *DeleteRangeTask) sendReqOnRange(ctx context.Context, r kv.KeyRange) (Ta
return stat, errors.Errorf("unexpected delete range err: %v", err)
}
stat.CompletedRegions++
if isLast {
break
}
startKey = endKey
}

Expand Down

0 comments on commit e5f932a

Please sign in to comment.