Documentation
¶
Overview ¶
Package cockroachkvs provides CockroachDB's key-value schema and comparer implementation.
Index ¶
- Constants
- Variables
- func AppendTimestamp(key []byte, walltime uint64, logical uint32) []byte
- func Compare(a, b []byte) int
- func ComparePointSuffixes(a, b []byte) int
- func CompareRangeSuffixes(a, b []byte) int
- func DecodeEngineKey(b []byte) (roachKey, version []byte, ok bool)
- func DecodeMVCCTimestampSuffix(encodedTS []byte) (wallTime uint64, logical uint32, err error)
- func EncodeKey(dst, roachKey, version []byte) []byte
- func EncodeMVCCKey(dst []byte, key []byte, walltime uint64, logical uint32) []byte
- func EncodeTimestamp(key []byte, walltime uint64, logical uint32) []byte
- func Equal(a, b []byte) bool
- func FormatKey(key []byte) fmt.Formatter
- func FormatKeySuffix(suffix []byte) fmt.Formatter
- func NewMVCCTimeIntervalFilter(minWallTime, maxWallTime uint64) sstable.BlockPropertyFilter
- func NewTimestampSuffix(wallTime uint64, logical uint32) []byte
- func ParseFormattedKey(formattedKey string) []byte
- func ParseFormattedKeySuffix(formattedSuffix string) []byte
- func RandomKVs(rng *rand.Rand, count int, cfg KeyGenConfig, valueLen int) (keys, vals [][]byte)
- func Split(key []byte) int
- type KeyGenConfig
- type MVCCBlockIntervalSuffixReplacer
- type MVCCWallTimeIntervalRangeKeyMask
Constants ¶
const MaxSuffixLen = 1 + max(
engineKeyVersionLockTableLen,
engineKeyVersionWallLogicalAndSyntheticTimeLen,
engineKeyVersionWallAndLogicalTimeLen,
engineKeyVersionWallTimeLen,
)
MaxSuffixLen is the maximum length of the CockroachDB key suffix.
Variables ¶
var BlockPropertyCollectors = []func() pebble.BlockPropertyCollector{ func() pebble.BlockPropertyCollector { return sstable.NewBlockIntervalCollector( mvccWallTimeIntervalCollector, pebbleIntervalMapper{}, MVCCBlockIntervalSuffixReplacer{}, ) }, }
BlockPropertyCollectors is a list of constructors for block-property collectors used by CockroachDB.
var Comparer = base.Comparer{ Split: Split, ComparePointSuffixes: ComparePointSuffixes, CompareRangeSuffixes: CompareRangeSuffixes, Compare: Compare, Equal: Equal, AbbreviatedKey: func(k []byte) uint64 { key, ok := getKeyPartFromEngineKey(k) if !ok { return 0 } return base.DefaultComparer.AbbreviatedKey(key) }, FormatKey: FormatKey, Separator: func(dst, a, b []byte) []byte { if len(a) == 0 || len(b) == 0 { panic(errors.AssertionFailedf("cannot separate empty keys")) } aKey, ok := getKeyPartFromEngineKey(a) if !ok { return append(dst, a...) } bKey, ok := getKeyPartFromEngineKey(b) if !ok { return append(dst, a...) } if bytes.Equal(aKey, bKey) || len(aKey) == 0 || len(bKey) == 0 { return append(dst, a...) } n := len(dst) dst = base.DefaultComparer.Separator(dst, aKey, bKey) buf := dst[n:] if bytes.Equal(aKey, buf) { return append(dst[:n], a...) } return append(dst, 0) }, Successor: func(dst, a []byte) []byte { if len(a) == 0 { return append(dst, 0x00) } aKey, ok := getKeyPartFromEngineKey(a) if !ok { return append(dst, a...) } n := len(dst) dst = base.DefaultComparer.Successor(dst, aKey) buf := dst[n:] if bytes.Equal(aKey, buf) { return append(dst[:n], a...) } return append(dst, 0) }, ImmediateSuccessor: func(dst, a []byte) []byte { return append(append(dst, a...), 0) }, ValidateKey: validateEngineKey, Name: "cockroach_comparator", }
Comparer is a base.Comparer for CockroachDB keys.
var KeySchema = colblk.KeySchema{ Name: "crdb1", HeaderSize: 1, ColumnTypes: []colblk.DataType{ // contains filtered or unexported fields }, NewKeyWriter: func() colblk.KeyWriter { return makeCockroachKeyWriter() }, InitKeySeekerMetadata: func(meta *colblk.KeySeekerMetadata, d *colblk.DataBlockDecoder) { ks := (*cockroachKeySeeker)(unsafe.Pointer(meta)) ks.init(d) }, KeySeeker: func(meta *colblk.KeySeekerMetadata) colblk.KeySeeker { return (*cockroachKeySeeker)(unsafe.Pointer(meta)) }, }
Functions ¶
func AppendTimestamp ¶
AppendTimestamp appends an encoded MVCC timestamp onto key, returning the new key. The provided key should already have the 0x00 sentinel byte (i.e., key should be a proper prefix from the perspective of Pebble).
func Compare ¶
Compare compares cockroach keys, including the version (which could be MVCC timestamps).
func ComparePointSuffixes ¶
ComparePointSuffixes compares suffixes of Cockroach point keys (which are composed of the version and a trailing version-length byte); the version can be an MVCC timestamp or a lock key. ComparePointSuffixes differs from CompareRangeSuffixes in that the ComparePointSuffixes normalizes the suffixes. Ideally we'd have one function that implemented the semantics of EnginePointSuffixCompare, but due to historical reasons, range key suffix comparisons must not perform normalization.
func CompareRangeSuffixes ¶
CompareRangeSuffixes implements Comparer.CompareRangeSuffixes. It compares cockroach suffixes (which are composed of the version and a trailing sentinel byte); the version can be an MVCC timestamp or a lock key. It is stricter than ComparePointSuffixes due to historical reasons; see https://github.com/cockroachdb/cockroach/issues/130533
func DecodeEngineKey ¶
DecodeEngineKey decodes the given bytes as an EngineKey.
func DecodeMVCCTimestampSuffix ¶
DecodeMVCCTimestampSuffix decodes an MVCC timestamp from its Pebble representation, including the length suffix.
func EncodeKey ¶
EncodeKey serializes an engine key from a raochpb.Key and a version, appending the result to dst and returning the result.
func EncodeMVCCKey ¶
EncodeMVCCKey encodes a MVCC key into dst, growing dst as necessary.
func EncodeTimestamp ¶
EncodeTimestamp encodes a MVCC timestamp into a key, returning the new key. The key's capacity must be sufficiently large to hold the encoded timestamp.
func FormatKeySuffix ¶
FormatKeySuffix returns a formatter for the user key suffix.
func NewMVCCTimeIntervalFilter ¶
func NewMVCCTimeIntervalFilter(minWallTime, maxWallTime uint64) sstable.BlockPropertyFilter
NewMVCCTimeIntervalFilter constructs a new block-property filter that skips keys that encode timestamps with wall times that do not fall within the interval [minWallTime,maxWallTime].
func NewTimestampSuffix ¶
NewTimestampSuffix allocates and encodes a new suffix byte slice encoding the provided timestamp.
func ParseFormattedKey ¶
ParseFormattedKey parses a human-readable representation of a Cockroach key.
ParseFormattedKey expects the roach key and version to be delimited by a '@' character. The '@' may be omitted for version-less keys. Roach keys may be quoted using Go syntax. The version may be:
- A MVCC timestamp in one of the formats (these are cribbed from the Cockroach repository's hlc timestamp formatting): a. <WALLTIME-SECONDS> b. <WALLTIME-SECONDS>.<WALLTIME-NANOS> c. <WALLTIME-SECONDS>,<LOGICAL> d. <WALLTIME-SECONDS>.<WALLTIME-NANOS>,<LOGICAL>
- A lock table key in the format: <STRENGTH-BYTE>,<TXNUUID>
- A raw hex-encoded string prefixed with "hex:".
func ParseFormattedKeySuffix ¶
ParseFormattedKeySuffix parses a human-readable representation of a Cockroach key's version. The formatted suffix may be:
- A MVCC timestamp in one of the formats (these are cribbed from the Cockroach repository's hlc timestamp formatting): a. <WALLTIME-SECONDS> b. <WALLTIME-SECONDS>.<WALLTIME-NANOS> c. <WALLTIME-SECONDS>,<LOGICAL> d. <WALLTIME-SECONDS>.<WALLTIME-NANOS>,<LOGICAL>
- A lock table key in the format: <STRENGTH-BYTE>,<TXN-UUID>
Types ¶
type KeyGenConfig ¶
type KeyGenConfig struct {
PrefixAlphabetLen int // Number of bytes in the alphabet used for the prefix.
RoachKeyLen int // Number of bytes in the prefix (without the 0 sentinel byte).
AvgKeysPerPrefix int // Average number of keys (with varying suffixes) per prefix.
BaseWallTime uint64 // Smallest MVCC WallTime.
PercentLogical int // Percent of MVCC keys with non-zero MVCC logical time.
PercentEmptySuffix int // Percent of keys with empty suffix.
PercentLockSuffix int // Percent of keys with lock suffix.
}
KeyGenConfig configures the shape of the random keys generated.
func (KeyGenConfig) String ¶
func (cfg KeyGenConfig) String() string
type MVCCBlockIntervalSuffixReplacer ¶
type MVCCBlockIntervalSuffixReplacer struct{}
MVCCBlockIntervalSuffixReplacer implements the sstable.BlockIntervalSuffixReplacer interface for MVCC timestamp intervals.
func (MVCCBlockIntervalSuffixReplacer) ApplySuffixReplacement ¶
func (MVCCBlockIntervalSuffixReplacer) ApplySuffixReplacement( interval sstable.BlockInterval, newSuffix []byte, ) (sstable.BlockInterval, error)
type MVCCWallTimeIntervalRangeKeyMask ¶
type MVCCWallTimeIntervalRangeKeyMask struct {
sstable.BlockIntervalFilter
}
MVCCWallTimeIntervalRangeKeyMask implements pebble.BlockPropertyFilterMask for filtering blocks using the MVCCTimeInterval block property during range key masking.
func (*MVCCWallTimeIntervalRangeKeyMask) Init ¶
func (m *MVCCWallTimeIntervalRangeKeyMask) Init()
Init initializees the mask and its block interval filter.
func (*MVCCWallTimeIntervalRangeKeyMask) SetSuffix ¶
func (m *MVCCWallTimeIntervalRangeKeyMask) SetSuffix(suffix []byte) error
SetSuffix implements the pebble.BlockPropertyFilterMask interface.