use relate_core::{Result, Value};
use relate_ir::{Expr, MatchMode, PathPattern, SearchClause};
use relate_model::EntityKind;
use super::growth::{top_k_complete, widen_extra};
use super::{
attach_score, check_binding_kind, search_spec_candidates, spec_window, Growth, SearchEnv,
};
use crate::exec::match_clause::match_clause;
use crate::row::Row;
fn match_candidate(
env: &SearchEnv<'_>,
input: &Row,
search: &SearchClause,
(candidate, score): (Value, f64),
match_mode: MatchMode,
patterns: &[PathPattern],
where_clause: Option<&Expr>,
) -> Result<Vec<Row>> {
let mut seeded = input.clone();
seeded.insert(search.binding_var.to_string(), candidate);
attach_score(&mut seeded, search.score_var.as_ref(), Value::Float(score));
match_clause(
env.ctx,
vec![seeded],
false,
match_mode,
patterns,
where_clause,
None,
None,
None,
)
}
pub(super) fn direct_search_rows(
env: &SearchEnv<'_>,
input: &Row,
binding_kind: Option<EntityKind>,
match_mode: MatchMode,
patterns: &[PathPattern],
where_clause: Option<&Expr>,
search: &SearchClause,
) -> Result<Vec<Row>> {
let window = spec_window(env, input, &search.spec)?;
let mut out = Vec::new();
let mut extra = 0usize;
let mut processed = 0usize;
let mut skipped = 0usize;
let mut survivors = 0usize;
while let Some(fetched) =
search_spec_candidates(env, input, &search.spec, window, Growth::Widen(extra))?
{
check_binding_kind(binding_kind, fetched.kind)?;
if survivors >= window.limit {
break;
}
for candidate in fetched.candidates.into_iter().skip(processed) {
processed += 1;
let matched = match_candidate(
env,
input,
search,
candidate,
match_mode,
patterns,
where_clause,
)?;
if matched.is_empty() {
continue;
}
if skipped < window.skip {
skipped += 1;
continue;
}
out.extend(matched);
survivors += 1;
if survivors >= window.limit {
break;
}
}
if top_k_complete(fetched.may_widen, survivors, window.limit) {
break;
}
extra = widen_extra(extra, window.limit);
}
Ok(out)
}
pub(super) fn pre_bound_search_rows(
env: &SearchEnv<'_>,
input: &Row,
binding_kind: Option<EntityKind>,
match_mode: MatchMode,
patterns: &[PathPattern],
where_clause: Option<&Expr>,
search: &SearchClause,
) -> Result<Vec<Row>> {
let bound = input
.get_name(&search.binding_var)
.expect("the pre-bound source is picked only for a row that binds the SEARCH variable");
if bound.is_null() {
return Ok(Vec::new());
}
let window = spec_window(env, input, &search.spec)?;
if window.skip > 0 || window.limit == 0 {
return Ok(Vec::new());
}
let mut extra = 0usize;
let mut processed = 0usize;
while let Some(fetched) =
search_spec_candidates(env, input, &search.spec, window, Growth::Widen(extra))?
{
check_binding_kind(binding_kind, fetched.kind)?;
let found = fetched
.candidates
.into_iter()
.skip(processed)
.find(|(candidate, _)| {
processed += 1;
bound.eq_non_null(candidate)
});
if let Some(candidate) = found {
return match_candidate(
env,
input,
search,
candidate,
match_mode,
patterns,
where_clause,
);
}
if !fetched.may_widen {
break;
}
extra = widen_extra(extra, window.limit);
}
Ok(Vec::new())
}