MeiliSearch/milli/src/search/criteria/words.rs

133 lines
5.0 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-02-25 16:34:29 +01:00
use super::{resolve_query_tree, Candidates, Criterion, CriterionResult, Context};
2021-02-19 15:32:14 +01:00
pub struct Words<'t> {
ctx: &'t dyn Context,
query_trees: Vec<Operation>,
candidates: Candidates,
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>,
) -> anyhow::Result<Self>
2021-02-19 15:32:14 +01:00
{
Ok(Words {
ctx,
query_trees: query_tree.map(explode_query_tree).unwrap_or_default(),
candidates: candidates.map_or_else(Candidates::default, Candidates::Allowed),
bucket_candidates: RoaringBitmap::new(),
parent: None,
candidates_cache: HashMap::default(),
})
}
pub fn new(
ctx: &'t dyn Context,
parent: Box<dyn Criterion + 't>,
) -> anyhow::Result<Self>
2021-02-19 15:32:14 +01:00
{
Ok(Words {
ctx,
query_trees: Vec::default(),
candidates: Candidates::default(),
bucket_candidates: RoaringBitmap::new(),
parent: Some(parent),
candidates_cache: HashMap::default(),
})
}
}
impl<'t> Criterion for Words<'t> {
fn next(&mut self) -> anyhow::Result<Option<CriterionResult>> {
use Candidates::{Allowed, Forbidden};
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) {
(query_tree, Allowed(candidates)) if candidates.is_empty() => {
2021-02-19 15:32:14 +01:00
self.query_trees = Vec::new();
return Ok(Some(CriterionResult {
query_tree,
candidates: take(&mut self.candidates).into_inner(),
bucket_candidates: take(&mut self.bucket_candidates),
}));
2021-02-19 15:32:14 +01:00
},
(Some(qt), Allowed(candidates)) => {
2021-02-25 16:34:29 +01:00
let mut found_candidates = resolve_query_tree(self.ctx, &qt, &mut self.candidates_cache)?;
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),
candidates: found_candidates,
bucket_candidates,
}));
},
(Some(qt), Forbidden(candidates)) => {
2021-02-25 16:34:29 +01:00
let mut found_candidates = resolve_query_tree(self.ctx, &qt, &mut self.candidates_cache)?;
found_candidates.difference_with(&candidates);
candidates.union_with(&found_candidates);
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),
candidates: found_candidates,
2021-02-19 15:32:14 +01:00
bucket_candidates,
}));
},
(None, Allowed(_)) => {
let candidates = take(&mut self.candidates).into_inner();
return Ok(Some(CriterionResult {
query_tree: None,
candidates: candidates.clone(),
bucket_candidates: candidates,
}));
},
(None, Forbidden(_)) => {
match self.parent.as_mut() {
Some(parent) => {
match parent.next()? {
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
self.query_trees = query_tree.map(explode_query_tree).unwrap_or_default();
self.candidates = Candidates::Allowed(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],
}
}