Write-Ahead Log¶
Append-only, msgpack-framed WAL for crash recovery.
WALEntry¶
WALEntry(seq, timestamp_ms, op, key, value)
dataclass
¶
A single write-ahead log entry.
Fields are ordered to match the msgpack encoding:
packb((seq, timestamp_ms, op, key, value), use_bin_type=True)
Attributes:
| Name | Type | Description |
|---|---|---|
seq |
SeqNum
|
Monotonically increasing sequence number. |
timestamp_ms |
int
|
Wall-clock timestamp in milliseconds since epoch. |
op |
OpType
|
Operation type ( |
key |
Key
|
The raw byte key. |
value |
Value
|
The raw byte value (or |
is_tombstone
property
¶
Return True if this entry represents a deletion.
WALWriter¶
WALWriter(path)
¶
Append-only WAL file writer with msgpack-framed entries.
Owns the file handle for a single wal.log file. All methods are
synchronous — async wrapping is done by :class:WALManager.
Open or create a WAL file at path in append mode.
Parent directories are created automatically if they do not exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Filesystem path for the WAL file (e.g. |
required |
Raises:
| Type | Description |
|---|---|
OSError
|
If the file cannot be opened or created. |
Source code in app/wal/writer.py
path
property
¶
Return the WAL file path.
append(entry)
¶
Encode entry as msgpack, write to WAL, and fsync.
Source code in app/wal/writer.py
replay()
¶
Read the WAL file and return all entries sorted by seq.
Returns an empty list when the file does not exist or is empty.
Raises :class:WALCorruptError if decoding or CRC check fails.
The framed format is: [4B length][payload][4B CRC].
Source code in app/wal/writer.py
truncate_before(seq)
¶
Remove all entries with entry.seq <= seq.
Rewrites the WAL to a temp file, then atomically replaces the
original via :func:os.replace.
Source code in app/wal/writer.py
close()
¶
Fsync and close the WAL file handle.
Cleanup never raises — errors are logged as warnings.
Source code in app/wal/writer.py
WALManager¶
WALManager(wal)
¶
Thread-safe, async-aware wrapper around :class:WALWriter.
Wrap an existing WALWriter with thread-safe locking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wal
|
WALWriter
|
The underlying synchronous WAL writer to manage. |
required |
Source code in app/engine/wal_manager.py
open(path)
classmethod
¶
Create a :class:WALWriter at path and wrap it.
Source code in app/engine/wal_manager.py
sync_append(entry)
¶
append(entry)
async
¶
replay()
¶
Replay the WAL — synchronous, called before workers.