From 65ed2dcc1b6976fb8a6e61f65af6ceb565f68f89 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Mon, 18 May 2020 12:22:41 +0200 Subject: [PATCH] Remove the serde ConvertToNumber --- .../src/serde/convert_to_number.rs | 198 ------------------ meilisearch-core/src/serde/mod.rs | 2 - .../src/update/documents_addition.rs | 24 ++- 3 files changed, 18 insertions(+), 206 deletions(-) delete mode 100644 meilisearch-core/src/serde/convert_to_number.rs diff --git a/meilisearch-core/src/serde/convert_to_number.rs b/meilisearch-core/src/serde/convert_to_number.rs deleted file mode 100644 index a67e01692..000000000 --- a/meilisearch-core/src/serde/convert_to_number.rs +++ /dev/null @@ -1,198 +0,0 @@ -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; - type SerializeTuple = ser::Impossible; - type SerializeTupleStruct = ser::Impossible; - type SerializeTupleVariant = ser::Impossible; - type SerializeMap = ser::Impossible; - type SerializeStruct = ser::Impossible; - type SerializeStructVariant = ser::Impossible; - - fn serialize_bool(self, value: bool) -> Result { - Ok(Number::Unsigned(u64::from(value))) - } - - fn serialize_char(self, _value: char) -> Result { - Err(SerializerError::UnrankableType { type_name: "char" }) - } - - fn serialize_i8(self, value: i8) -> Result { - Ok(Number::Signed(i64::from(value))) - } - - fn serialize_i16(self, value: i16) -> Result { - Ok(Number::Signed(i64::from(value))) - } - - fn serialize_i32(self, value: i32) -> Result { - Ok(Number::Signed(i64::from(value))) - } - - fn serialize_i64(self, value: i64) -> Result { - Ok(Number::Signed(value)) - } - - fn serialize_u8(self, value: u8) -> Result { - Ok(Number::Unsigned(u64::from(value))) - } - - fn serialize_u16(self, value: u16) -> Result { - Ok(Number::Unsigned(u64::from(value))) - } - - fn serialize_u32(self, value: u32) -> Result { - Ok(Number::Unsigned(u64::from(value))) - } - - fn serialize_u64(self, value: u64) -> Result { - Ok(Number::Unsigned(value)) - } - - fn serialize_f32(self, value: f32) -> Result { - Ok(Number::Float(OrderedFloat(f64::from(value)))) - } - - fn serialize_f64(self, value: f64) -> Result { - Ok(Number::Float(OrderedFloat(value))) - } - - fn serialize_str(self, value: &str) -> Result { - Ok(Number::from_str(value)?) - } - - fn serialize_bytes(self, _v: &[u8]) -> Result { - Err(SerializerError::UnrankableType { type_name: "&[u8]" }) - } - - fn serialize_none(self) -> Result { - Err(SerializerError::UnrankableType { - type_name: "Option", - }) - } - - fn serialize_some(self, _value: &T) -> Result - where - T: Serialize, - { - Err(SerializerError::UnrankableType { - type_name: "Option", - }) - } - - fn serialize_unit(self) -> Result { - Err(SerializerError::UnrankableType { type_name: "()" }) - } - - fn serialize_unit_struct(self, _name: &'static str) -> Result { - Err(SerializerError::UnrankableType { - type_name: "unit struct", - }) - } - - fn serialize_unit_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - ) -> Result { - Err(SerializerError::UnrankableType { - type_name: "unit variant", - }) - } - - fn serialize_newtype_struct( - self, - _name: &'static str, - value: &T, - ) -> Result - where - T: Serialize, - { - value.serialize(self) - } - - fn serialize_newtype_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _value: &T, - ) -> Result - where - T: Serialize, - { - Err(SerializerError::UnrankableType { - type_name: "newtype variant", - }) - } - - fn serialize_seq(self, _len: Option) -> Result { - Err(SerializerError::UnrankableType { - type_name: "sequence", - }) - } - - fn serialize_tuple(self, _len: usize) -> Result { - Err(SerializerError::UnrankableType { type_name: "tuple" }) - } - - fn serialize_tuple_struct( - self, - _name: &'static str, - _len: usize, - ) -> Result { - Err(SerializerError::UnrankableType { - type_name: "tuple struct", - }) - } - - fn serialize_tuple_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize, - ) -> Result { - Err(SerializerError::UnrankableType { - type_name: "tuple variant", - }) - } - - fn serialize_map(self, _len: Option) -> Result { - Err(SerializerError::UnrankableType { type_name: "map" }) - } - - fn serialize_struct( - self, - _name: &'static str, - _len: usize, - ) -> Result { - Err(SerializerError::UnrankableType { - type_name: "struct", - }) - } - - fn serialize_struct_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize, - ) -> Result { - Err(SerializerError::UnrankableType { - type_name: "struct variant", - }) - } -} diff --git a/meilisearch-core/src/serde/mod.rs b/meilisearch-core/src/serde/mod.rs index 6a1e51a09..50e18687e 100644 --- a/meilisearch-core/src/serde/mod.rs +++ b/meilisearch-core/src/serde/mod.rs @@ -8,12 +8,10 @@ macro_rules! forward_to_unserializable_type { } } -mod convert_to_number; mod convert_to_string; mod deserializer; mod extract_document_id; -pub use self::convert_to_number::ConvertToNumber; pub use self::convert_to_string::ConvertToString; pub use self::deserializer::{Deserializer, DeserializerError}; pub use self::extract_document_id::{compute_document_id, extract_document_id, value_to_string}; diff --git a/meilisearch-core/src/update/documents_addition.rs b/meilisearch-core/src/update/documents_addition.rs index b7e6395af..ddb3c8340 100644 --- a/meilisearch-core/src/update/documents_addition.rs +++ b/meilisearch-core/src/update/documents_addition.rs @@ -1,11 +1,10 @@ use std::collections::HashMap; use std::fmt::Write as _; -use std::fmt; use fst::{set::OpBuilder, SetBuilder}; use indexmap::IndexMap; use sdset::{duo::Union, SetOperation}; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; use serde_json::Value; use meilisearch_types::DocumentId; @@ -16,10 +15,9 @@ use crate::database::{UpdateEvent, UpdateEventsEmitter}; use crate::facets; use crate::raw_indexer::RawIndexer; use crate::serde::{extract_document_id, Deserializer}; -use crate::serde::ConvertToNumber; use crate::store; use crate::update::{apply_documents_deletion, compute_short_prefixes, next_update_id, Update}; -use crate::{Error, MResult, RankedMap}; +use crate::{Error, Number, MResult, RankedMap}; pub struct DocumentsAddition { updates_store: store::Updates, @@ -174,6 +172,20 @@ fn index_value( } } +// TODO move this helper functions elsewhere +fn value_to_number(value: &Value) -> Option { + use std::str::FromStr; + + match value { + Value::Null => None, + Value::Bool(boolean) => Some(Number::Unsigned(*boolean as u64)), + Value::Number(number) => Number::from_str(&number.to_string()).ok(), // TODO improve that + Value::String(string) => Number::from_str(string).ok(), + Value::Array(_array) => None, + Value::Object(_object) => None, + } +} + pub fn apply_addition<'a, 'b>( writer: &'a mut heed::RwTxn<'b, MainT>, index: &store::Index, @@ -263,7 +275,7 @@ pub fn apply_addition<'a, 'b>( } if schema.is_ranked(field_id) { - let number = value.serialize(ConvertToNumber).unwrap_or_default(); + let number = value_to_number(&value).unwrap_or_default(); ranked_map.insert(document_id, field_id, number); } } @@ -360,7 +372,7 @@ pub fn reindex_all_documents(writer: &mut heed::RwTxn, index: &store::Ind } if schema.is_ranked(field_id) { - let number = value.serialize(ConvertToNumber).unwrap_or_default(); + let number = value_to_number(&value).unwrap_or_default(); ranked_map.insert(document_id, field_id, number); } }