8000 Clarify checkpoint and prepared query guarantees (main) by adityamukho · Pull Request #982 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ db.execute("(rule [(reachable ?a ?b) [?a :friend ?b]])")?;
db.execute("(rule [(reachable ?a ?b) [?a :friend ?m] (reachable ?m ?b)])")?;
db.execute("(query [:find ?friend :where (reachable :alice ?friend)])")?;

// Prepared statement — parse + plan once, execute many times
// Parse once, then execute with different bind values
use minigraf::{BindValue, EntityId};
// Keywords map to UUIDv5 IDs in the OID namespace, including the leading colon.
let alice_id = EntityId::new_v5(&EntityId::NAMESPACE_OID, b":alice");
Expand Down
4 changes: 2 additions & 2 deletions docs/testing/924-incoming-refs.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Scope: deliverable 1 of the [approved spec](../superpowers/specs/2026-11-28-924-

- Pending: `RefPositions`, a standard randomly seeded `HashMap<EntityId, Vec<usize>>`, records scalar-ref positions during `PendingStore::push` and `extend`. WAL replay uses the same insertion path. Query-only stores built by `Snapshot::with_staged` omit this unused index. A checkpoint publishes a new empty pending store together with its committed reader.
- Small staged transactions: the same position structure is maintained when an accepted statement is staged. Lookup borrows the facts directly, including in-memory transactions of unbounded size. It never calls `Staged::read_view`. Rejected statements add nothing. Successful promotion releases the staged facts and index. Promotion filters the staged vector in place with the existing last-statement mask before the writer stamps one transaction counter. It reuses the vector and retained facts without cloning. A failed staged-chunk write retains those facts and rebuilds their target positions, accounting for filtering or writer reordering; lookup uses committed/pending storage plus the completed staged statements. Only the error path rebuilds the index. Rollback/drop discard the transaction.
- Durable and large working trees: a dedicated raw VAET walk seeks the target prefix, checks only that prefix before charging, then decodes one version at a time. It uses the selected reader's dictionary, including new working-tree entities and attributes. Neither `live_keys` nor a buffered version-group reader is used. Tree-page decoding is bounded navigation; a single nonmatching successor probe is counted separately. A failed large-statement chunk restores its statement base. If finalization fails while reading the claims tree, lookup retains the completed statements’ roots and dictionary; their held pages remain allocated. Existing query readers still select the working roots. Commit remains refused with STG-046.
- Visibility: a per-lookup map retains the newest `(layer, tx_count, asserted)` per `(source, attribute)`, with tied retractions winning. Staged beats pending, which beats committed. It examines all target history rather than seeking past old versions. Only after all participating ranges finish are live links established and the links cap checked. Consequently, records exhaustion can precede links exhaustion even when some inspected assertions might eventually survive. No partial list escapes. Temporary state follows inspected distinct pairs, never a requested cap.
- Durable and large working trees: the lookup seeks the target in VAET, counts only matching records against the record budget and decodes one version at a time. It uses the selected reader's dictionary, including new working-tree entities and attributes. Neither `live_keys` nor a buffered version-group reader is used. Tree-page decoding is bounded navigation; a single nonmatching successor probe is counted separately. A failed large-statement chunk restores its statement base. If finalization fails while reading the claims tree, lookup retains the completed statements’ roots and dictionary; their held pages remain allocated. Existing query readers still select the working roots. Commit remains refused with STG-046.
- Visibility: a per-lookup map retains the newest `(layer, tx_count, asserted)` per `(source, attribute)`, with tied retractions winning. Staged beats pending, which beats committed. It examines all target history rather than seeking past old versions. If reading another matching record would exceed the record limit, the lookup returns record exhaustion. After scanning all participating ranges it checks the final count of live links and returns link exhaustion only if that count exceeds the link limit. Either limit can be met exactly without exhausting the budget. An exhausted lookup returns no links. Temporary memory grows with the number of distinct (source, attribute) pairs inspected, not the requested limit.

The test-only enumeration counter counts actual lookup advances as well as general staged copying, pending-store construction/extension, range copies and pending scans. These sites compile only for tests or `test-hooks`, independently of public `records_examined`.

Expand Down
24 changes: 15 additions & 9 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,9 +1372,12 @@ impl Minigraf {
/// Manually trigger a checkpoint: flush all in-memory facts to the main file
/// and delete the WAL sidecar.
///
/// A checkpoint is **not** a durability boundary: committed writes are already
/// crash-durable in the `<db>.wal` sidecar and are replayed on the next open.
/// Checkpointing less often only makes that replay (reopen) slower.
/// For an ordinary file-backed transaction in [`SyncMode::Full`], the
/// write syncs its WAL entry before applying the facts in memory. In
/// [`SyncMode::Normal`], the entry is not synced by that write. A later
/// `Full` write can sync it, or a successful checkpoint can make those
/// facts durable by syncing the main file. Reopen replays WAL entries
/// not yet checkpointed.
///
/// Cost: a checkpoint is copy-on-write. It writes only the index leaves that
/// receive new facts, their paths to the root, new long-string pages, a few
Expand All @@ -1392,11 +1395,13 @@ impl Minigraf {
/// `fsync` the operating system may have dropped the written pages, and a
/// retry could report success over them. Every later write, checkpoint
/// and index rebuild through the handle fails with STG-046; queries keep
/// working. Reopen the database: every committed transaction is still in
/// the WAL, and the file holds the previous checkpoint or the failed one
/// complete. The same applies to an automatic checkpoint, which fails the
/// `transact` or `commit` that triggered it after that transaction is
/// already durable in the WAL.
/// working. After reopening, recovery uses the WAL and whichever complete
/// checkpoint is on disk. A failed automatic checkpoint can return an
/// error from `transact` or `commit` after its facts were applied in
/// memory. An unsynced `Normal` entry is not guaranteed to survive power
/// loss. A large transaction can also report an error deleting the WAL
/// after it has committed to the main file. After reopening, check
/// whether the write took effect before retrying it.
pub fn checkpoint(&self) -> Result<(), MinigrafError> {
self.checkpoint_inner().map_err(MinigrafError::from)
}
Expand Down Expand Up @@ -1627,7 +1632,8 @@ impl Minigraf {
Ok(())
}

/// Parse and plan a query once; bind slots (`$name`) are left unresolved.
/// Parse and validate a query once. Bind slots (`$name`) are filled on
/// each execution, before the query is planned.
///
/// Returns a [`crate::query::datalog::prepared::PreparedQuery`] that can be executed
/// many times with different bind values via
Expand Down
Loading
0