From 5efbc5ceb3fc4f95064d3f4848a8bce4839d4a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Sat, 2 Feb 2019 14:42:12 +0100 Subject: [PATCH] feat: Introduce the revisited SortBy criterion --- src/rank/criterion/mod.rs | 4 ++-- src/rank/criterion/sort_by.rs | 43 ++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/rank/criterion/mod.rs b/src/rank/criterion/mod.rs index 6272cf89d..07c6a37e1 100644 --- a/src/rank/criterion/mod.rs +++ b/src/rank/criterion/mod.rs @@ -4,7 +4,7 @@ mod words_proximity; mod sum_of_words_attribute; mod sum_of_words_position; mod exact; -// mod sort_by; +mod sort_by; mod document_id; use std::cmp::Ordering; @@ -17,7 +17,7 @@ pub use self::{ sum_of_words_attribute::SumOfWordsAttribute, sum_of_words_position::SumOfWordsPosition, exact::Exact, - // sort_by::SortBy, + sort_by::SortBy, document_id::DocumentId, }; diff --git a/src/rank/criterion/sort_by.rs b/src/rank/criterion/sort_by.rs index 1604a492a..d1c7abf8c 100644 --- a/src/rank/criterion/sort_by.rs +++ b/src/rank/criterion/sort_by.rs @@ -24,7 +24,7 @@ use crate::rank::RawDocument; /// /// # Example /// -/// ```no-test +/// ```ignore /// use serde_derive::Deserialize; /// use meilidb::rank::criterion::*; /// @@ -40,39 +40,40 @@ use crate::rank::RawDocument; /// .add(SumOfWordsAttribute) /// .add(SumOfWordsPosition) /// .add(Exact) -/// .add(SortBy::::new()) +/// .add(SortBy::::new(&view)) /// .add(DocumentId); /// /// let criterion = builder.build(); /// /// ``` -pub struct SortBy { +pub struct SortBy<'a, T, D> +where D: Deref + Send + Sync, + T: Send + Sync +{ + view: &'a DatabaseView, _phantom: marker::PhantomData, } -impl SortBy { - pub fn new() -> Self { - SortBy::default() - } -} - -impl Default for SortBy { - fn default() -> SortBy { - SortBy { _phantom: marker::PhantomData } - } -} - -impl Criterion for SortBy -where D: Deref, - T: DeserializeOwned + Ord, +impl<'a, T, D> SortBy<'a, T, D> +where D: Deref + Send + Sync, + T: Send + Sync { - fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument, view: &DatabaseView) -> Ordering { - let lhs = match view.document_by_id::(lhs.id) { + pub fn new(view: &'a DatabaseView) -> Self { + SortBy { view, _phantom: marker::PhantomData } + } +} + +impl<'a, T, D> Criterion for SortBy<'a, T, D> +where D: Deref + Send + Sync, + T: DeserializeOwned + Ord + Send + Sync, +{ + fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering { + let lhs = match self.view.document_by_id::(lhs.id) { Ok(doc) => Some(doc), Err(e) => { eprintln!("{}", e); None }, }; - let rhs = match view.document_by_id::(rhs.id) { + let rhs = match self.view.document_by_id::(rhs.id) { Ok(doc) => Some(doc), Err(e) => { eprintln!("{}", e); None }, };