use rocksgraph::{
schema::{DataType, EdgeMode, GraphOptions, SchemaMode},
Graph, StoreError, TraversalBuilder,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = tempfile::tempdir()?;
let db_path = temp_dir.path();
println!("Initializing Graph Database at: {:?}", db_path);
println!("\n=== Part 1: Strict Schema Mode ===");
let options = GraphOptions { mode: SchemaMode::Strict, edge_mode: EdgeMode::Single };
let graph = Graph::open_with_options(db_path, options)?;
let mut mgmt = graph.open_management();
mgmt.add_vertex_label("person")
.add_edge_label("knows")
.add_property_key("name", DataType::String)
.add_property_key("age", DataType::Int32)
.add_property_key("weight", DataType::Float64);
println!("Visualizing declared schema:\n{}", mgmt);
mgmt.commit()?;
println!("Schema successfully declared and committed in Strict Mode.");
let mut tx = graph.begin();
tx.g().addV("person").property("id", 1i64).property("name", "Alice").property("age", 30i32).next()?;
tx.g().addV("person").property("id", 2i64).property("name", "Bob").property("age", 25i32).next()?;
tx.commit()?;
println!("Valid data committed successfully.");
let mut tx = graph.begin();
let res = tx
.g()
.addV("ghost") .property("id", 3i64)
.next();
match res {
Err(StoreError::SchemaViolation(msg)) => {
println!("Caught expected schema violation: {}", msg);
}
_ => panic!("Expected a SchemaViolation error for undeclared vertex label 'ghost'!"),
}
tx.rollback();
println!("\n=== Part 2: Upgrading to Multi-Edge Mode ===");
let mut mgmt = graph.open_management();
mgmt.set_edge_mode(EdgeMode::Multi);
mgmt.commit()?;
println!("Visualizing updated schema:\n{}", graph.open_management());
println!("EdgeMode upgraded to Multi successfully.");
let mut tx = graph.begin();
tx.g().addE("knows").from(1).to(2).property("weight", 0.8f64).next()?;
tx.commit()?;
println!("First 'knows' edge (rank 0, default) added successfully.");
let mut tx = graph.begin();
let res = tx.g().addE("knows").from(1).to(2).property("weight", 0.9f64).next();
match res {
Err(StoreError::DuplicateEdge(key)) => {
println!("Caught expected duplicate edge error at rank 0: {}", key);
}
_ => panic!("Expected a DuplicateEdge error since rank defaults to 0!"),
}
tx.rollback();
let mut tx = graph.begin();
tx.g()
.addE("knows")
.from(1)
.to(2)
.property("rank", 1u16) .property("weight", 0.9f64)
.next()?;
tx.commit()?;
println!("Second 'knows' edge with explicit rank 1 added successfully.");
let mut snap = graph.read();
let edge_count = snap.g().V([1]).outE(["knows"]).count().next()?.unwrap();
println!("Total incident edges from Alice to Bob: {:?}", edge_count);
Ok(())
}