use crate::{
db::query::plan::{OrderSpec, index_order_terms},
model::{entity::EntityModel, index::IndexModel},
};
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(in crate::db::query::plan) struct AccessCandidateScore {
pub(in crate::db::query::plan) prefix_len: usize,
pub(in crate::db::query::plan) exact: bool,
pub(in crate::db::query::plan) order_compatible: bool,
}
impl AccessCandidateScore {
#[must_use]
pub(in crate::db::query::plan) const fn new(
prefix_len: usize,
exact: bool,
order_compatible: bool,
) -> Self {
Self {
prefix_len,
exact,
order_compatible,
}
}
}
#[must_use]
pub(in crate::db::query::plan) const fn access_candidate_score_outranks(
candidate: AccessCandidateScore,
best: AccessCandidateScore,
exact_priority: bool,
) -> bool {
if candidate.prefix_len != best.prefix_len {
return candidate.prefix_len > best.prefix_len;
}
if exact_priority && candidate.exact != best.exact {
return candidate.exact;
}
if candidate.order_compatible != best.order_compatible {
return candidate.order_compatible;
}
false
}
#[must_use]
pub(in crate::db::query::plan) fn candidate_satisfies_secondary_order(
model: &EntityModel,
order: Option<&OrderSpec>,
index: &IndexModel,
prefix_len: usize,
) -> bool {
let Some(order_contract) = order
.and_then(|order| order.deterministic_secondary_order_contract(model.primary_key.name))
else {
return false;
};
let index_terms = index_order_terms(index);
order_contract.matches_index_suffix(&index_terms, prefix_len)
|| order_contract.matches_index_full(&index_terms)
}