2024-08-29 17:51:42 +02:00
|
|
|
use std::fs::File;
|
2024-09-04 11:39:53 +02:00
|
|
|
use std::marker::PhantomData;
|
2024-08-29 17:51:42 +02:00
|
|
|
|
2024-08-29 18:27:02 +02:00
|
|
|
use crossbeam_channel::{IntoIter, Receiver, SendError, Sender};
|
2024-09-03 11:02:39 +02:00
|
|
|
use grenad::Merger;
|
2024-08-29 15:07:59 +02:00
|
|
|
use heed::types::Bytes;
|
2024-09-04 14:30:09 +02:00
|
|
|
use memmap2::Mmap;
|
2024-08-29 15:07:59 +02:00
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
use super::extract::FacetKind;
|
2024-08-29 15:07:59 +02:00
|
|
|
use super::StdResult;
|
2024-09-04 09:59:19 +02:00
|
|
|
use crate::index::main_key::{DOCUMENTS_IDS_KEY, WORDS_FST_KEY};
|
2024-09-02 10:42:19 +02:00
|
|
|
use crate::update::new::KvReaderFieldId;
|
2024-09-03 11:02:39 +02:00
|
|
|
use crate::update::MergeDeladdCboRoaringBitmaps;
|
2024-09-04 14:30:09 +02:00
|
|
|
use crate::{DocumentId, Index};
|
2024-08-29 15:07:59 +02:00
|
|
|
|
|
|
|
/// The capacity of the channel is currently in number of messages.
|
2024-09-02 15:10:21 +02:00
|
|
|
pub fn merger_writer_channel(cap: usize) -> (MergerSender, WriterReceiver) {
|
2024-08-29 15:07:59 +02:00
|
|
|
let (sender, receiver) = crossbeam_channel::bounded(cap);
|
2024-09-23 11:07:59 +02:00
|
|
|
(
|
|
|
|
MergerSender {
|
|
|
|
sender,
|
|
|
|
send_count: Default::default(),
|
|
|
|
contentious_count: Default::default(),
|
|
|
|
},
|
|
|
|
WriterReceiver(receiver),
|
|
|
|
)
|
2024-08-29 15:07:59 +02:00
|
|
|
}
|
|
|
|
|
2024-08-29 17:51:42 +02:00
|
|
|
/// The capacity of the channel is currently in number of messages.
|
2024-09-04 11:39:53 +02:00
|
|
|
pub fn extractors_merger_channels(cap: usize) -> (ExtractorSender, MergerReceiver) {
|
2024-08-29 17:51:42 +02:00
|
|
|
let (sender, receiver) = crossbeam_channel::bounded(cap);
|
2024-09-04 11:39:53 +02:00
|
|
|
(ExtractorSender(sender), MergerReceiver(receiver))
|
2024-08-29 17:51:42 +02:00
|
|
|
}
|
|
|
|
|
2024-09-04 14:30:09 +02:00
|
|
|
pub enum KeyValueEntry {
|
|
|
|
SmallInMemory { key_length: usize, data: Box<[u8]> },
|
|
|
|
LargeOnDisk { key: Box<[u8]>, value: Mmap },
|
2024-08-29 15:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyValueEntry {
|
2024-09-04 14:30:09 +02:00
|
|
|
pub fn from_small_key_value(key: &[u8], value: &[u8]) -> Self {
|
2024-08-29 17:51:42 +02:00
|
|
|
let mut data = Vec::with_capacity(key.len() + value.len());
|
|
|
|
data.extend_from_slice(key);
|
|
|
|
data.extend_from_slice(value);
|
2024-09-04 14:30:09 +02:00
|
|
|
KeyValueEntry::SmallInMemory { key_length: key.len(), data: data.into_boxed_slice() }
|
|
|
|
}
|
2024-08-29 17:51:42 +02:00
|
|
|
|
2024-09-04 14:30:09 +02:00
|
|
|
pub fn from_large_key_value(key: &[u8], value: Mmap) -> Self {
|
|
|
|
KeyValueEntry::LargeOnDisk { key: key.to_vec().into_boxed_slice(), value }
|
2024-08-29 17:51:42 +02:00
|
|
|
}
|
|
|
|
|
2024-08-29 18:27:02 +02:00
|
|
|
pub fn key(&self) -> &[u8] {
|
2024-09-04 14:30:09 +02:00
|
|
|
match self {
|
|
|
|
KeyValueEntry::SmallInMemory { key_length, data } => &data.as_ref()[..*key_length],
|
|
|
|
KeyValueEntry::LargeOnDisk { key, value: _ } => key.as_ref(),
|
|
|
|
}
|
2024-08-29 18:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn value(&self) -> &[u8] {
|
2024-09-04 14:30:09 +02:00
|
|
|
match self {
|
|
|
|
KeyValueEntry::SmallInMemory { key_length, data } => &data.as_ref()[*key_length..],
|
|
|
|
KeyValueEntry::LargeOnDisk { key: _, value } => value.as_ref(),
|
|
|
|
}
|
2024-08-29 15:07:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-29 17:51:42 +02:00
|
|
|
pub struct KeyEntry {
|
|
|
|
data: Box<[u8]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyEntry {
|
|
|
|
pub fn from_key(key: &[u8]) -> Self {
|
|
|
|
KeyEntry { data: key.to_vec().into_boxed_slice() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn entry(&self) -> &[u8] {
|
|
|
|
self.data.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-29 18:27:02 +02:00
|
|
|
pub enum EntryOperation {
|
2024-08-29 17:51:42 +02:00
|
|
|
Delete(KeyEntry),
|
|
|
|
Write(KeyValueEntry),
|
|
|
|
}
|
|
|
|
|
2024-08-29 15:07:59 +02:00
|
|
|
pub struct DocumentEntry {
|
|
|
|
docid: DocumentId,
|
|
|
|
content: Box<[u8]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentEntry {
|
|
|
|
pub fn new_uncompressed(docid: DocumentId, content: Box<KvReaderFieldId>) -> Self {
|
|
|
|
DocumentEntry { docid, content: content.into() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_compressed(docid: DocumentId, content: Box<[u8]>) -> Self {
|
|
|
|
DocumentEntry { docid, content }
|
|
|
|
}
|
|
|
|
|
2024-08-29 18:27:02 +02:00
|
|
|
pub fn key(&self) -> [u8; 4] {
|
|
|
|
self.docid.to_be_bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn content(&self) -> &[u8] {
|
|
|
|
&self.content
|
2024-08-29 15:07:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 09:59:19 +02:00
|
|
|
pub struct DocumentDeletionEntry(DocumentId);
|
|
|
|
|
|
|
|
impl DocumentDeletionEntry {
|
|
|
|
pub fn key(&self) -> [u8; 4] {
|
|
|
|
self.0.to_be_bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WriterOperation {
|
|
|
|
database: Database,
|
|
|
|
entry: EntryOperation,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Database {
|
2024-09-05 10:56:22 +02:00
|
|
|
Documents,
|
2024-09-04 12:17:13 +02:00
|
|
|
ExactWordDocids,
|
2024-09-05 10:56:22 +02:00
|
|
|
FidWordCountDocids,
|
|
|
|
Main,
|
|
|
|
WordDocids,
|
2024-09-04 11:39:53 +02:00
|
|
|
WordFidDocids,
|
2024-09-05 10:56:22 +02:00
|
|
|
WordPairProximityDocids,
|
2024-09-04 12:17:13 +02:00
|
|
|
WordPositionDocids,
|
2024-09-16 09:34:10 +02:00
|
|
|
FacetIdIsNullDocids,
|
|
|
|
FacetIdIsEmptyDocids,
|
|
|
|
FacetIdExistsDocids,
|
|
|
|
FacetIdF64NumberDocids,
|
|
|
|
FacetIdStringDocids,
|
2024-08-29 15:07:59 +02:00
|
|
|
}
|
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
impl Database {
|
2024-08-29 15:07:59 +02:00
|
|
|
pub fn database(&self, index: &Index) -> heed::Database<Bytes, Bytes> {
|
2024-09-16 09:34:10 +02:00
|
|
|
match self {
|
2024-09-04 09:59:19 +02:00
|
|
|
Database::Documents => index.documents.remap_types(),
|
2024-09-04 12:17:13 +02:00
|
|
|
Database::ExactWordDocids => index.exact_word_docids.remap_types(),
|
2024-09-05 10:56:22 +02:00
|
|
|
Database::Main => index.main.remap_types(),
|
|
|
|
Database::WordDocids => index.word_docids.remap_types(),
|
2024-09-04 11:39:53 +02:00
|
|
|
Database::WordFidDocids => index.word_fid_docids.remap_types(),
|
2024-09-04 12:17:13 +02:00
|
|
|
Database::WordPositionDocids => index.word_position_docids.remap_types(),
|
2024-09-05 10:56:22 +02:00
|
|
|
Database::FidWordCountDocids => index.field_id_word_count_docids.remap_types(),
|
|
|
|
Database::WordPairProximityDocids => index.word_pair_proximity_docids.remap_types(),
|
2024-09-16 09:34:10 +02:00
|
|
|
Database::FacetIdIsNullDocids => index.facet_id_is_null_docids.remap_types(),
|
|
|
|
Database::FacetIdIsEmptyDocids => index.facet_id_is_empty_docids.remap_types(),
|
|
|
|
Database::FacetIdExistsDocids => index.facet_id_exists_docids.remap_types(),
|
|
|
|
Database::FacetIdF64NumberDocids => index.facet_id_f64_docids.remap_types(),
|
|
|
|
Database::FacetIdStringDocids => index.facet_id_string_docids.remap_types(),
|
2024-08-29 15:07:59 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-16 09:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WriterOperation {
|
|
|
|
pub fn database(&self, index: &Index) -> heed::Database<Bytes, Bytes> {
|
|
|
|
self.database.database(index)
|
|
|
|
}
|
2024-09-04 09:59:19 +02:00
|
|
|
|
|
|
|
pub fn entry(self) -> EntryOperation {
|
|
|
|
self.entry
|
|
|
|
}
|
2024-08-29 15:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WriterReceiver(Receiver<WriterOperation>);
|
|
|
|
|
2024-08-29 18:27:02 +02:00
|
|
|
impl IntoIterator for WriterReceiver {
|
|
|
|
type Item = WriterOperation;
|
|
|
|
type IntoIter = IntoIter<Self::Item>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
2024-08-29 15:07:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 11:07:59 +02:00
|
|
|
pub struct MergerSender {
|
|
|
|
sender: Sender<WriterOperation>,
|
|
|
|
/// The number of message we send in total in the channel.
|
|
|
|
send_count: std::cell::Cell<usize>,
|
|
|
|
/// The number of times we sent something in a channel that was full.
|
|
|
|
contentious_count: std::cell::Cell<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for MergerSender {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
tracing::info!(
|
|
|
|
"Merger channel stats: {} sends, {} were contentious (ratio {})",
|
|
|
|
self.send_count.get(),
|
|
|
|
self.contentious_count.get(),
|
|
|
|
self.contentious_count.get() as f64 / self.send_count.get() as f64
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2024-08-29 15:07:59 +02:00
|
|
|
|
2024-08-29 17:51:42 +02:00
|
|
|
impl MergerSender {
|
2024-09-04 09:59:19 +02:00
|
|
|
pub fn main(&self) -> MainSender<'_> {
|
2024-09-23 11:07:59 +02:00
|
|
|
MainSender(self)
|
2024-09-04 09:59:19 +02:00
|
|
|
}
|
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
pub fn docids<D: DatabaseType>(&self) -> WordDocidsSender<'_, D> {
|
2024-09-23 11:07:59 +02:00
|
|
|
WordDocidsSender { sender: self, _marker: PhantomData }
|
2024-09-16 09:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn facet_docids(&self) -> FacetDocidsSender<'_> {
|
2024-09-23 11:07:59 +02:00
|
|
|
FacetDocidsSender { sender: self }
|
2024-08-29 17:51:42 +02:00
|
|
|
}
|
2024-09-04 09:59:19 +02:00
|
|
|
|
|
|
|
pub fn documents(&self) -> DocumentsSender<'_> {
|
2024-09-23 11:07:59 +02:00
|
|
|
DocumentsSender(self)
|
2024-09-04 09:59:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send_documents_ids(&self, bitmap: &[u8]) -> StdResult<(), SendError<()>> {
|
2024-09-04 14:30:09 +02:00
|
|
|
let entry = EntryOperation::Write(KeyValueEntry::from_small_key_value(
|
2024-09-04 09:59:19 +02:00
|
|
|
DOCUMENTS_IDS_KEY.as_bytes(),
|
|
|
|
bitmap,
|
|
|
|
));
|
2024-09-23 11:07:59 +02:00
|
|
|
match self.send(WriterOperation { database: Database::Main, entry }) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send(&self, op: WriterOperation) -> StdResult<(), SendError<()>> {
|
|
|
|
if self.sender.is_full() {
|
|
|
|
self.contentious_count.set(self.contentious_count.get() + 1);
|
|
|
|
}
|
|
|
|
self.send_count.set(self.send_count.get() + 1);
|
|
|
|
match self.sender.send(op) {
|
2024-09-04 09:59:19 +02:00
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 11:07:59 +02:00
|
|
|
pub struct MainSender<'a>(&'a MergerSender);
|
2024-09-04 09:59:19 +02:00
|
|
|
|
|
|
|
impl MainSender<'_> {
|
2024-09-04 14:30:09 +02:00
|
|
|
pub fn write_words_fst(&self, value: Mmap) -> StdResult<(), SendError<()>> {
|
|
|
|
let entry = EntryOperation::Write(KeyValueEntry::from_large_key_value(
|
|
|
|
WORDS_FST_KEY.as_bytes(),
|
|
|
|
value,
|
|
|
|
));
|
2024-09-04 09:59:19 +02:00
|
|
|
match self.0.send(WriterOperation { database: Database::Main, entry }) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete(&self, key: &[u8]) -> StdResult<(), SendError<()>> {
|
|
|
|
let entry = EntryOperation::Delete(KeyEntry::from_key(key));
|
|
|
|
match self.0.send(WriterOperation { database: Database::Main, entry }) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
2024-08-29 17:51:42 +02:00
|
|
|
}
|
|
|
|
|
2024-09-04 12:17:13 +02:00
|
|
|
pub enum ExactWordDocids {}
|
2024-09-05 10:56:22 +02:00
|
|
|
pub enum FidWordCountDocids {}
|
|
|
|
pub enum WordDocids {}
|
2024-09-04 11:39:53 +02:00
|
|
|
pub enum WordFidDocids {}
|
2024-09-05 10:56:22 +02:00
|
|
|
pub enum WordPairProximityDocids {}
|
2024-09-04 12:17:13 +02:00
|
|
|
pub enum WordPositionDocids {}
|
2024-09-16 09:34:10 +02:00
|
|
|
pub enum FacetDocids {}
|
2024-09-04 11:39:53 +02:00
|
|
|
|
|
|
|
pub trait DatabaseType {
|
2024-09-04 12:17:13 +02:00
|
|
|
const DATABASE: Database;
|
2024-09-16 09:34:10 +02:00
|
|
|
}
|
2024-09-04 12:17:13 +02:00
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
pub trait MergerOperationType {
|
2024-09-04 12:17:13 +02:00
|
|
|
fn new_merger_operation(merger: Merger<File, MergeDeladdCboRoaringBitmaps>) -> MergerOperation;
|
2024-09-04 11:39:53 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 10:56:22 +02:00
|
|
|
impl DatabaseType for ExactWordDocids {
|
|
|
|
const DATABASE: Database = Database::ExactWordDocids;
|
2024-09-16 09:34:10 +02:00
|
|
|
}
|
2024-09-04 12:17:13 +02:00
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
impl MergerOperationType for ExactWordDocids {
|
2024-09-04 12:17:13 +02:00
|
|
|
fn new_merger_operation(merger: Merger<File, MergeDeladdCboRoaringBitmaps>) -> MergerOperation {
|
2024-09-05 10:56:22 +02:00
|
|
|
MergerOperation::ExactWordDocidsMerger(merger)
|
2024-09-04 12:17:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 10:56:22 +02:00
|
|
|
impl DatabaseType for FidWordCountDocids {
|
|
|
|
const DATABASE: Database = Database::FidWordCountDocids;
|
2024-09-16 09:34:10 +02:00
|
|
|
}
|
2024-09-04 12:17:13 +02:00
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
impl MergerOperationType for FidWordCountDocids {
|
2024-09-04 12:17:13 +02:00
|
|
|
fn new_merger_operation(merger: Merger<File, MergeDeladdCboRoaringBitmaps>) -> MergerOperation {
|
2024-09-05 10:56:22 +02:00
|
|
|
MergerOperation::FidWordCountDocidsMerger(merger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DatabaseType for WordDocids {
|
|
|
|
const DATABASE: Database = Database::WordDocids;
|
2024-09-16 09:34:10 +02:00
|
|
|
}
|
2024-09-05 10:56:22 +02:00
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
impl MergerOperationType for WordDocids {
|
2024-09-05 10:56:22 +02:00
|
|
|
fn new_merger_operation(merger: Merger<File, MergeDeladdCboRoaringBitmaps>) -> MergerOperation {
|
|
|
|
MergerOperation::WordDocidsMerger(merger)
|
2024-09-04 11:39:53 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-29 17:51:42 +02:00
|
|
|
|
2024-09-04 11:39:53 +02:00
|
|
|
impl DatabaseType for WordFidDocids {
|
2024-09-04 12:17:13 +02:00
|
|
|
const DATABASE: Database = Database::WordFidDocids;
|
2024-09-16 09:34:10 +02:00
|
|
|
}
|
2024-09-04 12:17:13 +02:00
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
impl MergerOperationType for WordFidDocids {
|
2024-09-04 12:17:13 +02:00
|
|
|
fn new_merger_operation(merger: Merger<File, MergeDeladdCboRoaringBitmaps>) -> MergerOperation {
|
|
|
|
MergerOperation::WordFidDocidsMerger(merger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 10:56:22 +02:00
|
|
|
impl DatabaseType for WordPairProximityDocids {
|
|
|
|
const DATABASE: Database = Database::WordPairProximityDocids;
|
2024-09-16 09:34:10 +02:00
|
|
|
}
|
2024-09-05 10:56:22 +02:00
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
impl MergerOperationType for WordPairProximityDocids {
|
2024-09-05 10:56:22 +02:00
|
|
|
fn new_merger_operation(merger: Merger<File, MergeDeladdCboRoaringBitmaps>) -> MergerOperation {
|
|
|
|
MergerOperation::WordPairProximityDocidsMerger(merger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 12:17:13 +02:00
|
|
|
impl DatabaseType for WordPositionDocids {
|
|
|
|
const DATABASE: Database = Database::WordPositionDocids;
|
2024-09-16 09:34:10 +02:00
|
|
|
}
|
2024-09-04 12:17:13 +02:00
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
impl MergerOperationType for WordPositionDocids {
|
2024-09-04 12:17:13 +02:00
|
|
|
fn new_merger_operation(merger: Merger<File, MergeDeladdCboRoaringBitmaps>) -> MergerOperation {
|
|
|
|
MergerOperation::WordPositionDocidsMerger(merger)
|
2024-09-04 11:39:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
impl MergerOperationType for FacetDocids {
|
|
|
|
fn new_merger_operation(merger: Merger<File, MergeDeladdCboRoaringBitmaps>) -> MergerOperation {
|
|
|
|
MergerOperation::FacetDocidsMerger(merger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait DocidsSender {
|
|
|
|
fn write(&self, key: &[u8], value: &[u8]) -> StdResult<(), SendError<()>>;
|
|
|
|
fn delete(&self, key: &[u8]) -> StdResult<(), SendError<()>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WordDocidsSender<'a, D> {
|
2024-09-23 11:07:59 +02:00
|
|
|
sender: &'a MergerSender,
|
2024-09-04 11:39:53 +02:00
|
|
|
_marker: PhantomData<D>,
|
|
|
|
}
|
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
impl<D: DatabaseType> DocidsSender for WordDocidsSender<'_, D> {
|
|
|
|
fn write(&self, key: &[u8], value: &[u8]) -> StdResult<(), SendError<()>> {
|
2024-09-04 14:30:09 +02:00
|
|
|
let entry = EntryOperation::Write(KeyValueEntry::from_small_key_value(key, value));
|
2024-09-04 12:17:13 +02:00
|
|
|
match self.sender.send(WriterOperation { database: D::DATABASE, entry }) {
|
2024-08-29 17:51:42 +02:00
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
fn delete(&self, key: &[u8]) -> StdResult<(), SendError<()>> {
|
2024-09-04 09:59:19 +02:00
|
|
|
let entry = EntryOperation::Delete(KeyEntry::from_key(key));
|
2024-09-04 12:17:13 +02:00
|
|
|
match self.sender.send(WriterOperation { database: D::DATABASE, entry }) {
|
2024-08-29 17:51:42 +02:00
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
pub struct FacetDocidsSender<'a> {
|
2024-09-23 11:07:59 +02:00
|
|
|
sender: &'a MergerSender,
|
2024-09-16 09:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocidsSender for FacetDocidsSender<'_> {
|
|
|
|
fn write(&self, key: &[u8], value: &[u8]) -> StdResult<(), SendError<()>> {
|
|
|
|
let (database, key) = self.extract_database(key);
|
|
|
|
let entry = EntryOperation::Write(KeyValueEntry::from_small_key_value(key, value));
|
|
|
|
match self.sender.send(WriterOperation { database, entry }) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn delete(&self, key: &[u8]) -> StdResult<(), SendError<()>> {
|
|
|
|
let (database, key) = self.extract_database(key);
|
|
|
|
let entry = EntryOperation::Delete(KeyEntry::from_key(key));
|
|
|
|
match self.sender.send(WriterOperation { database, entry }) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FacetDocidsSender<'_> {
|
|
|
|
fn extract_database<'a>(&self, key: &'a [u8]) -> (Database, &'a [u8]) {
|
|
|
|
let database = match FacetKind::from(key[0]) {
|
|
|
|
FacetKind::Number => Database::FacetIdF64NumberDocids,
|
|
|
|
FacetKind::String => Database::FacetIdStringDocids,
|
|
|
|
FacetKind::Null => Database::FacetIdIsNullDocids,
|
|
|
|
FacetKind::Empty => Database::FacetIdIsEmptyDocids,
|
|
|
|
FacetKind::Exists => Database::FacetIdExistsDocids,
|
|
|
|
};
|
|
|
|
(database, &key[1..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 11:07:59 +02:00
|
|
|
pub struct DocumentsSender<'a>(&'a MergerSender);
|
2024-09-04 09:59:19 +02:00
|
|
|
|
|
|
|
impl DocumentsSender<'_> {
|
|
|
|
/// TODO do that efficiently
|
|
|
|
pub fn uncompressed(
|
|
|
|
&self,
|
|
|
|
docid: DocumentId,
|
|
|
|
document: &KvReaderFieldId,
|
|
|
|
) -> StdResult<(), SendError<()>> {
|
2024-09-04 14:30:09 +02:00
|
|
|
let entry = EntryOperation::Write(KeyValueEntry::from_small_key_value(
|
2024-09-04 09:59:19 +02:00
|
|
|
&docid.to_be_bytes(),
|
|
|
|
document.as_bytes(),
|
|
|
|
));
|
|
|
|
match self.0.send(WriterOperation { database: Database::Documents, entry }) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
2024-08-29 15:07:59 +02:00
|
|
|
|
2024-09-04 09:59:19 +02:00
|
|
|
pub fn delete(&self, docid: DocumentId) -> StdResult<(), SendError<()>> {
|
|
|
|
let entry = EntryOperation::Delete(KeyEntry::from_key(&docid.to_be_bytes()));
|
|
|
|
match self.0.send(WriterOperation { database: Database::Documents, entry }) {
|
2024-08-29 15:07:59 +02:00
|
|
|
Ok(()) => Ok(()),
|
2024-08-29 17:51:42 +02:00
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
2024-08-29 15:07:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-29 17:51:42 +02:00
|
|
|
|
|
|
|
pub enum MergerOperation {
|
2024-09-04 12:17:13 +02:00
|
|
|
ExactWordDocidsMerger(Merger<File, MergeDeladdCboRoaringBitmaps>),
|
2024-09-05 10:56:22 +02:00
|
|
|
FidWordCountDocidsMerger(Merger<File, MergeDeladdCboRoaringBitmaps>),
|
|
|
|
WordDocidsMerger(Merger<File, MergeDeladdCboRoaringBitmaps>),
|
2024-09-04 11:39:53 +02:00
|
|
|
WordFidDocidsMerger(Merger<File, MergeDeladdCboRoaringBitmaps>),
|
2024-09-05 10:56:22 +02:00
|
|
|
WordPairProximityDocidsMerger(Merger<File, MergeDeladdCboRoaringBitmaps>),
|
2024-09-04 12:17:13 +02:00
|
|
|
WordPositionDocidsMerger(Merger<File, MergeDeladdCboRoaringBitmaps>),
|
2024-09-16 09:34:10 +02:00
|
|
|
FacetDocidsMerger(Merger<File, MergeDeladdCboRoaringBitmaps>),
|
2024-09-04 09:59:19 +02:00
|
|
|
DeleteDocument { docid: DocumentId },
|
2024-09-05 10:56:22 +02:00
|
|
|
InsertDocument { docid: DocumentId, document: Box<KvReaderFieldId> },
|
2024-09-12 18:01:02 +02:00
|
|
|
FinishedDocument,
|
2024-08-29 17:51:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MergerReceiver(Receiver<MergerOperation>);
|
|
|
|
|
|
|
|
impl IntoIterator for MergerReceiver {
|
|
|
|
type Item = MergerOperation;
|
2024-08-29 18:27:02 +02:00
|
|
|
type IntoIter = IntoIter<Self::Item>;
|
2024-08-29 17:51:42 +02:00
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 11:39:53 +02:00
|
|
|
pub struct ExtractorSender(Sender<MergerOperation>);
|
2024-09-03 11:02:39 +02:00
|
|
|
|
2024-09-04 11:39:53 +02:00
|
|
|
impl ExtractorSender {
|
2024-09-12 18:01:02 +02:00
|
|
|
pub fn document_sender(&self) -> DocumentSender<'_> {
|
2024-09-23 11:07:59 +02:00
|
|
|
DocumentSender(Some(&self.0))
|
2024-09-12 18:01:02 +02:00
|
|
|
}
|
|
|
|
|
2024-09-16 09:34:10 +02:00
|
|
|
pub fn send_searchable<D: MergerOperationType>(
|
2024-09-12 18:01:02 +02:00
|
|
|
&self,
|
|
|
|
merger: Merger<File, MergeDeladdCboRoaringBitmaps>,
|
|
|
|
) -> StdResult<(), SendError<()>> {
|
|
|
|
match self.0.send(D::new_merger_operation(merger)) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 11:07:59 +02:00
|
|
|
pub struct DocumentSender<'a>(Option<&'a Sender<MergerOperation>>);
|
2024-09-12 18:01:02 +02:00
|
|
|
|
|
|
|
impl DocumentSender<'_> {
|
|
|
|
pub fn insert(
|
2024-09-03 11:02:39 +02:00
|
|
|
&self,
|
2024-09-04 11:39:53 +02:00
|
|
|
docid: DocumentId,
|
|
|
|
document: Box<KvReaderFieldId>,
|
2024-09-03 11:02:39 +02:00
|
|
|
) -> StdResult<(), SendError<()>> {
|
2024-09-23 11:07:59 +02:00
|
|
|
let sender = self.0.unwrap();
|
|
|
|
match sender.send(MergerOperation::InsertDocument { docid, document }) {
|
2024-09-03 11:02:39 +02:00
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
2024-09-04 09:59:19 +02:00
|
|
|
|
2024-09-12 18:01:02 +02:00
|
|
|
pub fn delete(&self, docid: DocumentId) -> StdResult<(), SendError<()>> {
|
2024-09-23 11:07:59 +02:00
|
|
|
let sender = self.0.unwrap();
|
|
|
|
match sender.send(MergerOperation::DeleteDocument { docid }) {
|
2024-09-04 11:39:53 +02:00
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
2024-09-04 09:59:19 +02:00
|
|
|
|
2024-09-23 11:07:59 +02:00
|
|
|
pub fn finish(mut self) -> StdResult<(), SendError<()>> {
|
|
|
|
let sender = self.0.take().unwrap();
|
|
|
|
match sender.send(MergerOperation::FinishedDocument) {
|
2024-09-04 09:59:19 +02:00
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(SendError(_)) => Err(SendError(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-12 18:01:02 +02:00
|
|
|
|
|
|
|
impl Drop for DocumentSender<'_> {
|
|
|
|
fn drop(&mut self) {
|
2024-09-23 11:07:59 +02:00
|
|
|
if let Some(sender) = self.0.take() {
|
|
|
|
sender.send(MergerOperation::FinishedDocument);
|
|
|
|
}
|
2024-09-12 18:01:02 +02:00
|
|
|
}
|
|
|
|
}
|