111 lines
4.0 KiB
Rust
Raw Normal View History

2021-02-19 15:32:14 +01:00
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;
2021-06-16 18:33:33 +02:00
use super::{resolve_query_tree, Context, Criterion, CriterionParameters, CriterionResult};
2021-02-19 15:32:14 +01:00
use crate::search::query_tree::Operation;
use crate::Result;
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-05-05 20:46:56 +02:00
bucket_candidates: Option<RoaringBitmap>,
2021-05-10 12:33:37 +02:00
filtered_candidates: Option<RoaringBitmap>,
2021-03-23 15:25:46 +01:00
parent: Box<dyn Criterion + 't>,
2021-02-19 15:32:14 +01:00
}
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-05-05 20:46:56 +02:00
bucket_candidates: None,
2021-03-23 15:25:46 +01:00
parent,
2021-05-10 12:33:37 +02:00
filtered_candidates: None,
}
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) -> 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-05-05 20:46:56 +02:00
match self.query_trees.pop() {
Some(query_tree) => {
let candidates = match self.candidates.as_mut() {
2021-05-10 12:33:37 +02:00
Some(allowed_candidates) => {
2021-06-16 18:33:33 +02:00
let mut candidates =
resolve_query_tree(self.ctx, &query_tree, params.wdcache)?;
2021-05-05 20:46:56 +02:00
candidates &= &*allowed_candidates;
*allowed_candidates -= &candidates;
Some(candidates)
2021-06-16 18:33:33 +02:00
}
2021-05-10 12:33:37 +02:00
None => None,
2021-05-05 20:46:56 +02:00
};
let bucket_candidates = match self.bucket_candidates.as_mut() {
Some(bucket_candidates) => Some(take(bucket_candidates)),
None => None,
};
return Ok(Some(CriterionResult {
2021-05-05 20:46:56 +02:00
query_tree: Some(query_tree),
candidates,
2021-05-10 12:33:37 +02:00
filtered_candidates: self.filtered_candidates.clone(),
2021-05-05 20:46:56 +02:00
bucket_candidates,
2021-02-19 15:32:14 +01:00
}));
2021-06-16 18:33:33 +02:00
}
None => match self.parent.next(params)? {
Some(CriterionResult {
query_tree: Some(query_tree),
candidates,
filtered_candidates,
bucket_candidates,
}) => {
self.query_trees = explode_query_tree(query_tree);
self.candidates = candidates;
self.filtered_candidates = filtered_candidates;
2021-05-05 20:46:56 +02:00
2021-06-16 18:33:33 +02:00
self.bucket_candidates =
match (self.bucket_candidates.take(), bucket_candidates) {
2021-05-05 20:46:56 +02:00
(Some(self_bc), Some(parent_bc)) => Some(self_bc | parent_bc),
(self_bc, parent_bc) => self_bc.or(parent_bc),
};
2021-02-19 15:32:14 +01:00
}
2021-06-16 18:33:33 +02:00
Some(CriterionResult {
query_tree: None,
candidates,
filtered_candidates,
bucket_candidates,
}) => {
return Ok(Some(CriterionResult {
query_tree: None,
candidates,
filtered_candidates,
bucket_candidates,
}));
}
None => return Ok(None),
2021-02-19 15:32:14 +01:00
},
}
}
}
}
fn explode_query_tree(query_tree: Operation) -> Vec<Operation> {
match query_tree {
Operation::Or(true, ops) => ops,
otherwise => vec![otherwise],
}
}