use std::sync::Arc; use meilidb_schema::Schema; use zlmdb::types::{Str, OwnedType, ByteSlice, Serde}; use zlmdb::Result as ZResult; use crate::RankedMap; const CUSTOMS_KEY: &str = "customs-key"; 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"; #[derive(Copy, Clone)] pub struct Main { pub(crate) main: zlmdb::DynDatabase, } impl Main { pub fn put_words_fst(&self, writer: &mut zlmdb::RwTxn, fst: &fst::Set) -> ZResult<()> { let bytes = fst.as_fst().as_bytes(); self.main.put::(writer, WORDS_KEY, bytes) } pub fn words_fst(&self, reader: &zlmdb::RoTxn) -> ZResult> { match self.main.get::(reader, WORDS_KEY)? { Some(bytes) => { let len = bytes.len(); let bytes = Arc::from(bytes); let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len).unwrap(); Ok(Some(fst::Set::from(fst))) }, None => Ok(None), } } pub fn put_schema(&self, writer: &mut zlmdb::RwTxn, schema: &Schema) -> ZResult<()> { self.main.put::>(writer, SCHEMA_KEY, schema) } pub fn schema(&self, reader: &zlmdb::RoTxn) -> ZResult> { self.main.get::>(reader, SCHEMA_KEY) } pub fn put_ranked_map(&self, writer: &mut zlmdb::RwTxn, ranked_map: &RankedMap) -> ZResult<()> { self.main.put::>(writer, RANKED_MAP_KEY, &ranked_map) } pub fn ranked_map(&self, reader: &zlmdb::RoTxn) -> ZResult> { self.main.get::>(reader, RANKED_MAP_KEY) } pub fn put_synonyms_fst(&self, writer: &mut zlmdb::RwTxn, fst: &fst::Set) -> ZResult<()> { let bytes = fst.as_fst().as_bytes(); self.main.put::(writer, SYNONYMS_KEY, bytes) } pub fn synonyms_fst(&self, reader: &zlmdb::RoTxn) -> ZResult> { match self.main.get::(reader, SYNONYMS_KEY)? { Some(bytes) => { let len = bytes.len(); let bytes = Arc::from(bytes); let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len).unwrap(); Ok(Some(fst::Set::from(fst))) }, None => Ok(None), } } pub fn put_number_of_documents(&self, writer: &mut zlmdb::RwTxn, f: F) -> ZResult where F: Fn(u64) -> u64, { let new = self.number_of_documents(writer).map(f)?; self.main.put::>(writer, NUMBER_OF_DOCUMENTS_KEY, &new)?; Ok(new) } pub fn number_of_documents(&self, reader: &zlmdb::RoTxn) -> ZResult { match self.main.get::>(reader, NUMBER_OF_DOCUMENTS_KEY)? { Some(value) => Ok(value), None => Ok(0), } } pub fn put_customs(&self, writer: &mut zlmdb::RwTxn, customs: &[u8]) -> ZResult<()> { self.main.put::(writer, CUSTOMS_KEY, customs) } pub fn customs<'txn>(&self, reader: &'txn zlmdb::RoTxn) -> ZResult> { self.main.get::(reader, CUSTOMS_KEY) } }