Overview
DatabaseHeader manages the format, serialization, and validation of the database file header stored on page 0. The header contains critical metadata including magic bytes for format identification, page size, and total page count.
Module: databas_core::database_header
Visibility: pub(crate) - Internal to the core crate
Structure
Size of each page in bytes. Always
4096 for this implementation.Total number of pages in the database file, including the header page. Minimum value is 1.
Header Layout
The header is stored in page 0 with the following binary layout:| Offset | Size | Field | Description |
|---|---|---|---|
| 0 | 16 | Magic | Format identifier: "databas format1\0" |
| 16 | 2 | Page Size | Page size in bytes (little-endian u16) |
| 18 | 8 | Page Count | Total pages in file (little-endian u64) |
| 26 | 4066 | Reserved | Unused, filled with zeros |
| 4092 | 4 | Checksum | CRC32 of bytes [0, 4092) |
Methods
new
Creates a new header with the given page count.Total number of pages in the database. Must be at least 1.
Returns a new
DatabaseHeader with page_size set to PAGE_SIZE (4096).init_new
Initializes a header page buffer for a new database.Page buffer to initialize with a new database header.
- Creates a header with
page_count = 1(header page only) - Writes the header to the page buffer using
write() - Used when creating new database files
read
Deserializes and validates a header from a page buffer.Page buffer containing the database header (page 0).
Returns the parsed header on success.
- Magic bytes must match
"databas format1\0"exactly - Page size must equal
PAGE_SIZE(4096) - Does not validate checksum (caller’s responsibility)
DatabaseHeaderError::InvalidMagic- Magic bytes don’t match expected valueDatabaseHeaderError::InvalidPageSize- Page size doesn’t matchPAGE_SIZE
write
Serializes the header to a page buffer with checksum.Destination page buffer. Will be completely overwritten.
- Clears the entire page to zeros
- Writes magic bytes, page size, and page count
- Computes and writes CRC32 checksum in the last 4 bytes
- Page is ready to be written to disk
validate
Validates the header against the actual file page count.The actual number of pages in the file, computed from file size.
Returns
Ok(()) if validation passes.- Header
page_countmust not be zero - Header
page_countmust matchactual_page_count
DatabaseHeaderError::PageCountZero- Header page count is 0DatabaseHeaderError::PageCountMismatch- Mismatch between header and file size
Error Types
DatabaseHeaderError
InvalidMagic
The magic bytes don’t match
"databas format1\0". Indicates an invalid or corrupted database file.The page size in the header doesn’t match the expected
PAGE_SIZE (4096). Database cannot be opened.PageCountZero
The header declares zero pages, which is invalid. Every database must have at least the header page.
The page count in the header doesn’t match the file size. Indicates truncation or corruption.
actualis the page count from the headerexpectedis the page count computed from file size
Constants
The header is always stored on page 0.
First data page starts at page 1. Page 0 is reserved for the header.
Magic byte sequence identifying the database format.
Implementation Notes
Magic Bytes
The magic byte sequence"databas format1\0" serves as:
- Format identifier for the Databas system
- Version indicator (“format1” is the current version)
- Quick validation that a file is a Databas database
Page Count Semantics
- Includes the header page (page 0) in the count
- Minimum valid value is 1 (header only)
- Matches the file size:
file_size = page_count * PAGE_SIZE - Updated atomically when pages are allocated via
DiskManager::new_page()
Serialization Format
- All multi-byte integers are little-endian
- Page size is
u16to save space (max 65535 bytes) - Page count is
u64to support large databases (up to 16 exabytes) - Reserved space (4066 bytes) allows for future header extensions
Checksum
- The header page has a checksum like all other pages
- Checksum is computed and validated by the
page_checksummodule DiskManagervalidates the header checksum duringnew()
Usage Example
Typical usage when opening a database:Related Types
- DiskManager - Uses
DatabaseHeaderto initialize and validate database files - PageId - Type alias for
u64, used for page identifiers - PAGE_SIZE - Constant defining the fixed page size (4096 bytes)