2023-09-21 10:02:08 +02:00
|
|
|
use std::collections::HashSet;
|
2021-08-16 13:36:30 +02:00
|
|
|
use std::fs::File;
|
2023-09-28 16:26:01 +02:00
|
|
|
use std::io::{self, BufReader};
|
2021-08-16 13:36:30 +02:00
|
|
|
|
2023-09-18 09:59:38 +02:00
|
|
|
use obkv::KvReaderU16;
|
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
use super::helpers::{
|
2023-09-18 09:59:38 +02:00
|
|
|
create_sorter, merge_cbo_roaring_bitmaps, sorter_into_reader, try_split_array_at,
|
|
|
|
GrenadParameters,
|
2021-08-16 13:36:30 +02:00
|
|
|
};
|
2021-09-02 15:17:52 +02:00
|
|
|
use crate::error::SerializationError;
|
|
|
|
use crate::index::db_name::DOCID_WORD_POSITIONS;
|
2023-09-18 09:59:38 +02:00
|
|
|
use crate::{bucketed_position, DocumentId, Result};
|
2021-09-02 15:17:52 +02:00
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
/// Extracts the word positions and the documents ids where this word appear.
|
|
|
|
///
|
|
|
|
/// Returns a grenad reader with the list of extracted words at positions and
|
|
|
|
/// documents ids from the given chunk of docid word positions.
|
2021-08-24 13:55:53 +02:00
|
|
|
#[logging_timer::time]
|
2023-04-05 14:55:02 +02:00
|
|
|
pub fn extract_word_position_docids<R: io::Read + io::Seek>(
|
2022-02-16 15:28:48 +01:00
|
|
|
docid_word_positions: grenad::Reader<R>,
|
2021-08-16 13:36:30 +02:00
|
|
|
indexer: GrenadParameters,
|
2023-09-28 16:26:01 +02:00
|
|
|
) -> Result<grenad::Reader<BufReader<File>>> {
|
2023-07-10 18:41:54 +02:00
|
|
|
puffin::profile_function!();
|
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
let max_memory = indexer.max_memory_by_thread();
|
|
|
|
|
2021-10-05 11:18:42 +02:00
|
|
|
let mut word_position_docids_sorter = create_sorter(
|
2022-09-13 10:40:37 +02:00
|
|
|
grenad::SortAlgorithm::Unstable,
|
2021-08-16 13:36:30 +02:00
|
|
|
merge_cbo_roaring_bitmaps,
|
|
|
|
indexer.chunk_compression_type,
|
|
|
|
indexer.chunk_compression_level,
|
|
|
|
indexer.max_nb_chunks,
|
|
|
|
max_memory,
|
|
|
|
);
|
|
|
|
|
2023-09-21 10:02:08 +02:00
|
|
|
let mut word_positions: HashSet<(u16, Vec<u8>)> = HashSet::new();
|
2023-09-25 16:39:32 +02:00
|
|
|
let mut current_document_id: Option<u32> = None;
|
2021-08-16 13:36:30 +02:00
|
|
|
let mut key_buffer = Vec::new();
|
2022-02-16 15:28:48 +01:00
|
|
|
let mut cursor = docid_word_positions.into_cursor()?;
|
|
|
|
while let Some((key, value)) = cursor.move_on_next()? {
|
2023-09-21 10:02:08 +02:00
|
|
|
let (document_id_bytes, _fid_bytes) = try_split_array_at(key)
|
2022-10-13 22:02:54 +02:00
|
|
|
.ok_or(SerializationError::Decoding { db_name: Some(DOCID_WORD_POSITIONS) })?;
|
2021-08-16 13:36:30 +02:00
|
|
|
let document_id = DocumentId::from_be_bytes(document_id_bytes);
|
|
|
|
|
2023-09-21 10:02:08 +02:00
|
|
|
if current_document_id.map_or(false, |id| document_id != id) {
|
|
|
|
for (position, word_bytes) in word_positions.iter() {
|
|
|
|
key_buffer.clear();
|
|
|
|
key_buffer.extend_from_slice(word_bytes);
|
|
|
|
key_buffer.push(0);
|
|
|
|
key_buffer.extend_from_slice(&position.to_be_bytes());
|
2023-09-25 16:39:32 +02:00
|
|
|
word_position_docids_sorter
|
|
|
|
.insert(&key_buffer, current_document_id.unwrap().to_ne_bytes())?;
|
2023-09-21 10:02:08 +02:00
|
|
|
}
|
|
|
|
word_positions.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
current_document_id = Some(document_id);
|
|
|
|
|
2023-09-18 09:59:38 +02:00
|
|
|
for (position, word_bytes) in KvReaderU16::new(&value).iter() {
|
2023-09-21 10:02:08 +02:00
|
|
|
let position = bucketed_position(position);
|
|
|
|
word_positions.insert((position, word_bytes.to_vec()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(document_id) = current_document_id {
|
|
|
|
for (position, word_bytes) in word_positions {
|
2021-08-16 13:36:30 +02:00
|
|
|
key_buffer.clear();
|
2023-09-21 10:02:08 +02:00
|
|
|
key_buffer.extend_from_slice(&word_bytes);
|
2023-04-24 09:59:30 +02:00
|
|
|
key_buffer.push(0);
|
2021-08-16 13:36:30 +02:00
|
|
|
key_buffer.extend_from_slice(&position.to_be_bytes());
|
2022-11-04 00:59:58 +01:00
|
|
|
word_position_docids_sorter.insert(&key_buffer, document_id.to_ne_bytes())?;
|
2021-08-16 13:36:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 09:22:01 +01:00
|
|
|
let word_position_docids_reader = sorter_into_reader(word_position_docids_sorter, indexer)?;
|
|
|
|
|
|
|
|
Ok(word_position_docids_reader)
|
2021-08-16 13:36:30 +02:00
|
|
|
}
|