use core::{
pin::pin,
task::{Context, Poll, Waker},
};
use super::{AsyncGather, RetrieveError, filled};
use crate::retrieve::Ranked;
fn block_on<F: Future>(future: F) -> F::Output {
let mut future = pin!(future);
let mut cx = Context::from_waker(Waker::noop());
loop {
if let Poll::Ready(value) = future.as_mut().poll(&mut cx) {
return value;
}
}
}
#[derive(Debug, Clone, PartialEq)]
struct Segment {
key: i64,
title: String,
}
struct Corpus {
held: Vec<Segment>,
batches: Vec<Vec<i64>>,
}
impl Corpus {
fn new(held: impl IntoIterator<Item = (i64, &'static str)>) -> Self {
Self {
held: held
.into_iter()
.map(|(key, title)| Segment {
key,
title: title.to_owned(),
})
.collect(),
batches: Vec::new(),
}
}
}
impl AsyncGather<i64, Segment> for Corpus {
type Filter = [i64];
type Error = &'static str;
fn gather(
&mut self,
keys: &[i64],
filter: Option<&[i64]>,
) -> impl Future<Output = Result<Vec<Option<Segment>>, &'static str>> {
self.batches.push(keys.to_vec());
let found = keys
.iter()
.map(|key| {
if filter.is_some_and(|pruned| pruned.contains(key)) {
return None;
}
self.held.iter().find(|held| held.key == *key).cloned()
})
.collect();
core::future::ready(Ok(found))
}
}
struct Refusing;
impl AsyncGather<i64, Segment> for Refusing {
type Filter = ();
type Error = &'static str;
fn gather(
&mut self,
_keys: &[i64],
_filter: Option<&()>,
) -> impl Future<Output = Result<Vec<Option<Segment>>, &'static str>> {
core::future::ready(Err("the store refused"))
}
}
fn ranked(hits: impl IntoIterator<Item = (i64, f32, u64)>) -> Vec<Ranked<i64, f32>> {
hits
.into_iter()
.map(|(key, score, rank)| Ranked::new(score, rank, key))
.collect()
}
fn shape(filled: &[Ranked<Segment, f32>]) -> Vec<(&str, f32, u64)> {
filled
.iter()
.map(|hit| (hit.node_ref().title.as_str(), hit.score(), hit.rank()))
.collect()
}
#[test]
fn a_full_page_costs_one_composed_query() {
let mut store = Corpus::new([(1, "alpha"), (2, "beta"), (3, "gamma")]);
let page = block_on(filled(
ranked([(1, 0.9, 1), (2, 0.8, 4), (3, 0.7, 9)]),
&mut store,
None,
3,
))
.expect("the corpus answers");
assert_eq!(
shape(&page),
vec![("alpha", 0.9, 1), ("beta", 0.8, 4), ("gamma", 0.7, 9)]
);
assert_eq!(store.batches, vec![vec![1, 2, 3]]);
}
#[test]
fn the_page_is_in_engine_order_and_not_the_stores() {
let mut store = Corpus::new([(3, "gamma"), (2, "beta"), (1, "alpha")]);
let page = block_on(filled(
ranked([(2, 0.9, 1), (1, 0.8, 2), (3, 0.7, 3)]),
&mut store,
None,
3,
))
.expect("the corpus answers");
assert_eq!(
page
.iter()
.map(|hit| hit.node_ref().key)
.collect::<Vec<_>>(),
vec![2, 1, 3]
);
}
#[test]
fn a_filter_that_prunes_mid_page_pulls_another_batch() {
let mut store = Corpus::new((1..=9).map(|key| (key, "held")));
let pruned = [2_i64, 3, 5, 6, 8];
let page = block_on(filled(
ranked((1..=9).map(|key| (key, 1.0, key as u64))),
&mut store,
Some(&pruned[..]),
3,
))
.expect("the corpus answers");
assert_eq!(
page
.iter()
.map(|hit| (hit.node_ref().key, hit.rank()))
.collect::<Vec<_>>(),
vec![(1, 1), (4, 4), (7, 7)]
);
assert_eq!(
store.batches,
vec![vec![1, 2, 3], vec![4, 5], vec![6], vec![7]]
);
}
#[test]
fn a_store_missing_row_is_skipped_like_a_prune() {
let mut store = Corpus::new([(1, "alpha"), (3, "gamma")]);
let page = block_on(filled(
ranked([(1, 0.9, 1), (2, 0.8, 2), (3, 0.7, 3)]),
&mut store,
None,
3,
))
.expect("the corpus answers");
assert_eq!(shape(&page), vec![("alpha", 0.9, 1), ("gamma", 0.7, 3)]);
}
#[test]
fn the_page_comes_back_short_when_the_keys_run_out() {
let mut store = Corpus::new([(1, "alpha"), (2, "beta")]);
let page = block_on(filled(
ranked([(1, 0.9, 1), (2, 0.8, 2)]),
&mut store,
None,
10,
))
.expect("the corpus answers");
assert_eq!(page.len(), 2);
assert_eq!(store.batches, vec![vec![1, 2]]);
}
#[test]
fn the_budget_stops_the_loop_mid_batch() {
let mut store = Corpus::new((1..=9).map(|key| (key, "held")));
let page = block_on(filled(
ranked((1..=9).map(|key| (key, 1.0, key as u64))),
&mut store,
None,
2,
))
.expect("the corpus answers");
assert_eq!(page.len(), 2);
assert_eq!(store.batches, vec![vec![1, 2]]);
}
#[test]
fn an_empty_key_list_asks_the_store_nothing() {
let mut store = Corpus::new([(1, "alpha")]);
let page = block_on(filled(Vec::<Ranked<i64, f32>>::new(), &mut store, None, 10))
.expect("the corpus answers");
assert!(page.is_empty());
assert!(store.batches.is_empty());
}
#[test]
fn the_stores_refusal_is_carried() {
let mut store = Refusing;
let refused = block_on(filled(ranked([(1, 0.9, 1)]), &mut store, None, 3));
assert_eq!(refused, Err("the store refused"));
let error: RetrieveError<&str, &str> = RetrieveError::Gather("the store refused");
assert_eq!(
error.to_string(),
"the store refused the gather: the store refused"
);
assert_eq!(
RetrieveError::<&str, &str>::Engine("bad syntax").to_string(),
"the index refused the query: bad syntax"
);
}