MCPcopy
hub / github.com/pingcap/tidb / EncodeBytes

Function EncodeBytes

pkg/util/codec/bytes.go:50–73  ·  view source on GitHub ↗

EncodeBytes guarantees the encoded value is in ascending order for comparison, encoding with the following rule: [group1][marker1]...[groupN][markerN] group is 8 bytes slice which is padding with 0. marker is `0xFF - padding 0 count` For example: [] -> [0, 0, 0, 0, 0, 0, 0, 0, 247] [1, 2, 3]

(b []byte, data []byte)

Source from the content-addressed store, hash-verified

48//
49// Refer: https://github.com/facebook/mysql-5.6/wiki/MyRocks-record-format#memcomparable-format
50func EncodeBytes(b []byte, data []byte) []byte {
51 // Allocate more space to avoid unnecessary slice growing.
52 // Assume that the byte slice size is about `(len(data) / encGroupSize + 1) * (encGroupSize + 1)` bytes,
53 // that is `(len(data) / 8 + 1) * 9` in our implement.
54 dLen := len(data)
55 reallocSize := (dLen/encGroupSize + 1) * (encGroupSize + 1)
56 result := reallocBytes(b, reallocSize)
57 for idx := 0; idx <= dLen; idx += encGroupSize {
58 remain := dLen - idx
59 padCount := 0
60 if remain >= encGroupSize {
61 result = append(result, data[idx:idx+encGroupSize]...)
62 } else {
63 padCount = encGroupSize - remain
64 result = append(result, data[idx:]...)
65 result = append(result, pads[:padCount]...)
66 }
67
68 marker := encMarker - byte(padCount)
69 result = append(result, marker)
70 }
71
72 return result
73}
74
75// EncodeBytesExt is an extension of `EncodeBytes`, which will not encode for `isRawKv = true` but just append `data` to `b`.
76func EncodeBytesExt(b []byte, data []byte, isRawKv bool) []byte {

Callers 15

TestRewriteFileKeysFunction · 0.92
rewriteRawKeyFunction · 0.92
RewriteAndEncodeRawKeyFunction · 0.92
EncodeKeyPrefixFunction · 0.92
prepareDataFunction · 0.92
TestLogSplitStrategyFunction · 0.92
fakeRowKeyFunction · 0.92
TestRegionSplitFunction · 0.92
TestGetKeyRangeByModeFunction · 0.92

Calls 1

reallocBytesFunction · 0.70

Tested by 15

TestRewriteFileKeysFunction · 0.74
prepareDataFunction · 0.74
TestLogSplitStrategyFunction · 0.74
fakeRowKeyFunction · 0.74
TestRegionSplitFunction · 0.74
TestGetKeyRangeByModeFunction · 0.74
generateRegionsFunction · 0.74
TestSplitScatterFunction · 0.74
TestGetSplitKeyPerRegionFunction · 0.74