feat: Introduce the DocumentsAddition type

This commit is contained in:
Clément Renault
2019-05-09 14:23:39 +02:00
parent 42e39f6eb5
commit e67ada8823
5 changed files with 289 additions and 232 deletions

View File

@ -6,12 +6,12 @@ use rmp_serde::decode::{Deserializer as RmpDeserializer, ReadReader};
use rmp_serde::decode::{Error as RmpError};
use serde::{de, forward_to_deserialize_any};
use crate::database::RawIndex;
use crate::database::Index;
use crate::SchemaAttr;
pub struct Deserializer<'a> {
pub document_id: DocumentId,
pub raw_index: &'a RawIndex,
pub index: &'a Index,
pub fields: Option<&'a HashSet<SchemaAttr>>,
}
@ -26,15 +26,18 @@ impl<'de, 'a, 'b> de::Deserializer<'de> for &'b mut Deserializer<'a>
}
forward_to_deserialize_any! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq
bytes byte_buf unit_struct tuple_struct
identifier tuple ignored_any option newtype_struct enum struct
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct struct enum identifier ignored_any
}
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor<'de>
{
let document_attributes = self.raw_index.get_document_fields(self.document_id);
let schema = &self.index.lease_inner().schema;
let documents = &self.index.lease_inner().raw.documents;
let document_attributes = documents.document_fields(self.document_id);
let document_attributes = document_attributes.filter_map(|result| {
match result {
Ok(value) => Some(value),
@ -45,9 +48,10 @@ impl<'de, 'a, 'b> de::Deserializer<'de> for &'b mut Deserializer<'a>
},
}
});
let iter = document_attributes.filter_map(|(attr, value)| {
if self.fields.map_or(true, |f| f.contains(&attr)) {
let attribute_name = self.raw_index.schema().attribute_name(attr);
let attribute_name = schema.attribute_name(attr);
Some((attribute_name, Value::new(value)))
} else {
None

View File

@ -1,7 +1,7 @@
use meilidb_core::DocumentId;
use serde::ser;
use crate::database::RawIndex;
use crate::database::Index;
use crate::ranked_map::RankedMap;
use crate::indexer::Indexer as RawIndexer;
use crate::schema::Schema;
@ -9,7 +9,7 @@ use super::{SerializerError, ConvertToString, ConvertToNumber, Indexer};
pub struct Serializer<'a> {
pub schema: &'a Schema,
pub index: &'a RawIndex,
pub index: &'a Index,
pub indexer: &'a mut RawIndexer,
pub ranked_map: &'a mut RankedMap,
pub document_id: DocumentId,
@ -171,7 +171,7 @@ impl<'a> ser::Serializer for Serializer<'a> {
pub struct MapSerializer<'a> {
schema: &'a Schema,
document_id: DocumentId,
index: &'a RawIndex,
index: &'a Index,
indexer: &'a mut RawIndexer,
ranked_map: &'a mut RankedMap,
current_key_name: Option<String>,
@ -224,7 +224,7 @@ impl<'a> ser::SerializeMap for MapSerializer<'a> {
pub struct StructSerializer<'a> {
schema: &'a Schema,
document_id: DocumentId,
index: &'a RawIndex,
index: &'a Index,
indexer: &'a mut RawIndexer,
ranked_map: &'a mut RankedMap,
}
@ -259,7 +259,7 @@ impl<'a> ser::SerializeStruct for StructSerializer<'a> {
fn serialize_value<T: ?Sized>(
schema: &Schema,
document_id: DocumentId,
index: &RawIndex,
index: &Index,
indexer: &mut RawIndexer,
ranked_map: &mut RankedMap,
key: &str,
@ -272,7 +272,7 @@ where T: ser::Serialize,
if props.is_stored() {
let value = rmp_serde::to_vec_named(value)?;
index.set_document_attribute(document_id, attr, value)?;
index.lease_inner().raw.documents.set_document_field(document_id, attr, value)?;
}
if props.is_indexed() {