2022-03-24 17:00:29 +01:00
|
|
|
use std::collections::HashSet;
|
2021-08-16 13:36:30 +02:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
use std::iter::FromIterator;
|
|
|
|
|
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
|
|
|
use super::helpers::{
|
|
|
|
create_sorter, merge_roaring_bitmaps, serialize_roaring_bitmap, sorter_into_reader,
|
|
|
|
try_split_array_at, GrenadParameters,
|
|
|
|
};
|
2021-09-02 15:17:52 +02:00
|
|
|
use crate::error::SerializationError;
|
|
|
|
use crate::index::db_name::DOCID_WORD_POSITIONS;
|
2022-03-24 17:00:29 +01:00
|
|
|
use crate::update::index_documents::helpers::read_u32_ne_bytes;
|
2022-04-05 09:50:34 +02:00
|
|
|
use crate::{relative_from_absolute_position, FieldId, Result};
|
2021-08-16 13:36:30 +02:00
|
|
|
|
|
|
|
/// Extracts the word and the documents ids where this word appear.
|
|
|
|
///
|
|
|
|
/// Returns a grenad reader with the list of extracted words and
|
|
|
|
/// documents ids from the given chunk of docid word positions.
|
2022-03-24 15:22:57 +01:00
|
|
|
///
|
2022-04-04 20:59:20 +02:00
|
|
|
/// The first returned reader is the one for normal word_docids, and the second one is for
|
2022-03-24 15:22:57 +01:00
|
|
|
/// exact_word_docids
|
2021-08-24 13:55:53 +02:00
|
|
|
#[logging_timer::time]
|
2022-02-16 15:28:48 +01:00
|
|
|
pub fn extract_word_docids<R: io::Read + io::Seek>(
|
|
|
|
docid_word_positions: grenad::Reader<R>,
|
2021-08-16 13:36:30 +02:00
|
|
|
indexer: GrenadParameters,
|
2022-03-24 17:00:29 +01:00
|
|
|
exact_attributes: &HashSet<FieldId>,
|
2022-03-24 15:22:57 +01:00
|
|
|
) -> Result<(grenad::Reader<File>, grenad::Reader<File>)> {
|
2021-08-16 13:36:30 +02:00
|
|
|
let max_memory = indexer.max_memory_by_thread();
|
|
|
|
|
|
|
|
let mut word_docids_sorter = create_sorter(
|
2022-09-13 10:40:37 +02:00
|
|
|
grenad::SortAlgorithm::Unstable,
|
2021-08-16 13:36:30 +02:00
|
|
|
merge_roaring_bitmaps,
|
|
|
|
indexer.chunk_compression_type,
|
|
|
|
indexer.chunk_compression_level,
|
|
|
|
indexer.max_nb_chunks,
|
2022-04-05 14:14:15 +02:00
|
|
|
max_memory.map(|x| x / 2),
|
2021-08-16 13:36:30 +02:00
|
|
|
);
|
|
|
|
|
2022-03-24 17:00:29 +01:00
|
|
|
let mut exact_word_docids_sorter = create_sorter(
|
2022-09-13 10:40:37 +02:00
|
|
|
grenad::SortAlgorithm::Unstable,
|
2022-03-24 17:00:29 +01:00
|
|
|
merge_roaring_bitmaps,
|
|
|
|
indexer.chunk_compression_type,
|
|
|
|
indexer.chunk_compression_level,
|
|
|
|
indexer.max_nb_chunks,
|
2022-04-05 14:14:15 +02:00
|
|
|
max_memory.map(|x| x / 2),
|
2022-03-24 17:00:29 +01:00
|
|
|
);
|
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
let mut value_buffer = Vec::new();
|
2022-02-16 15:28:48 +01:00
|
|
|
let mut cursor = docid_word_positions.into_cursor()?;
|
2022-03-24 17:00:29 +01:00
|
|
|
while let Some((key, positions)) = cursor.move_on_next()? {
|
2021-09-02 15:17:52 +02:00
|
|
|
let (document_id_bytes, word_bytes) = try_split_array_at(key)
|
|
|
|
.ok_or_else(|| SerializationError::Decoding { db_name: Some(DOCID_WORD_POSITIONS) })?;
|
2021-08-16 13:36:30 +02:00
|
|
|
let document_id = u32::from_be_bytes(document_id_bytes);
|
|
|
|
|
|
|
|
let bitmap = RoaringBitmap::from_iter(Some(document_id));
|
|
|
|
serialize_roaring_bitmap(&bitmap, &mut value_buffer)?;
|
2022-03-24 17:00:29 +01:00
|
|
|
|
|
|
|
// If there are no exact attributes, we do not need to iterate over positions.
|
|
|
|
if exact_attributes.is_empty() {
|
|
|
|
word_docids_sorter.insert(word_bytes, &value_buffer)?;
|
|
|
|
} else {
|
|
|
|
let mut added_to_exact = false;
|
|
|
|
let mut added_to_word_docids = false;
|
|
|
|
for position in read_u32_ne_bytes(positions) {
|
|
|
|
// as soon as we know that this word had been to both readers, we don't need to
|
|
|
|
// iterate over the positions.
|
|
|
|
if added_to_exact && added_to_word_docids {
|
|
|
|
break;
|
|
|
|
}
|
2022-04-05 09:50:34 +02:00
|
|
|
let (fid, _) = relative_from_absolute_position(position);
|
2022-03-24 17:00:29 +01:00
|
|
|
if exact_attributes.contains(&fid) && !added_to_exact {
|
|
|
|
exact_word_docids_sorter.insert(word_bytes, &value_buffer)?;
|
|
|
|
added_to_exact = true;
|
|
|
|
} else if !added_to_word_docids {
|
|
|
|
word_docids_sorter.insert(word_bytes, &value_buffer)?;
|
|
|
|
added_to_word_docids = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-16 13:36:30 +02:00
|
|
|
}
|
|
|
|
|
2022-03-24 15:22:57 +01:00
|
|
|
Ok((
|
|
|
|
sorter_into_reader(word_docids_sorter, indexer)?,
|
2022-03-24 17:00:29 +01:00
|
|
|
sorter_into_reader(exact_word_docids_sorter, indexer)?,
|
2022-03-24 15:22:57 +01:00
|
|
|
))
|
2021-08-16 13:36:30 +02:00
|
|
|
}
|