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

122 lines
4.2 KiB
Rust
Raw Normal View History

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