mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-23 02:27:40 +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::cmp::Ordering;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
use rocksdb::DB;
|
|
||||||
|
|
||||||
use crate::rank::criterion::Criterion;
|
use crate::rank::criterion::Criterion;
|
||||||
use crate::database::DatabaseView;
|
use crate::rank::RawDocument;
|
||||||
use crate::rank::Document;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct DocumentId;
|
pub struct DocumentId;
|
||||||
|
|
||||||
impl<D> Criterion<D> for DocumentId
|
impl Criterion for DocumentId {
|
||||||
where D: Deref<Target=DB>
|
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
||||||
{
|
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView<D>) -> Ordering {
|
|
||||||
lhs.id.cmp(&rhs.id)
|
lhs.id.cmp(&rhs.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,60 +19,51 @@ pub use self::{
|
|||||||
sum_of_words_attribute::SumOfWordsAttribute,
|
sum_of_words_attribute::SumOfWordsAttribute,
|
||||||
sum_of_words_position::SumOfWordsPosition,
|
sum_of_words_position::SumOfWordsPosition,
|
||||||
exact::Exact,
|
exact::Exact,
|
||||||
sort_by::SortBy,
|
// sort_by::SortBy,
|
||||||
document_id::DocumentId,
|
document_id::DocumentId,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait Criterion<D>
|
pub trait Criterion: Send + Sync {
|
||||||
where D: Deref<Target=DB>
|
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering;
|
||||||
{
|
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> Ordering;
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> bool {
|
fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool {
|
||||||
self.evaluate(lhs, rhs, view) == Ordering::Equal
|
self.evaluate(lhs, rhs) == Ordering::Equal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, D, T: Criterion<D> + ?Sized> Criterion<D> for &'a T
|
impl<'a, T: Criterion + ?Sized + Send + Sync> Criterion for &'a T {
|
||||||
where D: Deref<Target=DB>
|
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
||||||
{
|
(**self).evaluate(lhs, rhs)
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> Ordering {
|
|
||||||
(**self).evaluate(lhs, rhs, view)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> bool {
|
fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool {
|
||||||
(**self).eq(lhs, rhs, view)
|
(**self).eq(lhs, rhs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D, T: Criterion<D> + ?Sized> Criterion<D> for Box<T>
|
impl<T: Criterion + ?Sized> Criterion for Box<T> {
|
||||||
where D: Deref<Target=DB>
|
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
||||||
{
|
(**self).evaluate(lhs, rhs)
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> Ordering {
|
|
||||||
(**self).evaluate(lhs, rhs, view)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> bool {
|
fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool {
|
||||||
(**self).eq(lhs, rhs, view)
|
(**self).eq(lhs, rhs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct CriteriaBuilder<D>
|
pub struct CriteriaBuilder {
|
||||||
where D: Deref<Target=DB>
|
inner: Vec<Box<dyn Criterion>>
|
||||||
{
|
|
||||||
inner: Vec<Box<dyn Criterion<D>>>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D> CriteriaBuilder<D>
|
impl CriteriaBuilder
|
||||||
where D: Deref<Target=DB>
|
|
||||||
{
|
{
|
||||||
pub fn new() -> CriteriaBuilder<D> {
|
pub fn new() -> CriteriaBuilder {
|
||||||
CriteriaBuilder { inner: Vec::new() }
|
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) }
|
CriteriaBuilder { inner: Vec::with_capacity(capacity) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,33 +71,29 @@ where D: Deref<Target=DB>
|
|||||||
self.inner.reserve(additional)
|
self.inner.reserve(additional)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add<C>(mut self, criterion: C) -> CriteriaBuilder<D>
|
pub fn add<C>(mut self, criterion: C) -> CriteriaBuilder
|
||||||
where C: 'static + Criterion<D>,
|
where C: 'static + Criterion,
|
||||||
{
|
{
|
||||||
self.push(criterion);
|
self.push(criterion);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push<C>(&mut self, criterion: C)
|
pub fn push<C>(&mut self, criterion: C)
|
||||||
where C: 'static + Criterion<D>,
|
where C: 'static + Criterion,
|
||||||
{
|
{
|
||||||
self.inner.push(Box::new(criterion));
|
self.inner.push(Box::new(criterion));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(self) -> Criteria<D> {
|
pub fn build(self) -> Criteria {
|
||||||
Criteria { inner: self.inner }
|
Criteria { inner: self.inner }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Criteria<D>
|
pub struct Criteria {
|
||||||
where D: Deref<Target=DB>
|
inner: Vec<Box<dyn Criterion>>,
|
||||||
{
|
|
||||||
inner: Vec<Box<dyn Criterion<D>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D> Default for Criteria<D>
|
impl Default for Criteria {
|
||||||
where D: Deref<Target=DB>
|
|
||||||
{
|
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
CriteriaBuilder::with_capacity(7)
|
CriteriaBuilder::with_capacity(7)
|
||||||
.add(SumOfTypos)
|
.add(SumOfTypos)
|
||||||
@ -120,10 +107,8 @@ where D: Deref<Target=DB>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D> AsRef<[Box<dyn Criterion<D>>]> for Criteria<D>
|
impl AsRef<[Box<dyn Criterion>]> for Criteria {
|
||||||
where D: Deref<Target=DB>
|
fn as_ref(&self) -> &[Box<dyn Criterion>] {
|
||||||
{
|
|
||||||
fn as_ref(&self) -> &[Box<dyn Criterion<D>>] {
|
|
||||||
&self.inner
|
&self.inner
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ impl<T, D> Criterion<D> for SortBy<T>
|
|||||||
where D: Deref<Target=DB>,
|
where D: Deref<Target=DB>,
|
||||||
T: DeserializeOwned + Ord,
|
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) {
|
let lhs = match view.document_by_id::<T>(lhs.id) {
|
||||||
Ok(doc) => Some(doc),
|
Ok(doc) => Some(doc),
|
||||||
Err(e) => { eprintln!("{}", e); None },
|
Err(e) => { eprintln!("{}", e); None },
|
||||||
|
@ -43,7 +43,7 @@ pub struct QueryBuilder<'a, D, FI>
|
|||||||
where D: Deref<Target=DB>
|
where D: Deref<Target=DB>
|
||||||
{
|
{
|
||||||
view: &'a DatabaseView<D>,
|
view: &'a DatabaseView<D>,
|
||||||
criteria: Criteria<D>,
|
criteria: Criteria,
|
||||||
filter: Option<FI>,
|
filter: Option<FI>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ where D: Deref<Target=DB>
|
|||||||
impl<'a, D, FI> QueryBuilder<'a, D, FI>
|
impl<'a, D, FI> QueryBuilder<'a, D, FI>
|
||||||
where D: Deref<Target=DB>,
|
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 })
|
Ok(QueryBuilder { view, criteria, filter: None })
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,12 +165,12 @@ where D: Deref<Target=DB>,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (elapsed, ()) = elapsed::measure_time(|| {
|
let (elapsed, _) = measure_time(|| {
|
||||||
group.sort_unstable_by(|a, b| criterion.evaluate(a, b, view));
|
group.par_sort_unstable_by(|a, b| criterion.evaluate(a, b));
|
||||||
});
|
});
|
||||||
info!("criterion {} sort took {}", ci, elapsed);
|
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();
|
documents_seen += group.len();
|
||||||
groups.push(group);
|
groups.push(group);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user