Question
Build a map whose values survive a process crash. A memory-only map does not meet the contract. `get` returns the stored value or nothing. `put` stores a pair and must reach disk before it returns. Startup reads the file and rebuilds the map. A torn or partial final line is ignored. ## Requirements - Part one is an append-only log. Each `put` adds one line. Startup replays the file. Never rewrite a line in place. - An update of an existing key appends a newer line. Replay keeps the last complete line for that key. - A second process that opens the same path must see committed values. - Part two is compaction. After many updates of one key, keep the live value and drop the dead lines. Compact by writing a temporary file and swapping it into place, after a write count, after a size cap, or on shutdown. - Part three stores one append-only file per key when values are large. Startup may open a key's file only when that key is requested. The live value is the last complete line. ## Review checklist 1. Does a crash during `put` leave either the old value or the new value, never a mix of both? 2. Does replay ignore a torn final line and still return every earlier committed key? 3. Can a second process read a value after the first process returns from `put`? 4. Does compaction preserve the latest value for every key and remove only dead lines? ## Staged follow-ups 1. Choose text or binary lines, and say what a torn write does. 2. Make `put` wait until the data is durable, and name the call that provides that wait. 3. Let several processes share the file, and state the lock that prevents two writers from interleaving lines. 4. When the map does not fit in memory, keep cold keys on disk and define which keys stay in memory.