Skip to content

feat: Add Write-Ahead Log With Torn-Write Recovery - #2

Merged
ThomasHartDev merged 2 commits into
mainfrom
thomas/feat/scaffold
Sep 10, 2026
Merged

ThomasHartDev merged 2 commits into
mainfrom
thomas/feat/scaffold

Conversation

@ThomasHartDev

Copy link
Copy Markdown
Owner

Puts a length-prefixed WAL under Store so put and delete survive process exit. Each record carries a CRC-32 over the payload. Open replays the log into the memtable. A short tail, or a last-record length that runs past EOF, is truncated to the last good offset and fsynced before appends resume. Payload-too-large is corruption only when those bytes are actually present in the file. A checksum miss in the middle of the file is still treated as corruption.

Mutations fsync before they hit the memtable, so a crash after return still recovers. Tests cover reopen, truncated tails, an inflated last-record length, a flipped last-record checksum, and a flipped mid-log byte.

Closes #1

Log put/delete records with CRC-32 framing and fsync, then apply to
the memtable. Open replays the log, truncates a torn tail, and
rejects checksum failures in the middle of the file.
Replay compared the length word to MAX_PAYLOAD before checking whether
those bytes exist. A 20MB length on a tiny log failed open and dropped
the good prefix. Truncate to the last good offset when the claimed
record runs past EOF, and fsync after every recovery set_len so a later
append cannot be buried by the old size.
@ThomasHartDev
ThomasHartDev merged commit 9b444f7 into main Sep 10, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a write-ahead log so the KV store survives crashes

1 participant