2019-07-08 01:55:15 +08:00
|
|
|
use std::fmt;
|
2019-10-18 19:05:28 +08:00
|
|
|
use std::sync::Arc;
|
2019-10-15 00:48:32 +08:00
|
|
|
|
2019-07-08 01:55:15 +08:00
|
|
|
use sdset::SetBuf;
|
|
|
|
use slice_group_by::GroupBy;
|
2019-10-15 00:48:32 +08:00
|
|
|
|
2019-11-29 23:31:47 +08:00
|
|
|
use crate::{DocumentId, Highlight, TmpMatch, AttrCount};
|
2019-07-08 01:55:15 +08:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct RawDocument {
|
|
|
|
pub id: DocumentId,
|
|
|
|
pub matches: SharedMatches,
|
|
|
|
pub highlights: Vec<Highlight>,
|
2019-11-29 23:31:47 +08:00
|
|
|
pub fields_counts: Option<SetBuf<AttrCount>>,
|
2019-07-08 01:55:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RawDocument {
|
|
|
|
pub fn query_index(&self) -> &[u32] {
|
|
|
|
let r = self.matches.range;
|
|
|
|
// it is safe because construction/modifications
|
|
|
|
// can only be done in this module
|
2019-10-18 19:05:28 +08:00
|
|
|
unsafe {
|
|
|
|
&self
|
|
|
|
.matches
|
|
|
|
.matches
|
|
|
|
.query_index
|
|
|
|
.get_unchecked(r.start..r.end)
|
|
|
|
}
|
2019-07-08 01:55:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn distance(&self) -> &[u8] {
|
|
|
|
let r = self.matches.range;
|
|
|
|
// it is safe because construction/modifications
|
|
|
|
// can only be done in this module
|
|
|
|
unsafe { &self.matches.matches.distance.get_unchecked(r.start..r.end) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn attribute(&self) -> &[u16] {
|
|
|
|
let r = self.matches.range;
|
|
|
|
// it is safe because construction/modifications
|
|
|
|
// can only be done in this module
|
|
|
|
unsafe { &self.matches.matches.attribute.get_unchecked(r.start..r.end) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn word_index(&self) -> &[u16] {
|
|
|
|
let r = self.matches.range;
|
|
|
|
// it is safe because construction/modifications
|
|
|
|
// can only be done in this module
|
2019-10-18 19:05:28 +08:00
|
|
|
unsafe {
|
|
|
|
&self
|
|
|
|
.matches
|
|
|
|
.matches
|
|
|
|
.word_index
|
|
|
|
.get_unchecked(r.start..r.end)
|
|
|
|
}
|
2019-07-08 01:55:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_exact(&self) -> &[bool] {
|
|
|
|
let r = self.matches.range;
|
|
|
|
// it is safe because construction/modifications
|
|
|
|
// can only be done in this module
|
|
|
|
unsafe { &self.matches.matches.is_exact.get_unchecked(r.start..r.end) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for RawDocument {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.write_str("RawDocument {\r\n")?;
|
2019-10-18 19:05:28 +08:00
|
|
|
f.write_fmt(format_args!("{:>15}: {:?},\r\n", "id", self.id))?;
|
|
|
|
f.write_fmt(format_args!(
|
|
|
|
"{:>15}: {:^5?},\r\n",
|
|
|
|
"query_index",
|
|
|
|
self.query_index()
|
|
|
|
))?;
|
|
|
|
f.write_fmt(format_args!(
|
|
|
|
"{:>15}: {:^5?},\r\n",
|
|
|
|
"distance",
|
|
|
|
self.distance()
|
|
|
|
))?;
|
|
|
|
f.write_fmt(format_args!(
|
|
|
|
"{:>15}: {:^5?},\r\n",
|
|
|
|
"attribute",
|
|
|
|
self.attribute()
|
|
|
|
))?;
|
|
|
|
f.write_fmt(format_args!(
|
|
|
|
"{:>15}: {:^5?},\r\n",
|
|
|
|
"word_index",
|
|
|
|
self.word_index()
|
|
|
|
))?;
|
|
|
|
f.write_fmt(format_args!(
|
|
|
|
"{:>15}: {:^5?},\r\n",
|
|
|
|
"is_exact",
|
|
|
|
self.is_exact()
|
|
|
|
))?;
|
2019-07-08 01:55:15 +08:00
|
|
|
f.write_str("}")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:34:53 +08:00
|
|
|
pub fn raw_documents_from(
|
|
|
|
matches: SetBuf<(DocumentId, TmpMatch)>,
|
2019-11-29 23:31:47 +08:00
|
|
|
highlights: SetBuf<(DocumentId, Highlight)>
|
2019-10-18 19:05:28 +08:00
|
|
|
) -> Vec<RawDocument> {
|
2019-10-15 00:48:32 +08:00
|
|
|
let mut docs_ranges: Vec<(_, Range, _, _)> = Vec::new();
|
2019-07-08 01:55:15 +08:00
|
|
|
let mut matches2 = Matches::with_capacity(matches.len());
|
|
|
|
|
2019-08-02 18:07:23 +08:00
|
|
|
let matches = matches.linear_group_by_key(|(id, _)| *id);
|
|
|
|
let highlights = highlights.linear_group_by_key(|(id, _)| *id);
|
2019-07-16 01:34:53 +08:00
|
|
|
|
2019-11-29 23:31:47 +08:00
|
|
|
for (mgroup, hgroup) in matches.zip(highlights) {
|
|
|
|
assert_eq!(mgroup[0].0, hgroup[0].0);
|
2019-07-16 01:34:53 +08:00
|
|
|
|
|
|
|
let document_id = mgroup[0].0;
|
2019-10-15 00:48:32 +08:00
|
|
|
let start = docs_ranges.last().map(|(_, r, _, _)| r.end).unwrap_or(0);
|
2019-07-16 01:34:53 +08:00
|
|
|
let end = start + mgroup.len();
|
|
|
|
let highlights = hgroup.iter().map(|(_, h)| *h).collect();
|
2019-11-29 23:31:47 +08:00
|
|
|
let fields_counts = None;
|
2019-07-08 01:55:15 +08:00
|
|
|
|
2019-10-15 00:48:32 +08:00
|
|
|
docs_ranges.push((document_id, Range { start, end }, highlights, fields_counts));
|
2019-11-29 23:31:47 +08:00
|
|
|
// TODO we could try to keep both data
|
2019-11-30 23:33:48 +08:00
|
|
|
// - the data oriented one and,
|
|
|
|
// - the raw one, the one that comes from the arguments of this function
|
2019-11-29 23:31:47 +08:00
|
|
|
// This way we would be able to only produce data oriented lazily.
|
|
|
|
//
|
|
|
|
// For example the default first criterion is `SumOfTypos`
|
|
|
|
// and just needs the `query_index` and the `distance` fields.
|
|
|
|
// It would probably be good to avoid wasting time sorting other fields of documents
|
|
|
|
// that will never ever reach the second criterion.
|
2019-07-16 01:34:53 +08:00
|
|
|
matches2.extend_from_slice(mgroup);
|
2019-07-08 01:55:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let matches = Arc::new(matches2);
|
2019-10-18 19:05:28 +08:00
|
|
|
docs_ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|(id, range, highlights, fields_counts)| {
|
2019-11-29 23:31:47 +08:00
|
|
|
let matches = SharedMatches { range, matches: matches.clone() };
|
|
|
|
RawDocument { id, matches, highlights, fields_counts }
|
2019-10-18 19:05:28 +08:00
|
|
|
})
|
|
|
|
.collect()
|
2019-07-08 01:55:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
struct Range {
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SharedMatches {
|
|
|
|
range: Range,
|
|
|
|
matches: Arc<Matches>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Matches {
|
|
|
|
query_index: Vec<u32>,
|
|
|
|
distance: Vec<u8>,
|
|
|
|
attribute: Vec<u16>,
|
|
|
|
word_index: Vec<u16>,
|
|
|
|
is_exact: Vec<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Matches {
|
|
|
|
fn with_capacity(cap: usize) -> Matches {
|
|
|
|
Matches {
|
|
|
|
query_index: Vec::with_capacity(cap),
|
|
|
|
distance: Vec::with_capacity(cap),
|
|
|
|
attribute: Vec::with_capacity(cap),
|
|
|
|
word_index: Vec::with_capacity(cap),
|
|
|
|
is_exact: Vec::with_capacity(cap),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:34:53 +08:00
|
|
|
fn extend_from_slice(&mut self, matches: &[(DocumentId, TmpMatch)]) {
|
|
|
|
for (_, match_) in matches {
|
2019-07-08 01:55:15 +08:00
|
|
|
self.query_index.push(match_.query_index);
|
|
|
|
self.distance.push(match_.distance);
|
|
|
|
self.attribute.push(match_.attribute);
|
|
|
|
self.word_index.push(match_.word_index);
|
|
|
|
self.is_exact.push(match_.is_exact);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|