125 lines
4.6 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;
2021-04-14 12:18:13 +02:00
use super::{resolve_query_tree, Criterion, CriterionResult, Context, WordDerivationsCache};
2021-02-19 15:32:14 +01:00
pub struct Words<'t> {
ctx: &'t dyn Context,
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,
parent: Option<Box<dyn Criterion + 't>>,
candidates_cache: HashMap<(Operation, u8), RoaringBitmap>,
}
impl<'t> Words<'t> {
pub fn initial(
ctx: &'t dyn Context,
query_tree: Option<Operation>,
candidates: Option<RoaringBitmap>,
) -> Self
2021-02-19 15:32:14 +01:00
{
Words {
2021-02-19 15:32:14 +01:00
ctx,
query_trees: query_tree.map(explode_query_tree).unwrap_or_default(),
2021-03-09 12:04:52 +01:00
candidates,
2021-02-19 15:32:14 +01:00
bucket_candidates: RoaringBitmap::new(),
parent: None,
candidates_cache: HashMap::default(),
}
2021-02-19 15:32:14 +01:00
}
pub fn new(ctx: &'t dyn Context, 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(),
parent: Some(parent),
candidates_cache: HashMap::default(),
}
2021-02-19 15:32:14 +01:00
}
}
impl<'t> Criterion for Words<'t> {
#[logging_timer::time("Words::{}")]
2021-04-14 12:18:13 +02:00
fn next(&mut self, wdcache: &mut WordDerivationsCache) -> anyhow::Result<Option<CriterionResult>> {
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)) => {
2021-04-14 12:18:13 +02:00
let mut found_candidates = resolve_query_tree(self.ctx, &qt, &mut self.candidates_cache, wdcache)?;
found_candidates.intersect_with(&candidates);
candidates.difference_with(&found_candidates);
2021-02-19 15:32:14 +01:00
let bucket_candidates = match self.parent {
Some(_) => take(&mut self.bucket_candidates),
None => found_candidates.clone(),
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: Some(found_candidates),
bucket_candidates,
}));
},
2021-03-09 12:04:52 +01:00
(Some(qt), None) => {
let bucket_candidates = match self.parent {
Some(_) => take(&mut self.bucket_candidates),
2021-03-09 12:04:52 +01:00
None => RoaringBitmap::new(),
};
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-02-19 15:32:14 +01:00
bucket_candidates,
}));
},
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) => {
2021-02-19 15:32:14 +01:00
match self.parent.as_mut() {
Some(parent) => {
2021-04-14 12:18:13 +02:00
match parent.next(wdcache)? {
2021-02-19 15:32:14 +01:00
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
self.query_trees = query_tree.map(explode_query_tree).unwrap_or_default();
2021-03-09 12:04:52 +01:00
self.candidates = candidates;
2021-02-25 17:28:20 +01:00
self.bucket_candidates.union_with(&bucket_candidates);
2021-02-19 15:32:14 +01:00
},
None => return Ok(None),
}
},
None => return Ok(None),
}
},
}
}
}
}
fn explode_query_tree(query_tree: Operation) -> Vec<Operation> {
match query_tree {
Operation::Or(true, ops) => ops,
otherwise => vec![otherwise],
}
}