fix: журнал базы складывается обратно и не занимает место впустую

Спутник history.db-wal парковался на 4 МБ и таким и оставался: SQLite
складывает журнал в базу только при тысяче накопленных страниц, а сам
файл после этого не усекает. Место занято впустую, а резервная копия
одного файла базы оказывается неполной.

Измерено на 200 загрузках: одного ограничения размера мало - спутник
всё равно дорастает до 3,5 МБ. Помогает явная просьба сложить и усечь
после каждой записи: спутник остаётся нулевым, цена 0,04 мс на загрузку.
Занятый читатель просто делает просьбу безвредной.

Живая проверка: 31 перечитка панели подряд - спутник 0 КБ.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-08-11 18:00:07 +10:00
co-authored by Claude Opus 5
parent b26bae7fec
commit 1ac934843b
3 changed files with 66 additions and 6 deletions
+11
View File
@@ -377,6 +377,11 @@ export function openHistory(dbPath, { readOnly = false } = {}) {
state.db = new DatabaseSync(file);
// WAL lets the report script read while the panel writes
state.db.exec('PRAGMA journal_mode = WAL');
// ...but the write-ahead log is only folded back into the DB at 1000
// pages and is never truncated afterwards, so it parks at ~4 MB forever.
// The limit alone does not help (measured): the fold must be asked for
// explicitly after each write - see checkpoint() below.
state.db.exec('PRAGMA journal_size_limit = 1048576');
migrate(state.db);
}
// a corrupt file fails on the first statement, not on open - probe now
@@ -448,6 +453,12 @@ export function openHistory(dbPath, { readOnly = false } = {}) {
const { loadId, nextLineage, nextRegions } = writeLoad(db, state.lineage, state.regions, result, reason);
state.lineage = nextLineage;
state.regions = nextRegions;
// Fold the write-ahead log back into the DB and truncate it. Measured on
// 200 loads: without this the log parks at ~3.5 MB, with it at zero, and
// the cost is 0.04 ms per load. Matters twice - the file stops holding
// space for nothing, and a backup of history.db alone stops being a
// half-copy. A busy reader just makes it a no-op, which is fine.
try { db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); } catch { /* reader holds it: next time */ }
return loadId;
}).catch((error) => {
// a failed write must never take the panel down; drop the connection