diff --git a/src/database/database_view.rs b/src/database/database_view.rs index 6e8d32a78..b37d84042 100644 --- a/src/database/database_view.rs +++ b/src/database/database_view.rs @@ -12,7 +12,7 @@ use crate::database::{retrieve_data_schema, retrieve_data_index}; use crate::database::blob::positive::PositiveBlob; use crate::database::deserializer::Deserializer; use crate::database::schema::Schema; -use crate::rank::QueryBuilder; +use crate::rank::{QueryBuilder, FilterFunc}; use crate::DocumentId; pub struct DatabaseView @@ -71,7 +71,7 @@ where D: Deref Ok(()) } - pub fn query_builder(&self) -> Result, Box> { + pub fn query_builder(&self) -> Result>, Box> { QueryBuilder::new(self) } diff --git a/src/rank/mod.rs b/src/rank/mod.rs index 81b9463e2..4d1b6b1ea 100644 --- a/src/rank/mod.rs +++ b/src/rank/mod.rs @@ -4,7 +4,7 @@ mod distinct_map; use crate::{Match, DocumentId}; -pub use self::query_builder::{QueryBuilder, DistinctQueryBuilder}; +pub use self::query_builder::{FilterFunc, QueryBuilder, DistinctQueryBuilder}; #[inline] fn match_query_index(a: &Match, b: &Match) -> bool { diff --git a/src/rank/query_builder.rs b/src/rank/query_builder.rs index fe0904160..586eb7c88 100644 --- a/src/rank/query_builder.rs +++ b/src/rank/query_builder.rs @@ -34,14 +34,17 @@ fn split_whitespace_automatons(query: &str) -> Vec { automatons } -pub struct QueryBuilder<'a, D> +pub type FilterFunc = fn(DocumentId, &DatabaseView) -> bool; + +pub struct QueryBuilder<'a, D, FI> where D: Deref { view: &'a DatabaseView, criteria: Criteria, + filter: Option, } -impl<'a, D> QueryBuilder<'a, D> +impl<'a, D> QueryBuilder<'a, D, FilterFunc> where D: Deref { pub fn new(view: &'a DatabaseView) -> Result> { @@ -49,19 +52,27 @@ where D: Deref } } -impl<'a, D> QueryBuilder<'a, D> -where D: Deref +impl<'a, D, FI> QueryBuilder<'a, D, FI> +where D: Deref, { pub fn with_criteria(view: &'a DatabaseView, criteria: Criteria) -> Result> { - Ok(QueryBuilder { view, criteria }) + Ok(QueryBuilder { view, criteria, filter: None }) } - pub fn criteria(&mut self, criteria: Criteria) -> &mut Self { - self.criteria = criteria; - self + pub fn with_filter(self, function: F) -> QueryBuilder<'a, D, F> + where F: Fn(DocumentId, &DatabaseView) -> bool, + { + QueryBuilder { + view: self.view, + criteria: self.criteria, + filter: Some(function) + } } - pub fn with_distinct(self, function: F, size: usize) -> DistinctQueryBuilder<'a, D, F> { + pub fn with_distinct(self, function: F, size: usize) -> DistinctQueryBuilder<'a, D, FI, F> + where F: Fn(DocumentId, &DatabaseView) -> Option, + K: Hash + Eq, + { DistinctQueryBuilder { inner: self, function: function, @@ -109,8 +120,9 @@ where D: Deref } } -impl<'a, D> QueryBuilder<'a, D> +impl<'a, D, FI> QueryBuilder<'a, D, FI> where D: Deref, + FI: Fn(DocumentId, &DatabaseView) -> bool, { pub fn query(&self, query: &str, range: Range) -> Vec { let mut documents = self.query_all(query); @@ -152,20 +164,35 @@ where D: Deref, } } -pub struct DistinctQueryBuilder<'a, D, F> +pub struct DistinctQueryBuilder<'a, D, FI, FD> where D: Deref { - inner: QueryBuilder<'a, D>, - function: F, + inner: QueryBuilder<'a, D, FI>, + function: FD, size: usize, } -impl<'a, D, F, K> DistinctQueryBuilder<'a, D, F> +impl<'a, D, FI, FD> DistinctQueryBuilder<'a, D, FI, FD> where D: Deref, - F: Fn(DocumentId, &DatabaseView) -> Option, +{ + pub fn with_filter(self, function: F) -> DistinctQueryBuilder<'a, D, F, FD> + where F: Fn(DocumentId, &DatabaseView) -> bool, + { + DistinctQueryBuilder { + inner: self.inner.with_filter(function), + function: self.function, + size: self.size + } + } +} + +impl<'a, D, FI, FD, K> DistinctQueryBuilder<'a, D, FI, FD> +where D: Deref, + FI: Fn(DocumentId, &DatabaseView) -> bool, + FD: Fn(DocumentId, &DatabaseView) -> Option, K: Hash + Eq, { - pub fn query(&self, query: &str, range: Range) -> Vec { + pub fn query(self, query: &str, range: Range) -> Vec { let mut documents = self.inner.query_all(query); let mut groups = vec![documents.as_mut_slice()]; let mut key_cache = HashMap::new();