From 4deee93a55929aff52e84eca71ad72dc55fc020d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 22 Aug 2019 11:25:33 +0200 Subject: [PATCH] feat: Introduce synonyms deletion using the update system --- meilidb-data/src/database/index/mod.rs | 19 +++++++----- meilidb-data/src/database/mod.rs | 2 +- .../src/database/synonyms_deletion.rs | 29 +++++++++++++++++-- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/meilidb-data/src/database/index/mod.rs b/meilidb-data/src/database/index/mod.rs index 6167d9f08..8a979eb67 100644 --- a/meilidb-data/src/database/index/mod.rs +++ b/meilidb-data/src/database/index/mod.rs @@ -27,7 +27,7 @@ use super::{ DocumentsAddition, FinalDocumentsAddition, DocumentsDeletion, FinalDocumentsDeletion, SynonymsAddition, FinalSynonymsAddition, - SynonymsDeletion, + SynonymsDeletion, FinalSynonymsDeletion, }; mod custom_settings_index; @@ -49,7 +49,7 @@ enum UpdateOwned { DocumentsAddition(Vec), DocumentsDeletion(Vec), SynonymsAddition(BTreeMap>), - SynonymsDeletion( () /*SynonymsDeletion*/), + SynonymsDeletion(BTreeMap>>), } #[derive(Serialize)] @@ -57,7 +57,7 @@ enum Update { DocumentsAddition(Vec), DocumentsDeletion(Vec), SynonymsAddition(BTreeMap>), - SynonymsDeletion( () /*SynonymsDeletion*/), + SynonymsDeletion(BTreeMap>>), } fn spawn_update_system(index: Index) -> thread::JoinHandle<()> { @@ -95,8 +95,9 @@ fn spawn_update_system(index: Index) -> thread::JoinHandle<()> { let addition = FinalSynonymsAddition::from_map(&index, synonyms); addition.finalize()?; }, - UpdateOwned::SynonymsDeletion(_) => { - // ... + UpdateOwned::SynonymsDeletion(synonyms) => { + let deletion = FinalSynonymsDeletion::from_map(&index, synonyms); + deletion.finalize()?; }, } Ok(()) @@ -323,8 +324,12 @@ impl Index { self.raw_push_update(update) } - pub(crate) fn push_synonyms_deletion(&self, deletion: SynonymsDeletion) -> Result { - let update = bincode::serialize(&())?; + pub(crate) fn push_synonyms_deletion( + &self, + deletion: BTreeMap>>, + ) -> 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 78b154bfd..2125763c7 100644 --- a/meilidb-data/src/database/mod.rs +++ b/meilidb-data/src/database/mod.rs @@ -18,7 +18,7 @@ pub use self::index::{Index, CustomSettingsIndex}; use self::documents_addition::{DocumentsAddition, FinalDocumentsAddition}; use self::documents_deletion::{DocumentsDeletion, FinalDocumentsDeletion}; use self::synonyms_addition::{SynonymsAddition, FinalSynonymsAddition}; -use self::synonyms_deletion::SynonymsDeletion; +use self::synonyms_deletion::{SynonymsDeletion, FinalSynonymsDeletion}; fn load_indexes(tree: &sled::Tree) -> Result, Error> { match tree.get("indexes")? { diff --git a/meilidb-data/src/database/synonyms_deletion.rs b/meilidb-data/src/database/synonyms_deletion.rs index 9ce415ead..ee4db7970 100644 --- a/meilidb-data/src/database/synonyms_deletion.rs +++ b/meilidb-data/src/database/synonyms_deletion.rs @@ -10,13 +10,13 @@ use super::{Error, Index}; use super::index::Cache; pub struct SynonymsDeletion<'a> { - inner: &'a Index, + index: &'a Index, synonyms: BTreeMap>>, } impl<'a> SynonymsDeletion<'a> { - pub fn new(inner: &'a Index) -> SynonymsDeletion<'a> { - SynonymsDeletion { inner, synonyms: BTreeMap::new() } + pub fn new(index: &'a Index) -> SynonymsDeletion<'a> { + SynonymsDeletion { index, synonyms: BTreeMap::new() } } pub fn delete_all_alternatives_of>(&mut self, synonym: S) { @@ -38,6 +38,29 @@ impl<'a> SynonymsDeletion<'a> { } } + pub fn finalize(self) -> Result { + self.index.push_synonyms_deletion(self.synonyms) + } +} + +pub struct FinalSynonymsDeletion<'a> { + inner: &'a Index, + synonyms: BTreeMap>>, +} + +impl<'a> FinalSynonymsDeletion<'a> { + pub fn new(inner: &'a Index) -> FinalSynonymsDeletion<'a> { + FinalSynonymsDeletion { inner, synonyms: BTreeMap::new() } + } + + pub fn from_map( + inner: &'a Index, + synonyms: BTreeMap>>, + ) -> FinalSynonymsDeletion<'a> + { + FinalSynonymsDeletion { inner, synonyms } + } + pub fn finalize(self) -> Result<(), Error> { let ref_index = self.inner.as_ref(); let synonyms = ref_index.synonyms_index;