2022-01-27 18:00:18 +08:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2021-03-25 18:10:12 +08:00
|
|
|
|
2022-01-19 22:02:04 +08:00
|
|
|
use grenad::{CompressionType, MergerBuilder};
|
2022-01-21 00:55:52 +08:00
|
|
|
use heed::types::ByteSlice;
|
2021-03-25 18:10:12 +08:00
|
|
|
|
2021-06-09 18:17:11 +08:00
|
|
|
use crate::update::index_documents::{
|
2022-01-27 18:00:18 +08:00
|
|
|
create_sorter, merge_roaring_bitmaps, sorter_into_lmdb_database, CursorClonableMmap, MergeFn,
|
2021-06-09 18:17:11 +08:00
|
|
|
};
|
2021-06-17 00:33:33 +08:00
|
|
|
use crate::{Index, Result};
|
2021-03-25 18:10:12 +08:00
|
|
|
|
|
|
|
pub struct WordPrefixDocids<'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> WordPrefixDocids<'t, 'u, 'i> {
|
2021-06-17 00:33:33 +08:00
|
|
|
pub fn new(
|
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
|
|
|
index: &'i Index,
|
|
|
|
) -> WordPrefixDocids<'t, 'u, 'i> {
|
2021-03-25 18:10:12 +08:00
|
|
|
WordPrefixDocids {
|
|
|
|
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("WordPrefixDocids::{}")]
|
2022-01-27 18:00:18 +08:00
|
|
|
pub fn execute(
|
2022-01-19 22:02:04 +08:00
|
|
|
self,
|
|
|
|
new_word_docids: Vec<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-19 22:02:04 +08:00
|
|
|
) -> Result<()> {
|
2021-03-25 18:10:12 +08:00
|
|
|
// It is forbidden to keep a mutable reference into the database
|
|
|
|
// and write into it at the same time, therefore we write into another file.
|
|
|
|
let mut prefix_docids_sorter = create_sorter(
|
2021-08-16 19:36:30 +08:00
|
|
|
merge_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-01-19 22:02:04 +08:00
|
|
|
let mut word_docids_merger = MergerBuilder::new(merge_roaring_bitmaps);
|
2022-02-16 22:28:48 +08:00
|
|
|
for reader in new_word_docids {
|
|
|
|
word_docids_merger.push(reader.into_cursor()?);
|
|
|
|
}
|
|
|
|
let mut word_docids_iter = word_docids_merger.build().into_stream_merger_iter()?;
|
2022-01-19 22:02:04 +08:00
|
|
|
|
|
|
|
let mut current_prefixes: Option<&&[String]> = None;
|
|
|
|
let mut prefixes_cache = HashMap::new();
|
|
|
|
while let Some((word, data)) = word_docids_iter.next()? {
|
|
|
|
current_prefixes = match current_prefixes.take() {
|
|
|
|
Some(prefixes) if word.starts_with(&prefixes[0].as_bytes()) => Some(prefixes),
|
|
|
|
_otherwise => {
|
|
|
|
write_prefixes_in_sorter(&mut prefixes_cache, &mut prefix_docids_sorter)?;
|
2022-01-27 18:00:18 +08:00
|
|
|
common_prefix_fst_words
|
2022-01-19 22:02:04 +08:00
|
|
|
.iter()
|
|
|
|
.find(|prefixes| word.starts_with(&prefixes[0].as_bytes()))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(prefixes) = current_prefixes {
|
|
|
|
for prefix in prefixes.iter() {
|
|
|
|
if word.starts_with(prefix.as_bytes()) {
|
|
|
|
match prefixes_cache.get_mut(prefix.as_bytes()) {
|
|
|
|
Some(value) => value.push(data.to_owned()),
|
|
|
|
None => {
|
|
|
|
prefixes_cache.insert(prefix.clone().into(), vec![data.to_owned()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 22:48:48 +08:00
|
|
|
write_prefixes_in_sorter(&mut prefixes_cache, &mut prefix_docids_sorter)?;
|
|
|
|
|
|
|
|
// We fetch the docids associated to the newly added word prefix fst only.
|
|
|
|
let db = self.index.word_docids.remap_data_type::<ByteSlice>();
|
2022-01-27 18:00:18 +08:00
|
|
|
for prefix in new_prefix_fst_words {
|
|
|
|
let prefix = std::str::from_utf8(prefix.as_bytes())?;
|
2022-01-25 22:48:48 +08:00
|
|
|
for result in db.prefix_iter(self.wtxn, prefix)? {
|
|
|
|
let (_word, data) = result?;
|
|
|
|
prefix_docids_sorter.insert(prefix, data)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 22:02:04 +08:00
|
|
|
// We remove all the entries that are no more required in this word prefix docids database.
|
|
|
|
let mut iter = self.index.word_prefix_docids.iter_mut(self.wtxn)?.lazily_decode_data();
|
|
|
|
while let Some((prefix, _)) = iter.next().transpose()? {
|
2022-01-27 18:00:18 +08:00
|
|
|
if del_prefix_fst_words.contains(prefix.as_bytes()) {
|
2022-01-19 22:02:04 +08:00
|
|
|
unsafe { iter.del_current()? };
|
2021-03-25 18:10:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 22:02:04 +08:00
|
|
|
drop(iter);
|
2021-03-25 18:10:12 +08:00
|
|
|
|
|
|
|
// We finally write the word prefix docids into the LMDB database.
|
|
|
|
sorter_into_lmdb_database(
|
|
|
|
self.wtxn,
|
|
|
|
*self.index.word_prefix_docids.as_polymorph(),
|
|
|
|
prefix_docids_sorter,
|
2021-08-16 19:36:30 +08:00
|
|
|
merge_roaring_bitmaps,
|
2021-03-25 18:10:12 +08:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 22:02:04 +08:00
|
|
|
|
|
|
|
fn write_prefixes_in_sorter(
|
|
|
|
prefixes: &mut HashMap<Vec<u8>, Vec<Vec<u8>>>,
|
|
|
|
sorter: &mut grenad::Sorter<MergeFn>,
|
|
|
|
) -> Result<()> {
|
|
|
|
for (key, data_slices) in prefixes.drain() {
|
|
|
|
for data in data_slices {
|
|
|
|
sorter.insert(&key, data)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|