use std::cell::Cell;
use relate_core::Result;
use relate_ir::PathPattern;
use super::super::pattern_policies::{MatchTail, Matched};
use super::super::pushdown::{MatchCap, Residual};
use super::super::rider::{PathProbe, Rider};
use super::{complete_state, GpmCtx};
pub(super) enum GpmEmit<'a, 'data> {
Cap {
path: &'a PathPattern,
cap: &'a mut MatchCap<'data>,
tail: MatchTail<'a>,
out: &'a mut Matched,
},
Probe {
probe: &'a PathProbe<'a>,
residual: &'a Residual<'a>,
},
}
pub(super) fn deliver(
gctx: &GpmCtx<'_>,
emit: &mut Option<GpmEmit<'_, '_>>,
state: super::GpmState,
out: &mut Vec<super::GpmState>,
) -> Result<bool> {
if let Some(emit) = emit.as_mut() {
emit.offer(gctx, state)?;
Ok(gctx.halted())
} else {
out.push(state);
Ok(false)
}
}
pub(super) fn deliver_all(
gctx: &GpmCtx<'_>,
emit: &mut Option<GpmEmit<'_, '_>>,
states: Vec<super::GpmState>,
) -> Result<Vec<super::GpmState>> {
let mut out = Vec::new();
for state in states {
if gctx.halted() {
break;
}
if deliver(gctx, emit, state, &mut out)? {
break;
}
}
Ok(out)
}
impl<'a, 'data> GpmEmit<'a, 'data> {
pub(super) fn offer(&mut self, gctx: &GpmCtx<'_>, mut state: super::GpmState) -> Result<()> {
let GpmCtx {
graph,
params,
cypher_version,
..
} = *gctx;
let path = match self {
GpmEmit::Cap { path, .. } => *path,
GpmEmit::Probe { probe, .. } => probe.pattern,
};
if !complete_state(gctx, path, &mut state)? {
return Ok(());
}
match self {
GpmEmit::Cap { cap, tail, out, .. } => {
let Some(row) = tail.finish_row(graph, state.row, Some(&mut **cap))? else {
return Ok(());
};
tail.store_row(row, || state.used, out);
if cap.full() {
gctx.halt.set(true);
}
}
GpmEmit::Probe { probe, residual } => {
probe.accept_row(
graph,
state.row,
state.used,
residual,
params,
cypher_version,
)?;
}
}
Ok(())
}
}
pub(super) fn mount_rider<'a, 'g, 'cap>(
rider: Option<&'a mut Rider<'g, 'cap>>,
path: &'a PathPattern,
where_clause: &'a Residual<'a>,
cap_halt: &'a Cell<bool>,
tail: MatchTail<'a>,
emitted: &'a mut Matched,
) -> (Option<GpmEmit<'a, 'g>>, &'a Cell<bool>)
where
'cap: 'a,
'g: 'a,
{
let Some(rider) = rider else {
return (None, cap_halt);
};
if path.selector.is_some() {
return (None, cap_halt);
}
match rider {
Rider::Cap(cap) => (
Some(GpmEmit::Cap {
path,
cap,
tail,
out: emitted,
}),
cap_halt,
),
Rider::Probe(probe) => (
Some(GpmEmit::Probe {
probe,
residual: where_clause,
}),
probe.halt,
),
}
}