2022-01-27 11:00:18 +01:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2021-03-25 11:10:12 +01:00
|
|
|
|
2022-02-28 10:14:54 +01:00
|
|
|
use grenad::CompressionType;
|
2023-11-27 11:52:22 +01:00
|
|
|
use heed::types::{Bytes, Str};
|
2022-03-25 10:20:39 +01:00
|
|
|
use heed::Database;
|
2021-03-25 11:10:12 +01:00
|
|
|
|
2023-11-08 16:41:26 +01:00
|
|
|
use crate::update::del_add::{deladd_serialize_add_side, DelAdd, KvWriterDelAdd};
|
2021-06-09 12:17:11 +02:00
|
|
|
use crate::update::index_documents::{
|
2024-08-30 11:49:47 +02:00
|
|
|
create_sorter, merge_deladd_cbo_roaring_bitmaps_into_cbo_roaring_bitmap, valid_lmdb_key,
|
|
|
|
write_sorter_into_database, CursorClonableMmap, MergeDeladdCboRoaringBitmaps,
|
2021-06-09 12:17:11 +02:00
|
|
|
};
|
2023-10-10 11:23:16 +02:00
|
|
|
use crate::{CboRoaringBitmapCodec, Result};
|
2021-03-25 11:10:12 +01:00
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
pub struct WordPrefixDocids<'t, 'i> {
|
|
|
|
wtxn: &'t mut heed::RwTxn<'i>,
|
2023-09-25 16:39:32 +02:00
|
|
|
word_docids: Database<Str, CboRoaringBitmapCodec>,
|
|
|
|
word_prefix_docids: Database<Str, CboRoaringBitmapCodec>,
|
2021-03-25 11:10:12 +01:00
|
|
|
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>,
|
|
|
|
}
|
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
impl<'t, 'i> WordPrefixDocids<'t, 'i> {
|
2021-06-16 18:33:33 +02:00
|
|
|
pub fn new(
|
2023-11-22 18:21:19 +01:00
|
|
|
wtxn: &'t mut heed::RwTxn<'i>,
|
2023-09-25 16:39:32 +02:00
|
|
|
word_docids: Database<Str, CboRoaringBitmapCodec>,
|
|
|
|
word_prefix_docids: Database<Str, CboRoaringBitmapCodec>,
|
2023-11-22 18:21:19 +01:00
|
|
|
) -> WordPrefixDocids<'t, 'i> {
|
2021-03-25 11:10:12 +01:00
|
|
|
WordPrefixDocids {
|
|
|
|
wtxn,
|
2022-03-25 10:20:39 +01:00
|
|
|
word_docids,
|
2022-03-25 16:17:55 +01:00
|
|
|
word_prefix_docids,
|
2021-03-25 11:10:12 +01:00
|
|
|
chunk_compression_type: CompressionType::None,
|
|
|
|
chunk_compression_level: None,
|
|
|
|
max_nb_chunks: None,
|
|
|
|
max_memory: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 09:42:48 +01:00
|
|
|
#[tracing::instrument(
|
|
|
|
level = "trace",
|
|
|
|
skip_all,
|
|
|
|
target = "indexing::prefix",
|
|
|
|
name = "word_prefix_docids"
|
|
|
|
)]
|
2022-01-27 11:00:18 +01:00
|
|
|
pub fn execute(
|
2022-01-19 15:02:04 +01:00
|
|
|
self,
|
2024-08-30 11:49:47 +02:00
|
|
|
new_word_docids: grenad::Merger<CursorClonableMmap, MergeDeladdCboRoaringBitmaps>,
|
2022-01-27 11:00:18 +01:00
|
|
|
new_prefix_fst_words: &[String],
|
|
|
|
common_prefix_fst_words: &[&[String]],
|
|
|
|
del_prefix_fst_words: &HashSet<Vec<u8>>,
|
2022-01-19 15:02:04 +01:00
|
|
|
) -> Result<()> {
|
2021-03-25 11:10:12 +01: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(
|
2022-09-13 10:40:37 +02:00
|
|
|
grenad::SortAlgorithm::Unstable,
|
2024-08-30 11:49:47 +02:00
|
|
|
MergeDeladdCboRoaringBitmaps,
|
2021-03-25 11:10:12 +01:00
|
|
|
self.chunk_compression_type,
|
|
|
|
self.chunk_compression_level,
|
|
|
|
self.max_nb_chunks,
|
|
|
|
self.max_memory,
|
|
|
|
);
|
|
|
|
|
2022-03-01 18:02:12 +01:00
|
|
|
if !common_prefix_fst_words.is_empty() {
|
|
|
|
let mut current_prefixes: Option<&&[String]> = None;
|
|
|
|
let mut prefixes_cache = HashMap::new();
|
2024-01-22 16:23:12 +01:00
|
|
|
let mut new_word_docids_iter = new_word_docids.into_stream_merger_iter()?;
|
|
|
|
while let Some((word, data)) = new_word_docids_iter.next()? {
|
2022-03-01 18:02:12 +01:00
|
|
|
current_prefixes = match current_prefixes.take() {
|
2022-10-13 22:02:54 +02:00
|
|
|
Some(prefixes) if word.starts_with(prefixes[0].as_bytes()) => Some(prefixes),
|
2022-03-01 18:02:12 +01:00
|
|
|
_otherwise => {
|
|
|
|
write_prefixes_in_sorter(&mut prefixes_cache, &mut prefix_docids_sorter)?;
|
|
|
|
common_prefix_fst_words
|
|
|
|
.iter()
|
2022-10-13 22:02:54 +02:00
|
|
|
.find(|prefixes| word.starts_with(prefixes[0].as_bytes()))
|
2022-03-01 18:02:12 +01:00
|
|
|
}
|
|
|
|
};
|
2022-01-19 15:02:04 +01:00
|
|
|
|
2022-03-01 18:02:12 +01:00
|
|
|
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-19 15:02:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 18:02:12 +01:00
|
|
|
write_prefixes_in_sorter(&mut prefixes_cache, &mut prefix_docids_sorter)?;
|
|
|
|
}
|
2022-01-25 15:48:48 +01:00
|
|
|
|
|
|
|
// We fetch the docids associated to the newly added word prefix fst only.
|
2023-11-27 11:52:22 +01:00
|
|
|
let db = self.word_docids.remap_data_type::<Bytes>();
|
2023-11-08 16:41:26 +01:00
|
|
|
let mut buffer = Vec::new();
|
2022-01-27 11:00:18 +01:00
|
|
|
for prefix in new_prefix_fst_words {
|
|
|
|
let prefix = std::str::from_utf8(prefix.as_bytes())?;
|
2022-01-25 15:48:48 +01:00
|
|
|
for result in db.prefix_iter(self.wtxn, prefix)? {
|
|
|
|
let (_word, data) = result?;
|
2023-11-08 16:41:26 +01:00
|
|
|
buffer.clear();
|
|
|
|
let mut writer = KvWriterDelAdd::new(&mut buffer);
|
|
|
|
writer.insert(DelAdd::Addition, data)?;
|
|
|
|
|
|
|
|
prefix_docids_sorter.insert(prefix, writer.into_inner()?)?;
|
2022-01-25 15:48:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 15:02:04 +01:00
|
|
|
// We remove all the entries that are no more required in this word prefix docids database.
|
2022-03-25 10:20:39 +01:00
|
|
|
let mut iter = self.word_prefix_docids.iter_mut(self.wtxn)?.lazily_decode_data();
|
2022-01-19 15:02:04 +01:00
|
|
|
while let Some((prefix, _)) = iter.next().transpose()? {
|
2022-01-27 11:00:18 +01:00
|
|
|
if del_prefix_fst_words.contains(prefix.as_bytes()) {
|
2022-01-19 15:02:04 +01:00
|
|
|
unsafe { iter.del_current()? };
|
2021-03-25 11:10:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 15:02:04 +01:00
|
|
|
drop(iter);
|
2021-03-25 11:10:12 +01:00
|
|
|
|
2023-11-08 16:41:26 +01:00
|
|
|
let database_is_empty = self.word_prefix_docids.is_empty(self.wtxn)?;
|
|
|
|
|
2021-03-25 11:10:12 +01:00
|
|
|
// We finally write the word prefix docids into the LMDB database.
|
2023-11-08 16:41:26 +01:00
|
|
|
write_sorter_into_database(
|
2021-03-25 11:10:12 +01:00
|
|
|
prefix_docids_sorter,
|
2023-11-08 16:41:26 +01:00
|
|
|
&self.word_prefix_docids,
|
|
|
|
self.wtxn,
|
|
|
|
database_is_empty,
|
|
|
|
deladd_serialize_add_side,
|
|
|
|
merge_deladd_cbo_roaring_bitmaps_into_cbo_roaring_bitmap,
|
2021-03-25 11:10:12 +01:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 15:02:04 +01:00
|
|
|
|
|
|
|
fn write_prefixes_in_sorter(
|
|
|
|
prefixes: &mut HashMap<Vec<u8>, Vec<Vec<u8>>>,
|
2024-08-30 11:49:47 +02:00
|
|
|
sorter: &mut grenad::Sorter<MergeDeladdCboRoaringBitmaps>,
|
2022-01-19 15:02:04 +01:00
|
|
|
) -> Result<()> {
|
|
|
|
for (key, data_slices) in prefixes.drain() {
|
|
|
|
for data in data_slices {
|
2022-05-03 09:57:03 +02:00
|
|
|
if valid_lmdb_key(&key) {
|
|
|
|
sorter.insert(&key, data)?;
|
|
|
|
}
|
2022-01-19 15:02:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|