2018-10-17 19:35:34 +08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::hash::Hash;
|
2018-10-11 22:09:28 +08:00
|
|
|
use std::ops::Range;
|
2018-10-10 22:57:21 +08:00
|
|
|
use std::rc::Rc;
|
2018-10-17 22:59:39 +08:00
|
|
|
use std::{mem, vec, cmp};
|
2018-10-10 22:57:21 +08:00
|
|
|
|
|
|
|
use fnv::FnvHashMap;
|
|
|
|
use fst::Streamer;
|
|
|
|
use group_by::GroupByMut;
|
|
|
|
|
|
|
|
use crate::automaton::{DfaExt, AutomatonExt};
|
|
|
|
use crate::metadata::Metadata;
|
2018-10-11 20:04:41 +08:00
|
|
|
use crate::metadata::ops::OpBuilder;
|
2018-10-17 19:35:34 +08:00
|
|
|
use crate::rank::criterion::{self, Criterion};
|
2018-10-10 22:57:21 +08:00
|
|
|
use crate::rank::Document;
|
2018-10-17 19:35:34 +08:00
|
|
|
use crate::{Match, DocumentId};
|
2018-10-10 22:57:21 +08:00
|
|
|
|
2018-10-17 19:35:34 +08:00
|
|
|
pub struct Config<'m, C, F> {
|
|
|
|
pub metadata: &'m Metadata,
|
|
|
|
pub automatons: Vec<DfaExt>,
|
|
|
|
pub criteria: Vec<C>,
|
|
|
|
pub distinct: (F, usize),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RankedStream<'m, C, F> {
|
|
|
|
stream: crate::metadata::ops::Union<'m>,
|
2018-10-11 20:04:41 +08:00
|
|
|
automatons: Vec<Rc<DfaExt>>,
|
|
|
|
criteria: Vec<C>,
|
2018-10-17 19:35:34 +08:00
|
|
|
distinct: (F, usize),
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|
|
|
|
|
2018-10-17 19:35:34 +08:00
|
|
|
impl<'m, C, F> RankedStream<'m, C, F> {
|
|
|
|
pub fn new(config: Config<'m, C, F>) -> Self {
|
|
|
|
let automatons: Vec<_> = config.automatons.into_iter().map(Rc::new).collect();
|
|
|
|
let mut builder = OpBuilder::with_automatons(automatons.clone());
|
|
|
|
builder.push(config.metadata);
|
2018-10-10 22:57:21 +08:00
|
|
|
|
2018-10-11 20:04:41 +08:00
|
|
|
RankedStream {
|
|
|
|
stream: builder.union(),
|
2018-10-17 19:35:34 +08:00
|
|
|
automatons: automatons,
|
|
|
|
criteria: config.criteria,
|
|
|
|
distinct: config.distinct,
|
2018-10-11 20:04:41 +08:00
|
|
|
}
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 19:35:34 +08:00
|
|
|
impl<'m, C, F> RankedStream<'m, C, F> {
|
|
|
|
fn retrieve_all_documents(&mut self) -> Vec<Document> {
|
2018-10-11 20:04:41 +08:00
|
|
|
let mut matches = FnvHashMap::default();
|
|
|
|
|
|
|
|
while let Some((string, indexed_values)) = self.stream.next() {
|
|
|
|
for iv in indexed_values {
|
|
|
|
let automaton = &self.automatons[iv.index];
|
|
|
|
let distance = automaton.eval(string).to_u8();
|
|
|
|
let is_exact = distance == 0 && string.len() == automaton.query_len();
|
|
|
|
|
2018-10-17 19:35:34 +08:00
|
|
|
for doc_index in iv.doc_indexes.as_slice() {
|
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,
|
|
|
|
attribute_index: doc_index.attribute_index,
|
2018-10-11 20:04:41 +08:00
|
|
|
is_exact: is_exact,
|
|
|
|
};
|
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-17 19:35:34 +08:00
|
|
|
matches.into_iter().map(|(id, mut matches)| {
|
2018-10-11 20:04:41 +08:00
|
|
|
matches.sort_unstable();
|
|
|
|
unsafe { Document::from_sorted_matches(id, matches) }
|
2018-10-17 19:35:34 +08:00
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, C, F> RankedStream<'a, C, F>
|
|
|
|
where C: Criterion
|
|
|
|
{
|
|
|
|
pub fn retrieve_documents(mut self, range: Range<usize>) -> Vec<Document> {
|
|
|
|
let mut documents = self.retrieve_all_documents();
|
|
|
|
let mut groups = vec![documents.as_mut_slice()];
|
|
|
|
|
|
|
|
for criterion in self.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)) {
|
|
|
|
groups.push(group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 22:59:39 +08:00
|
|
|
let range = Range {
|
|
|
|
start: cmp::min(range.start, documents.len()),
|
|
|
|
end: cmp::min(range.end, documents.len()),
|
|
|
|
};
|
2018-10-17 19:35:34 +08:00
|
|
|
documents[range].to_vec()
|
|
|
|
}
|
2018-10-11 20:04:41 +08:00
|
|
|
|
2018-10-17 19:35:34 +08:00
|
|
|
pub fn retrieve_distinct_documents<K>(mut self, range: Range<usize>) -> Vec<Document>
|
2018-10-18 21:08:04 +08:00
|
|
|
where F: Fn(&DocumentId) -> Option<K>,
|
2018-10-17 19:35:34 +08:00
|
|
|
K: Hash + Eq,
|
|
|
|
{
|
|
|
|
let mut documents = self.retrieve_all_documents();
|
2018-10-11 20:04:41 +08:00
|
|
|
let mut groups = vec![documents.as_mut_slice()];
|
|
|
|
|
|
|
|
for criterion in self.criteria {
|
2018-10-11 22:09:28 +08:00
|
|
|
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
2018-10-17 19:35:34 +08:00
|
|
|
|
|
|
|
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)) {
|
|
|
|
groups.push(group);
|
2018-10-11 20:04:41 +08:00
|
|
|
}
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-11 22:09:28 +08:00
|
|
|
|
2018-10-17 19:35:34 +08:00
|
|
|
let mut out_documents = Vec::with_capacity(range.len());
|
|
|
|
let (distinct, limit) = self.distinct;
|
|
|
|
let mut seen = DistinctMap::new(limit);
|
|
|
|
|
|
|
|
for document in documents {
|
2018-10-18 21:08:04 +08:00
|
|
|
let accepted = match distinct(&document.id) {
|
|
|
|
Some(key) => seen.digest(key),
|
|
|
|
None => seen.accept_without_key(),
|
|
|
|
};
|
2018-10-17 19:35:34 +08:00
|
|
|
|
|
|
|
if accepted {
|
|
|
|
if seen.len() == range.end { break }
|
|
|
|
if seen.len() >= range.start {
|
|
|
|
out_documents.push(document);
|
|
|
|
}
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 19:35:34 +08:00
|
|
|
out_documents
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DistinctMap<K> {
|
|
|
|
inner: HashMap<K, usize>,
|
|
|
|
limit: usize,
|
|
|
|
len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: Hash + Eq> DistinctMap<K> {
|
|
|
|
pub fn new(limit: usize) -> Self {
|
|
|
|
DistinctMap {
|
|
|
|
inner: HashMap::new(),
|
|
|
|
limit: limit,
|
|
|
|
len: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn digest(&mut self, key: K) -> bool {
|
|
|
|
let seen = self.inner.entry(key).or_insert(0);
|
2018-10-18 21:08:04 +08:00
|
|
|
if *seen < self.limit {
|
|
|
|
*seen += 1;
|
|
|
|
self.len += 1;
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn accept_without_key(&mut self) -> bool {
|
|
|
|
self.len += 1;
|
|
|
|
true
|
2018-10-17 19:35:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn easy_distinct_map() {
|
|
|
|
let mut map = DistinctMap::new(2);
|
|
|
|
for x in &[1, 1, 1, 2, 3, 4, 5, 6, 6, 6, 6, 6] {
|
|
|
|
map.digest(x);
|
|
|
|
}
|
|
|
|
assert_eq!(map.len(), 8);
|
|
|
|
|
|
|
|
let mut map = DistinctMap::new(2);
|
|
|
|
assert_eq!(map.digest(1), true);
|
|
|
|
assert_eq!(map.digest(1), true);
|
|
|
|
assert_eq!(map.digest(1), false);
|
|
|
|
assert_eq!(map.digest(1), false);
|
|
|
|
|
|
|
|
assert_eq!(map.digest(2), true);
|
|
|
|
assert_eq!(map.digest(3), true);
|
|
|
|
assert_eq!(map.digest(2), true);
|
|
|
|
assert_eq!(map.digest(2), false);
|
|
|
|
|
|
|
|
assert_eq!(map.len(), 5);
|
2018-10-11 20:04:41 +08:00
|
|
|
}
|
2018-10-10 22:57:21 +08:00
|
|
|
}
|