Skip to content

Commit

Permalink
txn-file: Get first key inside range of both region & txn chunk (#1455)
Browse files Browse the repository at this point in the history
 

Signed-off-by: Ping Yu <[email protected]>
  • Loading branch information
pingyu authored Sep 2, 2024
1 parent 196943c commit 8cc71f4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
7 changes: 6 additions & 1 deletion txnkv/transaction/txn_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,18 @@ func (r *txnChunkRange) getOverlapRegions(c *locate.RegionCache, bo *retry.Backo
regions := make([]*locate.KeyLocation, 0)
firstKeys := make([][]byte, 0)
startKey := r.smallest
exclusiveBiggest := kv.NextKey(r.biggest)
for bytes.Compare(startKey, r.biggest) <= 0 {
loc, err := c.LocateKey(bo, startKey)
if err != nil {
logutil.Logger(bo.GetCtx()).Error("locate key failed", zap.Error(err), zap.String("startKey", kv.StrKey(startKey)))
return nil, nil, errors.Wrap(err, "locate key failed")
}
firstKey, ok := MutationsHasDataInRange(mutations, loc.StartKey, loc.EndKey)
firstKey, ok := MutationsHasDataInRange(
mutations,
util.GetMaxStartKey(r.smallest, loc.StartKey),
util.GetMinEndKey(exclusiveBiggest, loc.EndKey),
)
if ok {
regions = append(regions, loc)
firstKeys = append(firstKeys, firstKey)
Expand Down
21 changes: 21 additions & 0 deletions util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
package util

import (
"bytes"
"context"
"encoding/hex"
"fmt"
Expand Down Expand Up @@ -207,3 +208,23 @@ func HexRegionKey(key []byte) []byte {
func HexRegionKeyStr(key []byte) string {
return String(HexRegionKey(key))
}

func GetMaxStartKey(lhs []byte, rhs []byte) []byte {
if bytes.Compare(lhs, rhs) > 0 {
return lhs
}
return rhs
}

func GetMinEndKey(lhs []byte, rhs []byte) []byte {
if len(rhs) == 0 {
return lhs
}
if len(lhs) == 0 {
return rhs
}
if bytes.Compare(lhs, rhs) < 0 {
return lhs
}
return rhs
}
35 changes: 35 additions & 0 deletions util/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,38 @@ func TestBytesToString(t *testing.T) {
assert.Equal(str, String(bytes))
}
}

func TestGetMaxStartKey(t *testing.T) {
assert := assert.New(t)

cases := []struct {
lhs, rhs, expected string
}{
{"", "", ""},
{"", "a", "a"},
{"a", "a", "a"},
}

for _, c := range cases {
assert.Equal([]byte(c.expected), GetMaxStartKey([]byte(c.lhs), []byte(c.rhs)))
assert.Equal([]byte(c.expected), GetMaxStartKey([]byte(c.rhs), []byte(c.lhs)))
}
}

func TestGetMinEndKey(t *testing.T) {
assert := assert.New(t)

cases := []struct {
lhs, rhs, expected string
}{
{"", "", ""},
{"a", "", "a"},
{"a", "a", "a"},
{"a", "b", "a"},
}

for _, c := range cases {
assert.Equal([]byte(c.expected), GetMinEndKey([]byte(c.lhs), []byte(c.rhs)))
assert.Equal([]byte(c.expected), GetMinEndKey([]byte(c.rhs), []byte(c.lhs)))
}
}

0 comments on commit 8cc71f4

Please sign in to comment.