feat: Introduce documents deletion using the update system

This commit is contained in:
Clément Renault 2019-08-22 10:43:40 +02:00 committed by Clément Renault
parent f83d6df4ef
commit 0db3e6c58c
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
4 changed files with 45 additions and 17 deletions

View File

@ -9,7 +9,7 @@ use crate::indexer::Indexer;
use crate::serde::{extract_document_id, Serializer, RamDocumentStore}; use crate::serde::{extract_document_id, Serializer, RamDocumentStore};
use crate::RankedMap; use crate::RankedMap;
use super::{Error, Index, DocumentsDeletion}; use super::{Error, Index, FinalDocumentsDeletion};
use super::index::Cache; use super::index::Cache;
pub struct DocumentsAddition<'a, D> { pub struct DocumentsAddition<'a, D> {
@ -88,7 +88,7 @@ impl<'a> FinalDocumentsAddition<'a> {
let words = ref_index.words_index; let words = ref_index.words_index;
// 1. remove the previous documents match indexes // 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.extend(self.document_ids);
documents_deletion.finalize()?; documents_deletion.finalize()?;

View File

@ -12,14 +12,39 @@ use super::{Index, Error};
use super::index::Cache; use super::index::Cache;
pub struct DocumentsDeletion<'a> { pub struct DocumentsDeletion<'a> {
index: &'a Index,
documents: Vec<DocumentId>,
}
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<u64, Error> {
self.index.push_documents_deletion(self.documents)
}
}
impl Extend<DocumentId> for DocumentsDeletion<'_> {
fn extend<T: IntoIterator<Item=DocumentId>>(&mut self, iter: T) {
self.documents.extend(iter)
}
}
pub struct FinalDocumentsDeletion<'a> {
inner: &'a Index, inner: &'a Index,
documents: Vec<DocumentId>, documents: Vec<DocumentId>,
ranked_map: RankedMap, ranked_map: RankedMap,
} }
impl<'a> DocumentsDeletion<'a> { impl<'a> FinalDocumentsDeletion<'a> {
pub fn new(inner: &'a Index, ranked_map: RankedMap) -> DocumentsDeletion { pub fn new(inner: &'a Index, ranked_map: RankedMap) -> FinalDocumentsDeletion {
DocumentsDeletion { inner, documents: Vec::new(), ranked_map } FinalDocumentsDeletion { inner, documents: Vec::new(), ranked_map }
} }
fn delete_document_by_id(&mut self, id: DocumentId) { fn delete_document_by_id(&mut self, id: DocumentId) {
@ -132,7 +157,7 @@ impl<'a> DocumentsDeletion<'a> {
} }
} }
impl<'a> Extend<DocumentId> for DocumentsDeletion<'a> { impl Extend<DocumentId> for FinalDocumentsDeletion<'_> {
fn extend<T: IntoIterator<Item=DocumentId>>(&mut self, iter: T) { fn extend<T: IntoIterator<Item=DocumentId>>(&mut self, iter: T) {
self.documents.extend(iter) self.documents.extend(iter)
} }

View File

@ -25,8 +25,9 @@ use self::words_index::WordsIndex;
use super::{ use super::{
DocumentsAddition, FinalDocumentsAddition, DocumentsAddition, FinalDocumentsAddition,
DocumentsDeletion, DocumentsDeletion, FinalDocumentsDeletion,
SynonymsAddition, SynonymsDeletion, SynonymsAddition,
SynonymsDeletion,
}; };
mod custom_settings_index; mod custom_settings_index;
@ -46,7 +47,7 @@ fn event_is_set(event: &sled::Event) -> bool {
#[derive(Deserialize)] #[derive(Deserialize)]
enum UpdateOwned { enum UpdateOwned {
DocumentsAddition(Vec<serde_json::Value>), DocumentsAddition(Vec<serde_json::Value>),
DocumentsDeletion( () /*DocumentsDeletion*/), DocumentsDeletion(Vec<DocumentId>),
SynonymsAddition( () /*SynonymsAddition*/), SynonymsAddition( () /*SynonymsAddition*/),
SynonymsDeletion( () /*SynonymsDeletion*/), SynonymsDeletion( () /*SynonymsDeletion*/),
} }
@ -54,7 +55,7 @@ enum UpdateOwned {
#[derive(Serialize)] #[derive(Serialize)]
enum Update<D: serde::Serialize> { enum Update<D: serde::Serialize> {
DocumentsAddition(Vec<D>), DocumentsAddition(Vec<D>),
DocumentsDeletion( () /*DocumentsDeletion*/), DocumentsDeletion(Vec<DocumentId>),
SynonymsAddition( () /*SynonymsAddition*/), SynonymsAddition( () /*SynonymsAddition*/),
SynonymsDeletion( () /*SynonymsDeletion*/), SynonymsDeletion( () /*SynonymsDeletion*/),
} }
@ -84,8 +85,11 @@ fn spawn_update_system(index: Index) -> thread::JoinHandle<()> {
} }
addition.finalize()?; 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(_) => { UpdateOwned::SynonymsAddition(_) => {
// ... // ...
@ -259,8 +263,7 @@ impl Index {
} }
pub fn documents_deletion(&self) -> DocumentsDeletion { pub fn documents_deletion(&self) -> DocumentsDeletion {
let ranked_map = self.cache.load().ranked_map.clone(); DocumentsDeletion::new(self)
DocumentsDeletion::new(self, ranked_map)
} }
pub fn synonyms_addition(&self) -> SynonymsAddition { pub fn synonyms_addition(&self) -> SynonymsAddition {
@ -305,8 +308,8 @@ impl Index {
self.raw_push_update(update) self.raw_push_update(update)
} }
pub(crate) fn push_documents_deletion(&self, deletion: DocumentsDeletion) -> Result<u64, Error> { pub(crate) fn push_documents_deletion(&self, deletion: Vec<DocumentId>) -> Result<u64, Error> {
let update = bincode::serialize(&())?; let update = bincode::serialize(&deletion)?;
self.raw_push_update(update) self.raw_push_update(update)
} }

View File

@ -16,7 +16,7 @@ pub use self::error::Error;
pub use self::index::{Index, CustomSettingsIndex}; pub use self::index::{Index, CustomSettingsIndex};
use self::documents_addition::{DocumentsAddition, FinalDocumentsAddition}; 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_addition::SynonymsAddition;
use self::synonyms_deletion::SynonymsDeletion; use self::synonyms_deletion::SynonymsDeletion;