# project-mydb/mydb architecture

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

- Interactive diagram: https://gitdiagram.com/project-mydb/mydb
- Repository: https://github.com/project-mydb/mydb

## Overview

MYDB is a partition-isolated relational database served by one daemon. A client connects over Unix socket or TCP, authenticates, submits SQL, and receives a result; the engine coordinates parsing, execution, and partition-scoped state. SQL capabilities include DDL, DML, queries, joins, transactions, and ANALYZE-driven planning. The graph preserves the documented planner and clustered-storage branches. Some implementation files were not sampled, so uncertain runtime links are dashed rather than asserted.

Main entry points: [`engine/src/engine.c`](https://github.com/project-mydb/mydb/blob/main/engine/src/engine.c) (Engine and sessions), [`partition_manager/src/pm_api.c`](https://github.com/project-mydb/mydb/blob/main/partition_manager/src/pm_api.c) (Partition operations), [`storage_engine/src/storage.c`](https://github.com/project-mydb/mydb/blob/main/storage_engine/src/storage.c) (Table storage API), [`server/src/server.c`](https://github.com/project-mydb/mydb/blob/main/server/src/server.c) (Session dispatch)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_repl["Command-line REPL<br/>[client_repl.c]"]
  node_client_conn["Client connection<br/>[client_conn.c]"]
end

subgraph group_server["Server and identity"]
  node_daemon["Database daemon<br/>[mydbd.c]"]
  node_listeners["Unix and TCP listeners<br/>[listener.c]"]
  node_protocol["Packet protocol<br/>[protocol.c]"]
  node_auth["Challenge-response auth<br/>[auth.c]"]
  node_crypto["Password hashing<br/>[crypto.c]"]
  node_session["Session dispatch<br/>[server.c]"]
end

subgraph group_sql["SQL processing"]
  node_engine["Engine and sessions<br/>[engine.c]"]
  node_parser["SQL lexer and parser<br/>[Parser.cpp]"]
  node_executor["Statement execution"]
  node_planner["Cost-based planner<br/>[planner.c]"]
end

subgraph group_partition["Partition management"]
  node_pm_api["Partition operations<br/>[pm_api.c]"]
  node_partition_ctx["Partition lifecycle<br/>[partition_ctx.c]"]
  node_partition_files[("Partition catalog<br/>[partition.c]")]
  node_schema[("Schema definitions<br/>[schema_file.c]")]
  node_stats[("Column statistics<br/>[stats.c]")]
end

subgraph group_storage["Storage and metadata"]
  node_storage["Table storage API<br/>[storage.c]"]
  node_btree["Clustered B+ trees<br/>[btree.c]"]
  node_buffer["Buffer pool<br/>[buffer_pool.c]"]
  node_disk[("Table files and pages<br/>[disk_manager.c]")]
  node_system_schema[("System schema<br/>[system_schema.c]")]
end

node_user(("SQL user"))

node_user -->|"enters SQL"| node_repl
node_repl -->|"submits queries"| node_client_conn
node_client_conn -->|"exchanges packets"| node_protocol
node_client_conn -->|"connects over socket"| node_listeners
node_daemon -.->|"runs event loop"| node_session
node_listeners -.->|"accepts sessions"| node_session
node_session -.->|"authenticates"| node_auth
node_auth -->|"checks response"| node_crypto
node_session -.->|"dispatches SQL"| node_engine
node_engine -.->|"parses statements"| node_parser
node_engine -->|"executes AST"| node_executor
node_executor -.->|"runs table operations"| node_pm_api
node_executor -.->|"plans queries"| node_planner
node_planner -.->|"reads statistics"| node_stats
node_planner -.->|"reads definitions"| node_schema
node_engine -->|"acquires partition"| node_partition_ctx
node_pm_api -->|"uses context"| node_partition_ctx
node_pm_api -->|"updates metadata"| node_schema
node_pm_api -.->|"collects ANALYZE stats"| node_stats
node_pm_api -->|"performs table I/O"| node_storage
node_storage -.->|"uses indexes"| node_btree
node_storage -.->|"caches pages"| node_buffer
node_storage -->|"reads and writes pages"| node_disk
node_engine -.->|"manages accounts"| node_system_schema
node_engine -->|"opens partition catalog"| node_partition_files

click node_repl "https://github.com/project-mydb/mydb/blob/main/client/src/client_repl.c"
click node_client_conn "https://github.com/project-mydb/mydb/blob/main/client/src/client_conn.c"
click node_daemon "https://github.com/project-mydb/mydb/blob/main/bin/mydbd.c"
click node_listeners "https://github.com/project-mydb/mydb/blob/main/server/src/listener.c"
click node_protocol "https://github.com/project-mydb/mydb/blob/main/server/src/protocol.c"
click node_auth "https://github.com/project-mydb/mydb/blob/main/server/src/auth.c"
click node_crypto "https://github.com/project-mydb/mydb/blob/main/crypto/src/crypto.c"
click node_session "https://github.com/project-mydb/mydb/blob/main/server/src/server.c"
click node_engine "https://github.com/project-mydb/mydb/blob/main/engine/src/engine.c"
click node_parser "https://github.com/project-mydb/mydb/blob/main/query_parser/src/Parser.cpp"
click node_executor "https://github.com/project-mydb/mydb/blob/main/execution_engine/src/exec_engine_api.cpp"
click node_planner "https://github.com/project-mydb/mydb/blob/main/planner/src/planner.c"
click node_pm_api "https://github.com/project-mydb/mydb/blob/main/partition_manager/src/pm_api.c"
click node_partition_ctx "https://github.com/project-mydb/mydb/blob/main/partition_manager/src/partition_ctx.c"
click node_partition_files "https://github.com/project-mydb/mydb/blob/main/partition_manager/src/partition.c"
click node_schema "https://github.com/project-mydb/mydb/blob/main/partition_manager/src/schema_file.c"
click node_stats "https://github.com/project-mydb/mydb/blob/main/partition_manager/src/stats.c"
click node_storage "https://github.com/project-mydb/mydb/blob/main/storage_engine/src/storage.c"
click node_btree "https://github.com/project-mydb/mydb/blob/main/storage_engine/src/btree.c"
click node_buffer "https://github.com/project-mydb/mydb/blob/main/storage_engine/src/buffer_pool.c"
click node_disk "https://github.com/project-mydb/mydb/blob/main/storage_engine/src/disk_manager.c"
click node_system_schema "https://github.com/project-mydb/mydb/blob/main/system_schema/src/system_schema.c"

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_repl,node_client_conn,node_user toneBlue
class node_daemon,node_listeners,node_protocol,node_auth,node_crypto,node_session toneAmber
class node_engine,node_parser,node_executor,node_planner toneMint
class node_pm_api,node_partition_ctx,node_partition_files,node_schema,node_stats toneRose
class node_storage,node_btree,node_buffer,node_disk,node_system_schema toneIndigo
```

## Components

### Client

- [Command-line REPL](https://github.com/project-mydb/mydb/blob/main/client/src/client_repl.c): `client/src/client_repl.c`
- [Client connection](https://github.com/project-mydb/mydb/blob/main/client/src/client_conn.c): `client/src/client_conn.c`

### Server and identity

- [Database daemon](https://github.com/project-mydb/mydb/blob/main/bin/mydbd.c): `bin/mydbd.c`
- [Unix and TCP listeners](https://github.com/project-mydb/mydb/blob/main/server/src/listener.c): `server/src/listener.c`
- [Packet protocol](https://github.com/project-mydb/mydb/blob/main/server/src/protocol.c): `server/src/protocol.c`
- [Challenge-response auth](https://github.com/project-mydb/mydb/blob/main/server/src/auth.c): `server/src/auth.c`
- [Password hashing](https://github.com/project-mydb/mydb/blob/main/crypto/src/crypto.c): `crypto/src/crypto.c`
- [Session dispatch](https://github.com/project-mydb/mydb/blob/main/server/src/server.c): `server/src/server.c`

### SQL processing

- [Engine and sessions](https://github.com/project-mydb/mydb/blob/main/engine/src/engine.c): `engine/src/engine.c`
- [SQL lexer and parser](https://github.com/project-mydb/mydb/blob/main/query_parser/src/Parser.cpp): `query_parser/src/Parser.cpp`
- [Statement execution](https://github.com/project-mydb/mydb/blob/main/execution_engine/src/exec_engine_api.cpp): `execution_engine/src/exec_engine_api.cpp`
- [Cost-based planner](https://github.com/project-mydb/mydb/blob/main/planner/src/planner.c): `planner/src/planner.c`

### Partition management

- [Partition operations](https://github.com/project-mydb/mydb/blob/main/partition_manager/src/pm_api.c): `partition_manager/src/pm_api.c`
- [Partition lifecycle](https://github.com/project-mydb/mydb/blob/main/partition_manager/src/partition_ctx.c): `partition_manager/src/partition_ctx.c`
- [Partition catalog](https://github.com/project-mydb/mydb/blob/main/partition_manager/src/partition.c): `partition_manager/src/partition.c`
- [Schema definitions](https://github.com/project-mydb/mydb/blob/main/partition_manager/src/schema_file.c): `partition_manager/src/schema_file.c`
- [Column statistics](https://github.com/project-mydb/mydb/blob/main/partition_manager/src/stats.c): `partition_manager/src/stats.c`

### Storage and metadata

- [Table storage API](https://github.com/project-mydb/mydb/blob/main/storage_engine/src/storage.c): `storage_engine/src/storage.c`
- [Clustered B+ trees](https://github.com/project-mydb/mydb/blob/main/storage_engine/src/btree.c): `storage_engine/src/btree.c`
- [Buffer pool](https://github.com/project-mydb/mydb/blob/main/storage_engine/src/buffer_pool.c): `storage_engine/src/buffer_pool.c`
- [Table files and pages](https://github.com/project-mydb/mydb/blob/main/storage_engine/src/disk_manager.c): `storage_engine/src/disk_manager.c`
- [System schema](https://github.com/project-mydb/mydb/blob/main/system_schema/src/system_schema.c): `system_schema/src/system_schema.c`

### Other components

- **SQL user**

## Connections

- SQL user → Command-line REPL: enters SQL (evidence: `README.md`)
- Command-line REPL → Client connection: submits queries (evidence: `client/src/client_repl.c`)
- Client connection → Packet protocol: exchanges packets (evidence: `client/src/client_conn.c`)
- Client connection → Unix and TCP listeners: connects over socket (evidence: `README.md`)
- Database daemon → Session dispatch: runs event loop
- Unix and TCP listeners → Session dispatch: accepts sessions
- Session dispatch → Challenge-response auth: authenticates
- Challenge-response auth → Password hashing: checks response (evidence: `server/src/auth.c`)
- Session dispatch → Engine and sessions: dispatches SQL
- Engine and sessions → SQL lexer and parser: parses statements
- Engine and sessions → Statement execution: executes AST
- Statement execution → Partition operations: runs table operations
- Statement execution → Cost-based planner: plans queries
- Cost-based planner → Column statistics: reads statistics
- Cost-based planner → Schema definitions: reads definitions
- Engine and sessions → Partition lifecycle: acquires partition (evidence: `engine/src/engine.c`)
- Partition operations → Partition lifecycle: uses context (evidence: `partition_manager/src/pm_api.c`)
- Partition operations → Schema definitions: updates metadata (evidence: `partition_manager/src/pm_api.c`)
- Partition operations → Column statistics: collects ANALYZE stats
- Partition operations → Table storage API: performs table I/O (evidence: `partition_manager/src/pm_api.c`)
- Table storage API → Clustered B+ trees: uses indexes
- Table storage API → Buffer pool: caches pages
- Table storage API → Table files and pages: reads and writes pages
- Engine and sessions → System schema: manages accounts
- Engine and sessions → Partition catalog: opens partition catalog (evidence: `engine/src/engine.c`)
