encoder

package
v1.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompositeIndexColBytesLen added in v1.1.6

func CompositeIndexColBytesLen(colValues []types.Value, colTypes []types.TypeID) (int, error)

CompositeIndexColBytesLen 返回复合索引前缀中各列编码的总字节数。 用于从非唯一复合索引 key 中截取 pkBytes(key = prefix(6B) + colBytes... + pkBytes)。

func DecodeCompositeIndexKey added in v1.1.6

func DecodeCompositeIndexKey(data []byte, colTypes []types.TypeID) ([]types.Value, int, error)

DecodeCompositeIndexKey 从复合索引 key 字节序列中依次解码各列值。 data 从列编码起始位置开始(即已跳过 6B 前缀之后的部分)。 返回解码出的列值列表和消耗的总字节数。

func DecodeRow

func DecodeRow(data []byte, table TableDesc) ([]types.Value, error)

DecodeRow 将字节序列解码为行数据。

func EncodePrimaryKey

func EncodePrimaryKey(pkValue types.Value, pkType types.TypeID) ([]byte, error)

EncodePrimaryKey 将主键值编码为可排序字节序列。 保序性:编码后的字节序与原始值大小顺序一致(INT 用大端 + 符号位翻转)。

func EncodeRow

func EncodeRow(row []types.Value, table TableDesc) ([]byte, error)

EncodeRow 将一行数据编码为字节序列。 row 中每个元素对应 table.Columns() 中同索引的列。

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

func MakeIndexPrefix(tableID uint32, indexID uint8) []byte

MakeIndexPrefix 构造二级索引前缀:0x02 <tableID:4B> <indexID:1B>

func MakeIndexPrefixEnd

func MakeIndexPrefixEnd(tableID uint32, indexID uint8) []byte

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

func MakeTablePrefix(tableID uint32) []byte

MakeTablePrefix 构造用户数据 key 前缀:0x01 <table_id: 4B>

func MakeTablePrefixEnd

func MakeTablePrefixEnd(tableID uint32) []byte

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

func ParseNonUniqueIndexKeyPK(key []byte, colBytesLen int) []byte

ParseNonUniqueIndexKeyPK 从非唯一索引 key 中提取 pkBytes。 key 格式:prefix(6B) + colBytes + pkBytes,colBytesLen 由调用方传入。

Types

type ColumnDesc

type ColumnDesc interface {
	ColType() types.TypeID
}

ColumnDesc 列描述(encoder 包内部使用的最小接口,避免循环依赖 catalog) catalog.ColumnDesc 实现此接口。

type TableDesc

type TableDesc interface {
	TableID() uint32
	Columns() []ColumnDesc
	PKIndex() int // 主键列索引
}

TableDesc 表描述(encoder 包内部使用的最小接口)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL