Skip to main content

Module pager

Module pager 

Source
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 of TableLeaf pages. Cell layout and slot directory are in cell.rs / table_page.rs; cells that exceed the inline threshold spill into an overflow chain via overflow.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 of sqlrite_master itself is hardcoded into the engine so the open path can bootstrap.
  • Page 0’s schema_root_page field points at the first leaf of sqlrite_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_mode with AccessMode::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. an INSERT, or a save_database call against it) bottom out in a cannot commit: database is opened read-only error from the Pager.
open_database_with_mode
Opens a database file and reconstructs the in-memory Database, leaving the long-lived Pager attached for subsequent auto-save (read-write) or consistent-snapshot reads (read-only).
save_database
Persists db to disk. Same diff-commit behavior as before: only pages whose bytes actually changed get written.