# pjtatlow/jammdb architecture

> Architecture diagram of the GitHub repository pjtatlow/jammdb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pjtatlow/jammdb
- Repository: https://github.com/pjtatlow/jammdb

## Overview

##### Main Components

a. Core Database Components:
- Database Manager (db.rs)
- Transaction Manager (tx.rs)
- Bucket Storage (bucket.rs)
- B+ Tree Implementation (node.rs, page_node.rs)
- Page Management (page.rs)
- Memory Management (freelist.rs)
- Metadata Handler (meta.rs)

b. Supporting Components:
- Error Handling System (errors.rs)
- Byte Management (bytes.rs)
- Cursor Implementation (cursor.rs)

**┌─ Database API (lib.rs) ─┐**
- Open/Close Operations│
- Transaction Handling │

**┌─ Transaction Manager ─┐**
- Read-only Txs     │
- Write Txs         │

**┌─ Bucket Management ─┐    ┌─ B+ Tree ─┐**
- Key-Value Pairs  │ ─► │ Nodes     │
- Nested Buckets   │    │ Pages     │

**┌─ Page Manager ─┐    ┌─ Memory Manager ─┐**
- Page Alloc  │ ◄► │ Free List       │
- Page Access │    │ Memory Mapping   │

##### Key Relationships to Highlight

- One-way flow from Bucket Management to B+ Tree
- Page Manager with both B+ Tree and Memory Manager
- Error handling system connecting to all components

##### Implementation Details to Note

- The single-level store architecture
- The separation between read and write transactions
- The bucket hierarchy system
- The page management system
- The memory mapping strategy

##### Additional Elements

- External interfaces (file system interaction)
- Serialization/deserialization support
- Error propagation paths
- Testing infrastructure

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles
    classDef api fill:#90EE90,stroke:#333,stroke-width:2px
    classDef core fill:#ADD8E6,stroke:#333,stroke-width:2px
    classDef memory fill:#FFE4B5,stroke:#333,stroke-width:2px
    classDef utility fill:#D3D3D3,stroke:#333,stroke-width:2px
    classDef critical fill:#FFB6C1,stroke:#333,stroke-width:2px

    %% Layer 1 - Public API
    subgraph API
        PublicAPI["Database API"]:::api
        LifetimeMgr["Lifetime Manager"]:::utility
    end
    click PublicAPI "https://github.com/pjtatlow/jammdb/blob/master/src/lib.rs"
    click LifetimeMgr "https://github.com/pjtatlow/jammdb/blob/master/src/lifetimes.rs"

    %% Layer 2 - Transaction Management
    subgraph TransactionLayer
        TxManager["Transaction Manager"]:::core
        ErrorHandler["Error Handler"]:::utility
    end
    click TxManager "https://github.com/pjtatlow/jammdb/blob/master/src/tx.rs"
    click ErrorHandler "https://github.com/pjtatlow/jammdb/blob/master/src/errors.rs"

    %% Layer 3 - Data Organization
    subgraph DataLayer
        BucketMgr["Bucket Manager"]:::core
        BPlusTree["B+ Tree"]:::core
        DataHandler["Data Handler"]:::core
        Node["Node Manager"]:::core
        PageNode["Page Node"]:::core
    end
    click BucketMgr "https://github.com/pjtatlow/jammdb/blob/master/src/bucket.rs"
    click Node "https://github.com/pjtatlow/jammdb/blob/master/src/node.rs"
    click PageNode "https://github.com/pjtatlow/jammdb/blob/master/src/page_node.rs"
    click DataHandler "https://github.com/pjtatlow/jammdb/blob/master/src/data.rs"

    %% Layer 4 - Storage Management
    subgraph StorageLayer
        PageMgr["Page Manager"]:::memory
        MemoryMgr["Memory Manager"]:::memory
        FreeList["Free List"]:::memory
        ByteMgr["Byte Manager"]:::utility
        Cursor["Cursor"]:::utility
        MetaHandler["Metadata Handler"]:::core
    end
    click PageMgr "https://github.com/pjtatlow/jammdb/blob/master/src/page.rs"
    click FreeList "https://github.com/pjtatlow/jammdb/blob/master/src/freelist.rs"
    click ByteMgr "https://github.com/pjtatlow/jammdb/blob/master/src/bytes.rs"
    click Cursor "https://github.com/pjtatlow/jammdb/blob/master/src/cursor.rs"
    click MetaHandler "https://github.com/pjtatlow/jammdb/blob/master/src/meta.rs"

    %% Relationships
    PublicAPI --> TxManager
    PublicAPI --> LifetimeMgr
    TxManager --> BucketMgr
    TxManager --> ErrorHandler
    BucketMgr --> BPlusTree
    BPlusTree --> Node
    BPlusTree --> PageNode
    Node --> PageMgr
    PageNode --> PageMgr
    BucketMgr --> DataHandler
    PageMgr <--> MemoryMgr
    MemoryMgr --> FreeList
    PageMgr --> ByteMgr
    BucketMgr --> Cursor
    MemoryMgr --> MetaHandler

    %% Legend
    subgraph Legend
        L1[Public API]:::api
        L2[Core Components]:::core
        L3[Memory Management]:::memory
        L4[Utilities]:::utility
        L5[Critical Path]:::critical
    end
```
