106 lines
4.1 KiB
Rust
Raw Normal View History

2021-02-19 15:32:14 +01:00
use std::collections::HashMap;
use std::mem::take;
2021-02-24 15:37:37 +01:00
use log::debug;
2021-02-19 15:32:14 +01:00
use roaring::RoaringBitmap;
use crate::search::query_tree::Operation;
use super::{Context, Criterion, CriterionParameters, CriterionResult, resolve_query_tree};
2021-02-19 15:32:14 +01:00
pub struct Words<'t> {
ctx: &'t dyn Context<'t>,
2021-02-19 15:32:14 +01:00
query_trees: Vec<Operation>,
2021-03-09 12:04:52 +01:00
candidates: Option<RoaringBitmap>,
2021-02-19 15:32:14 +01:00
bucket_candidates: RoaringBitmap,
2021-03-23 15:25:46 +01:00
parent: Box<dyn Criterion + 't>,
2021-02-19 15:32:14 +01:00
candidates_cache: HashMap<(Operation, u8), RoaringBitmap>,
}
impl<'t> Words<'t> {
pub fn new(ctx: &'t dyn Context<'t>, parent: Box<dyn Criterion + 't>) -> Self {
Words {
2021-02-19 15:32:14 +01:00
ctx,
query_trees: Vec::default(),
2021-03-09 12:04:52 +01:00
candidates: None,
2021-02-19 15:32:14 +01:00
bucket_candidates: RoaringBitmap::new(),
2021-03-23 15:25:46 +01:00
parent,
2021-02-19 15:32:14 +01:00
candidates_cache: HashMap::default(),
}
2021-02-19 15:32:14 +01:00
}
}
impl<'t> Criterion for Words<'t> {
#[logging_timer::time("Words::{}")]
fn next(&mut self, params: &mut CriterionParameters) -> anyhow::Result<Option<CriterionResult>> {
// remove excluded candidates when next is called, instead of doing it in the loop.
if let Some(candidates) = self.candidates.as_mut() {
*candidates -= params.excluded_candidates;
}
2021-02-19 15:32:14 +01:00
loop {
2021-02-24 15:37:37 +01:00
debug!("Words at iteration {} ({:?})", self.query_trees.len(), self.candidates);
2021-02-19 15:32:14 +01:00
match (self.query_trees.pop(), &mut self.candidates) {
2021-03-09 12:04:52 +01:00
(query_tree, Some(candidates)) if candidates.is_empty() => {
2021-02-19 15:32:14 +01:00
self.query_trees = Vec::new();
return Ok(Some(CriterionResult {
query_tree,
2021-03-09 12:04:52 +01:00
candidates: self.candidates.take(),
bucket_candidates: take(&mut self.bucket_candidates),
}));
2021-02-19 15:32:14 +01:00
},
2021-03-09 12:04:52 +01:00
(Some(qt), Some(candidates)) => {
let mut found_candidates = resolve_query_tree(self.ctx, &qt, &mut self.candidates_cache, params.wdcache)?;
found_candidates.intersect_with(&candidates);
candidates.difference_with(&found_candidates);
return Ok(Some(CriterionResult {
query_tree: Some(qt),
2021-03-09 12:04:52 +01:00
candidates: Some(found_candidates),
2021-03-23 15:25:46 +01:00
bucket_candidates: take(&mut self.bucket_candidates),
}));
},
2021-03-09 12:04:52 +01:00
(Some(qt), None) => {
2021-02-19 15:32:14 +01:00
return Ok(Some(CriterionResult {
query_tree: Some(qt),
2021-03-09 12:04:52 +01:00
candidates: None,
2021-03-23 15:25:46 +01:00
bucket_candidates: take(&mut self.bucket_candidates),
2021-02-19 15:32:14 +01:00
}));
},
2021-03-09 12:04:52 +01:00
(None, Some(_)) => {
let candidates = self.candidates.take();
2021-02-19 15:32:14 +01:00
return Ok(Some(CriterionResult {
query_tree: None,
candidates: candidates.clone(),
2021-03-09 12:04:52 +01:00
bucket_candidates: candidates.unwrap_or_default(),
2021-02-19 15:32:14 +01:00
}));
},
2021-03-09 12:04:52 +01:00
(None, None) => {
match self.parent.next(params)? {
2021-03-23 15:25:46 +01:00
Some(CriterionResult { query_tree: None, candidates: None, bucket_candidates }) => {
return Ok(Some(CriterionResult {
query_tree: None,
candidates: None,
bucket_candidates,
}));
},
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
self.query_trees = query_tree.map(explode_query_tree).unwrap_or_default();
self.candidates = candidates;
self.bucket_candidates.union_with(&bucket_candidates);
2021-02-19 15:32:14 +01:00
},
None => return Ok(None),
}
},
}
}
}
}
fn explode_query_tree(query_tree: Operation) -> Vec<Operation> {
match query_tree {
Operation::Or(true, ops) => ops,
otherwise => vec![otherwise],
}
}