diff --git a/meilidb-data/src/database.rs b/meilidb-data/src/database.rs index 44babb55d..927d22e21 100644 --- a/meilidb-data/src/database.rs +++ b/meilidb-data/src/database.rs @@ -5,19 +5,19 @@ use std::path::Path; use std::sync::Arc; use arc_swap::{ArcSwap, Lease}; +use byteorder::{ReadBytesExt, BigEndian}; use hashbrown::HashMap; use meilidb_core::criterion::Criteria; use meilidb_core::QueryBuilder; use meilidb_core::shared_data_cursor::{FromSharedDataCursor, SharedDataCursor}; use meilidb_core::write_to_bytes::WriteToBytes; use meilidb_core::{DocumentId, Index as WordIndex}; -use rmp_serde::decode::{Deserializer as RmpDeserializer, ReadReader}; use rmp_serde::decode::{Error as RmpError}; -use serde::{de, forward_to_deserialize_any}; +use serde::de; use sled::IVec; -use byteorder::{ReadBytesExt, BigEndian}; use crate::{Schema, SchemaAttr, RankedMap}; +use crate::serde::Deserializer; #[derive(Debug)] pub enum Error { @@ -342,90 +342,3 @@ impl Index { T::deserialize(&mut deserializer).map(Some) } } - -struct Deserializer<'a> { - document_id: DocumentId, - raw_index: &'a RawIndex, - fields: Option<&'a HashSet>, -} - -impl<'de, 'a, 'b> de::Deserializer<'de> for &'b mut Deserializer<'a> -{ - type Error = RmpError; - - fn deserialize_any(self, visitor: V) -> Result - where V: de::Visitor<'de> - { - self.deserialize_map(visitor) - } - - 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 - } - - fn deserialize_map(self, visitor: V) -> Result - where V: de::Visitor<'de> - { - let document_attributes = self.raw_index.get_document_fields(self.document_id); - let document_attributes = document_attributes.filter_map(|result| { - match result { - Ok(value) => Some(value), - Err(e) => { - // TODO: must log the error - // error!("sled iter error; {}", e); - None - }, - } - }); - 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); - Some((attribute_name, Value::new(value))) - } else { - None - } - }); - - let map_deserializer = de::value::MapDeserializer::new(iter); - visitor.visit_map(map_deserializer) - } -} - -struct Value(RmpDeserializer>>) where A: AsRef<[u8]>; - -impl Value where A: AsRef<[u8]> -{ - fn new(value: A) -> Value { - Value(RmpDeserializer::new(Cursor::new(value))) - } -} - -impl<'de, A> de::IntoDeserializer<'de, RmpError> for Value -where A: AsRef<[u8]>, -{ - type Deserializer = Self; - - fn into_deserializer(self) -> Self::Deserializer { - self - } -} - -impl<'de, 'a, A> de::Deserializer<'de> for Value -where A: AsRef<[u8]>, -{ - type Error = RmpError; - - fn deserialize_any(mut self, visitor: V) -> Result - where V: de::Visitor<'de> - { - self.0.deserialize_any(visitor) - } - - forward_to_deserialize_any! { - 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 map struct enum identifier ignored_any - } -} diff --git a/meilidb-data/src/lib.rs b/meilidb-data/src/lib.rs index a19418a3f..542741171 100644 --- a/meilidb-data/src/lib.rs +++ b/meilidb-data/src/lib.rs @@ -3,6 +3,7 @@ mod index_event; mod indexer; mod number; mod ranked_map; +mod serde; pub mod schema; pub use self::database::{Database, Index}; diff --git a/meilidb-data/src/serde/deserializer.rs b/meilidb-data/src/serde/deserializer.rs new file mode 100644 index 000000000..12873713b --- /dev/null +++ b/meilidb-data/src/serde/deserializer.rs @@ -0,0 +1,97 @@ +use std::collections::HashSet; +use std::io::Cursor; + +use meilidb_core::DocumentId; +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::SchemaAttr; + +pub struct Deserializer<'a> { + pub document_id: DocumentId, + pub raw_index: &'a RawIndex, + pub fields: Option<&'a HashSet>, +} + +impl<'de, 'a, 'b> de::Deserializer<'de> for &'b mut Deserializer<'a> +{ + type Error = RmpError; + + fn deserialize_any(self, visitor: V) -> Result + where V: de::Visitor<'de> + { + self.deserialize_map(visitor) + } + + 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 + } + + fn deserialize_map(self, visitor: V) -> Result + where V: de::Visitor<'de> + { + let document_attributes = self.raw_index.get_document_fields(self.document_id); + let document_attributes = document_attributes.filter_map(|result| { + match result { + Ok(value) => Some(value), + Err(e) => { + // TODO: must log the error + // error!("sled iter error; {}", e); + None + }, + } + }); + 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); + Some((attribute_name, Value::new(value))) + } else { + None + } + }); + + let map_deserializer = de::value::MapDeserializer::new(iter); + visitor.visit_map(map_deserializer) + } +} + +struct Value(RmpDeserializer>>) where A: AsRef<[u8]>; + +impl Value where A: AsRef<[u8]> +{ + fn new(value: A) -> Value { + Value(RmpDeserializer::new(Cursor::new(value))) + } +} + +impl<'de, A> de::IntoDeserializer<'de, RmpError> for Value +where A: AsRef<[u8]>, +{ + type Deserializer = Self; + + fn into_deserializer(self) -> Self::Deserializer { + self + } +} + +impl<'de, 'a, A> de::Deserializer<'de> for Value +where A: AsRef<[u8]>, +{ + type Error = RmpError; + + fn deserialize_any(mut self, visitor: V) -> Result + where V: de::Visitor<'de> + { + self.0.deserialize_any(visitor) + } + + forward_to_deserialize_any! { + 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 map struct enum identifier ignored_any + } +} diff --git a/meilidb-data/src/serde/mod.rs b/meilidb-data/src/serde/mod.rs new file mode 100644 index 000000000..beb7660e9 --- /dev/null +++ b/meilidb-data/src/serde/mod.rs @@ -0,0 +1,3 @@ +mod deserializer; + +pub use self::deserializer::Deserializer;