2018-12-16 21:21:06 +08:00
|
|
|
use std::{cmp, mem, vec, str, char};
|
|
|
|
use std::ops::{Deref, Range};
|
2018-11-28 02:11:33 +08:00
|
|
|
use std::error::Error;
|
2018-10-17 19:35:34 +08:00
|
|
|
use std::hash::Hash;
|
2018-12-16 21:21:06 +08:00
|
|
|
use std::rc::Rc;
|
2018-10-10 22:57:21 +08:00
|
|
|
|
2018-12-02 20:11:02 +08:00
|
|
|
use group_by::GroupByMut;
|
|
|
|
use hashbrown::HashMap;
|
|
|
|
use fst::Streamer;
|
2018-12-08 00:59:03 +08:00
|
|
|
use rocksdb::DB;
|
2018-10-10 22:57:21 +08:00
|
|
|
|
2018-11-28 02:11:33 +08:00
|
|
|
use crate::automaton::{self, DfaExt, AutomatonExt};
|
2018-12-16 21:21:41 +08:00
|
|
|
use crate::rank::distinct_map::{DistinctMap, BufferedDistinctMap};
|
2018-12-11 03:14:16 +08:00
|
|
|
use crate::rank::criterion::Criteria;
|
2018-12-07 21:41:06 +08:00
|
|
|
use crate::database::DatabaseView;
|
2018-10-17 19:35:34 +08:00
|
|
|
use crate::{Match, DocumentId};
|
2018-11-28 02:11:33 +08:00
|
|
|
use crate::rank::Document;
|
2018-10-10 22:57:21 +08:00
|
|
|
|
2018-11-28 02:11:33 +08:00
|
|
|
fn split_whitespace_automatons(query: &str) -> Vec<DfaExt> {
|
2018-12-11 21:49:45 +08:00
|
|
|
let has_end_whitespace = query.chars().last().map_or(false, char::is_whitespace);
|
2018-11-28 02:11:33 +08:00
|
|
|
let mut automatons = Vec::new();
|
2018-12-10 19:16:24 +08:00
|
|
|
let mut words = query.split_whitespace().map(str::to_lowercase).peekable();
|
2018-12-11 21:49:45 +08:00
|
|
|
|
2018-12-10 19:16:24 +08:00
|
|
|
while let Some(word) = words.next() {
|
2018-12-11 21:49:45 +08:00
|
|
|
let has_following_word = words.peek().is_some();
|
|
|
|
let lev = if has_following_word || has_end_whitespace {
|
|
|
|
automaton::build_dfa(&word)
|
|
|
|
} else {
|
|
|
|
automaton::build_prefix_dfa(&word)
|
2018-12-10 19:16:24 +08:00
|
|
|
};
|
2018-11-28 02:11:33 +08:00
|
|
|
automatons.push(lev);
|
|
|
|
}
|
2018-12-11 21:49:45 +08:00
|
|
|
|
2018-11-28 02:11:33 +08:00
|
|
|
automatons
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 03:14:16 +08:00
|
|
|
pub struct QueryBuilder<'a, D>
|
2018-12-08 00:59:03 +08:00
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
view: &'a DatabaseView<D>,
|
2018-12-11 03:14:16 +08:00
|
|
|
criteria: Criteria<D>,
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 03:14:16 +08:00
|
|
|
impl<'a, D> QueryBuilder<'a, D>
|
2018-12-08 00:59:03 +08:00
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
pub fn new(view: &'a DatabaseView<D>) -> Result<Self, Box<Error>> {
|
2018-12-11 03:14:16 +08:00
|
|
|
QueryBuilder::with_criteria(view, Criteria::default())
|
2018-11-28 02:11:33 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 22:57:21 +08:00
|
|
|
|
2018-12-11 03:14:16 +08:00
|
|
|
impl<'a, D> QueryBuilder<'a, D>
|
2018-12-08 00:59:03 +08:00
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
2018-12-11 03:14:16 +08:00
|
|
|
pub fn with_criteria(view: &'a DatabaseView<D>, criteria: Criteria<D>) -> Result<Self, Box<Error>> {
|
2018-12-07 23:20:12 +08:00
|
|
|
Ok(QueryBuilder { view, criteria })
|
2018-11-28 02:11:33 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 03:14:16 +08:00
|
|
|
pub fn criteria(&mut self, criteria: Criteria<D>) -> &mut Self {
|
2018-11-28 02:11:33 +08:00
|
|
|
self.criteria = criteria;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-12-11 03:14:16 +08:00
|
|
|
pub fn with_distinct<F>(self, function: F, size: usize) -> DistinctQueryBuilder<'a, D, F> {
|
2018-11-28 02:11:33 +08:00
|
|
|
DistinctQueryBuilder {
|
2018-11-29 00:12:24 +08:00
|
|
|
inner: self,
|
2018-11-28 02:11:33 +08:00
|
|
|
function: function,
|
|
|
|
size: size
|
2018-10-11 20:04:41 +08:00
|
|
|
}
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|
|
|
|
|
2018-11-28 02:11:33 +08:00
|
|
|
fn query_all(&self, query: &str) -> Vec<Document> {
|
|
|
|
let automatons = split_whitespace_automatons(query);
|
2018-11-29 00:12:24 +08:00
|
|
|
|
|
|
|
let mut stream = {
|
|
|
|
let mut op_builder = fst::map::OpBuilder::new();
|
|
|
|
for automaton in &automatons {
|
2018-12-07 23:20:12 +08:00
|
|
|
let stream = self.view.blob().as_map().search(automaton);
|
2018-11-29 00:12:24 +08:00
|
|
|
op_builder.push(stream);
|
|
|
|
}
|
|
|
|
op_builder.union()
|
|
|
|
};
|
|
|
|
|
2018-12-02 20:11:02 +08:00
|
|
|
let mut matches = HashMap::new();
|
2018-10-11 20:04:41 +08:00
|
|
|
|
2018-11-29 00:12:24 +08:00
|
|
|
while let Some((input, indexed_values)) = stream.next() {
|
2018-10-11 20:04:41 +08:00
|
|
|
for iv in indexed_values {
|
2018-11-28 02:11:33 +08:00
|
|
|
let automaton = &automatons[iv.index];
|
2018-11-29 00:12:24 +08:00
|
|
|
let distance = automaton.eval(input).to_u8();
|
|
|
|
let is_exact = distance == 0 && input.len() == automaton.query_len();
|
|
|
|
|
2018-12-07 23:20:12 +08:00
|
|
|
let doc_indexes = self.view.blob().as_indexes();
|
2018-12-02 01:37:21 +08:00
|
|
|
let doc_indexes = &doc_indexes[iv.value as usize];
|
2018-10-11 20:04:41 +08:00
|
|
|
|
2018-11-29 00:12:24 +08:00
|
|
|
for doc_index in doc_indexes {
|
2018-10-11 20:04:41 +08:00
|
|
|
let match_ = Match {
|
|
|
|
query_index: iv.index as u32,
|
|
|
|
distance: distance,
|
2018-10-17 19:35:34 +08:00
|
|
|
attribute: doc_index.attribute,
|
2018-10-11 20:04:41 +08:00
|
|
|
is_exact: is_exact,
|
2018-12-23 23:46:49 +08:00
|
|
|
word_area: doc_index.word_area,
|
2018-10-11 20:04:41 +08:00
|
|
|
};
|
2018-10-17 19:35:34 +08:00
|
|
|
matches.entry(doc_index.document_id).or_insert_with(Vec::new).push(match_);
|
2018-10-11 20:04:41 +08:00
|
|
|
}
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 22:42:19 +08:00
|
|
|
matches.into_iter().map(|(id, matches)| Document::from_matches(id, matches)).collect()
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 03:14:16 +08:00
|
|
|
impl<'a, D> QueryBuilder<'a, D>
|
2018-12-08 00:59:03 +08:00
|
|
|
where D: Deref<Target=DB>,
|
2018-10-17 19:35:34 +08:00
|
|
|
{
|
2018-12-16 21:21:06 +08:00
|
|
|
pub fn query(&self, query: &str, range: Range<usize>) -> Vec<Document> {
|
2018-11-28 02:11:33 +08:00
|
|
|
let mut documents = self.query_all(query);
|
2018-10-17 19:35:34 +08:00
|
|
|
let mut groups = vec![documents.as_mut_slice()];
|
2018-12-07 21:41:06 +08:00
|
|
|
let view = &self.view;
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2018-12-16 21:21:06 +08:00
|
|
|
'criteria: for criterion in self.criteria.as_ref() {
|
2018-10-17 19:35:34 +08:00
|
|
|
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
2018-12-16 21:21:06 +08:00
|
|
|
let mut documents_seen = 0;
|
|
|
|
|
|
|
|
for group in tmp_groups {
|
|
|
|
// if this group does not overlap with the requested range,
|
|
|
|
// push it without sorting and splitting it
|
|
|
|
if documents_seen + group.len() < range.start {
|
|
|
|
documents_seen += group.len();
|
|
|
|
groups.push(group);
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2018-12-07 21:41:06 +08:00
|
|
|
group.sort_unstable_by(|a, b| criterion.evaluate(a, b, view));
|
2018-12-16 21:21:06 +08:00
|
|
|
|
2018-12-07 21:41:06 +08:00
|
|
|
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b, view)) {
|
2018-12-16 21:21:06 +08:00
|
|
|
documents_seen += group.len();
|
2018-10-17 19:35:34 +08:00
|
|
|
groups.push(group);
|
2018-12-16 21:21:06 +08:00
|
|
|
|
|
|
|
// we have sort enough documents if the last document sorted is after
|
|
|
|
// the end of the requested range, we can continue to the next criterion
|
|
|
|
if documents_seen >= range.end { continue 'criteria }
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 21:21:06 +08:00
|
|
|
// `drain` removes the documents efficiently using `ptr::copy`
|
|
|
|
// TODO it could be more efficient to have a custom iterator
|
|
|
|
let offset = cmp::min(documents.len(), range.start);
|
|
|
|
documents.drain(0..offset);
|
|
|
|
documents.truncate(range.len());
|
2018-12-07 18:53:17 +08:00
|
|
|
documents
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
2018-11-28 02:11:33 +08:00
|
|
|
}
|
2018-10-11 20:04:41 +08:00
|
|
|
|
2018-12-11 03:14:16 +08:00
|
|
|
pub struct DistinctQueryBuilder<'a, D, F>
|
2018-12-08 00:59:03 +08:00
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
2018-12-11 03:14:16 +08:00
|
|
|
inner: QueryBuilder<'a, D>,
|
2018-11-28 02:11:33 +08:00
|
|
|
function: F,
|
|
|
|
size: usize,
|
|
|
|
}
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2018-12-11 03:14:16 +08:00
|
|
|
impl<'a, D, F, K> DistinctQueryBuilder<'a, D, F>
|
2018-12-08 00:59:03 +08:00
|
|
|
where D: Deref<Target=DB>,
|
|
|
|
F: Fn(DocumentId, &DatabaseView<D>) -> Option<K>,
|
2018-11-29 00:12:24 +08:00
|
|
|
K: Hash + Eq,
|
2018-11-28 02:11:33 +08:00
|
|
|
{
|
2018-12-16 21:22:04 +08:00
|
|
|
pub fn query(&self, query: &str, range: Range<usize>) -> Vec<Document> {
|
2018-11-29 00:12:24 +08:00
|
|
|
let mut documents = self.inner.query_all(query);
|
|
|
|
let mut groups = vec![documents.as_mut_slice()];
|
2018-12-16 21:22:04 +08:00
|
|
|
let mut key_cache = HashMap::new();
|
2018-12-07 21:41:06 +08:00
|
|
|
let view = &self.inner.view;
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2018-12-16 21:22:04 +08:00
|
|
|
// these two variables informs on the current distinct map and
|
|
|
|
// on the raw offset of the start of the group where the
|
|
|
|
// range.start bound is located according to the distinct function
|
|
|
|
let mut distinct_map = DistinctMap::new(self.size);
|
|
|
|
let mut distinct_raw_offset = 0;
|
|
|
|
|
|
|
|
'criteria: for criterion in self.inner.criteria.as_ref() {
|
2018-11-29 00:12:24 +08:00
|
|
|
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
2018-12-16 21:22:04 +08:00
|
|
|
let mut buf_distinct = BufferedDistinctMap::new(&mut distinct_map);
|
|
|
|
let mut documents_seen = 0;
|
|
|
|
|
|
|
|
for group in tmp_groups {
|
|
|
|
// if this group does not overlap with the requested range,
|
|
|
|
// push it without sorting and splitting it
|
|
|
|
if documents_seen + group.len() < distinct_raw_offset {
|
|
|
|
documents_seen += group.len();
|
|
|
|
groups.push(group);
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2018-12-07 21:41:06 +08:00
|
|
|
group.sort_unstable_by(|a, b| criterion.evaluate(a, b, view));
|
2018-12-16 21:22:04 +08:00
|
|
|
|
2018-12-07 21:41:06 +08:00
|
|
|
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b, view)) {
|
2018-12-16 21:22:04 +08:00
|
|
|
// we must compute the real distinguished len of this sub-group
|
2018-12-13 18:54:09 +08:00
|
|
|
for document in group.iter() {
|
2018-12-16 21:22:04 +08:00
|
|
|
let entry = key_cache.entry(document.id);
|
|
|
|
let key = entry.or_insert_with(|| (self.function)(document.id, view).map(Rc::new));
|
|
|
|
|
|
|
|
match key.clone() {
|
|
|
|
Some(key) => buf_distinct.register(key),
|
|
|
|
None => buf_distinct.register_without_key(),
|
2018-12-13 18:54:09 +08:00
|
|
|
};
|
2018-12-16 21:22:04 +08:00
|
|
|
|
|
|
|
// the requested range end is reached: stop computing distinct
|
|
|
|
if buf_distinct.len() >= range.end { break }
|
2018-12-13 18:54:09 +08:00
|
|
|
}
|
2018-12-16 21:22:04 +08:00
|
|
|
|
|
|
|
documents_seen += group.len();
|
2018-11-29 00:12:24 +08:00
|
|
|
groups.push(group);
|
2018-12-16 21:22:04 +08:00
|
|
|
|
|
|
|
// if this sub-group does not overlap with the requested range
|
|
|
|
// we must update the distinct map and its start index
|
|
|
|
if buf_distinct.len() < range.start {
|
|
|
|
buf_distinct.transfert_to_internal();
|
|
|
|
distinct_raw_offset = documents_seen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have sort enough documents if the last document sorted is after
|
|
|
|
// the end of the requested range, we can continue to the next criterion
|
|
|
|
if buf_distinct.len() >= range.end { continue 'criteria }
|
2018-11-29 00:12:24 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-18 21:08:04 +08:00
|
|
|
}
|
|
|
|
|
2018-12-16 21:22:04 +08:00
|
|
|
let mut out_documents = Vec::with_capacity(range.len());
|
|
|
|
let mut seen = BufferedDistinctMap::new(&mut distinct_map);
|
|
|
|
|
|
|
|
for document in documents.into_iter().skip(distinct_raw_offset) {
|
|
|
|
let key = key_cache.remove(&document.id).expect("BUG: cached key not found");
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2018-12-16 21:22:04 +08:00
|
|
|
let accepted = match key {
|
2018-12-13 18:54:09 +08:00
|
|
|
Some(key) => seen.register(key),
|
2018-12-16 21:22:04 +08:00
|
|
|
None => seen.register_without_key(),
|
2018-11-29 00:12:24 +08:00
|
|
|
};
|
2018-10-17 19:35:34 +08:00
|
|
|
|
2018-12-16 21:22:04 +08:00
|
|
|
if accepted && seen.len() > range.start {
|
2018-12-13 18:54:47 +08:00
|
|
|
out_documents.push(document);
|
2018-12-16 21:22:04 +08:00
|
|
|
if out_documents.len() == range.len() { break }
|
2018-11-29 00:12:24 +08:00
|
|
|
}
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:12:24 +08:00
|
|
|
out_documents
|
2018-10-11 20:04:41 +08:00
|
|
|
}
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|