use std::collections::BTreeMap;
use relate_core::{NodeId, RelId, Value};
use super::adjacency::AdjSide;
use super::Graph;
use crate::adjacency::COMPACTION_DELTA_FLOOR;
pub(crate) fn labelled_node(g: &Graph, labels: &[&str]) -> NodeId {
g.create_node(
labels.iter().map(|l| l.to_string()).collect(),
BTreeMap::new(),
)
.unwrap()
}
pub(crate) fn endpoints(g: &Graph) -> (NodeId, NodeId) {
g.begin_write().unwrap();
let a = g.create_node(vec!["N".into()], BTreeMap::new()).unwrap();
let b = g.create_node(vec!["N".into()], BTreeMap::new()).unwrap();
g.commit_write();
(a, b)
}
pub(crate) fn folded_floor_chunk(g: &Graph) -> (NodeId, NodeId, Vec<RelId>, u32) {
let (a, b) = endpoints(g);
let mut ids = Vec::new();
g.begin_write().unwrap();
for i in 0..COMPACTION_DELTA_FLOOR as i64 {
ids.push(
g.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(i))]),
)
.unwrap(),
);
}
g.commit_write();
let type_id = g.interner.get_id("T").unwrap();
g.gc_versions();
(a, b, ids, type_id)
}
const ADVERSARIAL_NODES: &[(&str, &[&str])] = &[
("a", &["U"]),
("n0", &["U"]),
("n1", &["U"]),
("n2", &["U"]),
("n3", &["U"]),
("n4", &["U"]),
("n5", &["U"]),
("n6", &["U"]),
("bare", &[]),
("hub", &["U", "Hub"]),
];
const ADVERSARIAL_EDGES: &[(&str, &str, &str)] = &[
("a", "a", "F"),
("n1", "n1", "F"),
("n3", "n3", "F"),
("a", "n0", "F"),
("a", "n0", "F"),
("a", "n0", "G"),
("n0", "n1", "F"),
("n1", "n0", "F"),
("n1", "n2", "F"),
("n2", "n3", "G"),
("n3", "n4", "F"),
("n4", "a", "F"),
("n2", "bare", "F"),
("bare", "n3", "F"),
("n0", "hub", "F"),
("hub", "n0", "F"),
("n1", "hub", "F"),
("hub", "n1", "F"),
("n2", "hub", "F"),
("hub", "n2", "F"),
("n3", "hub", "F"),
("hub", "n3", "F"),
("n4", "hub", "F"),
("hub", "n4", "F"),
("n5", "hub", "F"),
("hub", "n5", "F"),
("n6", "hub", "F"),
("hub", "n6", "F"),
("a", "hub", "F"),
];
pub(crate) type AdversarialMultigraph = (
Graph,
Vec<(&'static str, NodeId)>,
Vec<(NodeId, NodeId, &'static str, RelId)>,
);
pub(crate) fn adversarial_multigraph(each: usize) -> AdversarialMultigraph {
let g = Graph::default();
g.begin_write().unwrap();
let mut ids = BTreeMap::new();
let nodes = ADVERSARIAL_NODES
.iter()
.map(|(name, labels)| {
let id = g
.create_node(
labels.iter().map(|l| l.to_string()).collect(),
BTreeMap::from([("id".into(), Value::String((*name).into()))]),
)
.unwrap();
ids.insert(*name, id);
(*name, id)
})
.collect();
let mut rels = Vec::new();
for (from, to, t) in ADVERSARIAL_EDGES {
for _ in 0..each {
let rid = g
.create_rel(ids[from], ids[to], (*t).into(), BTreeMap::new())
.unwrap();
rels.push((ids[from], ids[to], *t, rid));
}
}
g.commit_write();
(g, nodes, rels)
}
pub(crate) fn first_side_map_id(slot: u64) -> i64 {
let mut id = 1i64;
while crate::memory::SpanPlacement::classify(0, usize::try_from(id).unwrap(), slot)
!= crate::memory::SpanPlacement::SideEntry
{
id = id.saturating_mul(2);
}
id
}
pub(crate) fn doomed_rel(g: &Graph, a: NodeId, b: NodeId, rel_type: &str) -> RelId {
g.begin_write().unwrap();
let doomed = g
.create_rel(a, b, rel_type.into(), BTreeMap::new())
.unwrap();
g.commit_write();
doomed
}
pub(crate) fn range_index(g: &Graph, label: &str, property: &str) {
g.materialize_range_index(label, property);
}
pub(super) fn sealed_chunk_len(side: &AdjSide<'_>, node: NodeId, type_id: u32) -> usize {
side.sealed()
.region(node)
.expect("the node has a sealed region")
.chunk_len(type_id)
}