use relate_core::NodeId;
use relate_ir::PathPattern;
use super::binds::{node_bind_name, rel_bind_name, SeatPrefixes};
use crate::row::{push_bind_name, Row, RowRung};
pub(crate) enum Selection {
Pivot((usize, NodeId)),
Cheapest(usize),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum LadderArm {
Pivot(usize),
Cheapest(usize),
}
impl LadderArm {
pub(crate) fn of(selection: Option<&Selection>) -> Self {
match selection {
None | Some(Selection::Pivot((0, _))) => LadderArm::Cheapest(0),
Some(Selection::Pivot((k, _))) => LadderArm::Pivot(*k),
Some(Selection::Cheapest(k)) => LadderArm::Cheapest(*k),
}
}
}
pub(crate) struct WalkLadder {
pub(crate) node: Vec<RowRung>,
pub(crate) rel: Vec<RowRung>,
pub(crate) complete: RowRung,
}
impl WalkLadder {
pub(crate) fn new(
seats: &SeatPrefixes,
entry: &Row,
path: &PathPattern,
arm: LadderArm,
named: bool,
) -> Self {
debug_assert_eq!(
path.nodes.len(),
path.rels.len() + 1,
"the compact projections alternate node, rel, node — the shape this ladder indexes"
);
let mut names: Vec<String> = entry.keys().cloned().collect();
let mut node: Vec<RowRung> = Vec::with_capacity(path.nodes.len());
let mut rel: Vec<RowRung> = Vec::with_capacity(path.rels.len());
match arm {
LadderArm::Cheapest(k) => {
push_bind_name(
&mut names,
node_bind_name(seats, &path.nodes[k], k, named, |n| n.to_string()).as_deref(),
);
let start = RowRung::from_names(&names);
backward_prefix(seats, &mut names, &mut node, &mut rel, path, named, k);
node.push(start);
forward_suffix(seats, &mut names, &mut node, &mut rel, path, named, k);
}
LadderArm::Pivot(k) => {
debug_assert!(k > 0, "a pivot at element 0 normalizes onto Cheapest(0)");
backward_prefix(seats, &mut names, &mut node, &mut rel, path, named, k);
node.push(node[0].clone());
forward_suffix(seats, &mut names, &mut node, &mut rel, path, named, k);
}
}
for name in path.path_names() {
name.with_str(|text| push_bind_name(&mut names, Some(text)));
}
let complete = RowRung::from_names(&names);
Self {
node,
rel,
complete,
}
}
}
fn backward_prefix(
seats: &SeatPrefixes,
names: &mut Vec<String>,
node: &mut Vec<RowRung>,
rel: &mut Vec<RowRung>,
path: &PathPattern,
named: bool,
k: usize,
) {
let rel_named = path.has_path_name();
let mut back_rel = Vec::with_capacity(k);
let mut back_node = Vec::with_capacity(k);
for j in (0..k).rev() {
push_bind_name(
names,
rel_bind_name(seats, &path.rels[j], j, rel_named, |n| n.to_string()).as_deref(),
);
back_rel.push(RowRung::from_names(names));
push_bind_name(
names,
node_bind_name(seats, &path.nodes[j], j, named, |n| n.to_string()).as_deref(),
);
back_node.push(RowRung::from_names(names));
}
back_rel.reverse();
back_node.reverse();
rel.extend(back_rel);
node.extend(back_node);
}
fn forward_suffix(
seats: &SeatPrefixes,
names: &mut Vec<String>,
node: &mut Vec<RowRung>,
rel: &mut Vec<RowRung>,
path: &PathPattern,
named: bool,
k: usize,
) {
let rel_named = path.has_path_name();
for j in k..path.rels.len() {
push_bind_name(
names,
rel_bind_name(seats, &path.rels[j], j, rel_named, |n| n.to_string()).as_deref(),
);
rel.push(RowRung::from_names(names));
let np = &path.nodes[j + 1];
push_bind_name(
names,
node_bind_name(seats, np, j + 1, named, |n| n.to_string()).as_deref(),
);
node.push(RowRung::from_names(names));
}
}