meilisearch/meilidb-core/src/store/main.rs

128 lines
3.5 KiB
Rust
Raw Normal View History

2019-10-03 21:04:11 +08:00
use std::sync::Arc;
2019-10-03 22:13:09 +08:00
use std::convert::TryInto;
2019-10-04 23:23:46 +08:00
use meilidb_schema::Schema;
2019-10-03 22:13:09 +08:00
use rkv::Value;
2019-10-03 23:33:15 +08:00
use crate::{RankedMap, MResult};
2019-10-03 21:04:11 +08:00
2019-10-03 22:13:09 +08:00
const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents";
const RANKED_MAP_KEY: &str = "ranked-map";
const SCHEMA_KEY: &str = "schema";
const SYNONYMS_KEY: &str = "synonyms";
const WORDS_KEY: &str = "words";
2019-10-03 21:04:11 +08:00
#[derive(Copy, Clone)]
pub struct Main {
pub(crate) main: rkv::SingleStore,
}
impl Main {
pub fn put_words_fst(
&self,
writer: &mut rkv::Writer,
fst: &fst::Set,
) -> Result<(), rkv::StoreError>
{
let blob = rkv::Value::Blob(fst.as_fst().as_bytes());
self.main.put(writer, WORDS_KEY, &blob)
}
2019-10-04 16:21:09 +08:00
pub fn words_fst(
2019-10-03 21:04:11 +08:00
&self,
2019-10-04 16:21:09 +08:00
reader: &impl rkv::Readable,
2019-10-03 23:33:15 +08:00
) -> MResult<Option<fst::Set>>
2019-10-03 21:04:11 +08:00
{
match self.main.get(reader, WORDS_KEY)? {
2019-10-03 22:13:09 +08:00
Some(Value::Blob(bytes)) => {
2019-10-03 21:04:11 +08:00
let len = bytes.len();
let bytes = Arc::from(bytes);
2019-10-03 23:33:15 +08:00
let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len)?;
2019-10-03 21:04:11 +08:00
Ok(Some(fst::Set::from(fst)))
},
Some(value) => panic!("invalid type {:?}", value),
None => Ok(None),
}
}
2019-10-04 23:23:46 +08:00
pub fn put_schema(
&self,
writer: &mut rkv::Writer,
schema: &Schema,
) -> MResult<()>
{
let bytes = bincode::serialize(schema)?;
let blob = Value::Blob(&bytes[..]);
self.main.put(writer, SCHEMA_KEY, &blob)?;
Ok(())
}
pub fn schema(
&self,
reader: &impl rkv::Readable,
) -> MResult<Option<Schema>>
{
match self.main.get(reader, SCHEMA_KEY)? {
Some(Value::Blob(bytes)) => {
let schema = bincode::deserialize_from(bytes.as_ref())?;
Ok(Some(schema))
},
Some(value) => panic!("invalid type {:?}", value),
None => Ok(None),
}
}
2019-10-03 21:04:11 +08:00
pub fn put_ranked_map(
&self,
writer: &mut rkv::Writer,
ranked_map: &RankedMap,
2019-10-03 23:33:15 +08:00
) -> MResult<()>
2019-10-03 21:04:11 +08:00
{
2019-10-03 22:13:09 +08:00
let mut bytes = Vec::new();
2019-10-03 23:33:15 +08:00
ranked_map.write_to_bin(&mut bytes)?;
2019-10-03 22:13:09 +08:00
let blob = Value::Blob(&bytes[..]);
2019-10-03 23:33:15 +08:00
self.main.put(writer, RANKED_MAP_KEY, &blob)?;
Ok(())
2019-10-03 21:04:11 +08:00
}
2019-10-04 16:21:09 +08:00
pub fn ranked_map(
2019-10-03 21:04:11 +08:00
&self,
2019-10-04 16:21:09 +08:00
reader: &impl rkv::Readable,
2019-10-03 23:33:15 +08:00
) -> MResult<Option<RankedMap>>
2019-10-03 21:04:11 +08:00
{
2019-10-03 22:13:09 +08:00
match self.main.get(reader, RANKED_MAP_KEY)? {
Some(Value::Blob(bytes)) => {
2019-10-03 23:33:15 +08:00
let ranked_map = RankedMap::read_from_bin(bytes)?;
2019-10-03 22:13:09 +08:00
Ok(Some(ranked_map))
},
Some(value) => panic!("invalid type {:?}", value),
None => Ok(None),
}
2019-10-03 21:04:11 +08:00
}
pub fn put_number_of_documents<F: Fn(u64) -> u64>(
&self,
writer: &mut rkv::Writer,
2019-10-03 22:13:09 +08:00
f: F,
) -> Result<u64, rkv::StoreError>
2019-10-03 21:04:11 +08:00
{
2019-10-03 22:13:09 +08:00
let new = self.number_of_documents(writer).map(f)?;
self.main.put(writer, NUMBER_OF_DOCUMENTS_KEY, &Value::Blob(&new.to_be_bytes()))?;
Ok(new)
}
2019-10-04 16:21:09 +08:00
pub fn number_of_documents(
2019-10-03 22:13:09 +08:00
&self,
2019-10-04 16:21:09 +08:00
reader: &impl rkv::Readable,
2019-10-03 22:13:09 +08:00
) -> Result<u64, rkv::StoreError>
{
match self.main.get(reader, NUMBER_OF_DOCUMENTS_KEY)? {
Some(Value::Blob(bytes)) => {
let array = bytes.try_into().unwrap();
Ok(u64::from_be_bytes(array))
},
Some(value) => panic!("invalid type {:?}", value),
None => Ok(0),
}
2019-10-03 21:04:11 +08:00
}
}