aof module implements RadishDB’s append-only file (AOF) write-ahead log. Every mutation (SET, DEL) is appended as a structured record to a file on disk. On restart, aof_replay re-applies those records to restore state. aof_rewrite compacts the log by rewriting only the live entries.
The AOF module keeps a single module-level file handle. Only one AOF file can be open at a time.
Utility functions
trim_newline
Strips a trailing newline or carriage-return from a string in-place.
Mutable null-terminated string. Modified in-place.
aof_replay to clean up lines read from the AOF file. You will rarely need to call it directly.
split_tokens
Tokenizes a whitespace-delimited command line into an argument vector.
Mutable null-terminated command string. Modified in-place by
strtok.Output array of token pointers. Each element points into
line after tokenization.Maximum number of tokens to extract.
aof_replay to parse each AOF record line. Also shared with execute_command in engine.c.
Core functions
aof_open
Opens the AOF file for append-and-read access, creating it if it does not exist.
Path to the AOF file, e.g.
"aof/radish.aof". Opened in "ab+" mode.1 on success, 0 if fopen fails.
Sets the module-level aof_file pointer used by all subsequent aof_append_* and aof_rewrite calls.
aof_close
Flushes and closes the AOF file.
aof_append_set
Appends a SET record to the AOF.
The key being set.
The value being stored.
TTL string in seconds (e.g.
"30"), or NULL for no expiry. In C, passing integer 0 is equivalent to NULL and is treated as no expiry.aof_append_del
Appends a DEL record to the AOF.
The key being deleted.
aof_replay
Replays an AOF file into a hash table, restoring all SET and DEL operations.
The hash table to replay mutations into.
Path to the AOF file to replay.
1 always. A missing file is not treated as an error (it is expected on first run).
Replay behavior
- Reads and validates the
AOFX1header if present; skips it if the file starts without a header (legacy format). - Applies
SETrecords by callinght_setwith the stored key, value, and expiry. - Applies
DELrecords by callinght_delete. - Stops on any corrupt or unrecognized frame.
aof_rewrite
Compacts the AOF by rewriting only the current live entries from the hash table.
The hash table whose live entries will be written.
Target AOF filename. The rewrite writes to
<filename>.tmp, then atomically renames it to filename.Rewrite sequence
- Writes all live
SETentries (with their currentexpires_at) toaof/radish.aof.tmp. - Renames the temporary file to
filename(atomic on POSIX systems). - Reopens
aof_filein"ab"mode pointing to the new file.