2021-02-19 22:32:14 +08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::mem::take;
|
|
|
|
|
2021-02-24 22:37:37 +08:00
|
|
|
use log::debug;
|
2021-02-19 22:32:14 +08:00
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
|
|
|
use crate::search::query_tree::Operation;
|
2021-04-14 18:18:13 +08:00
|
|
|
use super::{resolve_query_tree, Criterion, CriterionResult, Context, WordDerivationsCache};
|
2021-02-19 22:32:14 +08:00
|
|
|
|
|
|
|
pub struct Words<'t> {
|
2021-04-01 01:23:02 +08:00
|
|
|
ctx: &'t dyn Context<'t>,
|
2021-02-19 22:32:14 +08:00
|
|
|
query_trees: Vec<Operation>,
|
2021-03-09 19:04:52 +08:00
|
|
|
candidates: Option<RoaringBitmap>,
|
2021-02-19 22:32:14 +08:00
|
|
|
bucket_candidates: RoaringBitmap,
|
2021-03-23 22:25:46 +08:00
|
|
|
parent: Box<dyn Criterion + 't>,
|
2021-02-19 22:32:14 +08:00
|
|
|
candidates_cache: HashMap<(Operation, u8), RoaringBitmap>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t> Words<'t> {
|
2021-04-01 01:23:02 +08:00
|
|
|
pub fn new(ctx: &'t dyn Context<'t>, parent: Box<dyn Criterion + 't>) -> Self {
|
2021-03-04 01:16:13 +08:00
|
|
|
Words {
|
2021-02-19 22:32:14 +08:00
|
|
|
ctx,
|
|
|
|
query_trees: Vec::default(),
|
2021-03-09 19:04:52 +08:00
|
|
|
candidates: None,
|
2021-02-19 22:32:14 +08:00
|
|
|
bucket_candidates: RoaringBitmap::new(),
|
2021-03-23 22:25:46 +08:00
|
|
|
parent,
|
2021-02-19 22:32:14 +08:00
|
|
|
candidates_cache: HashMap::default(),
|
2021-03-04 01:16:13 +08:00
|
|
|
}
|
2021-02-19 22:32:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t> Criterion for Words<'t> {
|
2021-03-06 18:28:22 +08:00
|
|
|
#[logging_timer::time("Words::{}")]
|
2021-04-14 18:18:13 +08:00
|
|
|
fn next(&mut self, wdcache: &mut WordDerivationsCache) -> anyhow::Result<Option<CriterionResult>> {
|
2021-02-19 22:32:14 +08:00
|
|
|
loop {
|
2021-02-24 22:37:37 +08:00
|
|
|
debug!("Words at iteration {} ({:?})", self.query_trees.len(), self.candidates);
|
|
|
|
|
2021-02-19 22:32:14 +08:00
|
|
|
match (self.query_trees.pop(), &mut self.candidates) {
|
2021-03-09 19:04:52 +08:00
|
|
|
(query_tree, Some(candidates)) if candidates.is_empty() => {
|
2021-02-19 22:32:14 +08:00
|
|
|
self.query_trees = Vec::new();
|
2021-03-01 21:03:12 +08:00
|
|
|
return Ok(Some(CriterionResult {
|
|
|
|
query_tree,
|
2021-03-09 19:04:52 +08:00
|
|
|
candidates: self.candidates.take(),
|
2021-03-01 21:03:12 +08:00
|
|
|
bucket_candidates: take(&mut self.bucket_candidates),
|
|
|
|
}));
|
2021-02-19 22:32:14 +08:00
|
|
|
},
|
2021-03-09 19:04:52 +08:00
|
|
|
(Some(qt), Some(candidates)) => {
|
2021-04-14 18:18:13 +08:00
|
|
|
let mut found_candidates = resolve_query_tree(self.ctx, &qt, &mut self.candidates_cache, wdcache)?;
|
2021-02-24 22:59:19 +08:00
|
|
|
found_candidates.intersect_with(&candidates);
|
|
|
|
candidates.difference_with(&found_candidates);
|
|
|
|
|
|
|
|
return Ok(Some(CriterionResult {
|
|
|
|
query_tree: Some(qt),
|
2021-03-09 19:04:52 +08:00
|
|
|
candidates: Some(found_candidates),
|
2021-03-23 22:25:46 +08:00
|
|
|
bucket_candidates: take(&mut self.bucket_candidates),
|
2021-02-24 22:59:19 +08:00
|
|
|
}));
|
|
|
|
},
|
2021-03-09 19:04:52 +08:00
|
|
|
(Some(qt), None) => {
|
2021-02-19 22:32:14 +08:00
|
|
|
return Ok(Some(CriterionResult {
|
|
|
|
query_tree: Some(qt),
|
2021-03-09 19:04:52 +08:00
|
|
|
candidates: None,
|
2021-03-23 22:25:46 +08:00
|
|
|
bucket_candidates: take(&mut self.bucket_candidates),
|
2021-02-19 22:32:14 +08:00
|
|
|
}));
|
|
|
|
},
|
2021-03-09 19:04:52 +08:00
|
|
|
(None, Some(_)) => {
|
|
|
|
let candidates = self.candidates.take();
|
2021-02-19 22:32:14 +08:00
|
|
|
return Ok(Some(CriterionResult {
|
|
|
|
query_tree: None,
|
|
|
|
candidates: candidates.clone(),
|
2021-03-09 19:04:52 +08:00
|
|
|
bucket_candidates: candidates.unwrap_or_default(),
|
2021-02-19 22:32:14 +08:00
|
|
|
}));
|
|
|
|
},
|
2021-03-09 19:04:52 +08:00
|
|
|
(None, None) => {
|
2021-03-23 22:25:46 +08:00
|
|
|
match self.parent.next(wdcache)? {
|
|
|
|
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 22:32:14 +08: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],
|
|
|
|
}
|
|
|
|
}
|