feat: Introduce the revisited SortBy criterion

This commit is contained in:
Clément Renault 2019-02-02 14:42:12 +01:00
parent 2e905bac08
commit 5efbc5ceb3
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
2 changed files with 24 additions and 23 deletions

View File

@ -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,
};

View File

@ -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::<TimeOnly>::new())
/// .add(SortBy::<TimeOnly>::new(&view))
/// .add(DocumentId);
///
/// let criterion = builder.build();
///
/// ```
pub struct SortBy<T> {
pub struct SortBy<'a, T, D>
where D: Deref<Target=DB> + Send + Sync,
T: Send + Sync
{
view: &'a DatabaseView<D>,
_phantom: marker::PhantomData<T>,
}
impl<T> SortBy<T> {
pub fn new() -> Self {
SortBy::default()
}
}
impl<T> Default for SortBy<T> {
fn default() -> SortBy<T> {
SortBy { _phantom: marker::PhantomData }
}
}
impl<T, D> Criterion<D> for SortBy<T>
where D: Deref<Target=DB>,
T: DeserializeOwned + Ord,
impl<'a, T, D> SortBy<'a, T, D>
where D: Deref<Target=DB> + Send + Sync,
T: Send + Sync
{
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument, view: &DatabaseView<D>) -> Ordering {
let lhs = match view.document_by_id::<T>(lhs.id) {
pub fn new(view: &'a DatabaseView<D>) -> Self {
SortBy { view, _phantom: marker::PhantomData }
}
}
impl<'a, T, D> Criterion for SortBy<'a, T, D>
where D: Deref<Target=DB> + Send + Sync,
T: DeserializeOwned + Ord + Send + Sync,
{
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
let lhs = match self.view.document_by_id::<T>(lhs.id) {
Ok(doc) => Some(doc),
Err(e) => { eprintln!("{}", e); None },
};
let rhs = match view.document_by_id::<T>(rhs.id) {
let rhs = match self.view.document_by_id::<T>(rhs.id) {
Ok(doc) => Some(doc),
Err(e) => { eprintln!("{}", e); None },
};