# ngthdong/yggdrasil architecture

> Architecture diagram of the GitHub repository ngthdong/yggdrasil, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ngthdong/yggdrasil
- Repository: https://github.com/ngthdong/yggdrasil

## Overview

Yggdrasil is a persistent key-value storage engine. The user-facing workflow runs through the CLI into the database API, which provides key operations, transactions, batches, snapshots, scans, and maintenance. The index and page-storage layers persist data, while logging and recovery support durability. Some subsystem links are supported by declarations and documented capabilities; unsampled implementation details are not inferred.

Main entry points: [`src/database.cc`](https://github.com/ngthdong/yggdrasil/blob/main/src/database.cc) (Database API), [`src/index/bptree/b_plus_tree.cc`](https://github.com/ngthdong/yggdrasil/blob/main/src/index/bptree/b_plus_tree.cc) (B+Tree index), [`src/buffer/buffer_pool_manager.cc`](https://github.com/ngthdong/yggdrasil/blob/main/src/buffer/buffer_pool_manager.cc) (Buffer pool), [`src/storage/freespace/free_page_manager.cc`](https://github.com/ngthdong/yggdrasil/blob/main/src/storage/freespace/free_page_manager.cc) (Free-page manager)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Database API"]
  node_cli["Command interface<br/>[yggdrasil-cli.cc]"]
  node_database["Database API<br/>[database.cc]"]
  node_kv["Key operations<br/>[database.cc]"]
  node_batches["Write batches<br/>[write_batch.h]"]
  node_snapshots["Point-in-time snapshots<br/>[snapshot.cc]"]
  node_scans["Ordered scans<br/>[bptree_iterator.cc]"]
  node_maintenance["Stats and verification<br/>[database.h]"]
end

subgraph group_index["Index and Pages"]
  node_tree["B+Tree index<br/>[b_plus_tree.cc]"]
  node_tree_pages["B+Tree pages"]
  node_buffer["Buffer pool"]
  node_disk[("Page-file storage<br/>[disk_manager.cc]")]
  node_free_pages["Free-page manager"]
end

subgraph group_durability["Durability"]
  node_wal["Write-ahead log<br/>[wal_manager.cc]"]
  node_records["Log records<br/>[log_record.cc]"]
  node_recovery["Crash recovery"]
  node_checkpoint["Checkpointing"]
end

subgraph group_transactions["Transactions"]
  node_txn["Transactions<br/>[transaction.cc]"]
  node_locks["Lock manager<br/>[lock_manager.cc]"]
  node_deadlocks["Deadlock detection"]
end

node_user(("CLI user"))

node_user -->|"issues commands"| node_cli
node_cli -->|"calls API"| node_database
node_database -->|"provides"| node_kv
node_database -->|"applies"| node_batches
node_database -->|"creates"| node_snapshots
node_database -->|"opens iterators"| node_scans
node_database -->|"exposes"| node_maintenance
node_database -->|"owns"| node_tree
node_tree -->|"fetches pages"| node_buffer
node_tree -->|"reads root metadata"| node_disk
node_tree -->|"allocates pages"| node_free_pages
node_free_pages -->|"fetches nodes"| node_buffer
node_free_pages -->|"updates free-list head"| node_disk
node_buffer -->|"reads and writes"| node_disk
node_tree -->|"uses page formats"| node_tree_pages
node_database -->|"begins transactions"| node_txn
node_txn -->|"acquires locks"| node_locks
node_database -->|"owns"| node_locks
node_database -->|"configures detector"| node_deadlocks
node_database -->|"owns"| node_wal
node_wal -->|"serializes records"| node_records
node_database -->|"runs on open"| node_recovery
node_recovery -->|"replays changes"| node_tree
node_recovery -->|"flushes recovered pages"| node_buffer
node_database -->|"checkpoints"| node_checkpoint
node_checkpoint -->|"flushes pages"| node_buffer
node_checkpoint -->|"records checkpoint"| node_wal

click node_cli "https://github.com/ngthdong/yggdrasil/blob/main/tools/yggdrasil-cli.cc"
click node_database "https://github.com/ngthdong/yggdrasil/blob/main/src/database.cc"
click node_kv "https://github.com/ngthdong/yggdrasil/blob/main/src/database.cc"
click node_batches "https://github.com/ngthdong/yggdrasil/blob/main/include/engine/write_batch.h"
click node_snapshots "https://github.com/ngthdong/yggdrasil/blob/main/src/snapshot.cc"
click node_scans "https://github.com/ngthdong/yggdrasil/blob/main/src/index/bptree/bptree_iterator.cc"
click node_maintenance "https://github.com/ngthdong/yggdrasil/blob/main/include/engine/database.h"
click node_tree "https://github.com/ngthdong/yggdrasil/blob/main/src/index/bptree/b_plus_tree.cc"
click node_tree_pages "https://github.com/ngthdong/yggdrasil/blob/main/src/index/bptree/bptree_leaf_page.cc"
click node_buffer "https://github.com/ngthdong/yggdrasil/blob/main/src/buffer/buffer_pool_manager.cc"
click node_disk "https://github.com/ngthdong/yggdrasil/blob/main/src/storage/disk/disk_manager.cc"
click node_free_pages "https://github.com/ngthdong/yggdrasil/blob/main/src/storage/freespace/free_page_manager.cc"
click node_txn "https://github.com/ngthdong/yggdrasil/blob/main/src/txn/transaction.cc"
click node_locks "https://github.com/ngthdong/yggdrasil/blob/main/src/txn/lock_manager.cc"
click node_deadlocks "https://github.com/ngthdong/yggdrasil/blob/main/src/txn/deadlock_detector.cc"
click node_wal "https://github.com/ngthdong/yggdrasil/blob/main/src/log/wal_manager.cc"
click node_records "https://github.com/ngthdong/yggdrasil/blob/main/src/log/log_record.cc"
click node_recovery "https://github.com/ngthdong/yggdrasil/blob/main/src/recovery/recovery_manager.cc"
click node_checkpoint "https://github.com/ngthdong/yggdrasil/blob/main/src/recovery/checkpoint_manager.cc"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_database,node_kv,node_batches,node_snapshots,node_scans,node_maintenance,node_user toneBlue
class node_tree,node_tree_pages,node_buffer,node_disk,node_free_pages toneAmber
class node_wal,node_records,node_recovery,node_checkpoint toneMint
class node_txn,node_locks,node_deadlocks toneRose
```

## Components

### Database API

- [Command interface](https://github.com/ngthdong/yggdrasil/blob/main/tools/yggdrasil-cli.cc): `tools/yggdrasil-cli.cc`
- [Database API](https://github.com/ngthdong/yggdrasil/blob/main/src/database.cc): `src/database.cc`
- [Key operations](https://github.com/ngthdong/yggdrasil/blob/main/src/database.cc): `src/database.cc`
- [Write batches](https://github.com/ngthdong/yggdrasil/blob/main/include/engine/write_batch.h): `include/engine/write_batch.h`
- [Point-in-time snapshots](https://github.com/ngthdong/yggdrasil/blob/main/src/snapshot.cc): `src/snapshot.cc`
- [Ordered scans](https://github.com/ngthdong/yggdrasil/blob/main/src/index/bptree/bptree_iterator.cc): `src/index/bptree/bptree_iterator.cc`
- [Stats and verification](https://github.com/ngthdong/yggdrasil/blob/main/include/engine/database.h): `include/engine/database.h`

### Index and Pages

- [B+Tree index](https://github.com/ngthdong/yggdrasil/blob/main/src/index/bptree/b_plus_tree.cc): `src/index/bptree/b_plus_tree.cc`
- [B+Tree pages](https://github.com/ngthdong/yggdrasil/blob/main/src/index/bptree/bptree_leaf_page.cc): `src/index/bptree/bptree_leaf_page.cc`
- [Buffer pool](https://github.com/ngthdong/yggdrasil/blob/main/src/buffer/buffer_pool_manager.cc): `src/buffer/buffer_pool_manager.cc`
- [Page-file storage](https://github.com/ngthdong/yggdrasil/blob/main/src/storage/disk/disk_manager.cc): `src/storage/disk/disk_manager.cc`
- [Free-page manager](https://github.com/ngthdong/yggdrasil/blob/main/src/storage/freespace/free_page_manager.cc): `src/storage/freespace/free_page_manager.cc`

### Durability

- [Write-ahead log](https://github.com/ngthdong/yggdrasil/blob/main/src/log/wal_manager.cc): `src/log/wal_manager.cc`
- [Log records](https://github.com/ngthdong/yggdrasil/blob/main/src/log/log_record.cc): `src/log/log_record.cc`
- [Crash recovery](https://github.com/ngthdong/yggdrasil/blob/main/src/recovery/recovery_manager.cc): `src/recovery/recovery_manager.cc`
- [Checkpointing](https://github.com/ngthdong/yggdrasil/blob/main/src/recovery/checkpoint_manager.cc): `src/recovery/checkpoint_manager.cc`

### Transactions

- [Transactions](https://github.com/ngthdong/yggdrasil/blob/main/src/txn/transaction.cc): `src/txn/transaction.cc`
- [Lock manager](https://github.com/ngthdong/yggdrasil/blob/main/src/txn/lock_manager.cc): `src/txn/lock_manager.cc`
- [Deadlock detection](https://github.com/ngthdong/yggdrasil/blob/main/src/txn/deadlock_detector.cc): `src/txn/deadlock_detector.cc`

### Other components

- **CLI user**

## Connections

- CLI user → Command interface: issues commands (evidence: `README.md`)
- Command interface → Database API: calls API (evidence: `tools/yggdrasil-cli.cc`)
- Database API → Key operations: provides (evidence: `src/database.cc`)
- Database API → Write batches: applies
- Database API → Point-in-time snapshots: creates
- Database API → Ordered scans: opens iterators
- Database API → Stats and verification: exposes (evidence: `include/engine/database.h`)
- Database API → B+Tree index: owns
- B+Tree index → Buffer pool: fetches pages
- B+Tree index → Page-file storage: reads root metadata
- B+Tree index → Free-page manager: allocates pages
- Free-page manager → Buffer pool: fetches nodes
- Free-page manager → Page-file storage: updates free-list head
- Buffer pool → Page-file storage: reads and writes
- B+Tree index → B+Tree pages: uses page formats
- Database API → Transactions: begins transactions
- Transactions → Lock manager: acquires locks
- Database API → Lock manager: owns
- Database API → Deadlock detection: configures detector
- Database API → Write-ahead log: owns
- Write-ahead log → Log records: serializes records
- Database API → Crash recovery: runs on open
- Crash recovery → B+Tree index: replays changes
- Crash recovery → Buffer pool: flushes recovered pages
- Database API → Checkpointing: checkpoints
- Checkpointing → Buffer pool: flushes pages
- Checkpointing → Write-ahead log: records checkpoint
