package page import ( "errors" "unsafe" ) type PageType byte const ( TableIndex PageType = 1 // 表页 DataIndex PageType = 2 // 数据页 HeapIndex PageType = 3 // 堆页 IndexMetaIndex PageType = 4 // 索引元数据页(全局) IndexDataIndex PageType = 5 // 索引数据页(B+树节点) DataOverflowPage PageType = 6 // 数据页溢出页 WALIndex PageType = 7 // WAL 日志页 ) // 操作类的回调函数 type HandleDb struct { NewPage func() uint32 FreePage func(pageID uint32) // 回收页到空闲链表 SavePage func(pageID uint32, buf []byte) error ReadPage func(pageID uint32, buf []byte) error } // PageHdr 公共头 所有页统一头部 18个字节头部 type PageHdr struct { Type PageType // page 页的类型 1,表信息,2 数据页,3 堆页 _ [3]byte // 无用数据 对齐用 go 内存会自动对齐 最大4个字节 PageID uint32 // 当前页数 PrePage uint32 // 前一页 0表示开始 NextPage uint32 // 下一页 0表示结束 UseSize uint32 // 已用数量 / 字节 } const PageHdrSize = uint32(unsafe.Sizeof(PageHdr{})) // ======================== 索引共享类型 ======================== // IndexPageSize 索引页大小 (字节) const IndexPageSize = 16384 // IndexType 索引类型 type IndexType byte const ( IndexTypeTime IndexType = 1 // 时序索引 IndexTypePrimary IndexType = 2 // 主键索引 IndexTypeStr IndexType = 3 // 已废弃,合并到 IndexTypeBTree (保留仅为向后兼容旧数据库文件) IndexTypeCompose IndexType = 4 // 复合索引 IndexTypeBTree IndexType = 5 // B+树二级索引 (叶子存 PKID) ) // IndexHead B+ 树节点头 / 索引页头 (从页缓冲区 unsafe 解析) type IndexHead struct { Hdr PageHdr // 18 bytes IndexType IndexType // 1 byte EntryLen uint16 // 2 bytes Count uint32 // 4 bytes IsLeaf byte // 1 byte _ [3]byte } // IndexHeadSize IndexHead 结构体字节大小 const IndexHeadSize = int(unsafe.Sizeof(IndexHead{})) // BTreeFillFactor B+ 树分裂填充因子 (百分比 50~100) var BTreeFillFactor = 100 // 索引共享错误 var ( ErrNotFound = errors.New("key not found") ErrRowTooLarge = errors.New("row too large, need heap overflow") ErrDuplicateKey = errors.New("duplicate key") )