use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use khive_runtime::{KhiveRuntime, RuntimeError};
use khive_storage::types::{SqlStatement, SqlValue};
use uuid::Uuid;
use super::super::lexical_timeout::{LexicalBound, LexicalPhase, LexicalStage, LexicalTimeout};
use super::super::matching;
use super::super::schema::Atom;
use super::super::scoring::expand_terms;
use super::super::sections::to_slug;
#[cfg(all(test, feature = "namespace-trigram-proto"))]
use super::super::util::row_str;
use super::super::util::{
atom_from_row, is_stop, row_i64, sql_err, status_sql_clause, MIN_TERM_LEN,
};
#[cfg(test)]
use super::tests;
use super::{phase_b_hydration_statement, HYDRATION_ID_CHUNK, LEXICAL_STAGE_BUDGET_MS};
#[path = "lexical_sql.rs"]
mod lexical_sql;
fn quote_fts5_phrase(raw_query: &str) -> String {
let escaped = raw_query.replace('"', "\"\"");
format!("\"{escaped}\"")
}
pub(super) fn fts5_candidate_terms(raw_query: &str) -> Vec<String> {
let mut seen = HashSet::new();
let mut terms: Vec<String> = matching::tokenize_field(raw_query)
.into_iter()
.filter(|term| term.len() >= MIN_TERM_LEN && !is_stop(term))
.filter(|term| seen.insert(term.clone()))
.collect();
if terms.is_empty() {
vec![quote_fts5_phrase(raw_query)]
} else {
let _ = expand_terms(&mut terms);
terms.iter().map(|term| quote_fts5_phrase(term)).collect()
}
}
#[cfg(all(test, feature = "namespace-trigram-proto"))]
#[derive(Clone)]
pub(super) enum NamespaceTrigramExperiment {
SlotTable { key: String },
Prefixed { key: String },
}
#[cfg(all(test, feature = "namespace-trigram-proto"))]
tokio::task_local! {
pub(super) static NAMESPACE_TRIGRAM_EXPERIMENT: NamespaceTrigramExperiment;
}
#[cfg(all(test, feature = "namespace-trigram-proto"))]
fn prototype_fts_target(term: &str) -> Option<(&'static str, String)> {
NAMESPACE_TRIGRAM_EXPERIMENT
.try_with(Clone::clone)
.ok()
.map(|experiment| match experiment {
NamespaceTrigramExperiment::SlotTable { key } => (
"fts_knowledge",
format!(
"namespace_key : {} AND {{slug name content}} : {term}",
quote_fts5_phrase(&key)
),
),
NamespaceTrigramExperiment::Prefixed { key } => {
let raw = term
.strip_prefix('"')
.and_then(|term| term.strip_suffix('"'))
.expect("candidate terms are quoted FTS5 phrases")
.replace("\"\"", "\"");
(
"fts_knowledge_namespace_proto",
khive_db::namespace_trigram_proto::scoped_match(&key, &raw)
.expect("the fixture uses a valid namespace key"),
)
}
})
}
fn type_eligibility_sql(type_filter: Option<&str>, atom_alias: &str) -> String {
match type_filter {
Some("domain") => format!(" AND khive_tag_contains({atom_alias}.tags, 'type:domain')"),
Some(filter) if !filter.is_empty() => {
format!(" AND NOT khive_tag_contains({atom_alias}.tags, 'type:domain')")
}
_ => String::new(),
}
}
pub(super) const FTS_TERM_LIMIT: usize = 500;
pub(super) const FTS_TERM_COUNT_LIMIT: usize = 32;
pub(super) struct FtsTermBudget {
remaining: AtomicUsize,
truncated: AtomicBool,
}
impl FtsTermBudget {
pub(super) fn new() -> Self {
Self {
remaining: AtomicUsize::new(FTS_TERM_COUNT_LIMIT),
truncated: AtomicBool::new(false),
}
}
pub(super) fn admit(&self, mut terms: Vec<String>) -> Vec<String> {
let remaining = self
.remaining
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |remaining| {
Some(remaining.saturating_sub(terms.len()))
})
.expect("term reservation always succeeds");
if terms.len() > remaining {
self.truncated.store(true, Ordering::Relaxed);
terms.truncate(remaining);
}
terms
}
pub(super) fn truncated(&self) -> bool {
self.truncated.load(Ordering::Relaxed)
}
pub(super) fn available_for(&self, raw_query: &str) -> usize {
fts5_candidate_terms(raw_query)
.len()
.min(self.remaining.load(Ordering::Relaxed))
}
}
pub(super) fn phase_a_rowids_statement(term: &str, limit: usize) -> SqlStatement {
#[cfg(all(test, feature = "namespace-trigram-proto"))]
if let Some((table, scoped_term)) = prototype_fts_target(term) {
return SqlStatement {
sql: format!(
"SELECT rowid FROM {table} WHERE {table} MATCH ?1 \
ORDER BY rowid LIMIT ?2"
),
params: vec![SqlValue::Text(scoped_term), SqlValue::Integer(limit as i64)],
label: Some("knowledge.fts_rowids".into()),
};
}
SqlStatement {
sql: khive_runtime::sql!("knowledge_search_fts_rowids").into(),
params: vec![SqlValue::Text(term.into()), SqlValue::Integer(limit as i64)],
label: Some("knowledge.fts_rowids".into()),
}
}
pub(super) fn term_frequency_statement(term: &str) -> SqlStatement {
#[cfg(all(test, feature = "namespace-trigram-proto"))]
if let Some((table, scoped_term)) = prototype_fts_target(term) {
return SqlStatement {
sql: format!(
"SELECT count(*) AS frequency FROM ( \
SELECT rowid FROM {table} WHERE {table} MATCH ?1 \
ORDER BY rowid LIMIT ?2 \
)"
),
params: vec![
SqlValue::Text(scoped_term),
SqlValue::Integer((FTS_TERM_LIMIT + 1) as i64),
],
label: Some("knowledge.fts_term_frequency".into()),
};
}
SqlStatement {
sql: khive_runtime::sql!("knowledge_search_fts_term_frequency").into(),
params: vec![
SqlValue::Text(term.into()),
SqlValue::Integer((FTS_TERM_LIMIT + 1) as i64),
],
label: Some("knowledge.fts_term_frequency".into()),
}
}
pub(super) async fn rarest_fts_terms_first(
reader: &mut dyn khive_storage::SqlReader,
terms: Vec<String>,
stage: &mut LexicalStage,
) -> Result<Vec<String>, khive_storage::StorageError> {
let mut frequencies = Vec::with_capacity(terms.len());
#[cfg_attr(not(test), allow(clippy::unused_enumerate_index))]
for (_probe_index, term) in terms.into_iter().enumerate() {
let rows = stage
.read_bounded(
LexicalPhase::TermFrequency,
LexicalBound::OrderingProbe,
rarity_probe_budget(),
reader.query_all(term_frequency_statement(&term)),
)
.await?;
let frequency = rows
.first()
.and_then(|row| row_i64(row, "frequency"))
.ok_or_else(|| khive_storage::StorageError::Serialization {
capability: khive_storage::StorageCapability::Sql,
message: "term frequency probe returned no integer count".into(),
})?;
if frequency > 0 {
frequencies.push((term, frequency));
}
#[cfg(test)]
advance_fts_test_probe_deadline_after_term(_probe_index + 1).await;
}
frequencies
.sort_unstable_by(|(a, a_count), (b, b_count)| a_count.cmp(b_count).then_with(|| a.cmp(b)));
Ok(frequencies.into_iter().map(|(term, _)| term).collect())
}
pub(super) const PHASE_A_OVERFETCH_FACTOR: usize = 4;
const PHASE_A_WIDEN_CEILING: usize = 8000;
const LEXICAL_STAGE_EXTRA_TERM_QUARTERS: usize = 12;
pub(super) fn lexical_stage_budget_for_terms(
base: std::time::Duration,
admitted_terms: usize,
) -> std::time::Duration {
let quarters = 4 + admitted_terms
.saturating_sub(1)
.min(LEXICAL_STAGE_EXTRA_TERM_QUARTERS);
base.saturating_mul(quarters as u32) / 4
}
const RARITY_PROBE_BUDGET_NUMERATOR: u32 = 1;
const RARITY_PROBE_BUDGET_DENOMINATOR: u32 = 4;
fn rarity_probe_budget() -> std::time::Duration {
lexical_stage_budget() / RARITY_PROBE_BUDGET_DENOMINATOR * RARITY_PROBE_BUDGET_NUMERATOR
}
tokio::task_local! {
static LEXICAL_STAGE_BUDGET_OVERRIDE_MS: u64;
static PHASE_A_WIDEN_CEILING_OVERRIDE: usize;
}
#[cfg(test)]
tokio::task_local! {
pub(super) static MEMBER_SIZING_READ_TIMEOUT: ();
}
#[cfg(test)]
pub(crate) async fn with_member_sizing_read_timeout<F: std::future::Future>(
future: F,
) -> F::Output {
MEMBER_SIZING_READ_TIMEOUT.scope((), future).await
}
pub(super) fn lexical_stage_budget() -> std::time::Duration {
let ms = LEXICAL_STAGE_BUDGET_OVERRIDE_MS
.try_with(|ms| *ms)
.unwrap_or(LEXICAL_STAGE_BUDGET_MS);
std::time::Duration::from_millis(ms)
}
fn phase_a_widen_ceiling() -> usize {
PHASE_A_WIDEN_CEILING_OVERRIDE
.try_with(|ceiling| *ceiling)
.unwrap_or(PHASE_A_WIDEN_CEILING)
}
#[cfg(test)]
pub(crate) async fn with_lexical_stage_budget_override_ms<F>(ms: u64, future: F) -> F::Output
where
F: std::future::Future,
{
LEXICAL_STAGE_BUDGET_OVERRIDE_MS.scope(ms, future).await
}
#[cfg(test)]
pub(crate) async fn with_phase_a_widen_ceiling_override<F>(ceiling: usize, future: F) -> F::Output
where
F: std::future::Future,
{
PHASE_A_WIDEN_CEILING_OVERRIDE.scope(ceiling, future).await
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum LexicalCandidateState {
Matched,
ExactName,
NoMatch,
Filtered,
PartialTimeout,
TimedOut,
}
impl LexicalCandidateState {
pub(super) fn as_str(self) -> &'static str {
match self {
Self::Matched => "matched",
Self::ExactName => "exact_name",
Self::NoMatch => "no_match",
Self::Filtered => "filtered",
Self::PartialTimeout => "partial_timeout",
Self::TimedOut => "timed_out",
}
}
pub(super) fn merge(states: &[Self]) -> Self {
let timed_out = states
.iter()
.filter(|&&state| state == Self::TimedOut)
.count();
if !states.is_empty() && timed_out == states.len() {
Self::TimedOut
} else if timed_out > 0 || states.contains(&Self::PartialTimeout) {
Self::PartialTimeout
} else if states.contains(&Self::Matched) {
Self::Matched
} else if states.contains(&Self::ExactName) {
Self::ExactName
} else if states.contains(&Self::Filtered) {
Self::Filtered
} else {
Self::NoMatch
}
}
}
pub(super) struct FtsFetchOutcome {
pub(super) atoms: Vec<Atom>,
pub(super) timeout: Option<LexicalTimeout>,
pub(super) state: LexicalCandidateState,
}
#[cfg(test)]
#[derive(Clone, Copy)]
pub(super) struct FtsTestDeadlineAdvance {
pub(super) after_completed_terms: usize,
pub(super) by: std::time::Duration,
}
#[cfg(test)]
tokio::task_local! {
pub(super) static FTS_TEST_DEADLINE_ADVANCE: FtsTestDeadlineAdvance;
}
#[cfg(test)]
async fn advance_fts_test_deadline_after_term(completed_terms: usize) {
let advance_by = FTS_TEST_DEADLINE_ADVANCE
.try_with(|control| {
(completed_terms == control.after_completed_terms).then_some(control.by)
})
.ok()
.flatten();
if let Some(advance_by) = advance_by {
tokio::time::advance(advance_by).await;
}
}
#[cfg(test)]
tokio::task_local! {
static FTS_TEST_PROBE_DEADLINE_ADVANCE: FtsTestDeadlineAdvance;
}
#[cfg(test)]
async fn advance_fts_test_probe_deadline_after_term(completed_terms: usize) {
let advance_by = FTS_TEST_PROBE_DEADLINE_ADVANCE
.try_with(|control| {
(completed_terms == control.after_completed_terms).then_some(control.by)
})
.ok()
.flatten();
if let Some(advance_by) = advance_by {
tokio::time::advance(advance_by).await;
}
}
#[cfg(test)]
pub(crate) async fn with_fts_probe_deadline_advance_after_term<F>(
after_completed_terms: usize,
by: std::time::Duration,
future: F,
) -> F::Output
where
F: std::future::Future,
{
FTS_TEST_PROBE_DEADLINE_ADVANCE
.scope(
FtsTestDeadlineAdvance {
after_completed_terms,
by,
},
future,
)
.await
}
#[cfg(test)]
pub(crate) async fn with_fts_deadline_advance_after_term<F>(
after_completed_terms: usize,
by: std::time::Duration,
future: F,
) -> F::Output
where
F: std::future::Future,
{
FTS_TEST_DEADLINE_ADVANCE
.scope(
FtsTestDeadlineAdvance {
after_completed_terms,
by,
},
future,
)
.await
}
pub(super) fn is_timeout(e: &khive_storage::StorageError) -> bool {
matches!(e, khive_storage::StorageError::Timeout { .. })
}
pub(super) fn is_read_timeout(e: &RuntimeError) -> bool {
matches!(
e,
RuntimeError::Storage(khive_storage::StorageError::Timeout { .. })
)
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn fetch_fts_candidates(
runtime: &KhiveRuntime,
ns: &str,
raw_query: &str,
type_filter: Option<&str>,
statuses: &[String],
exclude_statuses: &[&str],
fetch_limit: usize,
term_budget: &FtsTermBudget,
mut stage: LexicalStage,
) -> Result<FtsFetchOutcome, RuntimeError> {
let mut terms = term_budget.admit(fts5_candidate_terms(raw_query));
if terms.is_empty() {
return Ok(FtsFetchOutcome {
atoms: Vec::new(),
timeout: None,
state: LexicalCandidateState::NoMatch,
});
}
let sql = runtime.sql();
let reader = match stage.read(LexicalPhase::ReaderOpen, sql.reader()).await {
Ok(reader) => reader,
Err(e) if is_timeout(&e) => {
return Ok(FtsFetchOutcome {
atoms: Vec::new(),
timeout: stage.timeout,
state: LexicalCandidateState::TimedOut,
});
}
Err(e) => return Err(sql_err("search fts reader", e)),
};
#[cfg(test)]
let reader = tests::record_term_probes(reader);
let mut reader = reader;
#[cfg(all(test, feature = "namespace-trigram-proto"))]
if let Ok(experiment) = NAMESPACE_TRIGRAM_EXPERIMENT.try_with(Clone::clone) {
let key_row = match stage
.read(
LexicalPhase::ReaderOpen,
reader.query_row(SqlStatement {
sql: khive_runtime::sql!("knowledge_search_namespace_key").into(),
params: vec![SqlValue::Text(ns.into())],
label: Some("knowledge.fts_namespace_key".into()),
}),
)
.await
{
Ok(row) => row,
Err(e) if is_timeout(&e) => {
return Ok(FtsFetchOutcome {
atoms: Vec::new(),
timeout: stage.timeout,
state: LexicalCandidateState::TimedOut,
});
}
Err(e) => return Err(sql_err("search fts namespace key", e)),
};
let found = key_row
.as_ref()
.and_then(|row| row_str(row, "namespace_key"))
.ok_or_else(|| {
RuntimeError::Internal(format!("missing knowledge FTS namespace key for {ns:?}"))
})?;
let expected = match experiment {
NamespaceTrigramExperiment::SlotTable { key }
| NamespaceTrigramExperiment::Prefixed { key } => key,
};
if found != expected {
return Err(RuntimeError::Internal(
"prototype namespace key differs from slot-table baseline".into(),
));
}
}
let type_clause = type_eligibility_sql(type_filter, "a");
let per_term_limit = if terms.len() == 1 {
fetch_limit
} else {
fetch_limit.clamp(1, FTS_TERM_LIMIT)
};
let has_eligibility_clause = !status_sql_clause(statuses, exclude_statuses, 1)
.0
.is_empty()
|| !type_clause.is_empty();
let widen_ceiling = phase_a_widen_ceiling();
let base_probe_limit = if has_eligibility_clause {
per_term_limit
.saturating_mul(PHASE_A_OVERFETCH_FACTOR)
.min(widen_ceiling)
} else {
per_term_limit
};
let mut rarity_probe_timed_out = false;
if terms.len() > 1 {
let arrival_order = terms.clone();
let probe_budget = rarity_probe_budget();
let probed = khive_storage::scope_request_read_deadline(probe_budget, async {
rarest_fts_terms_first(reader.as_mut(), terms, &mut stage).await
})
.await;
terms = match probed {
Ok(terms) => terms,
Err(e) if is_timeout(&e) => {
rarity_probe_timed_out = true;
arrival_order
}
Err(e) => return Err(sql_err("search fts term frequency probe", e)),
};
}
let mut per_term_rows: Vec<Vec<Atom>> = Vec::with_capacity(terms.len());
let mut term_probe_rowids: Vec<Vec<i64>> = Vec::with_capacity(terms.len());
let mut unexhausted_terms = Vec::new();
let mut term_query_timed_out = false;
'terms: for term in &terms {
let mut probe_limit = base_probe_limit;
let (mut eligible, probed_rowids, exhausted): (Vec<Atom>, Vec<i64>, bool) = loop {
let phase_a_rows = match stage
.read(
LexicalPhase::PhaseARowids,
reader.query_all(phase_a_rowids_statement(term, probe_limit)),
)
.await
{
Ok(rows) => rows,
Err(e) if is_timeout(&e) => {
term_query_timed_out = true;
break 'terms;
}
Err(e) => return Err(sql_err("search fts phase-a query", e)),
};
let rowids: Vec<i64> = phase_a_rows
.iter()
.filter_map(|r| row_i64(r, "rowid"))
.collect();
let phase_a_full = rowids.len() >= probe_limit;
if rowids.is_empty() {
break (Vec::new(), rowids, true);
}
let mut atoms_by_rowid: HashMap<i64, Atom> = HashMap::with_capacity(rowids.len());
for chunk in rowids.chunks(HYDRATION_ID_CHUNK) {
let statement = phase_b_hydration_statement(
ns,
chunk,
statuses,
exclude_statuses,
type_clause.as_str(),
);
let rows = match stage
.read(LexicalPhase::PhaseBHydration, reader.query_all(statement))
.await
{
Ok(rows) => rows,
Err(e) if is_timeout(&e) => {
term_query_timed_out = true;
break 'terms;
}
Err(e) => return Err(sql_err("search fts phase-b hydration", e)),
};
for row in &rows {
if let (Some(atom), Some(rowid)) = (atom_from_row(row), row_i64(row, "rowid")) {
atoms_by_rowid.insert(rowid, atom);
}
}
}
let eligible_now: Vec<Atom> = rowids
.iter()
.filter_map(|rowid| atoms_by_rowid.get(rowid).cloned())
.collect();
if has_eligibility_clause && eligible_now.len() < per_term_limit && phase_a_full {
if probe_limit < widen_ceiling {
probe_limit = probe_limit
.saturating_mul(PHASE_A_OVERFETCH_FACTOR)
.min(widen_ceiling);
continue;
}
let (scoped_status_clause, scoped_status_params) =
status_sql_clause(statuses, exclude_statuses, 4);
let scoped_sql =
lexical_sql::eligibility_fallback_sql(&scoped_status_clause, &type_clause);
let scoped_term = term.clone();
#[cfg(all(test, feature = "namespace-trigram-proto"))]
let (scoped_sql, scoped_term) =
if let Some((table, match_expression)) = prototype_fts_target(term) {
(
format!(
"SELECT a.* FROM {table} \
CROSS JOIN knowledge_atoms AS a ON a.rowid = {table}.rowid \
WHERE {table} MATCH ?1 \
AND +a.namespace = ?2 \
AND a.deleted_at IS NULL{scoped_status_clause}{type_clause} \
ORDER BY {table}.rowid LIMIT ?3"
),
match_expression,
)
} else {
(scoped_sql, scoped_term)
};
let mut scoped_params = vec![
SqlValue::Text(scoped_term),
SqlValue::Text(ns.to_owned()),
SqlValue::Integer(per_term_limit as i64),
];
scoped_params.extend(scoped_status_params);
let scoped_rows = match stage
.read(
LexicalPhase::EligibilityFallback,
reader.query_all(SqlStatement {
sql: scoped_sql,
params: scoped_params,
label: None,
}),
)
.await
{
Ok(rows) => rows,
Err(e) if is_timeout(&e) => {
term_query_timed_out = true;
break 'terms;
}
Err(e) => {
return Err(sql_err("search fts eligibility-scoped ceiling fallback", e));
}
};
break (
scoped_rows.iter().filter_map(atom_from_row).collect(),
rowids,
false,
);
}
break (eligible_now, rowids, !phase_a_full);
};
eligible.truncate(per_term_limit);
per_term_rows.push(eligible);
term_probe_rowids.push(probed_rowids);
if !exhausted {
unexhausted_terms.push(term.clone());
}
#[cfg(test)]
advance_fts_test_deadline_after_term(per_term_rows.len()).await;
}
let mut seen_ids: HashSet<Uuid> = HashSet::new();
let mut combined: Vec<Atom> = Vec::new();
let max_term_rows = per_term_rows.iter().map(Vec::len).max().unwrap_or(0);
'merge: for i in 0..max_term_rows {
for term_rows in &per_term_rows {
if combined.len() >= fetch_limit {
break 'merge;
}
if let Some(atom) = term_rows.get(i) {
if seen_ids.insert(atom.id) {
combined.push(atom.clone());
}
}
}
}
if term_query_timed_out || rarity_probe_timed_out {
return Ok(FtsFetchOutcome {
state: if combined.is_empty() {
LexicalCandidateState::TimedOut
} else {
LexicalCandidateState::PartialTimeout
},
atoms: combined,
timeout: stage.timeout,
});
}
if !combined.is_empty() {
return Ok(FtsFetchOutcome {
atoms: combined,
timeout: None,
state: LexicalCandidateState::Matched,
});
}
let mut probe_rowids: Vec<i64> = term_probe_rowids.into_iter().flatten().collect();
probe_rowids.sort_unstable();
probe_rowids.dedup();
let mut namespace_has_match = false;
for chunk in probe_rowids.chunks(HYDRATION_ID_CHUNK) {
let placeholders = chunk
.iter()
.enumerate()
.map(|(i, _)| format!("?{}", i + 2))
.collect::<Vec<_>>()
.join(",");
let mut params = vec![SqlValue::Text(ns.to_owned())];
params.extend(chunk.iter().map(|rowid| SqlValue::Integer(*rowid)));
let membership_sql = format!(
"SELECT 1 AS present FROM knowledge_atoms \
WHERE rowid IN ({placeholders}) AND namespace = ?1 LIMIT 1"
);
let row = match stage
.read(
LexicalPhase::NamespaceMembership,
reader.query_row(SqlStatement {
sql: membership_sql,
params,
label: None,
}),
)
.await
{
Ok(row) => row,
Err(e) if is_timeout(&e) => {
return Ok(FtsFetchOutcome {
atoms: Vec::new(),
timeout: stage.timeout,
state: LexicalCandidateState::TimedOut,
});
}
Err(e) => return Err(sql_err("search fts namespace membership probe", e)),
};
if row.is_some() {
namespace_has_match = true;
break;
}
}
if !namespace_has_match {
for term in unexhausted_terms {
let statement = SqlStatement {
sql: khive_runtime::sql!("knowledge_search_fts_namespace_present").into(),
params: vec![SqlValue::Text(term.clone()), SqlValue::Text(ns.to_owned())],
label: None,
};
#[cfg(all(test, feature = "namespace-trigram-proto"))]
let statement = if let Some((table, match_expression)) = prototype_fts_target(&term) {
SqlStatement {
sql: format!(
"SELECT 1 AS present FROM {table} \
CROSS JOIN knowledge_atoms AS a ON a.rowid = {table}.rowid \
WHERE {table} MATCH ?1 AND +a.namespace = ?2 LIMIT 1"
),
params: vec![
SqlValue::Text(match_expression),
SqlValue::Text(ns.to_owned()),
],
label: None,
}
} else {
statement
};
let row = match stage
.read(
LexicalPhase::NamespaceExistence,
reader.query_row(statement),
)
.await
{
Ok(row) => row,
Err(e) if is_timeout(&e) => {
return Ok(FtsFetchOutcome {
atoms: Vec::new(),
timeout: stage.timeout,
state: LexicalCandidateState::TimedOut,
});
}
Err(e) => return Err(sql_err("search fts scoped namespace existence", e)),
};
if row.is_some() {
namespace_has_match = true;
break;
}
}
}
if namespace_has_match {
return Ok(FtsFetchOutcome {
atoms: Vec::new(),
timeout: None,
state: LexicalCandidateState::Filtered,
});
}
let no_scoreable_terms = !matching::tokenize_field(raw_query)
.iter()
.any(|term| term.len() >= MIN_TERM_LEN && !is_stop(term));
if no_scoreable_terms {
match fetch_exact_name_candidate(
reader.as_mut(),
ns,
raw_query,
type_filter,
statuses,
exclude_statuses,
&mut stage,
)
.await?
{
ExactNameProbe::Hit(atom) => {
return Ok(FtsFetchOutcome {
atoms: vec![*atom],
timeout: None,
state: LexicalCandidateState::ExactName,
})
}
ExactNameProbe::Filtered => {
return Ok(FtsFetchOutcome {
atoms: Vec::new(),
timeout: None,
state: LexicalCandidateState::Filtered,
})
}
ExactNameProbe::TimedOut => {
return Ok(FtsFetchOutcome {
atoms: Vec::new(),
timeout: stage.timeout,
state: LexicalCandidateState::TimedOut,
})
}
ExactNameProbe::Miss => {}
}
}
Ok(FtsFetchOutcome {
atoms: Vec::new(),
timeout: None,
state: LexicalCandidateState::NoMatch,
})
}
pub(super) fn exact_name_statement(
ns: &str,
slug: &str,
type_filter: Option<&str>,
statuses: &[String],
exclude_statuses: &[&str],
) -> SqlStatement {
let (status_clause, status_params) = status_sql_clause(statuses, exclude_statuses, 3);
let type_clause = type_eligibility_sql(type_filter, "knowledge_atoms");
let mut params = vec![
SqlValue::Text(ns.to_owned()),
SqlValue::Text(slug.to_owned()),
];
params.extend(status_params);
SqlStatement {
sql: format!(
"SELECT *, CASE WHEN 1{status_clause}{type_clause} THEN 1 ELSE 0 END AS exact_name_eligible \
FROM knowledge_atoms \
WHERE namespace = ?1 AND slug = ?2 AND deleted_at IS NULL LIMIT 1"
),
params,
label: Some("knowledge.exact_name".into()),
}
}
pub(super) async fn fetch_exact_name_candidate(
reader: &mut dyn khive_storage::SqlReader,
ns: &str,
raw_query: &str,
type_filter: Option<&str>,
statuses: &[String],
exclude_statuses: &[&str],
stage: &mut LexicalStage,
) -> Result<ExactNameProbe, RuntimeError> {
let slug = to_slug(raw_query);
if slug.is_empty() {
return Ok(ExactNameProbe::Miss);
}
let row = stage
.read(
LexicalPhase::ExactNameProbe,
reader.query_row(exact_name_statement(
ns,
&slug,
type_filter,
statuses,
exclude_statuses,
)),
)
.await;
match row {
Ok(Some(row)) if row_i64(&row, "exact_name_eligible") != Some(1) => {
Ok(ExactNameProbe::Filtered)
}
Ok(Some(row)) => Ok(atom_from_row(&row).map_or(ExactNameProbe::Miss, |atom| {
ExactNameProbe::Hit(Box::new(atom))
})),
Ok(None) => Ok(ExactNameProbe::Miss),
Err(e) if is_timeout(&e) => Ok(ExactNameProbe::TimedOut),
Err(e) => Err(sql_err("search exact-name query", e)),
}
}
#[derive(Debug)]
pub(super) enum ExactNameProbe {
Hit(Box<Atom>),
Miss,
Filtered,
TimedOut,
}