Skip to content

Commit

Permalink
metamorphic: add CockroachKeyFormat
Browse files Browse the repository at this point in the history
Add a new KeyFormat that uses the cockroach encoding of KV pairs in the
metamorphic test, expanding test coverage to cover the cockroachkvs package.

Fixes cockroachdb#4167.
  • Loading branch information
jbowens committed Jan 29, 2025
1 parent 310fac7 commit bb38471
Show file tree
Hide file tree
Showing 5 changed files with 536 additions and 1 deletion.
25 changes: 25 additions & 0 deletions cockroachkvs/cockroachkvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@ func EncodeTimestamp(key []byte, walltime uint64, logical uint32) []byte {
return key
}

// NewTimestampSuffix allocates and encodes a new suffix byte slice encoding the
// provided timestamp.
func NewTimestampSuffix(wallTime uint64, logical uint32) []byte {
switch {
case wallTime == 0 && logical == 0:
return nil
case logical == 0:
v := make([]byte, suffixLenWithWall)
binary.BigEndian.PutUint64(v, wallTime)
v[suffixLenWithWall-1] = suffixLenWithWall
return v
default:
v := make([]byte, suffixLenWithLogical)
binary.BigEndian.PutUint64(v, wallTime)
binary.BigEndian.PutUint32(v[8:], logical)
v[suffixLenWithLogical-1] = suffixLenWithLogical
return v
}
}

// DecodeMVCCTimestampSuffix decodes an MVCC timestamp from its Pebble representation,
// including the length suffix.
func DecodeMVCCTimestampSuffix(encodedTS []byte) (wallTime uint64, logical uint32, err error) {
Expand Down Expand Up @@ -1059,6 +1079,11 @@ func (fk formatKey) Format(f fmt.State, c rune) {
formatKeySuffix(fk[i:]).Format(f, c)
}

// FormatKeySuffix returns a formatter for the user key suffix.
func FormatKeySuffix(suffix []byte) fmt.Formatter {
return formatKeySuffix(suffix)
}

type formatKeySuffix []byte

func (fk formatKeySuffix) Format(f fmt.State, c rune) {
Expand Down
3 changes: 2 additions & 1 deletion internal/metamorphic/metaflags/meta_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ func (c *CommonFlags) KeyFormat() metamorphic.KeyFormat {

// KeyFormats is a map of available key formats.
var KeyFormats = map[string]metamorphic.KeyFormat{
"testkeys": metamorphic.TestkeysKeyFormat,
"testkeys": metamorphic.TestkeysKeyFormat,
"cockroachkvs": metamorphic.CockroachKeyFormat,
}

func initCommonFlags() *CommonFlags {
Expand Down
Loading

0 comments on commit bb38471

Please sign in to comment.