feat: Make the DocumentsDeletion public interface to take serde types

This commit is contained in:
Clément Renault 2019-05-16 11:59:31 +02:00
parent 3de633c869
commit 921b063a71
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE

View File

@ -4,6 +4,7 @@ use std::sync::Arc;
use sdset::{SetBuf, SetOperation, duo::DifferenceByKey}; use sdset::{SetBuf, SetOperation, duo::DifferenceByKey};
use fst::{SetBuilder, Streamer}; use fst::{SetBuilder, Streamer};
use meilidb_core::DocumentId; use meilidb_core::DocumentId;
use crate::serde::extract_document_id;
use super::{Index, Error, InnerIndex}; use super::{Index, Error, InnerIndex};
@ -17,10 +18,26 @@ impl<'a> DocumentsDeletion<'a> {
DocumentsDeletion { inner, documents: Vec::new() } DocumentsDeletion { inner, documents: Vec::new() }
} }
pub fn delete_document(&mut self, id: DocumentId) { fn delete_document_by_id(&mut self, id: DocumentId) {
self.documents.push(id); self.documents.push(id);
} }
pub fn delete_document<D>(&mut self, document: D) -> Result<(), Error>
where D: serde::Serialize,
{
let schema = &self.inner.lease_inner().schema;
let identifier = schema.identifier_name();
let document_id = match extract_document_id(identifier, &document)? {
Some(id) => id,
None => return Err(Error::MissingDocumentId),
};
self.delete_document_by_id(document_id);
Ok(())
}
pub fn finalize(mut self) -> Result<(), Error> { pub fn finalize(mut self) -> Result<(), Error> {
let lease_inner = self.inner.lease_inner(); let lease_inner = self.inner.lease_inner();
let main = &lease_inner.raw.main; let main = &lease_inner.raw.main;