English
ours-journal
libs/journal — on-disk state as an append-only journal of records with a checksum, and the encoding the records are written in.
Format
An 8-byte header: SBCJ, the version (1), Kind — the owner's label, so that a journal of one kind cannot be read as another. Then the records: length (u32 LE), CRC32 (crc32fast), bytes.
Design
read(path, kind)— the journal's records. No file, a foreign version or a foreignKind— empty (a foreign header or a read error is logged). Reading stops at the first record that fails its length or CRC, so a torn last record loses only itself.Journal::create(path, kind, records, immediate)— a new file from the initial records.appendaccumulates a record in memory; withimmediateit writes right away.flushwrites what was accumulated, and so doesDrop.tick— periodic maintenance:flush, and if the file has grown more than twice compared with the last rewrite (and by no less than 64 KiB), the journal is rewritten from the owner's snapshot. The snapshot comes from the closure given toset_snapshot; it returnsNoneonce the owner is gone, and the file is then left as is rather than rewritten empty.rewritecan also be called directly; records added during a rewrite go after the snapshot — each sets what it names, so a repeat is harmless.replacewrites the file next to it (<path>%tmp) and renames it into place: after a crash the disk has either the whole old file or the whole new one.codec—Encoder/Decoder: fixed-width little-endian numbers and length-prefixed bytes/strings.
Who uses it
ours-mod-cache (modules/system/cache, the persistence feature) — the cache directory: the journals of the fakeip table (fakeip/<server>), of DNS responses and of rejected responses (<scope>/dns, <scope>/rdrc). The directory format is described in modules/system/cache/src/enabled.rs. ours_api::cache has its own Journal and Snapshot traits, which the cache module implements over this journal, so ours-api does not depend on it; dns encodes its records with its codec; ours-mod-subscriptions (the cache feature) encodes saved subscriptions with its codec.
Specifics
- Dependencies —
crc32fast,tracing. - Write errors are not returned but logged: the cache must not stop the proxy.
Tests
Unit tests in src/ (a torn record, a rewrite, a foreign kind).