feat: Introduce the RankedMap real type

This commit is contained in:
Clément Renault 2019-05-06 14:25:11 +02:00
parent 0c18026240
commit 8434ecbb43
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
4 changed files with 32 additions and 9 deletions

View File

@ -225,7 +225,7 @@ impl MainIndex {
fn ranked_map(&self) -> Result<Option<RankedMap>, Error> { fn ranked_map(&self) -> Result<Option<RankedMap>, Error> {
match self.0.get("ranked-map")? { match self.0.get("ranked-map")? {
Some(bytes) => { Some(bytes) => {
let ranked_map = bincode::deserialize(bytes.as_ref())?; let ranked_map = RankedMap::read_from_bin(bytes.as_ref())?;
Ok(Some(ranked_map)) Ok(Some(ranked_map))
}, },
None => Ok(None), None => Ok(None),
@ -309,7 +309,7 @@ impl RawIndex {
let ranked_map = { let ranked_map = {
let map = match inner.get("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(), None => RankedMap::default(),
}; };
@ -355,8 +355,10 @@ impl RawIndex {
} }
pub fn update_ranked_map(&self, ranked_map: Arc<RankedMap>) -> sled::Result<()> { pub fn update_ranked_map(&self, ranked_map: Arc<RankedMap>) -> sled::Result<()> {
let data = bincode::serialize(ranked_map.as_ref()).unwrap(); let mut bytes = Vec::new();
self.inner.set("ranked-map", data).map(drop)?; ranked_map.as_ref().write_to_bin(&mut bytes).unwrap();
self.inner.set("ranked-map", bytes).map(drop)?;
self.ranked_map.store(ranked_map); self.ranked_map.store(ranked_map);
Ok(()) Ok(())

View File

@ -1,5 +1,27 @@
use std::io::{Read, Write};
use hashbrown::HashMap; use hashbrown::HashMap;
use meilidb_core::DocumentId; use meilidb_core::DocumentId;
use crate::{SchemaAttr, Number}; 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<Number> {
self.0.get(&(document, attribute)).cloned()
}
pub fn read_from_bin<R: Read>(reader: R) -> bincode::Result<RankedMap> {
bincode::deserialize_from(reader).map(RankedMap)
}
pub fn write_to_bin<W: Write>(&self, writer: W) -> bincode::Result<()> {
bincode::serialize_into(writer, &self.0)
}
}

View File

@ -285,9 +285,8 @@ where T: ser::Serialize,
} }
if props.is_ranked() { if props.is_ranked() {
let key = (document_id, attr);
let number = value.serialize(ConvertToNumber)?; let number = value.serialize(ConvertToNumber)?;
ranked_map.insert(key, number); ranked_map.insert(document_id, attr, number);
} }
} }

View File

@ -87,8 +87,8 @@ impl<'a> SortByAttr<'a> {
impl<'a> Criterion for SortByAttr<'a> { impl<'a> Criterion for SortByAttr<'a> {
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering { fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
let lhs = self.ranked_map.get(&(lhs.id, self.attr)); let lhs = self.ranked_map.get(lhs.id, self.attr);
let rhs = self.ranked_map.get(&(rhs.id, self.attr)); let rhs = self.ranked_map.get(rhs.id, self.attr);
match (lhs, rhs) { match (lhs, rhs) {
(Some(lhs), Some(rhs)) => { (Some(lhs), Some(rhs)) => {