mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-30 00:34:26 +01:00
feat: Simplify the steps to query the database
This commit is contained in:
parent
8bee31078d
commit
9342290afe
@ -7,7 +7,9 @@ use serde::de::DeserializeOwned;
|
|||||||
|
|
||||||
use crate::database::{retrieve_data_schema, DocumentKey, DocumentKeyAttr};
|
use crate::database::{retrieve_data_schema, DocumentKey, DocumentKeyAttr};
|
||||||
use crate::database::deserializer::Deserializer;
|
use crate::database::deserializer::Deserializer;
|
||||||
|
use crate::rank::criterion::Criterion;
|
||||||
use crate::database::schema::Schema;
|
use crate::database::schema::Schema;
|
||||||
|
use crate::rank::QueryBuilder;
|
||||||
use crate::DocumentId;
|
use crate::DocumentId;
|
||||||
|
|
||||||
pub struct DatabaseView<'a> {
|
pub struct DatabaseView<'a> {
|
||||||
@ -21,14 +23,26 @@ impl<'a> DatabaseView<'a> {
|
|||||||
Ok(DatabaseView { snapshot, schema })
|
Ok(DatabaseView { snapshot, schema })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn schema(&self) -> &Schema {
|
||||||
|
&self.schema
|
||||||
|
}
|
||||||
|
|
||||||
pub fn into_snapshot(self) -> Snapshot<&'a DB> {
|
pub fn into_snapshot(self) -> Snapshot<&'a DB> {
|
||||||
self.snapshot
|
self.snapshot
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn snapshot(&self) -> &Snapshot<&'a DB> {
|
||||||
|
&self.snapshot
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get(&self, key: &[u8]) -> Result<Option<DBVector>, Box<Error>> {
|
pub fn get(&self, key: &[u8]) -> Result<Option<DBVector>, Box<Error>> {
|
||||||
Ok(self.snapshot.get(key)?)
|
Ok(self.snapshot.get(key)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn query_builder(&self) -> Result<QueryBuilder<Box<dyn Criterion>>, Box<Error>> {
|
||||||
|
QueryBuilder::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO create an enum error type
|
// TODO create an enum error type
|
||||||
pub fn retrieve_document<D>(&self, id: DocumentId) -> Result<D, Box<Error>>
|
pub fn retrieve_document<D>(&self, id: DocumentId) -> Result<D, Box<Error>>
|
||||||
where D: DeserializeOwned
|
where D: DeserializeOwned
|
||||||
|
@ -8,7 +8,7 @@ use rocksdb::{DB, DBVector, MergeOperands, SeekKey};
|
|||||||
use rocksdb::rocksdb::{Writable, Snapshot};
|
use rocksdb::rocksdb::{Writable, Snapshot};
|
||||||
|
|
||||||
pub use self::document_key::{DocumentKey, DocumentKeyAttr};
|
pub use self::document_key::{DocumentKey, DocumentKeyAttr};
|
||||||
pub use self::database_view::DatabaseView;
|
pub use self::database_view::{DatabaseView, DocumentIter};
|
||||||
use self::blob::positive::PositiveBlob;
|
use self::blob::positive::PositiveBlob;
|
||||||
use self::update::Update;
|
use self::update::Update;
|
||||||
use self::schema::Schema;
|
use self::schema::Schema;
|
||||||
@ -126,7 +126,7 @@ impl fmt::Debug for Database {
|
|||||||
let mut iter = self.0.iter();
|
let mut iter = self.0.iter();
|
||||||
iter.seek(SeekKey::Start);
|
iter.seek(SeekKey::Start);
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for (key, value) in &mut iter {
|
for (key, _value) in &mut iter {
|
||||||
if !first { write!(f, ", ")?; }
|
if !first { write!(f, ", ")?; }
|
||||||
first = false;
|
first = false;
|
||||||
let key = String::from_utf8_lossy(&key);
|
let key = String::from_utf8_lossy(&key);
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
@ -107,7 +107,7 @@ struct Serializer<'a, B> {
|
|||||||
macro_rules! forward_to_unserializable_type {
|
macro_rules! forward_to_unserializable_type {
|
||||||
($($ty:ident => $se_method:ident,)*) => {
|
($($ty:ident => $se_method:ident,)*) => {
|
||||||
$(
|
$(
|
||||||
fn $se_method(self, v: $ty) -> Result<Self::Ok, Self::Error> {
|
fn $se_method(self, _v: $ty) -> Result<Self::Ok, Self::Error> {
|
||||||
Err(SerializerError::UnserializableType { name: "$ty" })
|
Err(SerializerError::UnserializableType { name: "$ty" })
|
||||||
}
|
}
|
||||||
)*
|
)*
|
||||||
@ -145,11 +145,11 @@ where B: TokenizerBuilder
|
|||||||
f64 => serialize_f64,
|
f64 => serialize_f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
|
fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> {
|
||||||
Err(SerializerError::UnserializableType { name: "str" })
|
Err(SerializerError::UnserializableType { name: "str" })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
|
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
|
||||||
Err(SerializerError::UnserializableType { name: "&[u8]" })
|
Err(SerializerError::UnserializableType { name: "&[u8]" })
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -375,7 +375,7 @@ where B: TokenizerBuilder
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
|
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
|
||||||
Err(SerializerError::UnserializableType { name: "&[u8]" })
|
Err(SerializerError::UnserializableType { name: "&[u8]" })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use group_by::GroupBy;
|
use group_by::GroupBy;
|
||||||
use crate::Match;
|
|
||||||
use crate::rank::{match_query_index, Document};
|
use crate::rank::{match_query_index, Document};
|
||||||
use crate::rank::criterion::Criterion;
|
use crate::rank::criterion::Criterion;
|
||||||
|
use crate::database::DatabaseView;
|
||||||
|
use crate::Match;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn contains_exact(matches: &[Match]) -> bool {
|
fn contains_exact(matches: &[Match]) -> bool {
|
||||||
@ -18,7 +21,7 @@ fn number_exact_matches(matches: &[Match]) -> usize {
|
|||||||
pub struct Exact;
|
pub struct Exact;
|
||||||
|
|
||||||
impl Criterion for Exact {
|
impl Criterion for Exact {
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document) -> Ordering {
|
fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView) -> Ordering {
|
||||||
let lhs = number_exact_matches(&lhs.matches);
|
let lhs = number_exact_matches(&lhs.matches);
|
||||||
let rhs = number_exact_matches(&rhs.matches);
|
let rhs = number_exact_matches(&rhs.matches);
|
||||||
|
|
||||||
|
@ -7,6 +7,8 @@ mod exact;
|
|||||||
|
|
||||||
use std::vec;
|
use std::vec;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
use crate::database::DatabaseView;
|
||||||
use crate::rank::Document;
|
use crate::rank::Document;
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
@ -20,31 +22,31 @@ pub use self::{
|
|||||||
|
|
||||||
pub trait Criterion {
|
pub trait Criterion {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document) -> Ordering;
|
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn eq(&self, lhs: &Document, rhs: &Document) -> bool {
|
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool {
|
||||||
self.evaluate(lhs, rhs) == Ordering::Equal
|
self.evaluate(lhs, rhs, view) == Ordering::Equal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: Criterion + ?Sized> Criterion for &'a T {
|
impl<'a, T: Criterion + ?Sized> Criterion for &'a T {
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document) -> Ordering {
|
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering {
|
||||||
(**self).evaluate(lhs, rhs)
|
(**self).evaluate(lhs, rhs, view)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eq(&self, lhs: &Document, rhs: &Document) -> bool {
|
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool {
|
||||||
(**self).eq(lhs, rhs)
|
(**self).eq(lhs, rhs, view)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Criterion + ?Sized> Criterion for Box<T> {
|
impl<T: Criterion + ?Sized> Criterion for Box<T> {
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document) -> Ordering {
|
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> Ordering {
|
||||||
(**self).evaluate(lhs, rhs)
|
(**self).evaluate(lhs, rhs, view)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eq(&self, lhs: &Document, rhs: &Document) -> bool {
|
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView) -> bool {
|
||||||
(**self).eq(lhs, rhs)
|
(**self).eq(lhs, rhs, view)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +54,7 @@ impl<T: Criterion + ?Sized> Criterion for Box<T> {
|
|||||||
pub struct DocumentId;
|
pub struct DocumentId;
|
||||||
|
|
||||||
impl Criterion for DocumentId {
|
impl Criterion for DocumentId {
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document) -> Ordering {
|
fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView) -> Ordering {
|
||||||
lhs.id.cmp(&rhs.id)
|
lhs.id.cmp(&rhs.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use group_by::GroupBy;
|
use group_by::GroupBy;
|
||||||
use crate::Match;
|
|
||||||
use crate::rank::{match_query_index, Document};
|
use crate::rank::{match_query_index, Document};
|
||||||
use crate::rank::criterion::Criterion;
|
use crate::rank::criterion::Criterion;
|
||||||
|
use crate::database::DatabaseView;
|
||||||
|
use crate::Match;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn number_of_query_words(matches: &[Match]) -> usize {
|
fn number_of_query_words(matches: &[Match]) -> usize {
|
||||||
@ -13,7 +16,7 @@ fn number_of_query_words(matches: &[Match]) -> usize {
|
|||||||
pub struct NumberOfWords;
|
pub struct NumberOfWords;
|
||||||
|
|
||||||
impl Criterion for NumberOfWords {
|
impl Criterion for NumberOfWords {
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document) -> Ordering {
|
fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView) -> Ordering {
|
||||||
let lhs = number_of_query_words(&lhs.matches);
|
let lhs = number_of_query_words(&lhs.matches);
|
||||||
let rhs = number_of_query_words(&rhs.matches);
|
let rhs = number_of_query_words(&rhs.matches);
|
||||||
|
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use group_by::GroupBy;
|
use group_by::GroupBy;
|
||||||
use crate::Match;
|
|
||||||
use crate::rank::{match_query_index, Document};
|
use crate::rank::{match_query_index, Document};
|
||||||
use crate::rank::criterion::Criterion;
|
use crate::rank::criterion::Criterion;
|
||||||
|
use crate::database::DatabaseView;
|
||||||
|
use crate::Match;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn sum_matches_typos(matches: &[Match]) -> i8 {
|
fn sum_matches_typos(matches: &[Match]) -> i8 {
|
||||||
@ -23,7 +26,7 @@ fn sum_matches_typos(matches: &[Match]) -> i8 {
|
|||||||
pub struct SumOfTypos;
|
pub struct SumOfTypos;
|
||||||
|
|
||||||
impl Criterion for SumOfTypos {
|
impl Criterion for SumOfTypos {
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document) -> Ordering {
|
fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView) -> Ordering {
|
||||||
let lhs = sum_matches_typos(&lhs.matches);
|
let lhs = sum_matches_typos(&lhs.matches);
|
||||||
let rhs = sum_matches_typos(&rhs.matches);
|
let rhs = sum_matches_typos(&rhs.matches);
|
||||||
|
|
||||||
@ -64,7 +67,9 @@ mod tests {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(SumOfTypos.evaluate(&doc0, &doc1), Ordering::Less);
|
let lhs = sum_matches_typos(&doc0.matches);
|
||||||
|
let rhs = sum_matches_typos(&doc1.matches);
|
||||||
|
assert_eq!(lhs.cmp(&rhs), Ordering::Less);
|
||||||
}
|
}
|
||||||
|
|
||||||
// typing: "bouton manchette"
|
// typing: "bouton manchette"
|
||||||
@ -94,7 +99,9 @@ mod tests {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(SumOfTypos.evaluate(&doc0, &doc1), Ordering::Less);
|
let lhs = sum_matches_typos(&doc0.matches);
|
||||||
|
let rhs = sum_matches_typos(&doc1.matches);
|
||||||
|
assert_eq!(lhs.cmp(&rhs), Ordering::Less);
|
||||||
}
|
}
|
||||||
|
|
||||||
// typing: "bouton manchztte"
|
// typing: "bouton manchztte"
|
||||||
@ -124,6 +131,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(SumOfTypos.evaluate(&doc0, &doc1), Ordering::Equal);
|
let lhs = sum_matches_typos(&doc0.matches);
|
||||||
|
let rhs = sum_matches_typos(&doc1.matches);
|
||||||
|
assert_eq!(lhs.cmp(&rhs), Ordering::Equal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use group_by::GroupBy;
|
use group_by::GroupBy;
|
||||||
use crate::Match;
|
|
||||||
|
use crate::database::DatabaseView;
|
||||||
use crate::rank::{match_query_index, Document};
|
use crate::rank::{match_query_index, Document};
|
||||||
use crate::rank::criterion::Criterion;
|
use crate::rank::criterion::Criterion;
|
||||||
|
use crate::Match;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn sum_matches_attributes(matches: &[Match]) -> u8 {
|
fn sum_matches_attributes(matches: &[Match]) -> u8 {
|
||||||
@ -17,7 +20,7 @@ fn sum_matches_attributes(matches: &[Match]) -> u8 {
|
|||||||
pub struct SumOfWordsAttribute;
|
pub struct SumOfWordsAttribute;
|
||||||
|
|
||||||
impl Criterion for SumOfWordsAttribute {
|
impl Criterion for SumOfWordsAttribute {
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document) -> Ordering {
|
fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView) -> Ordering {
|
||||||
let lhs = sum_matches_attributes(&lhs.matches);
|
let lhs = sum_matches_attributes(&lhs.matches);
|
||||||
let rhs = sum_matches_attributes(&rhs.matches);
|
let rhs = sum_matches_attributes(&rhs.matches);
|
||||||
|
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use group_by::GroupBy;
|
use group_by::GroupBy;
|
||||||
use crate::Match;
|
|
||||||
|
use crate::database::DatabaseView;
|
||||||
use crate::rank::{match_query_index, Document};
|
use crate::rank::{match_query_index, Document};
|
||||||
use crate::rank::criterion::Criterion;
|
use crate::rank::criterion::Criterion;
|
||||||
|
use crate::Match;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn sum_matches_attribute_index(matches: &[Match]) -> u32 {
|
fn sum_matches_attribute_index(matches: &[Match]) -> u32 {
|
||||||
@ -17,7 +20,7 @@ fn sum_matches_attribute_index(matches: &[Match]) -> u32 {
|
|||||||
pub struct SumOfWordsPosition;
|
pub struct SumOfWordsPosition;
|
||||||
|
|
||||||
impl Criterion for SumOfWordsPosition {
|
impl Criterion for SumOfWordsPosition {
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document) -> Ordering {
|
fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView) -> Ordering {
|
||||||
let lhs = sum_matches_attribute_index(&lhs.matches);
|
let lhs = sum_matches_attribute_index(&lhs.matches);
|
||||||
let rhs = sum_matches_attribute_index(&rhs.matches);
|
let rhs = sum_matches_attribute_index(&rhs.matches);
|
||||||
|
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
use std::cmp::{self, Ordering};
|
use std::cmp::{self, Ordering};
|
||||||
|
|
||||||
use group_by::GroupBy;
|
use group_by::GroupBy;
|
||||||
use crate::Match;
|
|
||||||
use crate::rank::{match_query_index, Document};
|
use crate::rank::{match_query_index, Document};
|
||||||
use crate::rank::criterion::Criterion;
|
use crate::rank::criterion::Criterion;
|
||||||
|
use crate::database::DatabaseView;
|
||||||
|
use crate::Match;
|
||||||
|
|
||||||
const MAX_DISTANCE: u32 = 8;
|
const MAX_DISTANCE: u32 = 8;
|
||||||
|
|
||||||
@ -47,7 +50,7 @@ fn matches_proximity(matches: &[Match]) -> u32 {
|
|||||||
pub struct WordsProximity;
|
pub struct WordsProximity;
|
||||||
|
|
||||||
impl Criterion for WordsProximity {
|
impl Criterion for WordsProximity {
|
||||||
fn evaluate(&self, lhs: &Document, rhs: &Document) -> Ordering {
|
fn evaluate(&self, lhs: &Document, rhs: &Document, _: &DatabaseView) -> Ordering {
|
||||||
let lhs = matches_proximity(&lhs.matches);
|
let lhs = matches_proximity(&lhs.matches);
|
||||||
let rhs = matches_proximity(&rhs.matches);
|
let rhs = matches_proximity(&rhs.matches);
|
||||||
|
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
use std::ops::{Deref, Range};
|
|
||||||
use std::{mem, vec, str};
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
use std::ops::Range;
|
||||||
|
use std::{mem, vec, str};
|
||||||
|
|
||||||
use ::rocksdb::rocksdb::{DB, Snapshot};
|
|
||||||
use group_by::GroupByMut;
|
use group_by::GroupByMut;
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
use fst::Streamer;
|
use fst::Streamer;
|
||||||
@ -13,16 +12,10 @@ use crate::rank::criterion::{self, Criterion};
|
|||||||
use crate::rank::distinct_map::DistinctMap;
|
use crate::rank::distinct_map::DistinctMap;
|
||||||
use crate::database::retrieve_data_index;
|
use crate::database::retrieve_data_index;
|
||||||
use crate::database::blob::PositiveBlob;
|
use crate::database::blob::PositiveBlob;
|
||||||
|
use crate::database::DatabaseView;
|
||||||
use crate::{Match, DocumentId};
|
use crate::{Match, DocumentId};
|
||||||
use crate::rank::Document;
|
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> {
|
fn split_whitespace_automatons(query: &str) -> Vec<DfaExt> {
|
||||||
let mut automatons = Vec::new();
|
let mut automatons = Vec::new();
|
||||||
for query in query.split_whitespace().map(str::to_lowercase) {
|
for query in query.split_whitespace().map(str::to_lowercase) {
|
||||||
@ -32,24 +25,22 @@ fn split_whitespace_automatons(query: &str) -> Vec<DfaExt> {
|
|||||||
automatons
|
automatons
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct QueryBuilder<T: Deref<Target=DB>, C> {
|
pub struct QueryBuilder<'a, C> {
|
||||||
snapshot: Snapshot<T>,
|
view: &'a DatabaseView<'a>,
|
||||||
blob: PositiveBlob,
|
blob: PositiveBlob,
|
||||||
criteria: Vec<C>,
|
criteria: Vec<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Deref<Target=DB>> QueryBuilder<T, Box<dyn Criterion>> {
|
impl<'a> QueryBuilder<'a, Box<dyn Criterion>> {
|
||||||
pub fn new(snapshot: Snapshot<T>) -> Result<Self, Box<Error>> {
|
pub fn new(view: &'a DatabaseView<'a>) -> Result<Self, Box<Error>> {
|
||||||
QueryBuilder::with_criteria(snapshot, criterion::default())
|
QueryBuilder::with_criteria(view, criterion::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, C> QueryBuilder<T, C>
|
impl<'a, C> QueryBuilder<'a, C> {
|
||||||
where T: Deref<Target=DB>,
|
pub fn with_criteria(view: &'a DatabaseView<'a>, criteria: Vec<C>) -> Result<Self, Box<Error>> {
|
||||||
{
|
let blob = retrieve_data_index(view.snapshot())?;
|
||||||
pub fn with_criteria(snapshot: Snapshot<T>, criteria: Vec<C>) -> Result<Self, Box<Error>> {
|
Ok(QueryBuilder { view, blob, criteria })
|
||||||
let blob = retrieve_data_index(&snapshot)?;
|
|
||||||
Ok(QueryBuilder { snapshot, blob, criteria })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn criteria(&mut self, criteria: Vec<C>) -> &mut Self {
|
pub fn criteria(&mut self, criteria: Vec<C>) -> &mut Self {
|
||||||
@ -57,7 +48,7 @@ where T: Deref<Target=DB>,
|
|||||||
self
|
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 {
|
DistinctQueryBuilder {
|
||||||
inner: self,
|
inner: self,
|
||||||
function: function,
|
function: function,
|
||||||
@ -105,23 +96,21 @@ where T: Deref<Target=DB>,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, C> QueryBuilder<T, C>
|
impl<'a, C> QueryBuilder<'a, C>
|
||||||
where T: Deref<Target=DB>,
|
where C: Criterion
|
||||||
C: Criterion,
|
|
||||||
{
|
{
|
||||||
pub fn query(&self, query: &str, limit: usize) -> Vec<Document> {
|
pub fn query(&self, query: &str, limit: usize) -> Vec<Document> {
|
||||||
let mut documents = self.query_all(query);
|
let mut documents = self.query_all(query);
|
||||||
let mut groups = vec![documents.as_mut_slice()];
|
let mut groups = vec![documents.as_mut_slice()];
|
||||||
|
let view = &self.view;
|
||||||
|
|
||||||
'group: for criterion in &self.criteria {
|
'group: for criterion in &self.criteria {
|
||||||
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
||||||
let mut computed = 0;
|
let mut computed = 0;
|
||||||
|
|
||||||
for group in tmp_groups {
|
for group in tmp_groups {
|
||||||
|
group.sort_unstable_by(|a, b| criterion.evaluate(a, b, view));
|
||||||
group.sort_unstable_by(|a, b| criterion.evaluate(a, b));
|
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b, view)) {
|
||||||
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b)) {
|
|
||||||
|
|
||||||
computed += group.len();
|
computed += group.len();
|
||||||
groups.push(group);
|
groups.push(group);
|
||||||
if computed >= limit { break 'group }
|
if computed >= limit { break 'group }
|
||||||
@ -134,41 +123,38 @@ where T: Deref<Target=DB>,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DistinctQueryBuilder<T: Deref<Target=DB>, F, C> {
|
pub struct DistinctQueryBuilder<'a, F, C> {
|
||||||
inner: QueryBuilder<T, C>,
|
inner: QueryBuilder<'a, C>,
|
||||||
function: F,
|
function: F,
|
||||||
size: usize,
|
size: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DocDatabase;
|
impl<'a, F, K, C> DistinctQueryBuilder<'a, F, C>
|
||||||
|
where F: Fn(DocumentId, &DatabaseView) -> Option<K>,
|
||||||
impl<T: Deref<Target=DB>, F, K, C> DistinctQueryBuilder<T, F, C>
|
|
||||||
where T: Deref<Target=DB>,
|
|
||||||
F: Fn(DocumentId, &DocDatabase) -> Option<K>,
|
|
||||||
K: Hash + Eq,
|
K: Hash + Eq,
|
||||||
C: Criterion,
|
C: Criterion,
|
||||||
{
|
{
|
||||||
pub fn query(&self, query: &str, range: Range<usize>) -> Vec<Document> {
|
pub fn query(&self, query: &str, range: Range<usize>) -> Vec<Document> {
|
||||||
let mut documents = self.inner.query_all(query);
|
let mut documents = self.inner.query_all(query);
|
||||||
let mut groups = vec![documents.as_mut_slice()];
|
let mut groups = vec![documents.as_mut_slice()];
|
||||||
|
let view = &self.inner.view;
|
||||||
|
|
||||||
for criterion in &self.inner.criteria {
|
for criterion in &self.inner.criteria {
|
||||||
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
||||||
|
|
||||||
for group in tmp_groups {
|
for group in tmp_groups {
|
||||||
group.sort_unstable_by(|a, b| criterion.evaluate(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)) {
|
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b, view)) {
|
||||||
groups.push(group);
|
groups.push(group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let doc_database = DocDatabase;
|
|
||||||
let mut out_documents = Vec::with_capacity(range.len());
|
let mut out_documents = Vec::with_capacity(range.len());
|
||||||
let mut seen = DistinctMap::new(self.size);
|
let mut seen = DistinctMap::new(self.size);
|
||||||
|
|
||||||
for document in documents {
|
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),
|
Some(key) => seen.digest(key),
|
||||||
None => seen.accept_without_key(),
|
None => seen.accept_without_key(),
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user