# krishnazawar/swiftcache architecture

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

- Interactive diagram: https://gitdiagram.com/krishnazawar/swiftcache
- Repository: https://github.com/krishnazawar/swiftcache

## Overview

SwiftCache is a single-threaded, in-memory key-value database exposed through a TCP command interface. A client command is received by the server, parsed and dispatched to a type-specific or generic driver, which operates on the database and returns a response. The database stores typed values, manages key expiry and incremental rehashing, and can optionally recover from and append to a write-ahead log. Source excerpts are partial, so links for unsampled drivers and database internals reflect documented capabilities and visible interfaces; uncertain implementation details are omitted.

Main entry points: [`src/main.cpp`](https://github.com/krishnazawar/swiftcache/blob/master/src/main.cpp) (Server loop), [`src/parser.cpp`](https://github.com/krishnazawar/swiftcache/blob/master/src/parser.cpp) (Command parser), [`src/database/database.cpp`](https://github.com/krishnazawar/swiftcache/blob/master/src/database/database.cpp) (Key database), [`src/WAL/wal.cpp`](https://github.com/krishnazawar/swiftcache/blob/master/src/WAL/wal.cpp) (WAL manager)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["TCP interface"]
  node_server["Server loop<br/>[main.cpp]"]
  node_socket["Socket platform<br/>[socket.cpp]"]
end

subgraph group_commands["Command handling"]
  node_parser["Command parser<br/>[parser.cpp]"]
  node_string_driver["String driver<br/>[stringDriver.cpp]"]
  node_list_driver["List driver<br/>[listDriver.cpp]"]
  node_hash_driver["Hash driver<br/>[hashDriver.cpp]"]
  node_generic_driver["Generic driver<br/>[genericsDriver.cpp]"]
end

subgraph group_storage["Database storage"]
  node_database[("Key database<br/>[database.cpp]")]
  node_expiry["Expiry and rehash<br/>[database.cpp]"]
end

subgraph group_types["Value types"]
  node_object["Key object<br/>[object.cpp]"]
  node_value["Typed value<br/>[value.cpp]"]
  node_list_type["List datatype<br/>[listDatatype.cpp]"]
  node_hash_type["Hash datatype<br/>[hashDatatype.cpp]"]
end

subgraph group_persistence["Persistence"]
  node_wal["WAL manager<br/>[wal.cpp]"]
end

node_client(("TCP client"))
node_log_file["WAL file"]

node_client -->|"sends commands"| node_server
node_server -->|"initializes sockets"| node_socket
node_server -->|"parses commands"| node_parser
node_server -->|"sends responses"| node_client
node_parser -->|"dispatches strings"| node_string_driver
node_parser -->|"dispatches lists"| node_list_driver
node_parser -->|"dispatches hashes"| node_hash_driver
node_parser -->|"dispatches generic"| node_generic_driver
node_string_driver -->|"reads and writes"| node_database
node_list_driver -->|"reads and writes"| node_database
node_hash_driver -->|"reads and writes"| node_database
node_generic_driver -->|"reads and writes"| node_database
node_database -->|"stores objects"| node_object
node_database -->|"runs maintenance"| node_expiry
node_object -->|"holds value"| node_value
node_value -->|"wraps lists"| node_list_type
node_value -->|"wraps hashes"| node_hash_type
node_server -.->|"configures optional WAL"| node_wal
node_server -.->|"replays log commands"| node_parser
node_parser -.->|"appends commands"| node_wal
node_database -.->|"coordinates compaction"| node_wal
node_wal -.->|"dumps and compacts"| node_log_file
node_server -.->|"loads on startup"| node_log_file

click node_server "https://github.com/krishnazawar/swiftcache/blob/master/src/main.cpp"
click node_socket "https://github.com/krishnazawar/swiftcache/blob/master/src/platform/socket.cpp"
click node_parser "https://github.com/krishnazawar/swiftcache/blob/master/src/parser.cpp"
click node_string_driver "https://github.com/krishnazawar/swiftcache/blob/master/src/drivers/stringDriver.cpp"
click node_list_driver "https://github.com/krishnazawar/swiftcache/blob/master/src/drivers/listDriver.cpp"
click node_hash_driver "https://github.com/krishnazawar/swiftcache/blob/master/src/drivers/hashDriver.cpp"
click node_generic_driver "https://github.com/krishnazawar/swiftcache/blob/master/src/drivers/genericsDriver.cpp"
click node_database "https://github.com/krishnazawar/swiftcache/blob/master/src/database/database.cpp"
click node_expiry "https://github.com/krishnazawar/swiftcache/blob/master/src/database/database.cpp"
click node_object "https://github.com/krishnazawar/swiftcache/blob/master/src/models/object.cpp"
click node_value "https://github.com/krishnazawar/swiftcache/blob/master/src/models/value.cpp"
click node_list_type "https://github.com/krishnazawar/swiftcache/blob/master/src/models/datatypes/listDatatype.cpp"
click node_hash_type "https://github.com/krishnazawar/swiftcache/blob/master/src/models/datatypes/hashDatatype.cpp"
click node_wal "https://github.com/krishnazawar/swiftcache/blob/master/src/WAL/wal.cpp"

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_server,node_socket,node_client toneBlue
class node_parser,node_string_driver,node_list_driver,node_hash_driver,node_generic_driver toneAmber
class node_database,node_expiry toneMint
class node_object,node_value,node_list_type,node_hash_type toneRose
class node_wal,node_log_file toneIndigo
```

## Components

### TCP interface

- [Server loop](https://github.com/krishnazawar/swiftcache/blob/master/src/main.cpp): `src/main.cpp`
- [Socket platform](https://github.com/krishnazawar/swiftcache/blob/master/src/platform/socket.cpp): `src/platform/socket.cpp`

### Command handling

- [Command parser](https://github.com/krishnazawar/swiftcache/blob/master/src/parser.cpp): `src/parser.cpp`
- [String driver](https://github.com/krishnazawar/swiftcache/blob/master/src/drivers/stringDriver.cpp): `src/drivers/stringDriver.cpp`
- [List driver](https://github.com/krishnazawar/swiftcache/blob/master/src/drivers/listDriver.cpp): `src/drivers/listDriver.cpp`
- [Hash driver](https://github.com/krishnazawar/swiftcache/blob/master/src/drivers/hashDriver.cpp): `src/drivers/hashDriver.cpp`
- [Generic driver](https://github.com/krishnazawar/swiftcache/blob/master/src/drivers/genericsDriver.cpp): `src/drivers/genericsDriver.cpp`

### Database storage

- [Key database](https://github.com/krishnazawar/swiftcache/blob/master/src/database/database.cpp): `src/database/database.cpp`
- [Expiry and rehash](https://github.com/krishnazawar/swiftcache/blob/master/src/database/database.cpp): `src/database/database.cpp`

### Value types

- [Key object](https://github.com/krishnazawar/swiftcache/blob/master/src/models/object.cpp): `src/models/object.cpp`
- [Typed value](https://github.com/krishnazawar/swiftcache/blob/master/src/models/value.cpp): `src/models/value.cpp`
- [List datatype](https://github.com/krishnazawar/swiftcache/blob/master/src/models/datatypes/listDatatype.cpp): `src/models/datatypes/listDatatype.cpp`
- [Hash datatype](https://github.com/krishnazawar/swiftcache/blob/master/src/models/datatypes/hashDatatype.cpp): `src/models/datatypes/hashDatatype.cpp`

### Persistence

- [WAL manager](https://github.com/krishnazawar/swiftcache/blob/master/src/WAL/wal.cpp): `src/WAL/wal.cpp`

### Other components

- **TCP client**
- **WAL file**

## Connections

- TCP client → Server loop: sends commands
- Server loop → Socket platform: initializes sockets
- Server loop → Command parser: parses commands
- Server loop → TCP client: sends responses
- Command parser → String driver: dispatches strings
- Command parser → List driver: dispatches lists
- Command parser → Hash driver: dispatches hashes
- Command parser → Generic driver: dispatches generic
- String driver → Key database: reads and writes
- List driver → Key database: reads and writes
- Hash driver → Key database: reads and writes
- Generic driver → Key database: reads and writes
- Key database → Key object: stores objects
- Key database → Expiry and rehash: runs maintenance
- Key object → Typed value: holds value
- Typed value → List datatype: wraps lists
- Typed value → Hash datatype: wraps hashes
- Server loop → WAL manager: configures optional WAL
- Server loop → Command parser: replays log commands
- Command parser → WAL manager: appends commands
- Key database → WAL manager: coordinates compaction
- WAL manager → WAL file: dumps and compacts
- Server loop → WAL file: loads on startup
