feat: Take ranked attributes into account

This commit is contained in:
Clément Renault 2019-04-26 12:18:39 +02:00
parent 068f1bc202
commit 0547671246
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
5 changed files with 224 additions and 10 deletions

View File

@ -251,8 +251,12 @@ impl RawIndex {
Ok(()) Ok(())
} }
pub fn update_ranked_map(&self, ranked_map: Arc<RankedMap>) { pub fn update_ranked_map(&self, ranked_map: Arc<RankedMap>) -> sled::Result<()> {
self.ranked_map.store(ranked_map) let data = bincode::serialize(ranked_map.as_ref()).unwrap();
self.inner.set("ranked-map", data).map(drop)?;
self.ranked_map.store(ranked_map);
Ok(())
} }
pub fn set_document_attribute<V>( pub fn set_document_attribute<V>(
@ -343,7 +347,8 @@ impl Index {
pub fn documents_addition(&self) -> DocumentsAddition { pub fn documents_addition(&self) -> DocumentsAddition {
let index = self.0.clone(); let index = self.0.clone();
DocumentsAddition::from_raw(index) let ranked_map = self.0.ranked_map().clone();
DocumentsAddition::from_raw(index, ranked_map)
} }
pub fn documents_deletion(&self) -> DocumentsDeletion { pub fn documents_deletion(&self) -> DocumentsDeletion {
@ -381,11 +386,12 @@ impl Index {
pub struct DocumentsAddition { pub struct DocumentsAddition {
inner: RawIndex, inner: RawIndex,
indexer: Indexer, indexer: Indexer,
ranked_map: RankedMap,
} }
impl DocumentsAddition { impl DocumentsAddition {
pub fn from_raw(inner: RawIndex) -> DocumentsAddition { pub fn from_raw(inner: RawIndex, ranked_map: RankedMap) -> DocumentsAddition {
DocumentsAddition { inner, indexer: Indexer::new() } DocumentsAddition { inner, indexer: Indexer::new(), ranked_map }
} }
pub fn update_document<D>(&mut self, document: D) -> Result<(), Error> pub fn update_document<D>(&mut self, document: D) -> Result<(), Error>
@ -403,6 +409,7 @@ impl DocumentsAddition {
schema, schema,
index: &self.inner, index: &self.inner,
indexer: &mut self.indexer, indexer: &mut self.indexer,
ranked_map: &mut self.ranked_map,
document_id, document_id,
}; };
@ -430,7 +437,10 @@ pub struct DocumentsDeletion {
impl DocumentsDeletion { impl DocumentsDeletion {
pub fn from_raw(inner: RawIndex) -> DocumentsDeletion { pub fn from_raw(inner: RawIndex) -> DocumentsDeletion {
DocumentsDeletion { inner, documents: Vec::new() } DocumentsDeletion {
inner,
documents: Vec::new(),
}
} }
pub fn delete_document(&mut self, id: DocumentId) { pub fn delete_document(&mut self, id: DocumentId) {

View File

@ -36,7 +36,7 @@ impl FromStr for Number {
} }
} }
#[derive(Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseNumberError { pub struct ParseNumberError {
uint_error: ParseIntError, uint_error: ParseIntError,
int_error: ParseIntError, int_error: ParseIntError,

View File

@ -0,0 +1,180 @@
use std::str::FromStr;
use ordered_float::OrderedFloat;
use serde::ser;
use serde::Serialize;
use super::SerializerError;
use crate::Number;
pub struct ConvertToNumber;
impl ser::Serializer for ConvertToNumber {
type Ok = Number;
type Error = SerializerError;
type SerializeSeq = ser::Impossible<Self::Ok, Self::Error>;
type SerializeTuple = ser::Impossible<Self::Ok, Self::Error>;
type SerializeTupleStruct = ser::Impossible<Self::Ok, Self::Error>;
type SerializeTupleVariant = ser::Impossible<Self::Ok, Self::Error>;
type SerializeMap = ser::Impossible<Self::Ok, Self::Error>;
type SerializeStruct = ser::Impossible<Self::Ok, Self::Error>;
type SerializeStructVariant = ser::Impossible<Self::Ok, Self::Error>;
fn serialize_bool(self, value: bool) -> Result<Self::Ok, Self::Error> {
Ok(Number::Unsigned(u64::from(value)))
}
fn serialize_char(self, value: char) -> Result<Self::Ok, Self::Error> {
unimplemented!()
}
fn serialize_i8(self, value: i8) -> Result<Self::Ok, Self::Error> {
Ok(Number::Signed(i64::from(value)))
}
fn serialize_i16(self, value: i16) -> Result<Self::Ok, Self::Error> {
Ok(Number::Signed(i64::from(value)))
}
fn serialize_i32(self, value: i32) -> Result<Self::Ok, Self::Error> {
Ok(Number::Signed(i64::from(value)))
}
fn serialize_i64(self, value: i64) -> Result<Self::Ok, Self::Error> {
Ok(Number::Signed(value))
}
fn serialize_u8(self, value: u8) -> Result<Self::Ok, Self::Error> {
Ok(Number::Unsigned(u64::from(value)))
}
fn serialize_u16(self, value: u16) -> Result<Self::Ok, Self::Error> {
Ok(Number::Unsigned(u64::from(value)))
}
fn serialize_u32(self, value: u32) -> Result<Self::Ok, Self::Error> {
Ok(Number::Unsigned(u64::from(value)))
}
fn serialize_u64(self, value: u64) -> Result<Self::Ok, Self::Error> {
Ok(Number::Unsigned(value))
}
fn serialize_f32(self, value: f32) -> Result<Self::Ok, Self::Error> {
Ok(Number::Float(OrderedFloat(value as f64)))
}
fn serialize_f64(self, value: f64) -> Result<Self::Ok, Self::Error> {
Ok(Number::Float(OrderedFloat(value)))
}
fn serialize_str(self, value: &str) -> Result<Self::Ok, Self::Error> {
Ok(Number::from_str(value)?)
}
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "&[u8]" })
}
fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "Option" })
}
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
where T: Serialize,
{
Err(SerializerError::UnserializableType { type_name: "Option" })
}
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "()" })
}
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "unit struct" })
}
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str
) -> Result<Self::Ok, Self::Error>
{
Err(SerializerError::UnserializableType { type_name: "unit variant" })
}
fn serialize_newtype_struct<T: ?Sized>(
self,
_name: &'static str,
value: &T
) -> Result<Self::Ok, Self::Error>
where T: Serialize,
{
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T
) -> Result<Self::Ok, Self::Error>
where T: Serialize,
{
Err(SerializerError::UnserializableType { type_name: "newtype variant" })
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "sequence" })
}
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "tuple" })
}
fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize
) -> Result<Self::SerializeTupleStruct, Self::Error>
{
Err(SerializerError::UnserializableType { type_name: "tuple struct" })
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize
) -> Result<Self::SerializeTupleVariant, Self::Error>
{
Err(SerializerError::UnserializableType { type_name: "tuple variant" })
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "map" })
}
fn serialize_struct(
self,
_name: &'static str,
_len: usize
) -> Result<Self::SerializeStruct, Self::Error>
{
Err(SerializerError::UnserializableType { type_name: "struct" })
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize
) -> Result<Self::SerializeStructVariant, Self::Error>
{
Err(SerializerError::UnserializableType { type_name: "struct variant" })
}
}

View File

@ -8,27 +8,31 @@ macro_rules! forward_to_unserializable_type {
} }
} }
mod convert_to_number;
mod convert_to_string;
mod deserializer; mod deserializer;
mod extract_document_id; mod extract_document_id;
mod convert_to_string;
mod indexer; mod indexer;
mod serializer; mod serializer;
pub use self::deserializer::Deserializer; pub use self::deserializer::Deserializer;
pub use self::extract_document_id::extract_document_id; pub use self::extract_document_id::extract_document_id;
pub use self::convert_to_string::ConvertToString; pub use self::convert_to_string::ConvertToString;
pub use self::convert_to_number::ConvertToNumber;
pub use self::indexer::Indexer; pub use self::indexer::Indexer;
pub use self::serializer::Serializer; pub use self::serializer::Serializer;
use std::{fmt, error::Error}; use std::{fmt, error::Error};
use rmp_serde::encode::Error as RmpError; use rmp_serde::encode::Error as RmpError;
use serde::ser; use serde::ser;
use crate::number::ParseNumberError;
#[derive(Debug)] #[derive(Debug)]
pub enum SerializerError { pub enum SerializerError {
DocumentIdNotFound, DocumentIdNotFound,
RmpError(RmpError), RmpError(RmpError),
SledError(sled::Error), SledError(sled::Error),
ParseNumberError(ParseNumberError),
UnserializableType { type_name: &'static str }, UnserializableType { type_name: &'static str },
UnindexableType { type_name: &'static str }, UnindexableType { type_name: &'static str },
Custom(String), Custom(String),
@ -48,6 +52,9 @@ impl fmt::Display for SerializerError {
} }
SerializerError::RmpError(e) => write!(f, "rmp serde related error: {}", e), SerializerError::RmpError(e) => write!(f, "rmp serde related error: {}", e),
SerializerError::SledError(e) => write!(f, "sled related error: {}", e), SerializerError::SledError(e) => write!(f, "sled related error: {}", e),
SerializerError::ParseNumberError(e) => {
write!(f, "error while trying to parse a number: {}", e)
},
SerializerError::UnserializableType { type_name } => { SerializerError::UnserializableType { type_name } => {
write!(f, "{} are not a serializable type", type_name) write!(f, "{} are not a serializable type", type_name)
}, },
@ -78,3 +85,9 @@ impl From<sled::Error> for SerializerError {
SerializerError::SledError(error) SerializerError::SledError(error)
} }
} }
impl From<ParseNumberError> for SerializerError {
fn from(error: ParseNumberError) -> SerializerError {
SerializerError::ParseNumberError(error)
}
}

View File

@ -2,14 +2,16 @@ use meilidb_core::DocumentId;
use serde::ser; use serde::ser;
use crate::database::RawIndex; use crate::database::RawIndex;
use crate::ranked_map::RankedMap;
use crate::indexer::Indexer as RawIndexer; use crate::indexer::Indexer as RawIndexer;
use crate::schema::{Schema, SchemaAttr}; use crate::schema::{Schema, SchemaAttr};
use super::{SerializerError, ConvertToString, Indexer}; use super::{SerializerError, ConvertToString, ConvertToNumber, Indexer};
pub struct Serializer<'a> { pub struct Serializer<'a> {
pub schema: &'a Schema, pub schema: &'a Schema,
pub index: &'a RawIndex, pub index: &'a RawIndex,
pub indexer: &'a mut RawIndexer, pub indexer: &'a mut RawIndexer,
pub ranked_map: &'a mut RankedMap,
pub document_id: DocumentId, pub document_id: DocumentId,
} }
@ -134,6 +136,7 @@ impl<'a> ser::Serializer for Serializer<'a> {
document_id: self.document_id, document_id: self.document_id,
index: self.index, index: self.index,
indexer: self.indexer, indexer: self.indexer,
ranked_map: self.ranked_map,
current_key_name: None, current_key_name: None,
}) })
} }
@ -149,6 +152,7 @@ impl<'a> ser::Serializer for Serializer<'a> {
document_id: self.document_id, document_id: self.document_id,
index: self.index, index: self.index,
indexer: self.indexer, indexer: self.indexer,
ranked_map: self.ranked_map,
}) })
} }
@ -169,6 +173,7 @@ pub struct MapSerializer<'a> {
document_id: DocumentId, document_id: DocumentId,
index: &'a RawIndex, index: &'a RawIndex,
indexer: &'a mut RawIndexer, indexer: &'a mut RawIndexer,
ranked_map: &'a mut RankedMap,
current_key_name: Option<String>, current_key_name: Option<String>,
} }
@ -205,6 +210,7 @@ impl<'a> ser::SerializeMap for MapSerializer<'a> {
self.document_id, self.document_id,
self.index, self.index,
self.indexer, self.indexer,
self.ranked_map,
&key, &key,
value, value,
) )
@ -220,6 +226,7 @@ pub struct StructSerializer<'a> {
document_id: DocumentId, document_id: DocumentId,
index: &'a RawIndex, index: &'a RawIndex,
indexer: &'a mut RawIndexer, indexer: &'a mut RawIndexer,
ranked_map: &'a mut RankedMap,
} }
impl<'a> ser::SerializeStruct for StructSerializer<'a> { impl<'a> ser::SerializeStruct for StructSerializer<'a> {
@ -238,6 +245,7 @@ impl<'a> ser::SerializeStruct for StructSerializer<'a> {
self.document_id, self.document_id,
self.index, self.index,
self.indexer, self.indexer,
self.ranked_map,
key, key,
value, value,
) )
@ -253,6 +261,7 @@ fn serialize_value<T: ?Sized>(
document_id: DocumentId, document_id: DocumentId,
index: &RawIndex, index: &RawIndex,
indexer: &mut RawIndexer, indexer: &mut RawIndexer,
ranked_map: &mut RankedMap,
key: &str, key: &str,
value: &T, value: &T,
) -> Result<(), SerializerError> ) -> Result<(), SerializerError>
@ -276,7 +285,9 @@ where T: ser::Serialize,
} }
if props.is_ranked() { if props.is_ranked() {
unimplemented!() let key = (document_id, attr);
let number = value.serialize(ConvertToNumber)?;
ranked_map.insert(key, number);
} }
} }