storage-format
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseStorage Format Guide
存储格式指南
Database File Structure
数据库文件结构
┌─────────────────────────────┐
│ Page 1: Header + Schema │ ← First 100 bytes = DB header
├─────────────────────────────┤
│ Page 2..N: B-tree pages │ ← Tables and indexes
│ Overflow pages │
│ Freelist pages │
└─────────────────────────────┘Page size: power of 2, 512-65536 bytes. Default 4096.
┌─────────────────────────────┐
│ Page 1: Header + Schema │ ← First 100 bytes = DB header
├─────────────────────────────┤
│ Page 2..N: B-tree pages │ ← Tables and indexes
│ Overflow pages │
│ Freelist pages │
└─────────────────────────────┘页面大小:2的幂,范围512-65536字节,默认值为4096。
Database Header (First 100 Bytes)
数据库头部(前100字节)
| Offset | Size | Field |
|---|---|---|
| 0 | 16 | Magic: |
| 16 | 2 | Page size (big-endian) |
| 18 | 1 | Write format version (1=rollback, 2=WAL) |
| 19 | 1 | Read format version |
| 24 | 4 | Change counter |
| 28 | 4 | Database size in pages |
| 32 | 4 | First freelist trunk page |
| 36 | 4 | Total freelist pages |
| 40 | 4 | Schema cookie |
| 56 | 4 | Text encoding (1=UTF8, 2=UTF16LE, 3=UTF16BE) |
All multi-byte integers: big-endian.
| Offset | Size | Field |
|---|---|---|
| 0 | 16 | Magic: |
| 16 | 2 | 页面大小(大端字节序) |
| 18 | 1 | 写入格式版本(1=回滚,2=WAL) |
| 19 | 1 | 读取格式版本 |
| 24 | 4 | 变更计数器 |
| 28 | 4 | 数据库总页面数 |
| 32 | 4 | 首个空闲列表主干页面 |
| 36 | 4 | 空闲列表总页面数 |
| 40 | 4 | 模式标识 |
| 56 | 4 | 文本编码(1=UTF8,2=UTF16LE,3=UTF16BE) |
所有多字节整数均采用大端字节序。
Page Types
页面类型
| Flag | Type | Purpose |
|---|---|---|
| 0x02 | Interior index | Index B-tree internal node |
| 0x05 | Interior table | Table B-tree internal node |
| 0x0a | Leaf index | Index B-tree leaf |
| 0x0d | Leaf table | Table B-tree leaf |
| - | Overflow | Payload exceeding cell capacity |
| - | Freelist | Unused pages (trunk or leaf) |
| Flag | Type | Purpose |
|---|---|---|
| 0x02 | 索引内部节点 | 索引B树内部节点 |
| 0x05 | 表内部节点 | 表B树内部节点 |
| 0x0a | 索引叶子节点 | 索引B树叶子节点 |
| 0x0d | 表叶子节点 | 表B树叶子节点 |
| - | 溢出页 | 负载超出单元格容量时使用 |
| - | 空闲页 | 未使用的页面(主干或叶子) |
B-tree Structure
B树结构
Two B-tree types:
- Table B-tree: 64-bit rowid keys, stores row data
- Index B-tree: Arbitrary keys (index columns + rowid)
Interior page: [ptr0] key1 [ptr1] key2 [ptr2] ...
│ │ │
▼ ▼ ▼
child child child
pages pages pages
Leaf page: key1:data key2:data key3:data ...Page 1 always root of table.
sqlite_schema两种B树类型:
- 表B树:使用64位rowid作为键,存储行数据
- 索引B树:使用任意键(索引列+rowid)
Interior page: [ptr0] key1 [ptr1] key2 [ptr2] ...
│ │ │
▼ ▼ ▼
child child child
pages pages pages
Leaf page: key1:data key2:data key3:data ...页面1始终是表的根节点。
sqlite_schemaCell Format
单元格格式
Table Leaf Cell
表叶子单元格
[payload_size: varint] [rowid: varint] [payload] [overflow_ptr: u32?][payload_size: varint] [rowid: varint] [payload] [overflow_ptr: u32?]Table Interior Cell
表内部单元格
[left_child_page: u32] [rowid: varint][left_child_page: u32] [rowid: varint]Index Cells
索引单元格
Similar but key is arbitrary (columns + rowid), not just rowid.
与表单元格类似,但键为任意值(列+rowid),而非仅rowid。
Record Format (Payload)
记录格式(负载)
[header_size: varint] [type1: varint] [type2: varint] ... [data1] [data2] ...Serial types:
| Type | Meaning |
|---|---|
| 0 | NULL |
| 1-4 | 1/2/3/4 byte signed int |
| 5 | 6 byte signed int |
| 6 | 8 byte signed int |
| 7 | IEEE 754 float |
| 8 | Integer 0 |
| 9 | Integer 1 |
| ≥12 even | BLOB, length=(N-12)/2 |
| ≥13 odd | Text, length=(N-13)/2 |
[header_size: varint] [type1: varint] [type2: varint] ... [data1] [data2] ...序列化类型:
| Type | Meaning |
|---|---|
| 0 | NULL |
| 1-4 | 1/2/3/4字节有符号整数 |
| 5 | 6字节有符号整数 |
| 6 | 8字节有符号整数 |
| 7 | IEEE 754浮点数 |
| 8 | 整数0 |
| 9 | 整数1 |
| ≥12 even | BLOB,长度=(N-12)/2 |
| ≥13 odd | 文本,长度=(N-13)/2 |
Overflow Pages
溢出页
When payload exceeds threshold, excess stored in overflow chain:
[next_page: u32] [data...]Last page has next_page=0.
当负载超过阈值时,超出部分存储在溢出链中:
[next_page: u32] [data...]最后一个页面的next_page值为0。
Freelist
空闲列表
Linked list of trunk pages, each containing leaf page numbers:
Trunk: [next_trunk: u32] [leaf_count: u32] [leaf_pages: u32...]由主干页面组成的链表,每个主干页面包含多个叶子页面编号:
Trunk: [next_trunk: u32] [leaf_count: u32] [leaf_pages: u32...]Turso Implementation
Turso实现
Key files:
- - On-disk format,
core/storage/sqlite3_ondisk.rsenumPageType - - B-tree operations (large file)
core/storage/btree.rs - - Page management
core/storage/pager.rs - - Page caching
core/storage/buffer_pool.rs
核心文件:
- - 磁盘格式定义,包含
core/storage/sqlite3_ondisk.rs枚举PageType - - B树操作(大文件处理)
core/storage/btree.rs - - 页面管理
core/storage/pager.rs - - 页面缓存
core/storage/buffer_pool.rs
Debugging Storage
存储调试
bash
undefinedbash
undefinedIntegrity check
Integrity check
cargo run --bin tursodb test.db "PRAGMA integrity_check;"
cargo run --bin tursodb test.db "PRAGMA integrity_check;"
Page count
Page count
cargo run --bin tursodb test.db "PRAGMA page_count;"
cargo run --bin tursodb test.db "PRAGMA page_count;"
Freelist info
Freelist info
cargo run --bin tursodb test.db "PRAGMA freelist_count;"
undefinedcargo run --bin tursodb test.db "PRAGMA freelist_count;"
undefined