2022-01-27 18:00:18 +08:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2021-03-25 18:10:12 +08:00
|
|
|
|
2022-02-28 17:14:54 +08:00
|
|
|
use grenad::CompressionType;
|
2022-01-25 18:18:20 +08:00
|
|
|
use heed::types::ByteSlice;
|
2022-01-18 22:23:18 +08:00
|
|
|
use heed::BytesDecode;
|
2021-03-25 18:10:12 +08:00
|
|
|
use log::debug;
|
2021-08-19 00:04:24 +08:00
|
|
|
use slice_group_by::GroupBy;
|
2021-03-25 18:10:12 +08:00
|
|
|
|
|
|
|
use crate::update::index_documents::{
|
2022-01-27 18:00:18 +08:00
|
|
|
create_sorter, merge_cbo_roaring_bitmaps, sorter_into_lmdb_database, CursorClonableMmap,
|
|
|
|
MergeFn,
|
2021-03-25 18:10:12 +08:00
|
|
|
};
|
2022-01-18 22:23:18 +08:00
|
|
|
use crate::{Index, Result, StrStrU8Codec};
|
2021-03-25 18:10:12 +08:00
|
|
|
|
|
|
|
pub struct WordPrefixPairProximityDocids<'t, 'u, 'i> {
|
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
|
|
|
index: &'i Index,
|
|
|
|
pub(crate) chunk_compression_type: CompressionType,
|
|
|
|
pub(crate) chunk_compression_level: Option<u32>,
|
|
|
|
pub(crate) max_nb_chunks: Option<usize>,
|
|
|
|
pub(crate) max_memory: Option<usize>,
|
2022-01-12 22:40:51 +08:00
|
|
|
max_proximity: u8,
|
2022-01-12 23:14:53 +08:00
|
|
|
max_prefix_length: usize,
|
2021-03-25 18:10:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t, 'u, 'i> WordPrefixPairProximityDocids<'t, 'u, 'i> {
|
|
|
|
pub fn new(
|
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
|
|
|
index: &'i Index,
|
2021-06-17 00:33:33 +08:00
|
|
|
) -> WordPrefixPairProximityDocids<'t, 'u, 'i> {
|
2021-03-25 18:10:12 +08:00
|
|
|
WordPrefixPairProximityDocids {
|
|
|
|
wtxn,
|
|
|
|
index,
|
|
|
|
chunk_compression_type: CompressionType::None,
|
|
|
|
chunk_compression_level: None,
|
|
|
|
max_nb_chunks: None,
|
|
|
|
max_memory: None,
|
2022-01-12 22:40:51 +08:00
|
|
|
max_proximity: 4,
|
2022-01-12 23:14:53 +08:00
|
|
|
max_prefix_length: 2,
|
2021-03-25 18:10:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 22:40:51 +08:00
|
|
|
/// Set the maximum proximity required to make a prefix be part of the words prefixes
|
2022-01-26 18:28:11 +08:00
|
|
|
/// database. If two words are too far from the threshold the associated documents will
|
2022-01-12 22:40:51 +08:00
|
|
|
/// not be part of the prefix database.
|
|
|
|
///
|
2022-01-26 18:28:11 +08:00
|
|
|
/// Default value is 4. This value must be lower or equal than 7 and will be clamped
|
2022-01-12 22:40:51 +08:00
|
|
|
/// to this bound otherwise.
|
|
|
|
pub fn max_proximity(&mut self, value: u8) -> &mut Self {
|
|
|
|
self.max_proximity = value.max(7);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-26 18:28:11 +08:00
|
|
|
/// Set the maximum length the prefix of a word pair is allowed to have to be part of the words
|
|
|
|
/// prefixes database. If the prefix length is higher than the threshold, the associated documents
|
2022-01-12 23:14:53 +08:00
|
|
|
/// will not be part of the prefix database.
|
|
|
|
///
|
2022-01-26 18:28:11 +08:00
|
|
|
/// Default value is 2.
|
2022-01-12 23:14:53 +08:00
|
|
|
pub fn max_prefix_length(&mut self, value: usize) -> &mut Self {
|
|
|
|
self.max_prefix_length = value;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:55:53 +08:00
|
|
|
#[logging_timer::time("WordPrefixPairProximityDocids::{}")]
|
2022-01-27 18:00:18 +08:00
|
|
|
pub fn execute(
|
2022-01-18 21:59:51 +08:00
|
|
|
self,
|
2022-02-28 17:14:54 +08:00
|
|
|
new_word_pair_proximity_docids: grenad::Reader<CursorClonableMmap>,
|
2022-01-27 18:00:18 +08:00
|
|
|
new_prefix_fst_words: &[String],
|
|
|
|
common_prefix_fst_words: &[&[String]],
|
|
|
|
del_prefix_fst_words: &HashSet<Vec<u8>>,
|
2022-01-18 21:59:51 +08:00
|
|
|
) -> Result<()> {
|
2021-03-25 18:10:12 +08:00
|
|
|
debug!("Computing and writing the word prefix pair proximity docids into LMDB on disk...");
|
|
|
|
|
2022-01-27 18:00:18 +08:00
|
|
|
let new_prefix_fst_words: Vec<_> =
|
|
|
|
new_prefix_fst_words.linear_group_by_key(|x| x.chars().nth(0).unwrap()).collect();
|
|
|
|
|
2022-02-28 17:14:54 +08:00
|
|
|
let mut new_wppd_iter = new_word_pair_proximity_docids.into_cursor()?;
|
2021-03-25 18:10:12 +08:00
|
|
|
let mut word_prefix_pair_proximity_docids_sorter = create_sorter(
|
2021-08-16 19:36:30 +08:00
|
|
|
merge_cbo_roaring_bitmaps,
|
2021-03-25 18:10:12 +08:00
|
|
|
self.chunk_compression_type,
|
|
|
|
self.chunk_compression_level,
|
|
|
|
self.max_nb_chunks,
|
|
|
|
self.max_memory,
|
|
|
|
);
|
|
|
|
|
2022-03-02 01:02:12 +08:00
|
|
|
if !common_prefix_fst_words.is_empty() {
|
|
|
|
// We compute the prefix docids associated with the common prefixes between
|
|
|
|
// the old and new word prefix fst.
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
let mut current_prefixes: Option<&&[String]> = None;
|
|
|
|
let mut prefixes_cache = HashMap::new();
|
|
|
|
while let Some((key, data)) = new_wppd_iter.move_on_next()? {
|
|
|
|
let (w1, w2, prox) =
|
|
|
|
StrStrU8Codec::bytes_decode(key).ok_or(heed::Error::Decoding)?;
|
|
|
|
if prox > self.max_proximity {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
insert_current_prefix_data_in_sorter(
|
|
|
|
&mut buffer,
|
|
|
|
&mut current_prefixes,
|
|
|
|
&mut prefixes_cache,
|
|
|
|
&mut word_prefix_pair_proximity_docids_sorter,
|
|
|
|
common_prefix_fst_words,
|
|
|
|
self.max_prefix_length,
|
|
|
|
w1,
|
|
|
|
w2,
|
|
|
|
prox,
|
|
|
|
data,
|
|
|
|
)?;
|
2022-01-12 22:40:51 +08:00
|
|
|
}
|
|
|
|
|
2022-03-02 01:02:12 +08:00
|
|
|
write_prefixes_in_sorter(
|
2022-01-25 18:34:56 +08:00
|
|
|
&mut prefixes_cache,
|
|
|
|
&mut word_prefix_pair_proximity_docids_sorter,
|
|
|
|
)?;
|
2022-01-25 18:18:20 +08:00
|
|
|
}
|
|
|
|
|
2022-03-02 01:02:12 +08:00
|
|
|
if !new_prefix_fst_words.is_empty() {
|
|
|
|
// We compute the prefix docids associated with the newly added prefixes
|
|
|
|
// in the new word prefix fst.
|
|
|
|
let mut db_iter = self
|
|
|
|
.index
|
|
|
|
.word_pair_proximity_docids
|
|
|
|
.remap_data_type::<ByteSlice>()
|
|
|
|
.iter(self.wtxn)?;
|
|
|
|
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
let mut current_prefixes: Option<&&[String]> = None;
|
|
|
|
let mut prefixes_cache = HashMap::new();
|
|
|
|
while let Some(((w1, w2, prox), data)) = db_iter.next().transpose()? {
|
|
|
|
if prox > self.max_proximity {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
insert_current_prefix_data_in_sorter(
|
|
|
|
&mut buffer,
|
|
|
|
&mut current_prefixes,
|
|
|
|
&mut prefixes_cache,
|
|
|
|
&mut word_prefix_pair_proximity_docids_sorter,
|
|
|
|
&new_prefix_fst_words,
|
|
|
|
self.max_prefix_length,
|
|
|
|
w1,
|
|
|
|
w2,
|
|
|
|
prox,
|
|
|
|
data,
|
|
|
|
)?;
|
2022-01-25 18:18:20 +08:00
|
|
|
}
|
|
|
|
|
2022-03-02 01:02:12 +08:00
|
|
|
write_prefixes_in_sorter(
|
2022-01-25 18:34:56 +08:00
|
|
|
&mut prefixes_cache,
|
|
|
|
&mut word_prefix_pair_proximity_docids_sorter,
|
|
|
|
)?;
|
2021-03-25 18:10:12 +08:00
|
|
|
}
|
|
|
|
|
2022-01-18 22:23:18 +08:00
|
|
|
// All of the word prefix pairs in the database that have a w2
|
|
|
|
// that is contained in the `suppr_pw` set must be removed as well.
|
2022-03-15 23:37:22 +08:00
|
|
|
if !del_prefix_fst_words.is_empty() {
|
|
|
|
let mut iter = self
|
|
|
|
.index
|
|
|
|
.word_prefix_pair_proximity_docids
|
|
|
|
.remap_data_type::<ByteSlice>()
|
|
|
|
.iter_mut(self.wtxn)?;
|
|
|
|
while let Some(((_, w2, _), _)) = iter.next().transpose()? {
|
|
|
|
if del_prefix_fst_words.contains(w2.as_bytes()) {
|
|
|
|
// Delete this entry as the w2 prefix is no more in the words prefix fst.
|
|
|
|
unsafe { iter.del_current()? };
|
|
|
|
}
|
2022-01-18 22:23:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We finally write and merge the new word prefix pair proximity docids
|
|
|
|
// in the LMDB database.
|
2021-03-25 18:10:12 +08:00
|
|
|
sorter_into_lmdb_database(
|
|
|
|
self.wtxn,
|
|
|
|
*self.index.word_prefix_pair_proximity_docids.as_polymorph(),
|
|
|
|
word_prefix_pair_proximity_docids_sorter,
|
2021-08-16 19:36:30 +08:00
|
|
|
merge_cbo_roaring_bitmaps,
|
2021-03-25 18:10:12 +08:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 00:04:24 +08:00
|
|
|
|
|
|
|
fn write_prefixes_in_sorter(
|
2022-01-18 22:23:18 +08:00
|
|
|
prefixes: &mut HashMap<Vec<u8>, Vec<Vec<u8>>>,
|
2021-08-19 00:04:24 +08:00
|
|
|
sorter: &mut grenad::Sorter<MergeFn>,
|
|
|
|
) -> Result<()> {
|
2022-01-12 22:22:06 +08:00
|
|
|
for (key, data_slices) in prefixes.drain() {
|
2022-01-12 22:23:46 +08:00
|
|
|
for data in data_slices {
|
|
|
|
sorter.insert(&key, data)?;
|
2021-08-19 00:04:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-01-25 18:34:56 +08:00
|
|
|
|
|
|
|
/// Computes the current prefix based on the previous and the currently iterated value
|
|
|
|
/// i.e. w1, w2, prox. It also makes sure to follow the `max_prefix_length` setting.
|
|
|
|
///
|
|
|
|
/// Uses the current prefixes values to insert the associated data i.e. RoaringBitmap,
|
|
|
|
/// into the sorter that will, later, be inserted in the LMDB database.
|
|
|
|
fn insert_current_prefix_data_in_sorter<'a>(
|
|
|
|
buffer: &mut Vec<u8>,
|
|
|
|
current_prefixes: &mut Option<&'a &'a [String]>,
|
|
|
|
prefixes_cache: &mut HashMap<Vec<u8>, Vec<Vec<u8>>>,
|
|
|
|
word_prefix_pair_proximity_docids_sorter: &mut grenad::Sorter<MergeFn>,
|
|
|
|
prefix_fst_keys: &'a [&'a [std::string::String]],
|
|
|
|
max_prefix_length: usize,
|
|
|
|
w1: &str,
|
|
|
|
w2: &str,
|
|
|
|
prox: u8,
|
|
|
|
data: &[u8],
|
|
|
|
) -> Result<()> {
|
|
|
|
*current_prefixes = match current_prefixes.take() {
|
|
|
|
Some(prefixes) if w2.starts_with(&prefixes[0]) => Some(prefixes),
|
|
|
|
_otherwise => {
|
|
|
|
write_prefixes_in_sorter(prefixes_cache, word_prefix_pair_proximity_docids_sorter)?;
|
|
|
|
prefix_fst_keys.iter().find(|prefixes| w2.starts_with(&prefixes[0]))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(prefixes) = current_prefixes {
|
|
|
|
buffer.clear();
|
|
|
|
buffer.extend_from_slice(w1.as_bytes());
|
|
|
|
buffer.push(0);
|
|
|
|
for prefix in prefixes.iter() {
|
|
|
|
if prefix.len() <= max_prefix_length && w2.starts_with(prefix) {
|
|
|
|
buffer.truncate(w1.len() + 1);
|
|
|
|
buffer.extend_from_slice(prefix.as_bytes());
|
|
|
|
buffer.push(prox);
|
|
|
|
|
|
|
|
match prefixes_cache.get_mut(buffer.as_slice()) {
|
|
|
|
Some(value) => value.push(data.to_owned()),
|
|
|
|
None => {
|
|
|
|
prefixes_cache.insert(buffer.clone(), vec![data.to_owned()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|