use crate::UsedSet;
use std::cell::Cell;
use std::collections::HashSet;
use relate_core::Result;
use relate_ir::{Expr, MatchMode, PathPattern};
use crate::ctx::ExecCtx;
use crate::eval::expr_vars;
use crate::exec::pushdown::Residual;
use crate::row::Row;
mod guard;
mod route;
use super::sink::MatchedSink;
use super::GpmFilters;
use guard::route_shape;
use route::{PathFilter, Route};
fn path_filter<'p>(path: &'p PathPattern, residual: &'p Residual<'_>) -> Option<PathFilter<'p>> {
let mut carriers: HashSet<String> = path
.rels
.iter()
.filter_map(|rel| rel.variable.as_ref().map(|v| v.to_string()))
.collect();
for name in path.path_names() {
carriers.insert(name.to_string());
}
let conjuncts: Vec<&Expr> = residual
.conjuncts
.iter()
.filter(|conjunct| {
let mut vars = HashSet::new();
expr_vars(conjunct, &mut vars);
vars.iter().any(|name| carriers.contains(name.as_str()))
})
.collect();
(path.where_clause.is_some() || !conjuncts.is_empty()).then_some(PathFilter {
conjuncts,
inline: path.where_clause.as_deref(),
scope: residual.scope,
})
}
#[cfg(test)]
mod anchor_tests;
#[cfg(test)]
mod bounded_tests;
#[cfg(test)]
mod expr_parity_tests;
#[cfg(test)]
mod fixtures;
#[cfg(test)]
mod parity_tests;
#[cfg(test)]
mod reject_tests;
#[cfg(test)]
mod route_tests;
#[cfg(test)]
thread_local! {
static FORCE_GPM: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
static ROUTE_RUNS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
#[inline(always)]
fn note_route_run() {
#[cfg(test)]
ROUTE_RUNS.with(|c| c.set(c.get() + 1));
}
#[cfg(test)]
pub(crate) fn take_route_runs() -> u64 {
ROUTE_RUNS.with(|c| c.replace(0))
}
#[cfg(test)]
fn with_forced_gpm<T>(f: impl FnOnce() -> T) -> T {
struct Restore(bool);
impl Drop for Restore {
fn drop(&mut self) {
FORCE_GPM.with(|c| c.set(self.0));
}
}
let _restore = Restore(FORCE_GPM.with(|c| c.replace(true)));
f()
}
pub(super) fn match_shortest_route(
mut sink: MatchedSink<'_, '_>,
ctx: ExecCtx<'_>,
path: &PathPattern,
filters: &GpmFilters,
row: &Row,
used: &UsedSet,
match_mode: MatchMode,
) -> Result<bool> {
#[cfg(test)]
if FORCE_GPM.with(|c| c.get()) {
return Ok(false);
}
let Some(shape) = route_shape(path, filters, match_mode, used, row, ctx.params) else {
return Ok(false);
};
let residual = sink.residual();
let halt = sink.halt_cell();
if !Route::new(ctx, path, row, shape, path_filter(path, residual), halt).run(&mut sink)? {
return Ok(false);
}
note_route_run();
Ok(true)
}
pub(crate) fn fold_shortest_route(
ctx: ExecCtx<'_>,
path: &PathPattern,
row: &Row,
fold: &mut dyn FnMut(Row) -> Result<()>,
) -> Result<bool> {
#[cfg(test)]
if FORCE_GPM.with(|c| c.get()) {
return Ok(false);
}
let filters = GpmFilters::resolve(ctx.graph, path);
let used = UsedSet::default();
let Some(mut shape) = route_shape(
path,
&filters,
MatchMode::DifferentRelationships,
&used,
row,
ctx.params,
) else {
return Ok(false);
};
shape.max = shape.max.or(Some(crate::pattern_walk::DEFAULT_MAX_HOPS));
let residual = Residual::empty();
let halt = Cell::new(false);
let mut sink = MatchedSink::fold(&residual, &halt, fold);
let filter = path_filter(path, &residual);
if !Route::new(ctx, path, row, shape, filter, &halt)
.first_layer_only()
.run(&mut sink)?
{
return Ok(false);
}
note_route_run();
Ok(true)
}