8000 fix(db): treat rule registration as write to prevent race (issue #94) by adityamukho · Pull Request #102 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content
8000
Merged
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
20 changes: 16 additions & 4 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,26 +417,38 @@ impl Minigraf {

let cmd = parse_datalog_command(input).map_err(|e| anyhow::anyhow!("{}", e))?;

// Determine if this is a read-only command (query / rule registration).
// Determine if this is a read-only command (query only).
// Rule registration is treated as a write because it mutates the shared RuleRegistry.
let is_write = matches!(
cmd,
DatalogCommand::Transact(_) | DatalogCommand::Retract(_)
DatalogCommand::Transact(_) | DatalogCommand::Retract(_) | DatalogCommand::Rule(_)
);

if is_write {
let mut ctx = self.inner.write_lock.lock().map_err(|_| {
anyhow::anyhow!("write lock is poisoned; database may be in an inconsistent state")
})?;

// Handle write commands with correct WAL-first ordering:
// Handle write commands with correct WAL-first ordering for Transact/Retract:
// 1. Materialize facts (no storage mutation yet)
// 2. Allocate tx_count + tx_id and stamp facts
// 3. Write WAL entry FIRST — if this fails, FactStorage is unchanged
// 4. Apply facts to shared FactStorage
// For Rule registration: acquire lock to serialize rule changes, no WAL needed
let (stamped, is_retract) = match &cmd {
DatalogCommand::Transact(tx) => (Minigraf::materialize_transaction(tx)?, false),
DatalogCommand::Retract(tx) => (Minigraf::materialize_retraction(tx)?, true),
_ => unreachable!("is_write guarantees Transact or Retract"),
DatalogCommand::Rule(_) => {
// Rule registration: execute while holding write_lock to serialize
// rule registry changes (no WAL needed for rules)
let executor = DatalogExecutor::new_with_rules_and_functions(
self.inner.fact_storage.clone(),
self.inner.rules.clone(),
self.inner.functions.clone(),
);
return executor.execute(cmd);
}
_ => unreachable!("is_write guarantees Transact, Retract, or Rule"),
};

let tx_count = self.inner.fact_storage.allocate_tx_count();
Expand Down
Loading
0