feat: Finalize index merging on document insertion

This commit is contained in:
Clément Renault 2019-04-21 22:40:21 +02:00
parent ad24ef8a25
commit ed6b6038ee
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE

View File

@ -18,7 +18,7 @@ use serde::de;
use sled::IVec; use sled::IVec;
use crate::{Schema, SchemaAttr, RankedMap}; use crate::{Schema, SchemaAttr, RankedMap};
use crate::serde::Deserializer; use crate::serde::{extract_document_id, Serializer, Deserializer, SerializerError};
use crate::indexer::Indexer; use crate::indexer::Indexer;
#[derive(Debug)] #[derive(Debug)]
@ -26,8 +26,10 @@ pub enum Error {
SchemaDiffer, SchemaDiffer,
SchemaMissing, SchemaMissing,
WordIndexMissing, WordIndexMissing,
MissingDocumentId,
SledError(sled::Error), SledError(sled::Error),
BincodeError(bincode::Error), BincodeError(bincode::Error),
SerializerError(SerializerError),
} }
impl From<sled::Error> for Error { impl From<sled::Error> for Error {
@ -42,6 +44,12 @@ impl From<bincode::Error> for Error {
} }
} }
impl From<SerializerError> for Error {
fn from(error: SerializerError) -> Error {
Error::SerializerError(error)
}
}
fn index_name(name: &str) -> Vec<u8> { fn index_name(name: &str) -> Vec<u8> {
format!("index-{}", name).into_bytes() format!("index-{}", name).into_bytes()
} }
@ -372,11 +380,36 @@ impl DocumentsAddition {
pub fn update_document<D>(&mut self, document: D) -> Result<(), Error> pub fn update_document<D>(&mut self, document: D) -> Result<(), Error>
where D: serde::Serialize, 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<()> { 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(())
} }
} }