System layers
The database is organized into distinct layers that build upon each other:Disk manager
TheDiskManager (disk_manager.rs:14) provides the lowest-level interface for reading and writing fixed-size pages to disk. It handles:
- Database file initialization and validation
- Page allocation with sequential page IDs
- Atomic page reads and writes
- Page checksum verification
Page cache
ThePageCache (page_cache.rs:26) implements a buffer pool using the CLOCK second-chance replacement algorithm:
- Pin guards: Pages are pinned in memory while in use via RAII
PinGuardhandles - Dirty tracking: Modified pages are automatically marked dirty and written back on eviction
- CLOCK eviction: Referenced frames get a second chance before eviction
- Automatic flushing: Dirty pages are flushed when the cache is dropped
Table pages
Databas uses a B-tree structure with two page types:- Leaf pages
- Interior pages
Leaf pages store actual row data. Each cell contains:
- 2-byte payload length
- 8-byte row ID (primary key)
- Variable-length payload bytes
Core constants
Databas uses fixed-size pages throughout the system:Data flow
A typical read operation flows through all layers:- B-tree navigation: Start at root page, traverse interior pages using separator keys
- Cache lookup: Check if page is resident in buffer pool
- Cache miss: Allocate frame using CLOCK, evict victim if needed
- Disk read: Read page from file at calculated offset
- Checksum validation: Verify page integrity before use
- Pin page: Increment pin count and return guard
All pages are checksummed using CRC32 to detect corruption. The
DiskManager validates checksums on every read and recalculates them on every write.Crate structure
Databas is organized into multiple crates:- databas_core: Page management, disk I/O, B-tree implementation
- databas_sql_parser: SQL lexer and parser
- databas_cli: Command-line interface
databas_core crate contains all architecture components described in this section.
Error handling
Each layer defines its own error types usingthiserror:
DiskManagerError: I/O errors, invalid page IDs, checksum failuresPageCacheError: No evictable frames, pinned pagesTablePageError: Corrupt pages, duplicate keys, page full
Result type, allowing higher layers to handle or transform them appropriately.