Expand description
SQLite on-disk file format.
SQLite stores data in a single database file, which is divided into fixed-size pages:
+----------+----------+----------+-----------------------------+----------+
| | | | | |
| Page 1 | Page 2 | Page 3 | ... | Page N |
| | | | | |
+----------+----------+----------+-----------------------------+----------+The first page is special because it contains a 100 byte header at the beginning.
Each page consists of a page header and N cells, which contain the records.
+-----------------+----------------+---------------------+----------------+
| | | | |
| Page header | Cell pointer | Unallocated | Cell content |
| (8 or 12 bytes) | array | space | area |
| | | | |
+-----------------+----------------+---------------------+----------------+The write-ahead log (WAL) is a separate file that contains the physical log of changes to a database file. The file starts with a WAL header and is followed by a sequence of WAL frames, which are database pages with additional metadata.
+-----------------+-----------------+-----------------+-----------------+
| | | | |
| WAL header | WAL frame 1 | WAL frame 2 | WAL frame N |
| | | | |
+-----------------+-----------------+-----------------+-----------------+For more information, see the SQLite file format specification:
https://www.sqlite.org/fileformat.html
Structs§
- Build
Shared Wal - Stream through frames in chunks, building frame_cache incrementally Track last valid commit frame for consistency Non-blocking driver for WAL recovery on open.
- Cache
Size - Read/Write file format version.
- Database
Header - Database Header Format
- Index
Interior Cell - Index
Leaf Cell - Overflow
Cell - Page
Inner - Page
Size - Read/Write file format version.
- RawVersion
- Raw version byte for use in DatabaseHeader where Pod is required.
Use
Version::try_from(raw.0)to convert to the validated enum. - Table
Interior Cell - Table
Leaf Cell - Text
Encoding - Text encoding.
- WalFrame
Header - Immediately following the wal-header are zero or more frames.
Each frame consists of a 24-byte frame-header followed by
bytes of page data. The frame-header is six big-endian 32-bit unsigned integer values, as follows: - WalHeader
- The Write-Ahead Log (WAL) header. The first 32 bytes of a WAL file comprise the WAL header. The WAL header is divided into the following fields stored in big-endian order.
Enums§
Constants§
- CELL_
PTR_ SIZE_ BYTES - FREELIST_
LEAF_ PTR_ SIZE - FREELIST_
TRUNK_ HEADER_ SIZE - FREELIST_
TRUNK_ OFFSET_ FIRST_ LEAF_ PTR - FREELIST_
TRUNK_ OFFSET_ LEAF_ COUNT - FREELIST_
TRUNK_ OFFSET_ NEXT_ TRUNK_ PTR - INTERIOR_
PAGE_ HEADER_ SIZE_ BYTES - LEAF_
PAGE_ HEADER_ SIZE_ BYTES - LEFT_
CHILD_ PTR_ SIZE_ BYTES - MINIMUM_
CELL_ SIZE - The minimum size of a cell in bytes.
- WAL_
FRAME_ HEADER_ SIZE - WAL_
HEADER_ SIZE - WAL_
MAGIC_ BE - WAL_
MAGIC_ LE
Functions§
- begin_
read_ page - Send read request for DB page read to the IO if allow_empty_read is set, than empty read will be raise error for the page, but will not panic
- begin_
read_ wal_ frame - begin_
read_ wal_ frame_ raw - begin_
sync - begin_
write_ btree_ page - begin_
write_ wal_ header - build_
shared_ wal - Blocking shim over
BuildSharedWal. Retained for the unit test and any caller not yet lifted to drive the recovery state machine directly. - checksum_
wal - The checksum is computed by interpreting the input as an even number of unsigned 32-bit integers: x(0) through x(N). The 32-bit integers are big-endian if the magic number in the first 4 bytes of the WAL header is 0x377f0683 and the integers are little-endian if the magic number is 0x377f0682. The checksum values are always stored in the frame header in a big-endian format regardless of which byte order is used to compute the checksum.
- finish_
read_ page - parse_
wal_ frame_ header - payload_
overflows - Checks if payload will overflow a cell based on the maximum allowed size. It will return the min size that will be stored in that case, including overflow pointer see e.g. https://github.com/sqlite/sqlite/blob/9591d3fe93936533c8c3b0dc4d025ac999539e11/src/dbstat.c#L371
- prepare_
wal_ frame - read_
btree_ cell - read_btree_cell contructs a BTreeCell which is basically a wrapper around pointer to the payload of a cell. buffer input “page” is static because we want the cell to point to the data in the page in case it has any payload.
- read_
integer - read_
u32 - read_
value - Reads a value that might reference the buffer it is reading from. Be sure to store RefValue with the buffer always.
- read_
value_ serial_ type - read_
varint - Reads varint integer from the buffer.
This function is similar to
sqlite3GetVarint32 - read_
varint_ partial - Reads a varint from the buffer, returning None if more data is needed.
- validate_
serial_ type - varint_
len - Compute the length of a varint encoding for a given u64 value.
- write_
pages_ vectored - Write a batch of pages to the database file.
- write_
varint - write_
varint_ to_ vec
Type Aliases§
- Page
Content - Type alias for backward compatibility - PageContent is now PageInner