2021-03-23 15:25:46 +01:00
|
|
|
use log::debug;
|
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
|
|
|
use crate::search::query_tree::Operation;
|
|
|
|
use crate::search::WordDerivationsCache;
|
2021-04-28 18:01:23 +02:00
|
|
|
use super::{resolve_query_tree, Criterion, CriterionResult, CriterionParameters, Context};
|
2021-03-23 15:25:46 +01:00
|
|
|
|
|
|
|
/// The result of a call to the fetcher.
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct FinalResult {
|
|
|
|
/// The query tree corresponding to the current bucket of the last criterion.
|
|
|
|
pub query_tree: Option<Operation>,
|
|
|
|
/// The candidates of the current bucket of the last criterion.
|
|
|
|
pub candidates: RoaringBitmap,
|
|
|
|
/// Candidates that comes from the current bucket of the initial criterion.
|
|
|
|
pub bucket_candidates: RoaringBitmap,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Final<'t> {
|
2021-03-31 19:23:02 +02:00
|
|
|
ctx: &'t dyn Context<'t>,
|
2021-03-23 15:25:46 +01:00
|
|
|
parent: Box<dyn Criterion + 't>,
|
|
|
|
wdcache: WordDerivationsCache,
|
2021-04-28 18:01:23 +02:00
|
|
|
returned_candidates: RoaringBitmap,
|
2021-03-23 15:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t> Final<'t> {
|
2021-03-31 19:23:02 +02:00
|
|
|
pub fn new(ctx: &'t dyn Context<'t>, parent: Box<dyn Criterion + 't>) -> Final<'t> {
|
2021-04-28 18:01:23 +02:00
|
|
|
Final { ctx, parent, wdcache: WordDerivationsCache::new(), returned_candidates: RoaringBitmap::new() }
|
2021-03-23 15:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[logging_timer::time("Final::{}")]
|
2021-04-28 18:01:23 +02:00
|
|
|
pub fn next(&mut self, excluded_candidates: &RoaringBitmap) -> anyhow::Result<Option<FinalResult>> {
|
2021-03-23 15:25:46 +01:00
|
|
|
loop {
|
|
|
|
debug!("Final iteration");
|
2021-04-28 18:01:23 +02:00
|
|
|
let mut criterion_parameters = CriterionParameters {
|
|
|
|
wdcache: &mut self.wdcache,
|
|
|
|
// returned_candidates is merged with excluded_candidates to avoid duplicas
|
|
|
|
excluded_candidates: &(&self.returned_candidates | excluded_candidates),
|
|
|
|
};
|
2021-03-23 15:25:46 +01:00
|
|
|
|
2021-04-28 18:01:23 +02:00
|
|
|
match self.parent.next(&mut criterion_parameters)? {
|
2021-05-05 20:46:56 +02:00
|
|
|
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
|
|
|
|
let candidates = match (candidates, query_tree.as_ref()) {
|
|
|
|
(Some(candidates), _) => candidates,
|
|
|
|
(None, Some(qt)) => resolve_query_tree(self.ctx, qt, &mut self.wdcache)?,
|
|
|
|
(None, None) => self.ctx.documents_ids()?,
|
2021-03-23 15:25:46 +01:00
|
|
|
};
|
|
|
|
|
2021-05-05 20:46:56 +02:00
|
|
|
let bucket_candidates = bucket_candidates.unwrap_or_else(|| candidates.clone());
|
|
|
|
|
2021-04-28 18:01:23 +02:00
|
|
|
self.returned_candidates |= &candidates;
|
2021-03-23 15:25:46 +01:00
|
|
|
|
2021-04-26 11:30:42 +02:00
|
|
|
return Ok(Some(FinalResult { query_tree, candidates, bucket_candidates }));
|
2021-03-23 15:25:46 +01:00
|
|
|
},
|
|
|
|
None => return Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|