mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-07-31 19:00:00 +00:00
feat: Simplify the steps to query the database
This commit is contained in:
@ -1,9 +1,8 @@
|
||||
use std::ops::{Deref, Range};
|
||||
use std::{mem, vec, str};
|
||||
use std::error::Error;
|
||||
use std::hash::Hash;
|
||||
use std::ops::Range;
|
||||
use std::{mem, vec, str};
|
||||
|
||||
use ::rocksdb::rocksdb::{DB, Snapshot};
|
||||
use group_by::GroupByMut;
|
||||
use hashbrown::HashMap;
|
||||
use fst::Streamer;
|
||||
@ -13,16 +12,10 @@ use crate::rank::criterion::{self, Criterion};
|
||||
use crate::rank::distinct_map::DistinctMap;
|
||||
use crate::database::retrieve_data_index;
|
||||
use crate::database::blob::PositiveBlob;
|
||||
use crate::database::DatabaseView;
|
||||
use crate::{Match, DocumentId};
|
||||
use crate::rank::Document;
|
||||
|
||||
fn clamp_range<T: Copy + Ord>(range: Range<T>, big: Range<T>) -> Range<T> {
|
||||
Range {
|
||||
start: range.start.min(big.end).max(big.start),
|
||||
end: range.end.min(big.end).max(big.start),
|
||||
}
|
||||
}
|
||||
|
||||
fn split_whitespace_automatons(query: &str) -> Vec<DfaExt> {
|
||||
let mut automatons = Vec::new();
|
||||
for query in query.split_whitespace().map(str::to_lowercase) {
|
||||
@ -32,24 +25,22 @@ fn split_whitespace_automatons(query: &str) -> Vec<DfaExt> {
|
||||
automatons
|
||||
}
|
||||
|
||||
pub struct QueryBuilder<T: Deref<Target=DB>, C> {
|
||||
snapshot: Snapshot<T>,
|
||||
pub struct QueryBuilder<'a, C> {
|
||||
view: &'a DatabaseView<'a>,
|
||||
blob: PositiveBlob,
|
||||
criteria: Vec<C>,
|
||||
}
|
||||
|
||||
impl<T: Deref<Target=DB>> QueryBuilder<T, Box<dyn Criterion>> {
|
||||
pub fn new(snapshot: Snapshot<T>) -> Result<Self, Box<Error>> {
|
||||
QueryBuilder::with_criteria(snapshot, criterion::default())
|
||||
impl<'a> QueryBuilder<'a, Box<dyn Criterion>> {
|
||||
pub fn new(view: &'a DatabaseView<'a>) -> Result<Self, Box<Error>> {
|
||||
QueryBuilder::with_criteria(view, criterion::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, C> QueryBuilder<T, C>
|
||||
where T: Deref<Target=DB>,
|
||||
{
|
||||
pub fn with_criteria(snapshot: Snapshot<T>, criteria: Vec<C>) -> Result<Self, Box<Error>> {
|
||||
let blob = retrieve_data_index(&snapshot)?;
|
||||
Ok(QueryBuilder { snapshot, blob, criteria })
|
||||
impl<'a, C> QueryBuilder<'a, C> {
|
||||
pub fn with_criteria(view: &'a DatabaseView<'a>, criteria: Vec<C>) -> Result<Self, Box<Error>> {
|
||||
let blob = retrieve_data_index(view.snapshot())?;
|
||||
Ok(QueryBuilder { view, blob, criteria })
|
||||
}
|
||||
|
||||
pub fn criteria(&mut self, criteria: Vec<C>) -> &mut Self {
|
||||
@ -57,7 +48,7 @@ where T: Deref<Target=DB>,
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_distinct<F>(self, function: F, size: usize) -> DistinctQueryBuilder<T, F, C> {
|
||||
pub fn with_distinct<F>(self, function: F, size: usize) -> DistinctQueryBuilder<'a, F, C> {
|
||||
DistinctQueryBuilder {
|
||||
inner: self,
|
||||
function: function,
|
||||
@ -105,23 +96,21 @@ where T: Deref<Target=DB>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, C> QueryBuilder<T, C>
|
||||
where T: Deref<Target=DB>,
|
||||
C: Criterion,
|
||||
impl<'a, C> QueryBuilder<'a, C>
|
||||
where C: Criterion
|
||||
{
|
||||
pub fn query(&self, query: &str, limit: usize) -> Vec<Document> {
|
||||
let mut documents = self.query_all(query);
|
||||
let mut groups = vec![documents.as_mut_slice()];
|
||||
let view = &self.view;
|
||||
|
||||
'group: for criterion in &self.criteria {
|
||||
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
||||
let mut computed = 0;
|
||||
|
||||
for group in tmp_groups {
|
||||
|
||||
group.sort_unstable_by(|a, b| criterion.evaluate(a, b));
|
||||
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b)) {
|
||||
|
||||
group.sort_unstable_by(|a, b| criterion.evaluate(a, b, view));
|
||||
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b, view)) {
|
||||
computed += group.len();
|
||||
groups.push(group);
|
||||
if computed >= limit { break 'group }
|
||||
@ -134,41 +123,38 @@ where T: Deref<Target=DB>,
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DistinctQueryBuilder<T: Deref<Target=DB>, F, C> {
|
||||
inner: QueryBuilder<T, C>,
|
||||
pub struct DistinctQueryBuilder<'a, F, C> {
|
||||
inner: QueryBuilder<'a, C>,
|
||||
function: F,
|
||||
size: usize,
|
||||
}
|
||||
|
||||
pub struct DocDatabase;
|
||||
|
||||
impl<T: Deref<Target=DB>, F, K, C> DistinctQueryBuilder<T, F, C>
|
||||
where T: Deref<Target=DB>,
|
||||
F: Fn(DocumentId, &DocDatabase) -> Option<K>,
|
||||
impl<'a, F, K, C> DistinctQueryBuilder<'a, F, C>
|
||||
where F: Fn(DocumentId, &DatabaseView) -> Option<K>,
|
||||
K: Hash + Eq,
|
||||
C: Criterion,
|
||||
{
|
||||
pub fn query(&self, query: &str, range: Range<usize>) -> Vec<Document> {
|
||||
let mut documents = self.inner.query_all(query);
|
||||
let mut groups = vec![documents.as_mut_slice()];
|
||||
let view = &self.inner.view;
|
||||
|
||||
for criterion in &self.inner.criteria {
|
||||
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
||||
|
||||
for group in tmp_groups {
|
||||
group.sort_unstable_by(|a, b| criterion.evaluate(a, b));
|
||||
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b)) {
|
||||
group.sort_unstable_by(|a, b| criterion.evaluate(a, b, view));
|
||||
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b, view)) {
|
||||
groups.push(group);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let doc_database = DocDatabase;
|
||||
let mut out_documents = Vec::with_capacity(range.len());
|
||||
let mut seen = DistinctMap::new(self.size);
|
||||
|
||||
for document in documents {
|
||||
let accepted = match (self.function)(document.id, &doc_database) {
|
||||
let accepted = match (self.function)(document.id, &self.inner.view) {
|
||||
Some(key) => seen.digest(key),
|
||||
None => seen.accept_without_key(),
|
||||
};
|
||||
|
Reference in New Issue
Block a user