MeiliSearch/meilidb-core/src/ranked_map.rs

42 lines
1.1 KiB
Rust
Raw Normal View History

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