2021-08-19 00:04:24 +08:00
|
|
|
use std::collections::HashMap;
|
2021-03-25 18:10:12 +08:00
|
|
|
|
2021-08-19 00:04:24 +08:00
|
|
|
use fst::IntoStreamer;
|
2021-03-25 18:10:12 +08:00
|
|
|
use grenad::CompressionType;
|
|
|
|
use heed::types::ByteSlice;
|
|
|
|
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::{
|
2021-08-19 00:04:24 +08:00
|
|
|
create_sorter, merge_cbo_roaring_bitmaps, sorter_into_lmdb_database, MergeFn, WriteMethod,
|
2021-03-25 18:10:12 +08:00
|
|
|
};
|
2021-06-17 00:33:33 +08:00
|
|
|
use crate::{Index, Result};
|
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>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:55:53 +08:00
|
|
|
#[logging_timer::time("WordPrefixPairProximityDocids::{}")]
|
2021-06-14 22:46:19 +08:00
|
|
|
pub fn execute(self) -> Result<()> {
|
2021-03-25 18:10:12 +08:00
|
|
|
debug!("Computing and writing the word prefix pair proximity docids into LMDB on disk...");
|
|
|
|
|
|
|
|
self.index.word_prefix_pair_proximity_docids.clear(self.wtxn)?;
|
|
|
|
|
|
|
|
// Here we create a sorter akin to the previous one.
|
|
|
|
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,
|
|
|
|
);
|
|
|
|
|
2021-08-19 00:04:24 +08:00
|
|
|
let prefix_fst = self.index.words_prefixes_fst(self.wtxn)?;
|
2022-01-12 22:22:06 +08:00
|
|
|
let prefix_fst_keys = prefix_fst.into_stream().into_strs()?;
|
|
|
|
let prefix_fst_keys: Vec<_> =
|
|
|
|
prefix_fst_keys.as_slice().linear_group_by_key(|x| x.chars().nth(0).unwrap()).collect();
|
2021-08-19 00:04:24 +08:00
|
|
|
|
|
|
|
let mut db =
|
|
|
|
self.index.word_pair_proximity_docids.remap_data_type::<ByteSlice>().iter(self.wtxn)?;
|
|
|
|
|
|
|
|
let mut buffer = Vec::new();
|
2022-01-12 22:22:06 +08:00
|
|
|
let mut current_prefixes: Option<&&[String]> = None;
|
2021-08-19 00:04:24 +08:00
|
|
|
let mut prefixes_cache = HashMap::new();
|
|
|
|
while let Some(((w1, w2, prox), data)) = db.next().transpose()? {
|
|
|
|
current_prefixes = match current_prefixes.take() {
|
2022-01-12 22:22:06 +08:00
|
|
|
Some(prefixes) if w2.starts_with(&prefixes[0]) => Some(prefixes),
|
2021-08-19 00:04:24 +08:00
|
|
|
_otherwise => {
|
|
|
|
write_prefixes_in_sorter(
|
|
|
|
&mut prefixes_cache,
|
|
|
|
&mut word_prefix_pair_proximity_docids_sorter,
|
|
|
|
)?;
|
2022-01-12 22:22:06 +08:00
|
|
|
prefix_fst_keys.iter().find(|prefixes| w2.starts_with(&prefixes[0]))
|
2021-08-19 00:04:24 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(prefixes) = current_prefixes {
|
|
|
|
buffer.clear();
|
|
|
|
buffer.extend_from_slice(w1.as_bytes());
|
|
|
|
buffer.push(0);
|
2022-01-12 22:22:06 +08:00
|
|
|
for prefix in prefixes.iter().filter(|prefix| w2.starts_with(prefix.as_str())) {
|
2021-08-19 00:04:24 +08:00
|
|
|
buffer.truncate(w1.len() + 1);
|
2022-01-12 22:22:06 +08:00
|
|
|
buffer.extend_from_slice(prefix.as_bytes());
|
2021-08-19 00:04:24 +08:00
|
|
|
buffer.push(prox);
|
|
|
|
|
|
|
|
match prefixes_cache.get_mut(&buffer) {
|
|
|
|
Some(value) => value.push(data),
|
|
|
|
None => {
|
|
|
|
prefixes_cache.insert(buffer.clone(), vec![data]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 18:10:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 00:04:24 +08:00
|
|
|
write_prefixes_in_sorter(
|
|
|
|
&mut prefixes_cache,
|
|
|
|
&mut word_prefix_pair_proximity_docids_sorter,
|
|
|
|
)?;
|
|
|
|
|
2021-03-25 18:10:12 +08:00
|
|
|
drop(prefix_fst);
|
2021-08-19 00:04:24 +08:00
|
|
|
drop(db);
|
2021-03-25 18:10:12 +08:00
|
|
|
|
|
|
|
// We finally write the word prefix pair proximity docids into the LMDB database.
|
|
|
|
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
|
|
|
WriteMethod::Append,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 00:04:24 +08:00
|
|
|
|
|
|
|
fn write_prefixes_in_sorter(
|
|
|
|
prefixes: &mut HashMap<Vec<u8>, Vec<&[u8]>>,
|
|
|
|
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(())
|
|
|
|
}
|