Documentation
¶
Overview ¶
Package encoder 实现 VDS SQL 层的行编码/解码与 key 编码工具。
行格式:[schema_version: 4B][null_bitmap: ceil(N/8)B][col_data...] Key 命名空间:
- 0x00 <系统数据> — catalog 系统表
- 0x01 <table_id: 4B> <pk_bytes> — 用户数据行
Index ¶
- func CompositeIndexColBytesLen(colValues []types.Value, colTypes []types.TypeID) (int, error)
- func DecodeCompositeIndexKey(data []byte, colTypes []types.TypeID) ([]types.Value, int, error)
- func DecodeRow(data []byte, table TableDesc) ([]types.Value, error)
- func EncodePrimaryKey(pkValue types.Value, pkType types.TypeID) ([]byte, error)
- func EncodeRow(row []types.Value, table TableDesc) ([]byte, error)
- func MakeCompositeIndexKey(tableID uint32, indexID uint8, colValues []types.Value, ...) ([]byte, error)
- func MakeCompositeNonUniqueIndexKey(tableID uint32, indexID uint8, colValues []types.Value, ...) ([]byte, error)
- func MakeIndexKey(tableID uint32, indexID uint8, colValue types.Value, colType types.TypeID) ([]byte, error)
- func MakeIndexPrefix(tableID uint32, indexID uint8) []byte
- func MakeIndexPrefixEnd(tableID uint32, indexID uint8) []byte
- func MakeNonUniqueIndexKey(tableID uint32, indexID uint8, colValue types.Value, colType types.TypeID, ...) ([]byte, error)
- func MakeRowKey(tableID uint32, pkValue types.Value, pkType types.TypeID, pkColName string) ([]byte, error)
- func MakeRowKeyComposite(tableID uint32, pkValues []types.Value, pkTypes []types.TypeID, ...) ([]byte, error)
- func MakeTablePrefix(tableID uint32) []byte
- func MakeTablePrefixEnd(tableID uint32) []byte
- func NonUniqueIndexPrefixForValue(tableID uint32, indexID uint8, colValue types.Value, colType types.TypeID) ([]byte, error)
- func ParseNonUniqueIndexKeyPK(key []byte, colBytesLen int) []byte
- type ColumnDesc
- type TableDesc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompositeIndexColBytesLen ¶ added in v1.1.6
CompositeIndexColBytesLen 返回复合索引前缀中各列编码的总字节数。 用于从非唯一复合索引 key 中截取 pkBytes(key = prefix(6B) + colBytes... + pkBytes)。
func DecodeCompositeIndexKey ¶ added in v1.1.6
DecodeCompositeIndexKey 从复合索引 key 字节序列中依次解码各列值。 data 从列编码起始位置开始(即已跳过 6B 前缀之后的部分)。 返回解码出的列值列表和消耗的总字节数。
func EncodePrimaryKey ¶
EncodePrimaryKey 将主键值编码为可排序字节序列。 保序性:编码后的字节序与原始值大小顺序一致(INT 用大端 + 符号位翻转)。
func MakeCompositeIndexKey ¶ added in v1.1.6
func MakeCompositeIndexKey(tableID uint32, indexID uint8, colValues []types.Value, colTypes []types.TypeID) ([]byte, error)
MakeCompositeIndexKey 构造复合索引 key: 0x02 <tableID:4B> <indexID:1B> <col1Encoded> <col2Encoded> ... 用于唯一复合索引(key 为完整列组合,value 存 pkBytes)。
func MakeCompositeNonUniqueIndexKey ¶ added in v1.1.6
func MakeCompositeNonUniqueIndexKey(tableID uint32, indexID uint8, colValues []types.Value, colTypes []types.TypeID, pkValue types.Value, pkType types.TypeID) ([]byte, error)
MakeCompositeNonUniqueIndexKey 构造非唯一复合索引 key: 0x02 <tableID:4B> <indexID:1B> <col1Encoded> ... <colNEncoded> <pkEncoded>
func MakeIndexKey ¶
func MakeIndexKey(tableID uint32, indexID uint8, colValue types.Value, colType types.TypeID) ([]byte, error)
MakeIndexKey 构造完整索引 key:0x02 <tableID:4B> <indexID:1B> <colValueEncoded> 用于唯一索引(key 即为查询键,value 存 pkBytes)。
func MakeIndexPrefix ¶
MakeIndexPrefix 构造二级索引前缀:0x02 <tableID:4B> <indexID:1B>
func MakeIndexPrefixEnd ¶
MakeIndexPrefixEnd 构造索引范围扫描结束 key(下一个 indexID)
func MakeNonUniqueIndexKey ¶
func MakeNonUniqueIndexKey(tableID uint32, indexID uint8, colValue types.Value, colType types.TypeID, pkValue types.Value, pkType types.TypeID) ([]byte, error)
MakeNonUniqueIndexKey 构造非唯一索引 key: 0x02 <tableID:4B> <indexID:1B> <colValueEncoded> <pkEncoded> 相同 colValue 不同 pk 的行具有不同的 key,支持一对多映射。 value 写空字节(pk 已编码在 key 中)。
func MakeRowKey ¶
func MakeRowKey(tableID uint32, pkValue types.Value, pkType types.TypeID, pkColName string) ([]byte, error)
MakeRowKey 构造完整行 key:0x01 <table_id: 4B> <pk_bytes>。 pkColName 用于 NULL 检查时的清晰错误信息(如 "primary key column 'id' cannot be NULL")。
func MakeRowKeyComposite ¶ added in v1.1.9
func MakeRowKeyComposite(tableID uint32, pkValues []types.Value, pkTypes []types.TypeID, pkColNames []string) ([]byte, error)
MakeRowKeyComposite 构造复合主键行 key:0x01 <table_id:4B> <pk1_bytes><pk2_bytes>...
func MakeTablePrefix ¶
MakeTablePrefix 构造用户数据 key 前缀:0x01 <table_id: 4B>
func MakeTablePrefixEnd ¶
MakeTablePrefixEnd 构造扫描结束 key(下一个 tableID 的前缀,用于半开区间扫描) 若 tableID 已是最大值(0xFFFFFFFF),返回 0x02 前缀(索引命名空间起点)作为上界
func NonUniqueIndexPrefixForValue ¶
func NonUniqueIndexPrefixForValue(tableID uint32, indexID uint8, colValue types.Value, colType types.TypeID) ([]byte, error)
NonUniqueIndexPrefixForValue 构造非唯一索引中某个 colValue 的范围前缀, 用于 IndexScan 等值/范围扫描时构建 startKey/endKey。 返回的前缀为:0x02 <tableID:4B> <indexID:1B> <colValueEncoded>
func ParseNonUniqueIndexKeyPK ¶
ParseNonUniqueIndexKeyPK 从非唯一索引 key 中提取 pkBytes。 key 格式:prefix(6B) + colBytes + pkBytes,colBytesLen 由调用方传入。
Types ¶
type ColumnDesc ¶
ColumnDesc 列描述(encoder 包内部使用的最小接口,避免循环依赖 catalog) catalog.ColumnDesc 实现此接口。
type TableDesc ¶
type TableDesc interface {
TableID() uint32
Columns() []ColumnDesc
PKIndex() int // 主键列索引
}
TableDesc 表描述(encoder 包内部使用的最小接口)