use std::collections::BTreeMap;
use relate_core::Value;
use relate_ir::Direction;
use crate::exec::test_support::{build, exec_autocommit, try_exec_cypher_params, Builder};
use crate::graph::Graph;
use crate::shortest::test_support::counting_expands;
use super::anchor::{take_anchor_arms, AnchorArm};
pub(super) use super::guard::InnerPlanKind;
use super::{take_qpp_plans, take_route_runs, with_forced_gpm};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct Counts {
pub expands: u64,
pub route_runs: u64,
pub arms: Vec<AnchorArm>,
pub qpp_plans: Vec<InnerPlanKind>,
}
pub(super) fn counting<T>(f: impl FnOnce() -> T) -> (T, Counts) {
let _ = take_route_runs();
let _ = take_anchor_arms();
let _ = take_qpp_plans();
let (out, expands) = counting_expands(f);
let counts = Counts {
expands,
route_runs: take_route_runs(),
arms: take_anchor_arms(),
qpp_plans: take_qpp_plans(),
};
(out, counts)
}
pub(super) fn plain(n: usize, rels: &[(usize, usize)]) -> Graph {
let rels: Vec<_> = rels.iter().map(|&(a, b)| (a, b, "R")).collect();
build(&vec![&[][..]; n], &rels).graph
}
pub(super) fn labelled(x: usize, n: usize, rels: &[(usize, usize)]) -> Graph {
let labels: Vec<&[&str]> = (0..n)
.map(|i| if i < x { &["X"][..] } else { &["Y"][..] })
.collect();
let rels: Vec<_> = rels.iter().map(|&(a, b)| (a, b, "R")).collect();
build(&labels, &rels).graph
}
pub(super) fn shortcut() -> Graph {
plain(5, &[(0, 1), (1, 2), (2, 3), (0, 4), (4, 3), (3, 1)])
}
pub(super) fn self_loops(loops: usize) -> Graph {
let b = Builder::new();
let m = b.node("m", &["M"]);
for i in 1..=loops {
b.rel(m, m, "B", &[("i", Value::Int(i as i64))]);
}
b.done()
}
fn ring_graph(indexed: bool) -> Graph {
let g = Graph::default();
if indexed {
exec_autocommit(&g, "CREATE INDEX FOR (n:N) ON (n.id)").unwrap();
}
exec_autocommit(
&g,
"UNWIND range(0, 199) AS i CREATE (:N {id: toString(i), g: i % 2})",
)
.unwrap();
exec_autocommit(
&g,
"MATCH (a:N), (b:N) WHERE (toInteger(a.id) + 1) % 200 = toInteger(b.id) \
OR (toInteger(a.id) * 3) % 200 = toInteger(b.id) CREATE (a)-[:R]->(b)",
)
.unwrap();
g
}
pub(super) fn ring() -> Graph {
ring_graph(true)
}
pub(super) fn ring_unindexed() -> Graph {
ring_graph(false)
}
pub(super) const BODY: &str = "-[:R*..6]->";
pub(super) fn run(g: &Graph, q: &str) -> (Vec<Vec<Value>>, Counts) {
run_params(g, q, &BTreeMap::new())
}
pub(super) fn run_params(
g: &Graph,
q: &str,
params: &BTreeMap<String, Value>,
) -> (Vec<Vec<Value>>, Counts) {
counting(|| try_exec_cypher_params(g, q, params).unwrap().rows)
}
pub(super) fn on_route(g: &Graph, q: &str) -> Vec<Vec<Value>> {
let (rows, counts) = run(g, q);
assert!(counts.route_runs > 0, "expected the BFS route for {q}");
assert!(counts.expands > 0, "the route expanded nothing for {q}");
sorted(rows)
}
pub(super) fn gpm(g: &Graph, q: &str) -> Vec<Vec<Value>> {
gpm_params(g, q, &BTreeMap::new())
}
pub(super) fn gpm_params(g: &Graph, q: &str, params: &BTreeMap<String, Value>) -> Vec<Vec<Value>> {
sorted(with_forced_gpm(|| {
try_exec_cypher_params(g, q, params).unwrap().rows
}))
}
pub(super) fn parity(g: &Graph, q: &str) -> Counts {
parity_params(g, q, &BTreeMap::new())
}
pub(super) fn parity_params(g: &Graph, q: &str, params: &BTreeMap<String, Value>) -> Counts {
let (routed, counts) = counting(|| try_exec_cypher_params(g, q, params));
let (oracle, oracle_counts) =
counting(|| with_forced_gpm(|| try_exec_cypher_params(g, q, params)));
assert_eq!(
oracle_counts.route_runs, 0,
"the oracle must bypass the route for {q}"
);
assert_eq!(
routed.map(|r| sorted(r.rows)),
oracle.map(|o| sorted(o.rows)),
"route and gpm disagree for {q}"
);
assert!(counts.route_runs > 0, "the route must take {q}");
counts
}
pub(super) fn sorted(mut rows: Vec<Vec<Value>>) -> Vec<Vec<Value>> {
rows.sort_by(|a, b| {
let mut cells = a.iter().zip(b).map(|(x, y)| x.order_by_cmp(y));
cells.find(|o| o.is_ne()).unwrap_or(a.len().cmp(&b.len()))
});
rows
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(super) struct Selector {
pub(super) open: &'static str,
pub(super) close: &'static str,
pub(super) all: bool,
}
#[rustfmt::skip]
pub(super) const SELECTORS: [Selector; 4] = [
Selector { open: "ANY SHORTEST ", close: "", all: false },
Selector { open: "SHORTEST 1 ", close: "", all: false },
Selector { open: "SHORTEST 1 GROUP ", close: "", all: true },
Selector { open: "ALL SHORTEST ", close: "", all: true },
];
pub(super) const DIRS: [Direction; 3] = [Direction::Outgoing, Direction::Incoming, Direction::Both];
pub(super) trait DirectionExt {
fn arrows(self) -> (&'static str, &'static str);
fn step(self, at: usize, rel: (usize, usize)) -> Option<usize>;
}
impl DirectionExt for Direction {
fn arrows(self) -> (&'static str, &'static str) {
match self {
Direction::Outgoing => ("-", "->"),
Direction::Incoming => ("<-", "-"),
Direction::Both => ("-", "-"),
}
}
fn step(self, at: usize, rel: (usize, usize)) -> Option<usize> {
let (from, to) = rel;
match self {
Direction::Outgoing => (from == at).then_some(to),
Direction::Incoming => (to == at).then_some(from),
Direction::Both if from == at => Some(to),
Direction::Both => (to == at).then_some(from),
}
}
}