#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WorkloadSample {
pub document_count: usize,
pub delta_fraction: f64,
pub p95_search_seconds: f64,
pub hnsw_available: bool,
}
impl WorkloadSample {
pub fn new(document_count: usize) -> Self {
Self {
document_count,
delta_fraction: 0.0,
p95_search_seconds: 0.0,
hnsw_available: false,
}
}
pub fn with_delta_fraction(mut self, delta_fraction: f64) -> Self {
self.delta_fraction = delta_fraction;
self
}
pub fn with_p95_search_seconds(mut self, p95_search_seconds: f64) -> Self {
self.p95_search_seconds = p95_search_seconds;
self
}
pub fn with_hnsw_available(mut self, hnsw_available: bool) -> Self {
self.hnsw_available = hnsw_available;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexAction {
NoActionNeeded,
FoldDeltaIntoBase,
ConsiderBuildingHnsw,
}
#[derive(Debug, Clone, PartialEq)]
pub struct IndexRecommendation {
pub action: IndexAction,
pub reason: String,
pub confidence: f32,
}
#[derive(Debug, Clone, Copy)]
pub struct IndexAdvisor {
delta_fold_threshold: f64,
hnsw_document_count_threshold: usize,
hnsw_latency_threshold_seconds: f64,
}
impl IndexAdvisor {
pub fn new() -> Self {
Self {
delta_fold_threshold: 0.02,
hnsw_document_count_threshold: 5_000,
hnsw_latency_threshold_seconds: 0.001,
}
}
pub fn with_delta_fold_threshold(mut self, delta_fold_threshold: f64) -> Self {
self.delta_fold_threshold = delta_fold_threshold;
self
}
pub fn with_hnsw_document_count_threshold(mut self, threshold: usize) -> Self {
self.hnsw_document_count_threshold = threshold;
self
}
pub fn with_hnsw_latency_threshold_seconds(mut self, threshold: f64) -> Self {
self.hnsw_latency_threshold_seconds = threshold;
self
}
pub fn recommend(&self, sample: &WorkloadSample) -> IndexRecommendation {
if sample.delta_fraction >= self.delta_fold_threshold {
let confidence = (sample.delta_fraction / self.delta_fold_threshold.max(f64::EPSILON)
- 1.0)
.clamp(0.0, 1.0) as f32;
return IndexRecommendation {
action: IndexAction::FoldDeltaIntoBase,
reason: format!(
"delta is {:.1}% of the combined document count, at or above the {:.1}% fold threshold",
sample.delta_fraction * 100.0,
self.delta_fold_threshold * 100.0
),
confidence,
};
}
if !sample.hnsw_available
&& sample.document_count >= self.hnsw_document_count_threshold
&& sample.p95_search_seconds >= self.hnsw_latency_threshold_seconds
{
let document_ratio =
sample.document_count as f64 / self.hnsw_document_count_threshold as f64;
let latency_ratio =
sample.p95_search_seconds / self.hnsw_latency_threshold_seconds.max(f64::EPSILON);
let confidence =
((document_ratio.min(latency_ratio) - 1.0) / 2.0).clamp(0.0, 1.0) as f32;
return IndexRecommendation {
action: IndexAction::ConsiderBuildingHnsw,
reason: format!(
"{} documents and p95 {:.2}ms exceed the {}-document / {:.2}ms threshold where HnswIndex's recall and latency advantage was measured to outweigh its build cost and memory",
sample.document_count,
sample.p95_search_seconds * 1000.0,
self.hnsw_document_count_threshold,
self.hnsw_latency_threshold_seconds * 1000.0
),
confidence,
};
}
IndexRecommendation {
action: IndexAction::NoActionNeeded,
reason: "delta fraction and query latency are both within the current index shape's \
comfortable range"
.to_string(),
confidence: 0.0,
}
}
}
impl Default for IndexAdvisor {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Copy)]
pub struct AutoIndexPolicy {
min_confidence: f32,
}
impl AutoIndexPolicy {
pub fn new(min_confidence: f32) -> Self {
Self { min_confidence }
}
pub fn is_eligible(&self, recommendation: &IndexRecommendation) -> bool {
recommendation.action != IndexAction::NoActionNeeded
&& recommendation.confidence >= self.min_confidence
}
}
impl Default for AutoIndexPolicy {
fn default() -> Self {
Self::new(0.1)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QueryPath {
ExactScan,
Hnsw,
}
#[derive(Debug, Clone, Copy)]
pub struct IndexPlanner;
impl IndexPlanner {
pub fn choose(hnsw_available: bool, candidate_filter_required: bool) -> QueryPath {
if hnsw_available && !candidate_filter_required {
QueryPath::Hnsw
} else {
QueryPath::ExactScan
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn recommends_no_action_for_a_small_healthy_workload() {
let advisor = IndexAdvisor::new();
let sample = WorkloadSample::new(500).with_p95_search_seconds(0.0002);
let recommendation = advisor.recommend(&sample);
assert_eq!(recommendation.action, IndexAction::NoActionNeeded);
assert_eq!(recommendation.confidence, 0.0);
}
#[test]
fn recommends_folding_the_delta_once_past_threshold() {
let advisor = IndexAdvisor::new();
let sample = WorkloadSample::new(10_000).with_delta_fraction(0.05);
let recommendation = advisor.recommend(&sample);
assert_eq!(recommendation.action, IndexAction::FoldDeltaIntoBase);
assert!(recommendation.confidence > 0.0);
assert!(recommendation.reason.contains("5.0%"));
}
#[test]
fn recommends_hnsw_for_a_large_slow_workload_without_one_already() {
let advisor = IndexAdvisor::new();
let sample = WorkloadSample::new(20_000).with_p95_search_seconds(0.003);
let recommendation = advisor.recommend(&sample);
assert_eq!(recommendation.action, IndexAction::ConsiderBuildingHnsw);
assert!(recommendation.confidence > 0.0);
}
#[test]
fn does_not_recommend_hnsw_again_once_one_is_already_available() {
let advisor = IndexAdvisor::new();
let sample = WorkloadSample::new(20_000)
.with_p95_search_seconds(0.003)
.with_hnsw_available(true);
let recommendation = advisor.recommend(&sample);
assert_eq!(recommendation.action, IndexAction::NoActionNeeded);
}
#[test]
fn delta_fold_is_checked_before_hnsw_consideration() {
let advisor = IndexAdvisor::new();
let sample = WorkloadSample::new(20_000)
.with_delta_fraction(0.1)
.with_p95_search_seconds(0.003);
let recommendation = advisor.recommend(&sample);
assert_eq!(recommendation.action, IndexAction::FoldDeltaIntoBase);
}
#[test]
fn policy_rejects_low_confidence_recommendations() {
let policy = AutoIndexPolicy::new(0.5);
let recommendation = IndexRecommendation {
action: IndexAction::FoldDeltaIntoBase,
reason: "test".to_string(),
confidence: 0.2,
};
assert!(!policy.is_eligible(&recommendation));
}
#[test]
fn policy_accepts_high_confidence_actionable_recommendations() {
let policy = AutoIndexPolicy::new(0.5);
let recommendation = IndexRecommendation {
action: IndexAction::FoldDeltaIntoBase,
reason: "test".to_string(),
confidence: 0.8,
};
assert!(policy.is_eligible(&recommendation));
}
#[test]
fn policy_never_treats_no_action_as_eligible() {
let policy = AutoIndexPolicy::new(0.0);
let recommendation = IndexRecommendation {
action: IndexAction::NoActionNeeded,
reason: "test".to_string(),
confidence: 1.0,
};
assert!(!policy.is_eligible(&recommendation));
}
#[test]
fn planner_prefers_hnsw_only_when_available_and_unfiltered() {
assert_eq!(IndexPlanner::choose(true, false), QueryPath::Hnsw);
assert_eq!(IndexPlanner::choose(true, true), QueryPath::ExactScan);
assert_eq!(IndexPlanner::choose(false, false), QueryPath::ExactScan);
assert_eq!(IndexPlanner::choose(false, true), QueryPath::ExactScan);
}
}