From ed6b6038ee66f51b3952d7e543653385699324a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Sun, 21 Apr 2019 22:40:21 +0200 Subject: [PATCH] feat: Finalize index merging on document insertion --- meilidb-data/src/database.rs | 39 +++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/meilidb-data/src/database.rs b/meilidb-data/src/database.rs index ef67227bd..0e71e0f5b 100644 --- a/meilidb-data/src/database.rs +++ b/meilidb-data/src/database.rs @@ -18,7 +18,7 @@ use serde::de; use sled::IVec; use crate::{Schema, SchemaAttr, RankedMap}; -use crate::serde::Deserializer; +use crate::serde::{extract_document_id, Serializer, Deserializer, SerializerError}; use crate::indexer::Indexer; #[derive(Debug)] @@ -26,8 +26,10 @@ pub enum Error { SchemaDiffer, SchemaMissing, WordIndexMissing, + MissingDocumentId, SledError(sled::Error), BincodeError(bincode::Error), + SerializerError(SerializerError), } impl From for Error { @@ -42,6 +44,12 @@ impl From for Error { } } +impl From for Error { + fn from(error: SerializerError) -> Error { + Error::SerializerError(error) + } +} + fn index_name(name: &str) -> Vec { format!("index-{}", name).into_bytes() } @@ -372,11 +380,36 @@ impl DocumentsAddition { pub fn update_document(&mut self, document: D) -> Result<(), Error> where D: serde::Serialize, { - unimplemented!() + let schema = self.inner.schema(); + let identifier = schema.identifier_name(); + + let document_id = match extract_document_id(identifier, &document)? { + Some(id) => id, + None => return Err(Error::MissingDocumentId), + }; + + let serializer = Serializer { + schema, + index: &self.inner, + indexer: &mut self.indexer, + document_id, + }; + + document.serialize(serializer)?; + + Ok(()) } pub fn finalize(self) -> sled::Result<()> { - unimplemented!() + let delta_index = self.indexer.build(); + + let index = self.inner.word_index(); + let new_index = index.r#union(&delta_index); + + let new_index = Arc::from(new_index); + self.inner.update_word_index(new_index)?; + + Ok(()) } }