2019-10-03 17:49:13 +08:00
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
|
|
|
use hashbrown::HashMap;
|
2020-01-11 01:20:30 +08:00
|
|
|
use meilisearch_schema::FieldId;
|
2019-10-18 19:05:28 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-10-03 17:49:13 +08:00
|
|
|
|
|
|
|
use crate::{DocumentId, Number};
|
|
|
|
|
2019-10-18 19:05:28 +08:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
2019-10-16 23:05:24 +08:00
|
|
|
#[serde(transparent)]
|
2020-01-11 01:20:30 +08:00
|
|
|
pub struct RankedMap(HashMap<(DocumentId, FieldId), Number>);
|
2019-10-03 17:49:13 +08:00
|
|
|
|
|
|
|
impl RankedMap {
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
|
|
|
|
2019-10-18 19:21:41 +08:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_empty()
|
|
|
|
}
|
|
|
|
|
2020-01-30 01:30:21 +08:00
|
|
|
pub fn insert(&mut self, document: DocumentId, field: FieldId, number: Number) {
|
|
|
|
self.0.insert((document, field), number);
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
2020-01-30 01:30:21 +08:00
|
|
|
pub fn remove(&mut self, document: DocumentId, field: FieldId) {
|
|
|
|
self.0.remove(&(document, field));
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
2020-01-30 01:30:21 +08:00
|
|
|
pub fn get(&self, document: DocumentId, field: FieldId) -> Option<Number> {
|
|
|
|
self.0.get(&(document, field)).cloned()
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|