feat: Introduce filtering methods for Distinct/QueryBuilder

This commit is contained in:
Clément Renault 2018-12-29 20:16:29 +01:00
parent a0a11faee5
commit e2a9dbc404
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
3 changed files with 46 additions and 19 deletions

View File

@ -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<D>
@ -71,7 +71,7 @@ where D: Deref<Target=DB>
Ok(())
}
pub fn query_builder(&self) -> Result<QueryBuilder<D>, Box<Error>> {
pub fn query_builder(&self) -> Result<QueryBuilder<D, FilterFunc<D>>, Box<Error>> {
QueryBuilder::new(self)
}

View File

@ -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 {

View File

@ -34,14 +34,17 @@ fn split_whitespace_automatons(query: &str) -> Vec<DfaExt> {
automatons
}
pub struct QueryBuilder<'a, D>
pub type FilterFunc<D> = fn(DocumentId, &DatabaseView<D>) -> bool;
pub struct QueryBuilder<'a, D, FI>
where D: Deref<Target=DB>
{
view: &'a DatabaseView<D>,
criteria: Criteria<D>,
filter: Option<FI>,
}
impl<'a, D> QueryBuilder<'a, D>
impl<'a, D> QueryBuilder<'a, D, FilterFunc<D>>
where D: Deref<Target=DB>
{
pub fn new(view: &'a DatabaseView<D>) -> Result<Self, Box<Error>> {
@ -49,19 +52,27 @@ where D: Deref<Target=DB>
}
}
impl<'a, D> QueryBuilder<'a, D>
where D: Deref<Target=DB>
impl<'a, D, FI> QueryBuilder<'a, D, FI>
where D: Deref<Target=DB>,
{
pub fn with_criteria(view: &'a DatabaseView<D>, criteria: Criteria<D>) -> Result<Self, Box<Error>> {
Ok(QueryBuilder { view, criteria })
Ok(QueryBuilder { view, criteria, filter: None })
}
pub fn criteria(&mut self, criteria: Criteria<D>) -> &mut Self {
self.criteria = criteria;
self
pub fn with_filter<F>(self, function: F) -> QueryBuilder<'a, D, F>
where F: Fn(DocumentId, &DatabaseView<D>) -> bool,
{
QueryBuilder {
view: self.view,
criteria: self.criteria,
filter: Some(function)
}
}
pub fn with_distinct<F>(self, function: F, size: usize) -> DistinctQueryBuilder<'a, D, F> {
pub fn with_distinct<F, K>(self, function: F, size: usize) -> DistinctQueryBuilder<'a, D, FI, F>
where F: Fn(DocumentId, &DatabaseView<D>) -> Option<K>,
K: Hash + Eq,
{
DistinctQueryBuilder {
inner: self,
function: function,
@ -109,8 +120,9 @@ where D: Deref<Target=DB>
}
}
impl<'a, D> QueryBuilder<'a, D>
impl<'a, D, FI> QueryBuilder<'a, D, FI>
where D: Deref<Target=DB>,
FI: Fn(DocumentId, &DatabaseView<D>) -> bool,
{
pub fn query(&self, query: &str, range: Range<usize>) -> Vec<Document> {
let mut documents = self.query_all(query);
@ -152,20 +164,35 @@ where D: Deref<Target=DB>,
}
}
pub struct DistinctQueryBuilder<'a, D, F>
pub struct DistinctQueryBuilder<'a, D, FI, FD>
where D: Deref<Target=DB>
{
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<Target=DB>,
F: Fn(DocumentId, &DatabaseView<D>) -> Option<K>,
{
pub fn with_filter<F>(self, function: F) -> DistinctQueryBuilder<'a, D, F, FD>
where F: Fn(DocumentId, &DatabaseView<D>) -> 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<Target=DB>,
FI: Fn(DocumentId, &DatabaseView<D>) -> bool,
FD: Fn(DocumentId, &DatabaseView<D>) -> Option<K>,
K: Hash + Eq,
{
pub fn query(&self, query: &str, range: Range<usize>) -> Vec<Document> {
pub fn query(self, query: &str, range: Range<usize>) -> Vec<Document> {
let mut documents = self.inner.query_all(query);
let mut groups = vec![documents.as_mut_slice()];
let mut key_cache = HashMap::new();