From 0db3e6c58cadeb4d20b6f102e13dd850e2e47804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 22 Aug 2019 10:43:40 +0200 Subject: [PATCH] feat: Introduce documents deletion using the update system --- .../src/database/documents_addition.rs | 4 +-- .../src/database/documents_deletion.rs | 33 ++++++++++++++++--- meilidb-data/src/database/index/mod.rs | 23 +++++++------ meilidb-data/src/database/mod.rs | 2 +- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/meilidb-data/src/database/documents_addition.rs b/meilidb-data/src/database/documents_addition.rs index e2739e9d6..3942ee92b 100644 --- a/meilidb-data/src/database/documents_addition.rs +++ b/meilidb-data/src/database/documents_addition.rs @@ -9,7 +9,7 @@ use crate::indexer::Indexer; use crate::serde::{extract_document_id, Serializer, RamDocumentStore}; use crate::RankedMap; -use super::{Error, Index, DocumentsDeletion}; +use super::{Error, Index, FinalDocumentsDeletion}; use super::index::Cache; pub struct DocumentsAddition<'a, D> { @@ -88,7 +88,7 @@ impl<'a> FinalDocumentsAddition<'a> { let words = ref_index.words_index; // 1. remove the previous documents match indexes - let mut documents_deletion = DocumentsDeletion::new(self.inner, self.ranked_map.clone()); + let mut documents_deletion = FinalDocumentsDeletion::new(self.inner, self.ranked_map.clone()); documents_deletion.extend(self.document_ids); documents_deletion.finalize()?; diff --git a/meilidb-data/src/database/documents_deletion.rs b/meilidb-data/src/database/documents_deletion.rs index 49f8313b9..51a7a9a70 100644 --- a/meilidb-data/src/database/documents_deletion.rs +++ b/meilidb-data/src/database/documents_deletion.rs @@ -12,14 +12,39 @@ use super::{Index, Error}; use super::index::Cache; pub struct DocumentsDeletion<'a> { + index: &'a Index, + documents: Vec, +} + +impl<'a> DocumentsDeletion<'a> { + pub fn new(index: &'a Index) -> DocumentsDeletion<'a> { + DocumentsDeletion { index, documents: Vec::new() } + } + + pub fn delete_document(&mut self, document_id: DocumentId) { + self.documents.push(document_id); + } + + pub fn finalize(self) -> Result { + self.index.push_documents_deletion(self.documents) + } +} + +impl Extend for DocumentsDeletion<'_> { + fn extend>(&mut self, iter: T) { + self.documents.extend(iter) + } +} + +pub struct FinalDocumentsDeletion<'a> { inner: &'a Index, documents: Vec, ranked_map: RankedMap, } -impl<'a> DocumentsDeletion<'a> { - pub fn new(inner: &'a Index, ranked_map: RankedMap) -> DocumentsDeletion { - DocumentsDeletion { inner, documents: Vec::new(), ranked_map } +impl<'a> FinalDocumentsDeletion<'a> { + pub fn new(inner: &'a Index, ranked_map: RankedMap) -> FinalDocumentsDeletion { + FinalDocumentsDeletion { inner, documents: Vec::new(), ranked_map } } fn delete_document_by_id(&mut self, id: DocumentId) { @@ -132,7 +157,7 @@ impl<'a> DocumentsDeletion<'a> { } } -impl<'a> Extend for DocumentsDeletion<'a> { +impl Extend for FinalDocumentsDeletion<'_> { fn extend>(&mut self, iter: T) { self.documents.extend(iter) } diff --git a/meilidb-data/src/database/index/mod.rs b/meilidb-data/src/database/index/mod.rs index edb548e0a..70a2eb176 100644 --- a/meilidb-data/src/database/index/mod.rs +++ b/meilidb-data/src/database/index/mod.rs @@ -25,8 +25,9 @@ use self::words_index::WordsIndex; use super::{ DocumentsAddition, FinalDocumentsAddition, - DocumentsDeletion, - SynonymsAddition, SynonymsDeletion, + DocumentsDeletion, FinalDocumentsDeletion, + SynonymsAddition, + SynonymsDeletion, }; mod custom_settings_index; @@ -46,7 +47,7 @@ fn event_is_set(event: &sled::Event) -> bool { #[derive(Deserialize)] enum UpdateOwned { DocumentsAddition(Vec), - DocumentsDeletion( () /*DocumentsDeletion*/), + DocumentsDeletion(Vec), SynonymsAddition( () /*SynonymsAddition*/), SynonymsDeletion( () /*SynonymsDeletion*/), } @@ -54,7 +55,7 @@ enum UpdateOwned { #[derive(Serialize)] enum Update { DocumentsAddition(Vec), - DocumentsDeletion( () /*DocumentsDeletion*/), + DocumentsDeletion(Vec), SynonymsAddition( () /*SynonymsAddition*/), SynonymsDeletion( () /*SynonymsDeletion*/), } @@ -84,8 +85,11 @@ fn spawn_update_system(index: Index) -> thread::JoinHandle<()> { } addition.finalize()?; }, - UpdateOwned::DocumentsDeletion(_) => { - // ... + UpdateOwned::DocumentsDeletion(documents) => { + let ranked_map = index.cache.load().ranked_map.clone(); + let mut deletion = FinalDocumentsDeletion::new(&index, ranked_map); + deletion.extend(documents); + deletion.finalize()?; }, UpdateOwned::SynonymsAddition(_) => { // ... @@ -259,8 +263,7 @@ impl Index { } pub fn documents_deletion(&self) -> DocumentsDeletion { - let ranked_map = self.cache.load().ranked_map.clone(); - DocumentsDeletion::new(self, ranked_map) + DocumentsDeletion::new(self) } pub fn synonyms_addition(&self) -> SynonymsAddition { @@ -305,8 +308,8 @@ impl Index { self.raw_push_update(update) } - pub(crate) fn push_documents_deletion(&self, deletion: DocumentsDeletion) -> Result { - let update = bincode::serialize(&())?; + pub(crate) fn push_documents_deletion(&self, deletion: Vec) -> Result { + let update = bincode::serialize(&deletion)?; self.raw_push_update(update) } diff --git a/meilidb-data/src/database/mod.rs b/meilidb-data/src/database/mod.rs index b23856ca3..82229c169 100644 --- a/meilidb-data/src/database/mod.rs +++ b/meilidb-data/src/database/mod.rs @@ -16,7 +16,7 @@ pub use self::error::Error; pub use self::index::{Index, CustomSettingsIndex}; use self::documents_addition::{DocumentsAddition, FinalDocumentsAddition}; -use self::documents_deletion::DocumentsDeletion; +use self::documents_deletion::{DocumentsDeletion, FinalDocumentsDeletion}; use self::synonyms_addition::SynonymsAddition; use self::synonyms_deletion::SynonymsDeletion;