A compact C++20 storage-engine project built around three database internals:
- a fixed-size buffer pool with LRU-K replacement and RAII page guards;
- a concurrent B+ tree supporting point lookups, updates, deletes, and range scans;
- write-ahead logging with analysis, redo, and loser-transaction undo.
The implementation favors explicit invariants and testable boundaries over framework code. It is small enough to read end to end while still exercising durable I/O, ownership, concurrency, and crash semantics.
| Subsystem | Implementation |
|---|---|
| Buffer pool | Fixed-size frames, page table, pin counts, dirty-page flushing, RAII guards |
| Replacement | LRU-K with backward K-distance and infinite-distance preference |
| B+ tree | Point lookup, insert/upsert, delete, split/merge, range scan, linked leaves |
| Concurrency | Shared reader / exclusive writer tree latch; synchronized buffer metadata |
| WAL | Checksummed binary records with transaction-local prev_lsn chains |
| Recovery | Analysis, page-LSN-based redo, loser undo, explicit transaction abort |
cmake -S . -B build
cmake --build build -j
ctest --test-dir build --output-on-failure
./build/storage_engine_demoFor an instrumented build:
cmake -S . -B build-sanitize -DDB_ENABLE_SANITIZERS=ON
cmake --build build-sanitize -j
ctest --test-dir build-sanitize --output-on-failurekey -> B+ tree -> page id -> buffer pool -> fixed-size database page
^
transaction update -> WAL -----+----> recovery analysis / redo / undo
The write path follows the WAL rule: the update record is flushed before the corresponding page write. A commit record is flushed before commit returns. Recovery repeats updates whose LSN is newer than the page LSN, then walks incomplete transactions backward through their log chains.
The B+ tree is intentionally in-memory so its structural algorithms can be studied independently from page serialization. The buffer pool and recovery path use durable files.
See docs/architecture.md for invariants, concurrency policy, and recovery semantics.
include/db/
buffer/ buffer pool and LRU-K interfaces
index/ header-only generic B+ tree
recovery/ log records, WAL, and recovery API
storage/ pages and durable page I/O
src/ non-template implementations
tests/ deterministic unit and integration tests
examples/ runnable end-to-end scenario
auto disk = std::make_shared<db::DiskManager>("database.db");
db::BufferPoolManager pool(64, disk, 2);
auto page = pool.NewPageGuard();
page->Data()[0] = std::byte{42};
page->MarkDirty();
db::BPlusTree<int, db::page_id_t> index;
index.Insert(1001, page->Id());
auto log = std::make_shared<db::LogManager>("database.log");
db::RecoveryManager recovery(disk, log);
auto last_lsn = recovery.Begin(7);
last_lsn = recovery.ApplyUpdate(7, last_lsn, page->Id(), 0, payload);
recovery.Commit(7, last_lsn);This is a storage-engine core, not a complete DBMS. It does not include SQL parsing, a catalog, query optimization, lock management, MVCC, persisted B+ tree nodes, checkpoints, or compensation log records. Recovery assumes strict write-conflict exclusion while transactions are active.
MIT