use kglite::api::session::{CommitOutcome, ExecuteOptions, Session};
use kglite::api::DirGraph;
use std::collections::HashMap;
use std::sync::Arc;
fn opts<'a>(params: &'a HashMap<String, kglite::api::Value>) -> ExecuteOptions<'a> {
ExecuteOptions {
params,
deadline: None,
max_rows: None,
lazy_eligible: false,
disabled_passes: None,
embedder: None,
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let session = Arc::new(Session::new(DirGraph::new()));
let params: HashMap<String, kglite::api::Value> = HashMap::new();
let mut tx_a = session.begin();
let working_a = tx_a.working_mut()?;
kglite::api::session::execute_mut(
working_a,
"CREATE (:Person {id: 1, name: 'Alice'})",
&opts(¶ms),
)?;
println!("Tx A: created Person(id=1, name='Alice') in working copy");
let mut tx_b = session.begin();
let working_b = tx_b.working_mut()?;
kglite::api::session::execute_mut(
working_b,
"CREATE (:Person {id: 2, name: 'Bob'})",
&opts(¶ms),
)?;
println!("Tx B: created Person(id=2, name='Bob') in working copy");
let outcome_a = session.commit(tx_a, true);
match outcome_a {
CommitOutcome::Committed { new_version } => {
println!("\n✓ Tx A committed → version {}", new_version);
}
other => panic!("expected Committed, got {:?}", other),
}
let outcome_b = session.commit(tx_b, true);
match outcome_b {
CommitOutcome::ConflictDetected {
current_version,
base_version,
} => {
println!(
"✗ Tx B rejected (OCC): base_version={} but current_version={}",
base_version, current_version
);
println!(" Client retry pattern: re-run the transaction against a fresh snapshot.");
}
other => panic!("expected ConflictDetected, got {:?}", other),
}
let snap = session.snapshot();
let outcome = kglite::api::session::execute_read(
&snap,
"MATCH (p:Person) RETURN p.name AS name ORDER BY p.id",
&opts(¶ms),
)?;
println!("\nFinal graph:");
for row in &outcome.result.rows {
if let Some(kglite::api::Value::String(s)) = row.first() {
println!(" - {}", s);
}
}
Ok(())
}