use std::sync::OnceLock;
use parking_lot::RwLock;
use sz_orm_graph::{CypherQuery, GraphNode, GraphRelationship, GraphResult, InMemoryGraphEngine};
static GRAPH_ENGINE: OnceLock<RwLock<InMemoryGraphEngine>> = OnceLock::new();
fn engine() -> &'static RwLock<InMemoryGraphEngine> {
GRAPH_ENGINE.get_or_init(|| RwLock::new(InMemoryGraphEngine::new()))
}
pub fn graph_query(query: &CypherQuery) -> Result<Vec<GraphResult>, sz_orm_graph::GraphError> {
let engine = engine().read();
engine.execute(query)
}
pub fn graph_add_node(node: GraphNode) -> Result<(), sz_orm_graph::GraphError> {
let mut engine = engine().write();
engine.add_node(node)
}
pub fn graph_add_relationship(rel: GraphRelationship) -> Result<(), sz_orm_graph::GraphError> {
let mut engine = engine().write();
engine.add_relationship(rel)
}
pub fn graph_query_count() -> u64 {
let engine = engine().read();
engine.query_count()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_graph_add_and_query() {
let node = GraphNode {
id: "1".into(),
labels: vec!["Person".into()],
properties: serde_json::json!({"name": "Alice"}),
};
graph_add_node(node).unwrap();
let q = CypherQuery::new("MATCH (n:Person) RETURN n");
let result = graph_query(&q).unwrap();
assert!(!result.is_empty());
assert!(result[0].as_node().is_some());
}
#[test]
fn test_graph_query_count_increments() {
let before = graph_query_count();
let q = CypherQuery::new("MATCH (n:Person) RETURN n");
let _ = graph_query(&q).unwrap();
let after = graph_query_count();
assert!(after > before);
}
}