2022-01-27 18:00:18 +08:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2023-04-27 17:12:46 +08:00
|
|
|
use std::str;
|
2021-10-05 17:18:42 +08:00
|
|
|
|
2022-02-28 17:14:54 +08:00
|
|
|
use grenad::CompressionType;
|
2023-11-27 18:52:22 +08:00
|
|
|
use heed::types::Bytes;
|
2023-04-27 17:12:46 +08:00
|
|
|
use heed::{BytesDecode, BytesEncode, Database};
|
2024-02-06 17:49:23 +08:00
|
|
|
use tracing::debug;
|
2021-10-05 17:18:42 +08:00
|
|
|
|
|
|
|
use crate::error::SerializationError;
|
2023-04-07 17:09:30 +08:00
|
|
|
use crate::heed_codec::StrBEU16Codec;
|
2021-10-05 17:18:42 +08:00
|
|
|
use crate::index::main_key::WORDS_PREFIXES_FST_KEY;
|
2023-11-09 18:34:26 +08:00
|
|
|
use crate::update::del_add::{deladd_serialize_add_side, DelAdd, KvWriterDelAdd};
|
2021-10-05 17:18:42 +08:00
|
|
|
use crate::update::index_documents::{
|
2023-11-09 18:34:26 +08:00
|
|
|
create_sorter, merge_deladd_cbo_roaring_bitmaps,
|
|
|
|
merge_deladd_cbo_roaring_bitmaps_into_cbo_roaring_bitmap, valid_lmdb_key,
|
|
|
|
write_sorter_into_database, CursorClonableMmap, MergeFn,
|
2021-10-05 17:18:42 +08:00
|
|
|
};
|
2023-04-27 17:12:46 +08:00
|
|
|
use crate::{CboRoaringBitmapCodec, Result};
|
2021-10-05 17:18:42 +08:00
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
pub struct WordPrefixIntegerDocids<'t, 'i> {
|
|
|
|
wtxn: &'t mut heed::RwTxn<'i>,
|
2023-04-27 17:12:46 +08:00
|
|
|
prefix_database: Database<StrBEU16Codec, CboRoaringBitmapCodec>,
|
|
|
|
word_database: Database<StrBEU16Codec, CboRoaringBitmapCodec>,
|
2021-10-05 17:18:42 +08: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-23 01:21:19 +08:00
|
|
|
impl<'t, 'i> WordPrefixIntegerDocids<'t, 'i> {
|
2021-10-05 17:18:42 +08:00
|
|
|
pub fn new(
|
2023-11-23 01:21:19 +08:00
|
|
|
wtxn: &'t mut heed::RwTxn<'i>,
|
2023-04-27 17:12:46 +08:00
|
|
|
prefix_database: Database<StrBEU16Codec, CboRoaringBitmapCodec>,
|
|
|
|
word_database: Database<StrBEU16Codec, CboRoaringBitmapCodec>,
|
2023-11-23 01:21:19 +08:00
|
|
|
) -> WordPrefixIntegerDocids<'t, 'i> {
|
2023-04-27 17:12:46 +08:00
|
|
|
WordPrefixIntegerDocids {
|
2021-10-05 17:18:42 +08:00
|
|
|
wtxn,
|
2023-04-27 17:12:46 +08:00
|
|
|
prefix_database,
|
|
|
|
word_database,
|
2021-10-05 17:18:42 +08:00
|
|
|
chunk_compression_type: CompressionType::None,
|
|
|
|
chunk_compression_level: None,
|
|
|
|
max_nb_chunks: None,
|
|
|
|
max_memory: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 16:42:48 +08:00
|
|
|
#[tracing::instrument(
|
|
|
|
level = "trace",
|
|
|
|
skip_all,
|
|
|
|
target = "indexing::prefix",
|
|
|
|
name = "words_prefix_integer_docids"
|
|
|
|
)]
|
2022-01-27 18:00:18 +08:00
|
|
|
pub fn execute(
|
2022-01-25 21:06:45 +08:00
|
|
|
self,
|
2024-01-22 23:23:12 +08:00
|
|
|
new_word_integer_docids: grenad::Merger<CursorClonableMmap, MergeFn>,
|
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-25 21:06:45 +08:00
|
|
|
) -> Result<()> {
|
2023-07-11 00:41:54 +08:00
|
|
|
puffin::profile_function!();
|
2023-04-27 17:12:46 +08:00
|
|
|
debug!("Computing and writing the word levels integers docids into LMDB on disk...");
|
2021-10-05 17:18:42 +08:00
|
|
|
|
2023-04-27 17:12:46 +08:00
|
|
|
let mut prefix_integer_docids_sorter = create_sorter(
|
2022-09-13 16:40:37 +08:00
|
|
|
grenad::SortAlgorithm::Unstable,
|
2023-11-09 18:34:26 +08:00
|
|
|
merge_deladd_cbo_roaring_bitmaps,
|
2021-10-05 17:18:42 +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 fetch all the new common prefixes between the previous and new prefix fst.
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
let mut current_prefixes: Option<&&[String]> = None;
|
|
|
|
let mut prefixes_cache = HashMap::new();
|
2024-01-22 23:23:12 +08:00
|
|
|
let mut new_word_integer_docids_iter =
|
|
|
|
new_word_integer_docids.into_stream_merger_iter()?;
|
|
|
|
while let Some((key, data)) = new_word_integer_docids_iter.next()? {
|
2023-11-23 01:21:19 +08:00
|
|
|
let (word, pos) =
|
|
|
|
StrBEU16Codec::bytes_decode(key).map_err(heed::Error::Decoding)?;
|
2022-01-25 21:06:45 +08:00
|
|
|
|
2022-03-02 01:02:12 +08:00
|
|
|
current_prefixes = match current_prefixes.take() {
|
|
|
|
Some(prefixes) if word.starts_with(&prefixes[0]) => Some(prefixes),
|
|
|
|
_otherwise => {
|
|
|
|
write_prefixes_in_sorter(
|
|
|
|
&mut prefixes_cache,
|
2023-04-27 17:12:46 +08:00
|
|
|
&mut prefix_integer_docids_sorter,
|
2022-03-02 01:02:12 +08:00
|
|
|
)?;
|
|
|
|
common_prefix_fst_words
|
|
|
|
.iter()
|
|
|
|
.find(|prefixes| word.starts_with(&prefixes[0]))
|
|
|
|
}
|
|
|
|
};
|
2022-01-25 21:06:45 +08:00
|
|
|
|
2022-03-02 01:02:12 +08:00
|
|
|
if let Some(prefixes) = current_prefixes {
|
|
|
|
for prefix in prefixes.iter() {
|
|
|
|
if word.starts_with(prefix) {
|
|
|
|
buffer.clear();
|
|
|
|
buffer.extend_from_slice(prefix.as_bytes());
|
2023-04-27 17:12:46 +08:00
|
|
|
buffer.push(0);
|
2022-03-02 01:02:12 +08:00
|
|
|
buffer.extend_from_slice(&pos.to_be_bytes());
|
|
|
|
match prefixes_cache.get_mut(&buffer) {
|
|
|
|
Some(value) => value.push(data.to_owned()),
|
|
|
|
None => {
|
|
|
|
prefixes_cache.insert(buffer.clone(), vec![data.to_owned()]);
|
|
|
|
}
|
2022-01-25 21:06:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:46 +08:00
|
|
|
write_prefixes_in_sorter(&mut prefixes_cache, &mut prefix_integer_docids_sorter)?;
|
2022-03-02 01:02:12 +08:00
|
|
|
}
|
2022-01-25 21:06:45 +08:00
|
|
|
|
|
|
|
// We fetch the docids associated to the newly added word prefix fst only.
|
2023-11-27 18:52:22 +08:00
|
|
|
let db = self.word_database.remap_data_type::<Bytes>();
|
2023-11-09 18:34:26 +08:00
|
|
|
let mut buffer = Vec::new();
|
2022-01-27 18:00:18 +08:00
|
|
|
for prefix_bytes in new_prefix_fst_words {
|
|
|
|
let prefix = str::from_utf8(prefix_bytes.as_bytes()).map_err(|_| {
|
2021-10-05 17:18:42 +08:00
|
|
|
SerializationError::Decoding { db_name: Some(WORDS_PREFIXES_FST_KEY) }
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// iter over all lines of the DB where the key is prefixed by the current prefix.
|
2022-01-25 21:06:45 +08:00
|
|
|
let iter = db
|
2023-11-27 18:52:22 +08:00
|
|
|
.remap_key_type::<Bytes>()
|
2022-01-27 18:00:18 +08:00
|
|
|
.prefix_iter(self.wtxn, prefix_bytes.as_bytes())?
|
2023-03-23 16:22:01 +08:00
|
|
|
.remap_key_type::<StrBEU16Codec>();
|
2022-01-25 21:06:45 +08:00
|
|
|
for result in iter {
|
|
|
|
let ((word, pos), data) = result?;
|
|
|
|
if word.starts_with(prefix) {
|
|
|
|
let key = (prefix, pos);
|
2023-03-23 16:22:01 +08:00
|
|
|
let bytes = StrBEU16Codec::bytes_encode(&key).unwrap();
|
2023-11-09 18:34:26 +08:00
|
|
|
|
|
|
|
buffer.clear();
|
|
|
|
let mut writer = KvWriterDelAdd::new(&mut buffer);
|
|
|
|
writer.insert(DelAdd::Addition, data)?;
|
|
|
|
prefix_integer_docids_sorter.insert(bytes, writer.into_inner()?)?;
|
2022-01-25 21:06:45 +08:00
|
|
|
}
|
2021-10-05 17:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:46 +08:00
|
|
|
// We remove all the entries that are no more required in this word prefix integer
|
2022-01-25 21:06:45 +08:00
|
|
|
// docids database.
|
2023-04-27 17:12:46 +08:00
|
|
|
// We also avoid iterating over the whole `word_prefix_integer_docids` database if we know in
|
2023-01-31 18:42:24 +08:00
|
|
|
// advance that the `if del_prefix_fst_words.contains(prefix.as_bytes()) {` condition below
|
|
|
|
// will always be false (i.e. if `del_prefix_fst_words` is empty).
|
|
|
|
if !del_prefix_fst_words.is_empty() {
|
2023-04-27 17:12:46 +08:00
|
|
|
let mut iter = self.prefix_database.iter_mut(self.wtxn)?.lazily_decode_data();
|
2023-01-31 18:42:24 +08:00
|
|
|
while let Some(((prefix, _), _)) = iter.next().transpose()? {
|
|
|
|
if del_prefix_fst_words.contains(prefix.as_bytes()) {
|
|
|
|
unsafe { iter.del_current()? };
|
|
|
|
}
|
2022-01-25 21:06:45 +08:00
|
|
|
}
|
2023-01-31 18:42:24 +08:00
|
|
|
drop(iter);
|
2022-01-25 21:06:45 +08:00
|
|
|
}
|
|
|
|
|
2023-11-09 18:34:26 +08:00
|
|
|
let database_is_empty = self.prefix_database.is_empty(self.wtxn)?;
|
|
|
|
|
2023-04-27 17:12:46 +08:00
|
|
|
// We finally write all the word prefix integer docids into the LMDB database.
|
2023-11-09 18:34:26 +08:00
|
|
|
write_sorter_into_database(
|
2023-04-27 17:12:46 +08:00
|
|
|
prefix_integer_docids_sorter,
|
2023-11-09 18:34:26 +08:00
|
|
|
&self.prefix_database,
|
|
|
|
self.wtxn,
|
|
|
|
database_is_empty,
|
|
|
|
deladd_serialize_add_side,
|
|
|
|
merge_deladd_cbo_roaring_bitmaps_into_cbo_roaring_bitmap,
|
2021-10-05 17:18:42 +08:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 21:06:45 +08:00
|
|
|
|
|
|
|
fn write_prefixes_in_sorter(
|
|
|
|
prefixes: &mut HashMap<Vec<u8>, Vec<Vec<u8>>>,
|
|
|
|
sorter: &mut grenad::Sorter<MergeFn>,
|
|
|
|
) -> Result<()> {
|
2023-11-09 18:34:26 +08:00
|
|
|
// TODO: Merge before insertion.
|
2022-01-25 21:06:45 +08:00
|
|
|
for (key, data_slices) in prefixes.drain() {
|
|
|
|
for data in data_slices {
|
2022-05-03 15:57:03 +08:00
|
|
|
if valid_lmdb_key(&key) {
|
|
|
|
sorter.insert(&key, data)?;
|
|
|
|
}
|
2022-01-25 21:06:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|