BasePage.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package page
  2. import (
  3. "errors"
  4. "unsafe"
  5. )
  6. type PageType byte
  7. const (
  8. TableIndex PageType = 1 // 表页
  9. DataIndex PageType = 2 // 数据页
  10. HeapIndex PageType = 3 // 堆页
  11. IndexMetaIndex PageType = 4 // 索引元数据页(全局)
  12. IndexDataIndex PageType = 5 // 索引数据页(B+树节点)
  13. DataOverflowPage PageType = 6 // 数据页溢出页
  14. WALIndex PageType = 7 // WAL 日志页
  15. )
  16. // 操作类的回调函数
  17. type HandleDb struct {
  18. NewPage func() uint32
  19. FreePage func(pageID uint32) // 回收页到空闲链表
  20. SavePage func(pageID uint32, buf []byte) error
  21. ReadPage func(pageID uint32, buf []byte) error
  22. }
  23. // PageHdr 公共头 所有页统一头部 18个字节头部
  24. type PageHdr struct {
  25. Type PageType // page 页的类型 1,表信息,2 数据页,3 堆页
  26. _ [3]byte // 无用数据 对齐用 go 内存会自动对齐 最大4个字节
  27. PageID uint32 // 当前页数
  28. PrePage uint32 // 前一页 0表示开始
  29. NextPage uint32 // 下一页 0表示结束
  30. UseSize uint32 // 已用数量 / 字节
  31. }
  32. const PageHdrSize = uint32(unsafe.Sizeof(PageHdr{}))
  33. // ======================== 索引共享类型 ========================
  34. // IndexPageSize 索引页大小 (字节)
  35. const IndexPageSize = 16384
  36. // IndexType 索引类型
  37. type IndexType byte
  38. const (
  39. IndexTypeTime IndexType = 1 // 时序索引
  40. IndexTypePrimary IndexType = 2 // 主键索引
  41. IndexTypeStr IndexType = 3 // 已废弃,合并到 IndexTypeBTree (保留仅为向后兼容旧数据库文件)
  42. IndexTypeCompose IndexType = 4 // 复合索引
  43. IndexTypeBTree IndexType = 5 // B+树二级索引 (叶子存 PKID)
  44. )
  45. // IndexHead B+ 树节点头 / 索引页头 (从页缓冲区 unsafe 解析)
  46. type IndexHead struct {
  47. Hdr PageHdr // 18 bytes
  48. IndexType IndexType // 1 byte
  49. EntryLen uint16 // 2 bytes
  50. Count uint32 // 4 bytes
  51. IsLeaf byte // 1 byte
  52. _ [3]byte
  53. }
  54. // IndexHeadSize IndexHead 结构体字节大小
  55. const IndexHeadSize = int(unsafe.Sizeof(IndexHead{}))
  56. // BTreeFillFactor B+ 树分裂填充因子 (百分比 50~100)
  57. var BTreeFillFactor = 100
  58. // 索引共享错误
  59. var (
  60. ErrNotFound = errors.New("key not found")
  61. ErrRowTooLarge = errors.New("row too large, need heap overflow")
  62. ErrDuplicateKey = errors.New("duplicate key")
  63. )