Expand description
On-disk persistence for a Database, using fixed-size paged files.
The file is a sequence of 4 KiB pages. Page 0 holds the header (magic, version, page count, schema-root pointer). Every other page carries a small per-page header (type tag + next-page pointer + payload length) followed by a payload of up to 4089 bytes.
Storage strategy (format version 2, Phase 3c.5).
- Each
Table’s rows live as cells in a chain ofTableLeafpages. Cell layout and slot directory are incell.rs/table_page.rs; cells that exceed the inline threshold spill into an overflow chain viaoverflow.rs. - The schema catalog is itself a regular table named
sqlrite_master, with one row per user table:(name TEXT PRIMARY KEY, sql TEXT NOT NULL, rootpage INTEGER NOT NULL, last_rowid INTEGER NOT NULL)This is the SQLite-style approach: the schema ofsqlrite_masteritself is hardcoded into the engine so the open path can bootstrap. - Page 0’s
schema_root_pagefield points at the first leaf ofsqlrite_master.
Format version. Version 2 is not compatible with files produced by earlier commits. Opening a v1 file returns a clean error — users on old files have to regenerate them from CREATE/INSERT, as there’s no production data to migrate yet.
Re-exports§
pub use crate::sql::pager::pager::AccessMode;
Modules§
- cell
- Cell format: one row per cell, hand-rolled length-prefixed encoding.
- file
- Page-indexed file I/O: read/write whole pages by page number, plus the
special page-0 header. Deliberately thin — this is the only place that
touches
std::fs, so higher layers deal in pages, not offsets. - header
- Database file header (page 0).
- index_
cell - On-disk format for a single secondary-index entry.
- interior_
page - Interior B-Tree page layout.
- overflow
- Overflow storage for cells that don’t fit on one table-leaf page.
- page
- Page layout primitives.
- pager
- Long-lived page cache + WAL-backed commits.
- table_
page - Table-leaf page layout: a slot directory + cell content.
- varint
- Variable-length integer encoding.
- wal
- Write-Ahead Log (WAL) file format.
Constants§
- MASTER_
TABLE_ NAME - Name of the internal catalog table. Reserved — user CREATEs of this name must be rejected upstream.
Functions§
- open_
database - Opens a database file in read-write mode. Shorthand for
open_database_with_modewithAccessMode::ReadWrite. - open_
database_ read_ only - Opens a database file in read-only mode. Acquires a shared OS-level
advisory lock, so other read-only openers coexist but any writer is
excluded. Attempts to mutate the returned
Database(e.g. anINSERT, or asave_databasecall against it) bottom out in acannot commit: database is opened read-onlyerror from the Pager. - open_
database_ with_ mode - Opens a database file and reconstructs the in-memory
Database, leaving the long-livedPagerattached for subsequent auto-save (read-write) or consistent-snapshot reads (read-only). - save_
database - Persists
dbto disk. Same diff-commit behavior as before: only pages whose bytes actually changed get written.