From 4c0ad5f96460aa5f9482b0651e356582a426748f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Sat, 2 Feb 2019 14:23:18 +0100 Subject: [PATCH] feat: Simplify the Criterion Trait by removing the DatabaseView param --- src/rank/criterion/document_id.rs | 11 ++--- src/rank/criterion/mod.rs | 73 ++++++++++++------------------- src/rank/criterion/sort_by.rs | 2 +- src/rank/query_builder.rs | 10 ++--- 4 files changed, 38 insertions(+), 58 deletions(-) diff --git a/src/rank/criterion/document_id.rs b/src/rank/criterion/document_id.rs index 2d8ca34c2..a388cf2de 100644 --- a/src/rank/criterion/document_id.rs +++ b/src/rank/criterion/document_id.rs @@ -1,19 +1,14 @@ use std::cmp::Ordering; use std::ops::Deref; -use rocksdb::DB; - use crate::rank::criterion::Criterion; -use crate::database::DatabaseView; -use crate::rank::Document; +use crate::rank::RawDocument; #[derive(Debug, Clone, Copy)] pub struct DocumentId; -impl Criterion for DocumentId -where D: Deref -{ - fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView) -> Ordering { +impl Criterion for DocumentId { + fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering { lhs.id.cmp(&rhs.id) } } diff --git a/src/rank/criterion/mod.rs b/src/rank/criterion/mod.rs index c7c547851..46b41ea0f 100644 --- a/src/rank/criterion/mod.rs +++ b/src/rank/criterion/mod.rs @@ -19,60 +19,51 @@ pub use self::{ sum_of_words_attribute::SumOfWordsAttribute, sum_of_words_position::SumOfWordsPosition, exact::Exact, - sort_by::SortBy, + // sort_by::SortBy, document_id::DocumentId, }; -pub trait Criterion -where D: Deref -{ - fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering; +pub trait Criterion: Send + Sync { + fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering; #[inline] - fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool { - self.evaluate(lhs, rhs, view) == Ordering::Equal + fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool { + self.evaluate(lhs, rhs) == Ordering::Equal } } -impl<'a, D, T: Criterion + ?Sized> Criterion for &'a T -where D: Deref -{ - fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering { - (**self).evaluate(lhs, rhs, view) +impl<'a, T: Criterion + ?Sized + Send + Sync> Criterion for &'a T { + fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering { + (**self).evaluate(lhs, rhs) } - fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool { - (**self).eq(lhs, rhs, view) + fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool { + (**self).eq(lhs, rhs) } } -impl + ?Sized> Criterion for Box -where D: Deref -{ - fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering { - (**self).evaluate(lhs, rhs, view) +impl Criterion for Box { + fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering { + (**self).evaluate(lhs, rhs) } - fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool { - (**self).eq(lhs, rhs, view) + fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool { + (**self).eq(lhs, rhs) } } #[derive(Default)] -pub struct CriteriaBuilder -where D: Deref -{ - inner: Vec>> +pub struct CriteriaBuilder { + inner: Vec> } -impl CriteriaBuilder -where D: Deref +impl CriteriaBuilder { - pub fn new() -> CriteriaBuilder { + pub fn new() -> CriteriaBuilder { CriteriaBuilder { inner: Vec::new() } } - pub fn with_capacity(capacity: usize) -> CriteriaBuilder { + pub fn with_capacity(capacity: usize) -> CriteriaBuilder { CriteriaBuilder { inner: Vec::with_capacity(capacity) } } @@ -80,33 +71,29 @@ where D: Deref self.inner.reserve(additional) } - pub fn add(mut self, criterion: C) -> CriteriaBuilder - where C: 'static + Criterion, + pub fn add(mut self, criterion: C) -> CriteriaBuilder + where C: 'static + Criterion, { self.push(criterion); self } pub fn push(&mut self, criterion: C) - where C: 'static + Criterion, + where C: 'static + Criterion, { self.inner.push(Box::new(criterion)); } - pub fn build(self) -> Criteria { + pub fn build(self) -> Criteria { Criteria { inner: self.inner } } } -pub struct Criteria -where D: Deref -{ - inner: Vec>>, +pub struct Criteria { + inner: Vec>, } -impl Default for Criteria -where D: Deref -{ +impl Default for Criteria { fn default() -> Self { CriteriaBuilder::with_capacity(7) .add(SumOfTypos) @@ -120,10 +107,8 @@ where D: Deref } } -impl AsRef<[Box>]> for Criteria -where D: Deref -{ - fn as_ref(&self) -> &[Box>] { +impl AsRef<[Box]> for Criteria { + fn as_ref(&self) -> &[Box] { &self.inner } } diff --git a/src/rank/criterion/sort_by.rs b/src/rank/criterion/sort_by.rs index 53b8bcac1..1604a492a 100644 --- a/src/rank/criterion/sort_by.rs +++ b/src/rank/criterion/sort_by.rs @@ -66,7 +66,7 @@ impl Criterion for SortBy where D: Deref, T: DeserializeOwned + Ord, { - fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering { + fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument, view: &DatabaseView) -> Ordering { let lhs = match view.document_by_id::(lhs.id) { Ok(doc) => Some(doc), Err(e) => { eprintln!("{}", e); None }, diff --git a/src/rank/query_builder.rs b/src/rank/query_builder.rs index 11fc75498..ff160da7f 100644 --- a/src/rank/query_builder.rs +++ b/src/rank/query_builder.rs @@ -43,7 +43,7 @@ pub struct QueryBuilder<'a, D, FI> where D: Deref { view: &'a DatabaseView, - criteria: Criteria, + criteria: Criteria, filter: Option, } @@ -58,7 +58,7 @@ where D: Deref impl<'a, D, FI> QueryBuilder<'a, D, FI> where D: Deref, { - pub fn with_criteria(view: &'a DatabaseView, criteria: Criteria) -> Result> { + pub fn with_criteria(view: &'a DatabaseView, criteria: Criteria) -> Result> { Ok(QueryBuilder { view, criteria, filter: None }) } @@ -165,12 +165,12 @@ where D: Deref, continue; } - let (elapsed, ()) = elapsed::measure_time(|| { - group.sort_unstable_by(|a, b| criterion.evaluate(a, b, view)); + let (elapsed, _) = measure_time(|| { + group.par_sort_unstable_by(|a, b| criterion.evaluate(a, b)); }); info!("criterion {} sort took {}", ci, elapsed); - for group in group.binary_group_by_mut(|a, b| criterion.eq(a, b, view)) { + for group in group.binary_group_by_mut(|a, b| criterion.eq(a, b)) { documents_seen += group.len(); groups.push(group);