2021-02-24 22:37:37 +08:00
|
|
|
use log::debug;
|
2021-02-19 22:32:14 +08:00
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
2021-06-17 00:33:33 +08:00
|
|
|
use super::{resolve_query_tree, Context, Criterion, CriterionParameters, CriterionResult};
|
2022-12-08 01:29:25 +08:00
|
|
|
use crate::search::criteria::InitialCandidates;
|
2021-02-19 22:32:14 +08:00
|
|
|
use crate::search::query_tree::Operation;
|
2021-06-14 22:46:19 +08:00
|
|
|
use crate::Result;
|
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>,
|
2022-12-08 01:29:25 +08:00
|
|
|
initial_candidates: Option<InitialCandidates>,
|
2021-05-10 18:33:37 +08:00
|
|
|
filtered_candidates: Option<RoaringBitmap>,
|
2021-03-23 22:25:46 +08:00
|
|
|
parent: Box<dyn Criterion + 't>,
|
2021-02-19 22:32:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-12-08 01:29:25 +08:00
|
|
|
initial_candidates: None,
|
2021-03-23 22:25:46 +08:00
|
|
|
parent,
|
2021-05-10 18:33:37 +08:00
|
|
|
filtered_candidates: None,
|
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-06-14 22:46:19 +08:00
|
|
|
fn next(&mut self, params: &mut CriterionParameters) -> Result<Option<CriterionResult>> {
|
2021-04-29 00:01:23 +08:00
|
|
|
// 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 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-05-06 02:46:56 +08:00
|
|
|
match self.query_trees.pop() {
|
|
|
|
Some(query_tree) => {
|
|
|
|
let candidates = match self.candidates.as_mut() {
|
2021-05-10 18:33:37 +08:00
|
|
|
Some(allowed_candidates) => {
|
2021-06-17 00:33:33 +08:00
|
|
|
let mut candidates =
|
|
|
|
resolve_query_tree(self.ctx, &query_tree, params.wdcache)?;
|
2021-05-06 02:46:56 +08:00
|
|
|
candidates &= &*allowed_candidates;
|
|
|
|
*allowed_candidates -= &candidates;
|
|
|
|
Some(candidates)
|
2021-06-17 00:33:33 +08:00
|
|
|
}
|
2021-05-10 18:33:37 +08:00
|
|
|
None => None,
|
2021-05-06 02:46:56 +08:00
|
|
|
};
|
|
|
|
|
2022-12-08 01:29:25 +08:00
|
|
|
let initial_candidates = self.initial_candidates.clone();
|
2021-02-24 22:59:19 +08:00
|
|
|
|
|
|
|
return Ok(Some(CriterionResult {
|
2021-05-06 02:46:56 +08:00
|
|
|
query_tree: Some(query_tree),
|
|
|
|
candidates,
|
2021-05-10 18:33:37 +08:00
|
|
|
filtered_candidates: self.filtered_candidates.clone(),
|
2022-12-08 01:29:25 +08:00
|
|
|
initial_candidates,
|
2021-02-19 22:32:14 +08:00
|
|
|
}));
|
2021-06-17 00:33:33 +08:00
|
|
|
}
|
|
|
|
None => match self.parent.next(params)? {
|
|
|
|
Some(CriterionResult {
|
|
|
|
query_tree: Some(query_tree),
|
|
|
|
candidates,
|
|
|
|
filtered_candidates,
|
2022-12-08 01:29:25 +08:00
|
|
|
initial_candidates,
|
2021-06-17 00:33:33 +08:00
|
|
|
}) => {
|
|
|
|
self.query_trees = explode_query_tree(query_tree);
|
|
|
|
self.candidates = candidates;
|
|
|
|
self.filtered_candidates = filtered_candidates;
|
2021-05-06 02:46:56 +08:00
|
|
|
|
2022-12-08 01:29:25 +08:00
|
|
|
self.initial_candidates =
|
|
|
|
match (self.initial_candidates.take(), initial_candidates) {
|
2022-12-13 17:56:38 +08:00
|
|
|
(Some(self_ic), Some(parent_ic)) => Some(self_ic | parent_ic),
|
|
|
|
(self_ic, parent_ic) => self_ic.or(parent_ic),
|
2021-05-06 02:46:56 +08:00
|
|
|
};
|
2021-02-19 22:32:14 +08:00
|
|
|
}
|
2021-06-17 00:33:33 +08:00
|
|
|
Some(CriterionResult {
|
|
|
|
query_tree: None,
|
|
|
|
candidates,
|
|
|
|
filtered_candidates,
|
2022-12-08 01:29:25 +08:00
|
|
|
initial_candidates,
|
2021-06-17 00:33:33 +08:00
|
|
|
}) => {
|
|
|
|
return Ok(Some(CriterionResult {
|
|
|
|
query_tree: None,
|
|
|
|
candidates,
|
|
|
|
filtered_candidates,
|
2022-12-08 01:29:25 +08:00
|
|
|
initial_candidates,
|
2021-06-17 00:33:33 +08:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
None => return Ok(None),
|
2021-02-19 22:32:14 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn explode_query_tree(query_tree: Operation) -> Vec<Operation> {
|
|
|
|
match query_tree {
|
|
|
|
Operation::Or(true, ops) => ops,
|
|
|
|
otherwise => vec![otherwise],
|
|
|
|
}
|
|
|
|
}
|