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

[CodecV2] cache keyspaceID when creating codecV2 #812

Merged
merged 2 commits into from
May 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions internal/apicodec/codec_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ var (
txnModePrefix byte = 'x'
keyspacePrefixLen = 4

// maxKeyspaceID is the maximum value of keyspaceID, its value is uint24Max.
maxKeyspaceID = uint32(0xFFFFFF)

// errKeyOutOfBound happens when key to be decoded lies outside the keyspace's range.
errKeyOutOfBound = errors.New("given key does not belong to the keyspace")
)
Expand All @@ -46,21 +49,27 @@ func BuildKeyspaceName(name string) string {

// codecV2 is used to encode/decode keys and request into APIv2 format.
type codecV2 struct {
prefix []byte
endKey []byte
memCodec memCodec
keyspaceID KeyspaceID
prefix []byte
endKey []byte
memCodec memCodec
}

// NewCodecV2 returns a codec that can be used to encode/decode
// keys and requests to and from APIv2 format.
func NewCodecV2(mode Mode, keyspaceID uint32) (Codec, error) {
if keyspaceID > maxKeyspaceID {
return nil, errors.Errorf("keyspaceID %d is out of range, maximum is %d", keyspaceID, maxKeyspaceID)
}
prefix, err := getIDByte(keyspaceID)
if err != nil {
return nil, err
}

// Region keys in CodecV2 are always encoded in memory comparable form.
codec := &codecV2{memCodec: &memComparableCodec{}}
codec := &codecV2{
keyspaceID: KeyspaceID(keyspaceID),
// Region keys in CodecV2 are always encoded in memory comparable form.
memCodec: &memComparableCodec{},
}
codec.prefix = make([]byte, 4)
codec.endKey = make([]byte, 4)
switch mode {
Expand Down Expand Up @@ -97,9 +106,7 @@ func (c *codecV2) GetKeyspace() []byte {
}

func (c *codecV2) GetKeyspaceID() KeyspaceID {
prefix := append([]byte{}, c.prefix...)
prefix[0] = 0
return KeyspaceID(binary.BigEndian.Uint32(prefix))
return c.keyspaceID
}

func (c *codecV2) GetAPIVersion() kvrpcpb.APIVersion {
Expand Down