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)
| 48 | // |
| 49 | // Refer: https://github.com/facebook/mysql-5.6/wiki/MyRocks-record-format#memcomparable-format |
| 50 | func 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`. |
| 76 | func EncodeBytesExt(b []byte, data []byte, isRawKv bool) []byte { |