MeiliSearch/meilisearch-core/src/serde/serializer.rs

362 lines
9.8 KiB
Rust
Raw Normal View History

2020-01-13 19:10:58 +01:00
use meilisearch_schema::{Schema, FieldId};
2019-10-03 11:49:13 +02:00
use serde::ser;
use crate::database::MainT;
2019-10-03 11:49:13 +02:00
use crate::raw_indexer::RawIndexer;
use crate::store::{DocumentsFields, DocumentsFieldsCounts};
2019-10-18 13:05:28 +02:00
use crate::{DocumentId, RankedMap};
2019-10-03 15:04:11 +02:00
2019-10-18 13:05:28 +02:00
use super::{ConvertToNumber, ConvertToString, Indexer, SerializerError};
2019-10-03 11:49:13 +02:00
2019-11-04 10:49:27 +01:00
pub struct Serializer<'a, 'b> {
pub txn: &'a mut heed::RwTxn<'b, MainT>,
2020-01-13 19:10:58 +01:00
pub schema: &'a mut Schema,
pub document_store: DocumentsFields,
pub document_fields_counts: DocumentsFieldsCounts,
2019-10-03 11:49:13 +02:00
pub indexer: &'a mut RawIndexer,
pub ranked_map: &'a mut RankedMap,
pub document_id: DocumentId,
}
2019-11-04 10:49:27 +01:00
impl<'a, 'b> ser::Serializer for Serializer<'a, 'b> {
2019-10-03 11:49:13 +02:00
type Ok = ();
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>;
2019-11-04 10:49:27 +01:00
type SerializeMap = MapSerializer<'a, 'b>;
type SerializeStruct = StructSerializer<'a, 'b>;
2019-10-03 11:49:13 +02:00
type SerializeStructVariant = ser::Impossible<Self::Ok, Self::Error>;
forward_to_unserializable_type! {
bool => serialize_bool,
char => serialize_char,
i8 => serialize_i8,
i16 => serialize_i16,
i32 => serialize_i32,
i64 => serialize_i64,
u8 => serialize_u8,
u16 => serialize_u16,
u32 => serialize_u32,
u64 => serialize_u64,
f32 => serialize_f32,
f64 => serialize_f64,
}
fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "str" })
}
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> {
2019-10-18 13:05:28 +02:00
Err(SerializerError::UnserializableType {
type_name: "Option",
})
2019-10-03 11:49:13 +02:00
}
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
2019-10-18 13:05:28 +02:00
where
T: ser::Serialize,
2019-10-03 11:49:13 +02:00
{
2019-10-18 13:05:28 +02:00
Err(SerializerError::UnserializableType {
type_name: "Option",
})
2019-10-03 11:49:13 +02:00
}
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> {
2019-10-18 13:05:28 +02:00
Err(SerializerError::UnserializableType {
type_name: "unit struct",
})
2019-10-03 11:49:13 +02:00
}
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
2019-10-18 13:05:28 +02:00
_variant: &'static str,
) -> Result<Self::Ok, Self::Error> {
Err(SerializerError::UnserializableType {
type_name: "unit variant",
})
2019-10-03 11:49:13 +02:00
}
fn serialize_newtype_struct<T: ?Sized>(
self,
_name: &'static str,
2019-10-18 13:05:28 +02:00
value: &T,
2019-10-03 11:49:13 +02:00
) -> Result<Self::Ok, Self::Error>
2019-10-18 13:05:28 +02:00
where
T: ser::Serialize,
2019-10-03 11:49:13 +02:00
{
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
2019-10-18 13:05:28 +02:00
_value: &T,
2019-10-03 11:49:13 +02:00
) -> Result<Self::Ok, Self::Error>
2019-10-18 13:05:28 +02:00
where
T: ser::Serialize,
2019-10-03 11:49:13 +02:00
{
2019-10-18 13:05:28 +02:00
Err(SerializerError::UnserializableType {
type_name: "newtype variant",
})
2019-10-03 11:49:13 +02:00
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
2019-10-18 13:05:28 +02:00
Err(SerializerError::UnserializableType {
type_name: "sequence",
})
2019-10-03 11:49:13 +02:00
}
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,
2019-10-18 13:05:28 +02:00
_len: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> {
Err(SerializerError::UnserializableType {
type_name: "tuple struct",
})
2019-10-03 11:49:13 +02:00
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
2019-10-18 13:05:28 +02:00
_len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(SerializerError::UnserializableType {
type_name: "tuple variant",
})
2019-10-03 11:49:13 +02:00
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Ok(MapSerializer {
txn: self.txn,
2019-10-03 11:49:13 +02:00
schema: self.schema,
document_id: self.document_id,
document_store: self.document_store,
document_fields_counts: self.document_fields_counts,
2019-10-03 11:49:13 +02:00
indexer: self.indexer,
ranked_map: self.ranked_map,
current_key_name: None,
})
}
fn serialize_struct(
self,
_name: &'static str,
2019-10-18 13:05:28 +02:00
_len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
2019-10-03 11:49:13 +02:00
Ok(StructSerializer {
txn: self.txn,
2019-10-03 11:49:13 +02:00
schema: self.schema,
document_id: self.document_id,
document_store: self.document_store,
document_fields_counts: self.document_fields_counts,
2019-10-03 11:49:13 +02:00
indexer: self.indexer,
ranked_map: self.ranked_map,
})
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
2019-10-18 13:05:28 +02:00
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(SerializerError::UnserializableType {
type_name: "struct variant",
})
2019-10-03 11:49:13 +02:00
}
}
2019-11-04 10:49:27 +01:00
pub struct MapSerializer<'a, 'b> {
txn: &'a mut heed::RwTxn<'b, MainT>,
2020-01-13 19:10:58 +01:00
schema: &'a mut Schema,
2019-10-03 11:49:13 +02:00
document_id: DocumentId,
document_store: DocumentsFields,
document_fields_counts: DocumentsFieldsCounts,
2019-10-03 11:49:13 +02:00
indexer: &'a mut RawIndexer,
ranked_map: &'a mut RankedMap,
current_key_name: Option<String>,
}
2019-11-04 10:49:27 +01:00
impl<'a, 'b> ser::SerializeMap for MapSerializer<'a, 'b> {
2019-10-03 11:49:13 +02:00
type Ok = ();
type Error = SerializerError;
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
2019-10-18 13:05:28 +02:00
where
T: ser::Serialize,
2019-10-03 11:49:13 +02:00
{
let key = key.serialize(ConvertToString)?;
self.current_key_name = Some(key);
Ok(())
}
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
2019-10-18 13:05:28 +02:00
where
T: ser::Serialize,
2019-10-03 11:49:13 +02:00
{
let key = self.current_key_name.take().unwrap();
self.serialize_entry(&key, value)
}
fn serialize_entry<K: ?Sized, V: ?Sized>(
&mut self,
key: &K,
value: &V,
) -> Result<(), Self::Error>
2019-10-18 13:05:28 +02:00
where
K: ser::Serialize,
V: ser::Serialize,
2019-10-03 11:49:13 +02:00
{
let key = key.serialize(ConvertToString)?;
2020-01-13 19:10:58 +01:00
serialize_value(
self.txn,
2020-02-11 15:16:02 +01:00
key.as_str(),
2020-01-13 19:10:58 +01:00
self.schema,
self.document_id,
self.document_store,
self.document_fields_counts,
self.indexer,
self.ranked_map,
value,
)
2019-10-03 11:49:13 +02:00
}
fn end(self) -> Result<Self::Ok, Self::Error> {
Ok(())
}
}
2019-11-04 10:49:27 +01:00
pub struct StructSerializer<'a, 'b> {
txn: &'a mut heed::RwTxn<'b, MainT>,
2020-01-13 19:10:58 +01:00
schema: &'a mut Schema,
2019-10-03 11:49:13 +02:00
document_id: DocumentId,
document_store: DocumentsFields,
document_fields_counts: DocumentsFieldsCounts,
2019-10-03 11:49:13 +02:00
indexer: &'a mut RawIndexer,
ranked_map: &'a mut RankedMap,
}
2019-11-04 10:49:27 +01:00
impl<'a, 'b> ser::SerializeStruct for StructSerializer<'a, 'b> {
2019-10-03 11:49:13 +02:00
type Ok = ();
type Error = SerializerError;
fn serialize_field<T: ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error>
2019-10-18 13:05:28 +02:00
where
T: ser::Serialize,
2019-10-03 11:49:13 +02:00
{
2020-01-10 18:20:30 +01:00
serialize_value(
self.txn,
2020-02-11 15:16:02 +01:00
key,
2020-01-13 19:10:58 +01:00
self.schema,
2020-01-10 18:20:30 +01:00
self.document_id,
self.document_store,
self.document_fields_counts,
self.indexer,
self.ranked_map,
value,
)
2019-10-03 11:49:13 +02:00
}
fn end(self) -> Result<Self::Ok, Self::Error> {
Ok(())
}
}
2020-01-10 18:20:30 +01:00
pub fn serialize_value<'a, T: ?Sized>(
txn: &mut heed::RwTxn<MainT>,
2020-02-11 15:16:02 +01:00
attribute: &str,
2020-01-13 19:10:58 +01:00
schema: &'a mut Schema,
document_id: DocumentId,
document_store: DocumentsFields,
documents_fields_counts: DocumentsFieldsCounts,
indexer: &mut RawIndexer,
ranked_map: &mut RankedMap,
value: &T,
) -> Result<(), SerializerError>
where
T: ser::Serialize,
{
2020-02-11 15:16:02 +01:00
let field_id = schema.insert_and_index(&attribute)?;
2020-01-13 19:10:58 +01:00
serialize_value_with_id(
txn,
field_id,
schema,
document_id,
document_store,
documents_fields_counts,
indexer,
ranked_map,
2020-01-27 08:52:36 +01:00
value,
2020-01-13 19:10:58 +01:00
)
}
pub fn serialize_value_with_id<'a, T: ?Sized>(
txn: &mut heed::RwTxn<MainT>,
field_id: FieldId,
2020-01-10 18:20:30 +01:00
schema: &'a Schema,
2019-10-03 11:49:13 +02:00
document_id: DocumentId,
document_store: DocumentsFields,
documents_fields_counts: DocumentsFieldsCounts,
2019-10-03 11:49:13 +02:00
indexer: &mut RawIndexer,
ranked_map: &mut RankedMap,
value: &T,
) -> Result<(), SerializerError>
2019-10-18 13:05:28 +02:00
where
T: ser::Serialize,
2019-10-03 11:49:13 +02:00
{
let serialized = serde_json::to_vec(value)?;
2020-01-10 18:20:30 +01:00
document_store.put_document_field(txn, document_id, field_id, &serialized)?;
2020-01-29 18:30:21 +01:00
if let Some(indexed_pos) = schema.is_indexed(field_id) {
let indexer = Indexer {
2020-01-13 19:10:58 +01:00
pos: *indexed_pos,
indexer,
document_id,
};
if let Some(number_of_words) = value.serialize(indexer)? {
documents_fields_counts.put_document_field_count(
txn,
2019-10-18 13:05:28 +02:00
document_id,
2020-01-13 19:10:58 +01:00
*indexed_pos,
2019-11-27 17:01:23 +01:00
number_of_words as u16,
)?;
2019-10-03 11:49:13 +02:00
}
}
2019-10-03 11:49:13 +02:00
2020-01-29 18:30:21 +01:00
if schema.is_ranked(field_id) {
let number = value.serialize(ConvertToNumber).unwrap_or_default();
2020-01-10 18:20:30 +01:00
ranked_map.insert(document_id, field_id, number);
2019-10-03 11:49:13 +02:00
}
Ok(())
}