2019-10-18 19:05:28 +08:00
|
|
|
use crate::RankedMap;
|
2019-10-21 18:05:53 +08:00
|
|
|
use heed::types::{ByteSlice, OwnedType, SerdeBincode, Str};
|
|
|
|
use heed::Result as ZResult;
|
2019-10-04 23:23:46 +08:00
|
|
|
use meilidb_schema::Schema;
|
2019-10-18 19:05:28 +08:00
|
|
|
use std::sync::Arc;
|
2019-10-03 21:04:11 +08:00
|
|
|
|
2019-10-18 19:05:28 +08:00
|
|
|
const CUSTOMS_KEY: &str = "customs-key";
|
2019-10-03 22:13:09 +08:00
|
|
|
const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents";
|
2019-10-18 19:05:28 +08:00
|
|
|
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 22:13:09 +08:00
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Main {
|
2019-10-21 18:05:53 +08:00
|
|
|
pub(crate) main: heed::PolyDatabase,
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Main {
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn put_words_fst(self, writer: &mut heed::RwTxn, fst: &fst::Set) -> ZResult<()> {
|
2019-10-16 23:05:24 +08:00
|
|
|
let bytes = fst.as_fst().as_bytes();
|
|
|
|
self.main.put::<Str, ByteSlice>(writer, WORDS_KEY, bytes)
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn words_fst(self, reader: &heed::RoTxn) -> ZResult<Option<fst::Set>> {
|
2019-10-16 23:05:24 +08:00
|
|
|
match self.main.get::<Str, ByteSlice>(reader, WORDS_KEY)? {
|
|
|
|
Some(bytes) => {
|
2019-10-03 21:04:11 +08:00
|
|
|
let len = bytes.len();
|
|
|
|
let bytes = Arc::from(bytes);
|
2019-10-16 23:05:24 +08:00
|
|
|
let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len).unwrap();
|
2019-10-03 21:04:11 +08:00
|
|
|
Ok(Some(fst::Set::from(fst)))
|
2019-10-18 19:05:28 +08:00
|
|
|
}
|
2019-10-03 21:04:11 +08:00
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn put_schema(self, writer: &mut heed::RwTxn, schema: &Schema) -> ZResult<()> {
|
2019-10-18 19:05:28 +08:00
|
|
|
self.main
|
2019-10-21 18:05:53 +08:00
|
|
|
.put::<Str, SerdeBincode<Schema>>(writer, SCHEMA_KEY, schema)
|
2019-10-04 23:23:46 +08:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn schema(self, reader: &heed::RoTxn) -> ZResult<Option<Schema>> {
|
|
|
|
self.main
|
|
|
|
.get::<Str, SerdeBincode<Schema>>(reader, SCHEMA_KEY)
|
2019-10-04 23:23:46 +08:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn put_ranked_map(self, writer: &mut heed::RwTxn, ranked_map: &RankedMap) -> ZResult<()> {
|
2019-10-18 19:05:28 +08:00
|
|
|
self.main
|
2019-10-21 18:05:53 +08:00
|
|
|
.put::<Str, SerdeBincode<RankedMap>>(writer, RANKED_MAP_KEY, &ranked_map)
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn ranked_map(self, reader: &heed::RoTxn) -> ZResult<Option<RankedMap>> {
|
2019-10-18 19:05:28 +08:00
|
|
|
self.main
|
2019-10-21 18:05:53 +08:00
|
|
|
.get::<Str, SerdeBincode<RankedMap>>(reader, RANKED_MAP_KEY)
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn put_synonyms_fst(self, writer: &mut heed::RwTxn, fst: &fst::Set) -> ZResult<()> {
|
2019-10-16 23:05:24 +08:00
|
|
|
let bytes = fst.as_fst().as_bytes();
|
|
|
|
self.main.put::<Str, ByteSlice>(writer, SYNONYMS_KEY, bytes)
|
2019-10-08 22:16:30 +08:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn synonyms_fst(self, reader: &heed::RoTxn) -> ZResult<Option<fst::Set>> {
|
2019-10-16 23:05:24 +08:00
|
|
|
match self.main.get::<Str, ByteSlice>(reader, SYNONYMS_KEY)? {
|
|
|
|
Some(bytes) => {
|
2019-10-08 22:16:30 +08:00
|
|
|
let len = bytes.len();
|
|
|
|
let bytes = Arc::from(bytes);
|
2019-10-16 23:05:24 +08:00
|
|
|
let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len).unwrap();
|
2019-10-08 22:16:30 +08:00
|
|
|
Ok(Some(fst::Set::from(fst)))
|
2019-10-18 19:05:28 +08:00
|
|
|
}
|
2019-10-08 22:16:30 +08:00
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn put_number_of_documents<F>(self, writer: &mut heed::RwTxn, f: F) -> ZResult<u64>
|
2019-10-18 19:05:28 +08:00
|
|
|
where
|
|
|
|
F: Fn(u64) -> u64,
|
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)?;
|
2019-10-18 19:05:28 +08:00
|
|
|
self.main
|
|
|
|
.put::<Str, OwnedType<u64>>(writer, NUMBER_OF_DOCUMENTS_KEY, &new)?;
|
2019-10-03 22:13:09 +08:00
|
|
|
Ok(new)
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn number_of_documents(self, reader: &heed::RoTxn) -> ZResult<u64> {
|
2019-10-18 19:05:28 +08:00
|
|
|
match self
|
|
|
|
.main
|
|
|
|
.get::<Str, OwnedType<u64>>(reader, NUMBER_OF_DOCUMENTS_KEY)?
|
|
|
|
{
|
2019-10-16 23:05:24 +08:00
|
|
|
Some(value) => Ok(value),
|
2019-10-03 22:13:09 +08:00
|
|
|
None => Ok(0),
|
|
|
|
}
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
2019-10-11 21:33:35 +08:00
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn put_customs(self, writer: &mut heed::RwTxn, customs: &[u8]) -> ZResult<()> {
|
2019-10-18 19:05:28 +08:00
|
|
|
self.main
|
|
|
|
.put::<Str, ByteSlice>(writer, CUSTOMS_KEY, customs)
|
2019-10-11 21:33:35 +08:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
pub fn customs<'txn>(self, reader: &'txn heed::RoTxn) -> ZResult<Option<&'txn [u8]>> {
|
2019-10-16 23:05:24 +08:00
|
|
|
self.main.get::<Str, ByteSlice>(reader, CUSTOMS_KEY)
|
2019-10-11 21:33:35 +08:00
|
|
|
}
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|