diff --git a/meilidb-data/src/database.rs b/meilidb-data/src/database.rs index 667baf2e0..e2fb2db6a 100644 --- a/meilidb-data/src/database.rs +++ b/meilidb-data/src/database.rs @@ -225,7 +225,7 @@ impl MainIndex { fn ranked_map(&self) -> Result, Error> { match self.0.get("ranked-map")? { Some(bytes) => { - let ranked_map = bincode::deserialize(bytes.as_ref())?; + let ranked_map = RankedMap::read_from_bin(bytes.as_ref())?; Ok(Some(ranked_map)) }, None => Ok(None), @@ -309,7 +309,7 @@ impl RawIndex { let ranked_map = { let map = match inner.get("ranked-map")? { - Some(bytes) => bincode::deserialize(bytes.as_ref())?, + Some(bytes) => RankedMap::read_from_bin(bytes.as_ref())?, None => RankedMap::default(), }; @@ -355,8 +355,10 @@ impl RawIndex { } pub fn update_ranked_map(&self, ranked_map: Arc) -> sled::Result<()> { - let data = bincode::serialize(ranked_map.as_ref()).unwrap(); - self.inner.set("ranked-map", data).map(drop)?; + let mut bytes = Vec::new(); + ranked_map.as_ref().write_to_bin(&mut bytes).unwrap(); + + self.inner.set("ranked-map", bytes).map(drop)?; self.ranked_map.store(ranked_map); Ok(()) diff --git a/meilidb-data/src/ranked_map.rs b/meilidb-data/src/ranked_map.rs index 7b4ff3735..aea7c8b20 100644 --- a/meilidb-data/src/ranked_map.rs +++ b/meilidb-data/src/ranked_map.rs @@ -1,5 +1,27 @@ +use std::io::{Read, Write}; + use hashbrown::HashMap; use meilidb_core::DocumentId; + use crate::{SchemaAttr, Number}; -pub type RankedMap = HashMap<(DocumentId, SchemaAttr), Number>; +#[derive(Debug, Default, Clone, PartialEq, Eq)] +pub struct RankedMap(HashMap<(DocumentId, SchemaAttr), Number>); + +impl RankedMap { + pub fn insert(&mut self, document: DocumentId, attribute: SchemaAttr, number: Number) { + self.0.insert((document, attribute), number); + } + + pub fn get(&self, document: DocumentId, attribute: SchemaAttr) -> Option { + self.0.get(&(document, attribute)).cloned() + } + + pub fn read_from_bin(reader: R) -> bincode::Result { + bincode::deserialize_from(reader).map(RankedMap) + } + + pub fn write_to_bin(&self, writer: W) -> bincode::Result<()> { + bincode::serialize_into(writer, &self.0) + } +} diff --git a/meilidb-data/src/serde/serializer.rs b/meilidb-data/src/serde/serializer.rs index 1857fe134..6f4ad3608 100644 --- a/meilidb-data/src/serde/serializer.rs +++ b/meilidb-data/src/serde/serializer.rs @@ -285,9 +285,8 @@ where T: ser::Serialize, } if props.is_ranked() { - let key = (document_id, attr); let number = value.serialize(ConvertToNumber)?; - ranked_map.insert(key, number); + ranked_map.insert(document_id, attr, number); } } diff --git a/meilidb/src/sort_by_attr.rs b/meilidb/src/sort_by_attr.rs index f4c4bcc41..2cacaae13 100644 --- a/meilidb/src/sort_by_attr.rs +++ b/meilidb/src/sort_by_attr.rs @@ -87,8 +87,8 @@ impl<'a> SortByAttr<'a> { impl<'a> Criterion for SortByAttr<'a> { fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering { - let lhs = self.ranked_map.get(&(lhs.id, self.attr)); - let rhs = self.ranked_map.get(&(rhs.id, self.attr)); + let lhs = self.ranked_map.get(lhs.id, self.attr); + let rhs = self.ranked_map.get(rhs.id, self.attr); match (lhs, rhs) { (Some(lhs), Some(rhs)) => {