meilisearch/src/store/main.rs

100 lines
2.8 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;
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)
}
pub fn words_fst<T: rkv::Readable>(
&self,
reader: &T,
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),
}
}
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
}
pub fn ranked_map<T: rkv::Readable>(
&self,
reader: &T,
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)
}
pub fn number_of_documents<T: rkv::Readable>(
&self,
reader: &T,
) -> 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
}
}