storage-format by tursodatabase/turso
npx skills add https://github.com/tursodatabase/turso --skill storage-format┌─────────────────────────────┐
│ 第1页:头部 + 架构 │ ← 前100字节 = 数据库头部
├─────────────────────────────┤
│ 第2..N页:B树页面 │ ← 表和索引
│ 溢出页面 │
│ 空闲列表页面 │
└─────────────────────────────┘
页面大小:2的幂次方,512-65536字节。默认4096。
| 偏移量 | 大小 | 字段 |
|---|---|---|
| 0 | 16 | 魔数:"SQLite format 3\0" |
| 16 | 2 | 页面大小(大端序) |
| 18 | 1 | 写入格式版本(1=回滚,2=WAL) |
| 19 | 1 | 读取格式版本 |
| 24 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 4 |
| 变更计数器 |
| 28 | 4 | 数据库总页数 |
| 32 | 4 | 第一个空闲列表主干页 |
| 36 | 4 | 空闲列表总页数 |
| 40 | 4 | 架构Cookie |
| 56 | 4 | 文本编码(1=UTF8,2=UTF16LE,3=UTF16BE) |
所有多字节整数:大端序。
| 标志 | 类型 | 用途 |
|---|---|---|
| 0x02 | 内部索引 | 索引B树内部节点 |
| 0x05 | 内部表 | 表B树内部节点 |
| 0x0a | 叶子索引 | 索引B树叶节点 |
| 0x0d | 叶子表 | 表B树叶节点 |
两种B树类型:
表B树:64位行ID键,存储行数据
索引B树:任意键(索引列 + 行ID)
内部页面: [ptr0] key1 [ptr1] key2 [ptr2] ... │ │ │ ▼ ▼ ▼ 子页面 子页面 子页面
叶子页面: key1:数据 key2:数据 key3:数据 ...
第1页始终是 sqlite_schema 表的根节点。
[负载大小: 变长整数] [行ID: 变长整数] [负载] [溢出指针: u32?]
[左子页面: u32] [行ID: 变长整数]
类似,但键是任意的(列 + 行ID),而不仅仅是行ID。
[头部大小: 变长整数] [类型1: 变长整数] [类型2: 变长整数] ... [数据1] [数据2] ...
序列化类型:
| 类型 | 含义 |
|---|---|
| 0 | NULL |
| 1-4 | 1/2/3/4字节有符号整数 |
| 5 | 6字节有符号整数 |
| 6 | 8字节有符号整数 |
| 7 | IEEE 754浮点数 |
| 8 | 整数0 |
| 9 | 整数1 |
| ≥12偶数 | BLOB,长度=(N-12)/2 |
| ≥13奇数 | 文本,长度=(N-13)/2 |
当负载超过阈值时,超出部分存储在溢出链中:
[下一页: u32] [数据...]
最后一页的下一页指针为0。
主干页面的链表,每个包含叶子页面编号:
主干: [下一个主干: u32] [叶子计数: u32] [叶子页面: u32...]
关键文件:
core/storage/sqlite3_ondisk.rs - 磁盘格式,PageType枚举core/storage/btree.rs - B树操作(大文件)core/storage/pager.rs - 页面管理core/storage/buffer_pool.rs - 页面缓存# 完整性检查
cargo run --bin tursodb test.db "PRAGMA integrity_check;"
# 页面计数
cargo run --bin tursodb test.db "PRAGMA page_count;"
# 空闲列表信息
cargo run --bin tursodb test.db "PRAGMA freelist_count;"
每周安装量
381
代码仓库
GitHub星标数
17.9K
首次出现
2026年1月28日
安全审计
安装于
claude-code295
github-copilot183
opencode120
gemini-cli118
codex118
cursor110
┌─────────────────────────────┐
│ 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.
| Offset | Size | Field |
|---|---|---|
| 0 | 16 | Magic: "SQLite format 3\0" |
| 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.
| 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 |
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 sqlite_schema table.
[payload_size: varint] [rowid: varint] [payload] [overflow_ptr: u32?]
[left_child_page: u32] [rowid: varint]
Similar but key is arbitrary (columns + rowid), not just rowid.
[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 |
When payload exceeds threshold, excess stored in overflow chain:
[next_page: u32] [data...]
Last page has next_page=0.
Linked list of trunk pages, each containing leaf page numbers:
Trunk: [next_trunk: u32] [leaf_count: u32] [leaf_pages: u32...]
Key files:
core/storage/sqlite3_ondisk.rs - On-disk format, PageType enumcore/storage/btree.rs - B-tree operations (large file)core/storage/pager.rs - Page managementcore/storage/buffer_pool.rs - Page caching# Integrity check
cargo run --bin tursodb test.db "PRAGMA integrity_check;"
# Page count
cargo run --bin tursodb test.db "PRAGMA page_count;"
# Freelist info
cargo run --bin tursodb test.db "PRAGMA freelist_count;"
Weekly Installs
381
Repository
GitHub Stars
17.9K
First Seen
Jan 28, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code295
github-copilot183
opencode120
gemini-cli118
codex118
cursor110
Android 整洁架构指南:模块化设计、依赖注入与数据层实现
902 周安装