Expand description
WAL segment management.
The WAL is split into fixed-size segment files for efficient truncation. Each segment is a standalone WAL file containing records within an LSN range.
§Naming convention
Segments are named wal-{first_lsn:020}.seg — zero-padded for lexicographic
ordering. This guarantees ls and readdir return segments in LSN order.
§Lifecycle
- Writer creates a new segment when the current segment exceeds
target_size. - The active segment is the one being appended to.
truncate_before(lsn)deletes all sealed segments whosemax_lsn < lsn.- The active segment is NEVER deleted — only sealed (closed) segments are eligible.
§Recovery
On startup, all segment files in the WAL directory are discovered via readdir,
sorted by first_lsn, and replayed in order. The last segment is the active one.
Structs§
- Segment
Meta - Metadata about a WAL segment file on disk.
- Truncate
Result - Result of a WAL truncation operation.
Constants§
- DEFAULT_
SEGMENT_ TARGET_ SIZE - Default segment target size: 64 MiB.
Functions§
- discover_
segments - Discover all WAL segments in a directory, sorted by first_lsn.
- fsync_
directory - Fsync a directory to ensure file creation/deletion metadata is durable.
- migrate_
legacy_ wal - Migrate a legacy single-file WAL to the segmented format.
- segment_
filename - Build a segment filename from a starting LSN.
- segment_
path - Build a full segment path within a WAL directory.
- truncate_
segments - Delete all sealed segments whose maximum LSN is strictly less than
checkpoint_lsn.