Overview
DiskManager provides the lowest-level interface to the database file, handling raw page reads and writes with checksum validation. It manages file-level operations including database initialization, page allocation, and atomic page updates.
Module: databas_core::disk_manager
Visibility: pub(crate) - Internal to the core crate
Structure
The database file handle for read/write operations.
Number of pages currently allocated in the database file. Page IDs range from 0 to
page_count - 1.Methods
new
Creates or opens a database file at the specified path.File system path to the database file. Created if it doesn’t exist.
Returns a new
DiskManager instance on success.- If the file is empty, initializes a new database with a header page (page 0)
- If the file exists, validates the database header including:
- Magic bytes:
"databas format1\0" - Page size matches
PAGE_SIZE(4096 bytes) - Page count matches file size
- Header page checksum
- Magic bytes:
- File size must be an exact multiple of
PAGE_SIZE
DiskManagerError::InvalidFileSize- File size not a multiple ofPAGE_SIZEDiskManagerError::InvalidPageChecksum- Header page checksum invalidDiskManagerError::InvalidDatabaseHeader- Header validation failedDiskManagerError::Io- File system error
new_page
Allocates a new page at the end of the database file.Returns the page ID of the newly allocated page.
- Extends the file by one page (4096 bytes)
- Initializes the new page with zeroed data and a valid checksum
- Updates the database header with the new page count
- Returns sequential page IDs starting from 1 (page 0 is the header)
- Newly allocated pages are immediately readable and contain only zeros
DiskManagerError::Io- File extension or write failed
read_page
Reads a page from disk into the provided buffer.The page identifier to read. Must be less than
page_count.Destination buffer for the page data. Must be exactly
PAGE_SIZE (4096) bytes.Returns
Ok(()) on successful read with checksum validation.- Seeks to the page offset:
page_id * PAGE_SIZE - Reads exactly
PAGE_SIZEbytes into the buffer - Validates the page checksum (last 4 bytes)
- Checksum covers the first 4092 bytes of the page
DiskManagerError::InvalidPageId- Page ID >=page_countDiskManagerError::InvalidPageChecksum- Checksum validation failedDiskManagerError::Io- Read operation failed
write_page
Writes a page buffer to disk with automatic checksum generation.The page identifier to write. Must be less than
page_count.Source buffer containing page data. The trailing 4 bytes are overwritten with a new checksum.
Returns
Ok(()) on successful write and sync.- Computes CRC32 checksum over the first 4092 bytes
- Writes the checksum to the last 4 bytes of a copy of the buffer
- Seeks to the page offset:
page_id * PAGE_SIZE - Writes exactly
PAGE_SIZEbytes to disk - Calls
sync_all()to ensure durability (fsync)
DiskManagerError::InvalidPageId- Page ID >=page_countDiskManagerError::Io- Write or sync operation failed
Error Types
DiskManagerError
File system I/O error during read, write, seek, or sync operations.
Attempted to access a page ID that doesn’t exist. Valid range is
0..page_count.Database file size is not a multiple of
PAGE_SIZE (4096 bytes).CRC32 checksum validation failed when reading a page, indicating corruption.
Database header validation failed. See
DatabaseHeader documentation for details.DiskManagerResult
Constants
Fixed page size of 4096 bytes. All database files are organized in these fixed-size pages.
The header page is always page 0. Contains database metadata including magic bytes, page size, and page count.
First data page is always page 1. Pages 1 and above are available for table data.
Implementation Notes
Page Allocation
- Page IDs are allocated sequentially starting from 1
- Page 0 is reserved for the database header
page_counttracks the total number of pages including the header- Valid page IDs are in the range
[0, page_count)
Checksums
- Uses CRC32 algorithm via the
crc32-v2crate - Checksum covers bytes
[0, 4092)of each page - Checksum is stored in bytes
[4092, 4096)as little-endianu32 - Automatically computed on writes, validated on reads
Durability
- Every write calls
File::sync_all()to ensure fsync - Header page is updated synchronously when allocating new pages
- Database is always in a consistent state after successful operations
Thread Safety
DiskManageris notSendorSyncby default (containsFile)- Expected to be used within a single-threaded context or protected by locks
- Multiple readers are not supported; use
PageCachefor concurrent access
Related Types
- DatabaseHeader - Manages the header page format and validation
- PageCache - Higher-level caching layer built on top of
DiskManager - PageId - Type alias for
u64, represents a page identifier