Maintenance
Compaction, garbage collection, the one call to schedule, recovery, and format levels.
On this page
Writes land in a log that queries also read. Something has to fold it into segments and build indexes. The library starts no background task itself; schedule maintain and index from cron, a loop or a worker, or start the Python maintainer.
Compaction
compact folds the WAL tail into a new segment and publishes it with one conditional manifest update. When there are more than max_segments (default 8) live segments, or one is at max_segment_bytes (default 128 MiB), it merges instead, into size-capped segments, streaming the inputs.
A segment’s bytes depend only on its WAL range, so a rerun after a crash writes the same object, and racing compactors cannot publish different answers. Compaction never deletes anything, and a query returns the same documents before, during and after it.
# By hand. Writes land in a log; compact folds it into a segment.
load("a", 5_000)
print(ns.compact()) let options = CompactionOptions::default();
let outcome = ns.compact(&options).await?;
assert!(matches!(outcome, Compaction::Published { folded_through: 1, .. }));
assert_eq!(ns.query(&[1.0], 1).await?[0].id, "a");
// Nothing new has been written, so there is nothing to fold.
assert_eq!(ns.compact(&options).await?, Compaction::Nothing);Garbage collection
gc deletes segments that a merge or a later fold replaced, once they and their replacement are at least min_age old (default 24 hours), so readers still holding an old manifest have time to finish. With fence_wal it also replaces folded WAL objects with 8-byte fences, which keep put-if-absent working at their keys. WAL objects are never deleted. dry_run reports what it would do.
# A merge leaves the old segments behind; gc deletes them once they are
# min_age old. Zero here only because nothing else reads this namespace.
load("b", 5_000)
print(ns.compact(max_segments=1))
print(ns.gc(min_age=timedelta(0))) // The default keeps anything younger than a day.
assert_eq!(ns.gc(&GcOptions::default()).await?.segments_deleted, 0);
let now = GcOptions { min_age: Duration::ZERO, ..GcOptions::default() };
assert_eq!(ns.gc(&now).await?.segments_deleted, 1);Which call should be scheduled?
maintain is the one call to schedule: it compacts when the tail is long enough (64 objects, 64 MiB, or an optional age) or there are too many segments, then runs gc.
# maintain() is the one call to schedule: it compacts when a trigger is
# crossed, then collects.
load("c", 100)
print(ns.maintain(tail_objects=1)) // Fold once the tail has two objects.
let options = MaintenanceOptions {
compact_when: CompactTrigger { tail_objects: 2, ..CompactTrigger::default() },
..MaintenanceOptions::default()
};
ns.upsert(vec![doc("a")]).await?;
assert_eq!(ns.maintain(&options).await?.compaction, None);
ns.upsert(vec![doc("b")]).await?;
let report = ns.maintain(&options).await?;
assert_eq!(report.tail_objects, 2);Background maintainer
In Python, start_maintenance runs maintain and index on a loop. Errors are logged to the itzma.maintenance logger and kept in m.last_error; the loop keeps going.
m = ns.start_maintenance(timedelta(seconds=60)) # maintain + index every minute
...
m.stop() # or ns.close()The returned Maintainer also tells you how the loop is doing (running, runs, errors, last_report); Results and reports lists its members.
A file:// store has no conditional update, so compaction and indexing fail there with StoreError; use S3, GCS, Azure or memory:// for those.
Recovery
Namespace::recover (Rust only) writes a new manifest for a namespace that lost its own, from schema.json, the segment names and the WAL. It refuses if a manifest exists, and stops with Unrecoverable and the keys to restore if a range has no readable copy. It does not restore indexes; run index and index_sparse again.
Format levels
The manifest records the newest WAL and segment formats the namespace may hold. A build that reads less refuses the namespace before touching it. upgrade_wal_format(level) and upgrade_segment_format(level) raise a level, one way. They’re Rust only; the Python package has no upgrade call. New namespaces start at WAL level 5 and segment level 2; upgrade_segment_format(3) turns on the typed attribute columns of segment format 3.
