mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-11 15:38:55 +01:00
feat: Introduce the RankedMap real type
This commit is contained in:
parent
0c18026240
commit
8434ecbb43
@ -225,7 +225,7 @@ impl MainIndex {
|
||||
fn ranked_map(&self) -> Result<Option<RankedMap>, 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<RankedMap>) -> 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(())
|
||||
|
@ -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<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)
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)) => {
|
||||
|
Loading…
Reference in New Issue
Block a user