meilisearch/milli/src/update/new/indexer/document_deletion.rs

54 lines
1.7 KiB
Rust
Raw Normal View History

use std::sync::Arc;
use rayon::iter::{ParallelBridge, ParallelIterator};
use roaring::RoaringBitmap;
2024-09-02 15:10:21 +02:00
use super::DocumentChanges;
use crate::documents::PrimaryKey;
use crate::update::new::{Deletion, DocumentChange, ItemsPool};
use crate::{FieldsIdsMap, Index, InternalError, Result};
2024-09-02 14:42:27 +02:00
pub struct DocumentDeletion {
pub to_delete: RoaringBitmap,
}
2024-09-02 14:42:27 +02:00
impl DocumentDeletion {
pub fn new() -> Self {
Self { to_delete: Default::default() }
}
pub fn delete_documents_by_docids(&mut self, docids: RoaringBitmap) {
self.to_delete |= docids;
}
}
2024-09-02 15:10:21 +02:00
impl<'p> DocumentChanges<'p> for DocumentDeletion {
type Parameter = (&'p Index, &'p FieldsIdsMap, &'p PrimaryKey<'p>);
fn document_changes(
self,
param: Self::Parameter,
) -> Result<impl ParallelIterator<Item = Result<DocumentChange>> + Clone + 'p> {
let (index, fields, primary_key) = param;
let items = Arc::new(ItemsPool::new(|| index.read_txn().map_err(crate::Error::from)));
Ok(self.to_delete.into_iter().par_bridge().map_with(items, |items, docid| {
items.with(|rtxn| {
let current = index.document(rtxn, docid)?;
let external_docid = match primary_key.document_id(current, fields)? {
Ok(document_id) => Ok(document_id) as Result<_>,
Err(_) => Err(InternalError::DocumentsError(
crate::documents::Error::InvalidDocumentFormat,
)
.into()),
}?;
Ok(DocumentChange::Deletion(Deletion::create(
docid,
external_docid,
current.boxed(),
)))
})
}))
}
}