fix: Improve the bucket sort algorithm

This commit is contained in:
Clément Renault 2018-12-07 11:53:17 +01:00
parent 731ed11153
commit 2c3d71dd8f
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
3 changed files with 12 additions and 7 deletions

View File

@ -121,7 +121,7 @@ impl Index {
let snapshot = self.database.snapshot(); let snapshot = self.database.snapshot();
let builder = QueryBuilder::new(snapshot)?; let builder = QueryBuilder::new(snapshot)?;
let documents = builder.query(query, 0..20); let documents = builder.query(query, 20);
Ok(documents) Ok(documents)
} }

View File

@ -1,10 +1,10 @@
pub mod criterion; pub mod criterion;
mod ranked_stream; mod query_builder;
mod distinct_map; mod distinct_map;
use crate::{Match, DocumentId}; use crate::{Match, DocumentId};
pub use self::ranked_stream::{QueryBuilder, DistinctQueryBuilder}; pub use self::query_builder::{QueryBuilder, DistinctQueryBuilder};
#[inline] #[inline]
fn match_query_index(a: &Match, b: &Match) -> bool { fn match_query_index(a: &Match, b: &Match) -> bool {

View File

@ -109,23 +109,28 @@ impl<T, C> QueryBuilder<T, C>
where T: Deref<Target=DB>, where T: Deref<Target=DB>,
C: Criterion, C: Criterion,
{ {
pub fn query(&self, query: &str, range: Range<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()];
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;
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));
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b)) { for group in GroupByMut::new(group, |a, b| criterion.eq(a, b)) {
computed += group.len();
groups.push(group); groups.push(group);
if computed >= limit { break 'group }
} }
} }
} }
let range = clamp_range(range, 0..documents.len()); documents.truncate(limit);
documents[range].to_vec() documents
} }
} }