mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-22 18:17:39 +08:00
feat: Simplify the Criterion Trait by removing the DatabaseView param
This commit is contained in:
parent
455cbf3bf4
commit
4c0ad5f964
@ -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<D> Criterion<D> for DocumentId
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView<D>) -> Ordering {
|
||||
impl Criterion for DocumentId {
|
||||
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
||||
lhs.id.cmp(&rhs.id)
|
||||
}
|
||||
}
|
||||
|
@ -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<D>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> Ordering;
|
||||
pub trait Criterion: Send + Sync {
|
||||
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering;
|
||||
|
||||
#[inline]
|
||||
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> 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<D> + ?Sized> Criterion<D> for &'a T
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> 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<D>) -> bool {
|
||||
(**self).eq(lhs, rhs, view)
|
||||
fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool {
|
||||
(**self).eq(lhs, rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D, T: Criterion<D> + ?Sized> Criterion<D> for Box<T>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> Ordering {
|
||||
(**self).evaluate(lhs, rhs, view)
|
||||
impl<T: Criterion + ?Sized> Criterion for Box<T> {
|
||||
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
||||
(**self).evaluate(lhs, rhs)
|
||||
}
|
||||
|
||||
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> bool {
|
||||
(**self).eq(lhs, rhs, view)
|
||||
fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool {
|
||||
(**self).eq(lhs, rhs)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct CriteriaBuilder<D>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
inner: Vec<Box<dyn Criterion<D>>>
|
||||
pub struct CriteriaBuilder {
|
||||
inner: Vec<Box<dyn Criterion>>
|
||||
}
|
||||
|
||||
impl<D> CriteriaBuilder<D>
|
||||
where D: Deref<Target=DB>
|
||||
impl CriteriaBuilder
|
||||
{
|
||||
pub fn new() -> CriteriaBuilder<D> {
|
||||
pub fn new() -> CriteriaBuilder {
|
||||
CriteriaBuilder { inner: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn with_capacity(capacity: usize) -> CriteriaBuilder<D> {
|
||||
pub fn with_capacity(capacity: usize) -> CriteriaBuilder {
|
||||
CriteriaBuilder { inner: Vec::with_capacity(capacity) }
|
||||
}
|
||||
|
||||
@ -80,33 +71,29 @@ where D: Deref<Target=DB>
|
||||
self.inner.reserve(additional)
|
||||
}
|
||||
|
||||
pub fn add<C>(mut self, criterion: C) -> CriteriaBuilder<D>
|
||||
where C: 'static + Criterion<D>,
|
||||
pub fn add<C>(mut self, criterion: C) -> CriteriaBuilder
|
||||
where C: 'static + Criterion,
|
||||
{
|
||||
self.push(criterion);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn push<C>(&mut self, criterion: C)
|
||||
where C: 'static + Criterion<D>,
|
||||
where C: 'static + Criterion,
|
||||
{
|
||||
self.inner.push(Box::new(criterion));
|
||||
}
|
||||
|
||||
pub fn build(self) -> Criteria<D> {
|
||||
pub fn build(self) -> Criteria {
|
||||
Criteria { inner: self.inner }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Criteria<D>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
inner: Vec<Box<dyn Criterion<D>>>,
|
||||
pub struct Criteria {
|
||||
inner: Vec<Box<dyn Criterion>>,
|
||||
}
|
||||
|
||||
impl<D> Default for Criteria<D>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
impl Default for Criteria {
|
||||
fn default() -> Self {
|
||||
CriteriaBuilder::with_capacity(7)
|
||||
.add(SumOfTypos)
|
||||
@ -120,10 +107,8 @@ where D: Deref<Target=DB>
|
||||
}
|
||||
}
|
||||
|
||||
impl<D> AsRef<[Box<dyn Criterion<D>>]> for Criteria<D>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
fn as_ref(&self) -> &[Box<dyn Criterion<D>>] {
|
||||
impl AsRef<[Box<dyn Criterion>]> for Criteria {
|
||||
fn as_ref(&self) -> &[Box<dyn Criterion>] {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ impl<T, D> Criterion<D> for SortBy<T>
|
||||
where D: Deref<Target=DB>,
|
||||
T: DeserializeOwned + Ord,
|
||||
{
|
||||
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> Ordering {
|
||||
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument, view: &DatabaseView<D>) -> Ordering {
|
||||
let lhs = match view.document_by_id::<T>(lhs.id) {
|
||||
Ok(doc) => Some(doc),
|
||||
Err(e) => { eprintln!("{}", e); None },
|
||||
|
@ -43,7 +43,7 @@ pub struct QueryBuilder<'a, D, FI>
|
||||
where D: Deref<Target=DB>
|
||||
{
|
||||
view: &'a DatabaseView<D>,
|
||||
criteria: Criteria<D>,
|
||||
criteria: Criteria,
|
||||
filter: Option<FI>,
|
||||
}
|
||||
|
||||
@ -58,7 +58,7 @@ 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>> {
|
||||
pub fn with_criteria(view: &'a DatabaseView<D>, criteria: Criteria) -> Result<Self, Box<Error>> {
|
||||
Ok(QueryBuilder { view, criteria, filter: None })
|
||||
}
|
||||
|
||||
@ -165,12 +165,12 @@ where D: Deref<Target=DB>,
|
||||
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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user