Expand description
Crash-safe file replacement and the workspace index lock.
Index binaries (content.bin, trigrams.bin) used to be opened at their
final path with truncate(true) and streamed into. A crash, a killed
process, or a second indexer starting mid-write left a short file behind,
and the next reader failed with content.bin is too small - appears to be corrupted. Every writer now goes through this module:
- write to
<final>.tmpin the same directory, sync_all,atomic_replacerenames the temp file over the final path.
Readers therefore only ever see the previous complete file or the new complete file. Because the rename lands in the same directory, it is atomic on every platform we ship to.
IndexLock is an OS advisory lock on .reflex/index.lock held for the
whole Indexer::index run. The OS releases it when the process dies, so
there is no stale-PID bookkeeping.
Structs§
- Index
Lock - RAII guard for the workspace index lock.
Constants§
- INDEX_
LOCK_ FILE - File name of the workspace index lock inside the cache directory.
- TMP_
SUFFIX - Suffix appended to a final path to build its in-progress temp path.
Functions§
- atomic_
replace - Rename
tmpoverfinal_path. - remove_
stale_ tmp - Remove leftover
*.tmpfiles indir(a crashed indexer leaves them). - tmp_
path_ for - Temp path for
final_path: same directory, same file name plus.tmp.