engine module parses and executes RadishDB commands against a HashTable. It is the layer between the REPL/server and the storage layer — all user-facing commands (SET, GET, DEL, TTL, INFO, SAVE, LOAD, BENCH, etc.) are dispatched here.
Data structures
ResultType
Discriminant tag for the Result union.
| Variant | Meaning |
|---|---|
RES_STRING | A heap-allocated string payload is present in result.value.string. |
RES_OK | The command succeeded with no value to return (e.g. SET). No payload. |
RES_ERROR | A heap-allocated error message is present in result.value.string. |
RES_INTEGER | A long result is present in result.value.integer (e.g. TTL, DEL, COUNT). |
RES_NIL | The command returned a null result (e.g. GET on a missing key). No payload. |
RES_CLEAN | Signals the REPL to clear the terminal screen. No payload. |
RES_EXIT | Signals the REPL or server to shut down cleanly. No payload. |
Result
The return value of execute_command. Carries both the result type and, for some variants, a payload.
Discriminant tag. Determines which field of
value is valid.engine_start_time
INFO command reads this to compute uptime. Set it before calling execute_command for the first time:
Functions
execute_command
Parses and executes a single RadishDB command string.
The hash table the command operates on.
A mutable, null-terminated command string such as
"SET name radish". The function modifies this buffer in-place: it calls trim_newline to strip trailing whitespace and strtok (via split_tokens) to tokenize it. Do not pass a string literal.Result struct. The caller must call free_result on it after use.
Handling each result type
free_result
Frees the heap-allocated payload of a Result, if any.
Pointer to the result to free. For
RES_STRING and RES_ERROR, frees r->value.string. For all other types, this is a no-op.It is safe to call
free_result on any result type — it only frees memory when a payload is present.print_result
Formats and writes a Result to a FILE * output stream.
Output file pointer —
stdout for the REPL, a client socket file pointer for the TCP server.The result to print. Each type is formatted as follows:
ResultType | Output |
|---|---|
RES_STRING | The string followed by \n |
RES_ERROR | The error message followed by \n |
RES_OK | OK\n |
RES_INTEGER | The integer as %ld\n |
RES_NIL | (nil)\n |
RES_CLEAN | No output (REPL clears screen separately) |
RES_EXIT | No output |
print_result does not call free_result. You must call free_result separately after printing.Full example
Server frontend
start_server
Starts the TCP server, binds to port 6379, and enters the command processing loop.
The hash table the server operates on. All commands received over TCP are executed against this table.
- Binds to
0.0.0.0:6379 - Accepts one client connection at a time (
listenbacklog of 1) - Reads commands line by line from the client socket
- Calls
execute_commandandprint_resultfor each line - Calls
expire_sweep(ht, 10)on every iteration - Exits when the client disconnects
server.h.