use std::cell::RefCell;
use std::collections::HashMap;
use relate_model::CypherValue;
thread_local! {
static VIRTUAL_NODES: RefCell<HashMap<i64, CypherValue>> = RefCell::new(HashMap::new());
}
pub(crate) fn register_virtual_node(id: i64, value: CypherValue) {
VIRTUAL_NODES.with(|cell| {
cell.borrow_mut().insert(id, value);
});
}
pub(crate) fn lookup_virtual_node(id: i64) -> Option<CypherValue> {
VIRTUAL_NODES.with(|cell| cell.borrow().get(&id).cloned())
}
#[cfg(test)]
mod tests {
use super::register_virtual_node;
use crate::graph::Graph;
use crate::overlay::ExecGraph;
use indexmap::IndexMap;
use relate_core::{NodeId, Value};
use relate_model::CypherValue;
#[test]
fn virtual_node_temporal_and_map_list_properties_survive_conversion() {
let date = chrono::NaiveDate::from_ymd_opt(2024, 2, 29).unwrap();
let mut attrs = IndexMap::new();
attrs.insert("nested".to_string(), CypherValue::Int(7));
let mut properties = IndexMap::new();
properties.insert("created".to_string(), CypherValue::Date(date));
properties.insert("attrs".to_string(), CypherValue::Map(attrs));
properties.insert(
"tags".to_string(),
CypherValue::List(vec![CypherValue::Str("a".into()), CypherValue::Int(2)]),
);
let id: NodeId = -1;
register_virtual_node(
id,
CypherValue::Node {
id,
labels: vec!["V".to_string()],
properties,
},
);
let g = Graph::default();
let graph = ExecGraph::read(&g);
let Value::Node(node) = graph.node_value(id) else {
panic!("expected virtual node, got non-node")
};
assert!(matches!(
node.properties.get("created"),
Some(Value::Temporal(relate_core::temporal::Temporal::Date(d))) if *d == date
));
assert!(
matches!(node.properties.get("attrs"), Some(Value::Map(m)) if m.get("nested") == Some(&Value::Int(7)))
);
match node.properties.get("tags") {
Some(Value::List(items)) => {
assert_eq!(items[0], Value::String("a".into()));
assert_eq!(items[1], Value::Int(2));
}
other => panic!("expected list, got {other:?}"),
}
}
}