diff --git a/meilidb-data/src/database/index/documents_index.rs b/meilidb-data/src/database/index/documents_index.rs index 11475e800..83e95530e 100644 --- a/meilidb-data/src/database/index/documents_index.rs +++ b/meilidb-data/src/database/index/documents_index.rs @@ -35,14 +35,16 @@ impl DocumentsIndex { Ok(()) } - pub fn del_all_document_fields(&self, id: DocumentId) -> RocksDbResult<()> { + pub fn del_all_document_fields(&self, id: DocumentId) -> RocksDbResult { let (start, end) = document_fields_range(id); + let mut count = 0; for (key, _) in self.0.range(start, end)? { self.0.remove(key)?; + count += 1; } - Ok(()) + Ok(count) } pub fn document_fields(&self, id: DocumentId) -> RocksDbResult { diff --git a/meilidb-data/src/database/update/documents_addition.rs b/meilidb-data/src/database/update/documents_addition.rs index 2e4e94736..397727fba 100644 --- a/meilidb-data/src/database/update/documents_addition.rs +++ b/meilidb-data/src/database/update/documents_addition.rs @@ -5,11 +5,10 @@ use fst::{SetBuilder, set::OpBuilder}; use sdset::{SetOperation, duo::Union}; use serde::Serialize; +use crate::RankedMap; +use crate::database::{Error, Index, index::Cache, apply_documents_deletion}; use crate::indexer::Indexer; use crate::serde::{extract_document_id, Serializer, RamDocumentStore}; -use crate::RankedMap; - -use crate::database::{Error, Index, index::Cache, apply_documents_deletion}; pub struct DocumentsAddition<'a, D> { index: &'a Index, @@ -73,8 +72,8 @@ pub fn apply_documents_addition( let words = ref_index.words_index; // 1. remove the previous documents match indexes - let document_ids = document_ids.into_iter().collect(); - apply_documents_deletion(index, ranked_map.clone(), document_ids)?; + let documents_to_insert = document_ids.iter().cloned().collect(); + apply_documents_deletion(index, ranked_map.clone(), documents_to_insert)?; // 2. insert new document attributes in the database for ((id, attr), value) in document_store.into_inner() { diff --git a/meilidb-data/src/database/update/documents_deletion.rs b/meilidb-data/src/database/update/documents_deletion.rs index 2c1036b79..b32fe349f 100644 --- a/meilidb-data/src/database/update/documents_deletion.rs +++ b/meilidb-data/src/database/update/documents_deletion.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, BTreeSet}; +use std::collections::{HashMap, HashSet, BTreeSet}; use std::sync::Arc; use fst::{SetBuilder, Streamer}; @@ -88,6 +88,7 @@ pub fn apply_documents_deletion( } } + let mut deleted_documents = HashSet::new(); let mut removed_words = BTreeSet::new(); for (word, document_ids) in words_document_ids { let document_ids = SetBuf::from_dirty(document_ids); @@ -105,7 +106,9 @@ pub fn apply_documents_deletion( } for id in document_ids { - documents.del_all_document_fields(id)?; + if documents.del_all_document_fields(id)? != 0 { + deleted_documents.insert(id); + } docs_words.del_doc_words(id)?; } }