MeiliSearch/meilisearch-core/src/store/synonyms.rs

41 lines
1.1 KiB
Rust
Raw Normal View History

2019-10-21 12:05:53 +02:00
use heed::types::ByteSlice;
use heed::Result as ZResult;
use std::sync::Arc;
2019-10-03 15:04:11 +02:00
#[derive(Copy, Clone)]
pub struct Synonyms {
2019-10-21 12:05:53 +02:00
pub(crate) synonyms: heed::Database<ByteSlice, ByteSlice>,
}
impl Synonyms {
pub fn put_synonyms(
2019-10-18 13:21:41 +02:00
self,
2019-10-21 12:05:53 +02:00
writer: &mut heed::RwTxn,
word: &[u8],
synonyms: &fst::Set,
2019-10-18 13:05:28 +02:00
) -> ZResult<()> {
2019-10-16 17:05:24 +02:00
let bytes = synonyms.as_fst().as_bytes();
self.synonyms.put(writer, word, bytes)
}
2019-10-21 12:05:53 +02:00
pub fn del_synonyms(self, writer: &mut heed::RwTxn, word: &[u8]) -> ZResult<bool> {
2019-10-16 17:05:24 +02:00
self.synonyms.delete(writer, word)
2019-10-08 17:16:48 +02:00
}
2019-11-06 10:49:13 +01:00
pub fn clear(self, writer: &mut heed::RwTxn) -> ZResult<()> {
self.synonyms.clear(writer)
}
2019-10-21 12:05:53 +02:00
pub fn synonyms(self, reader: &heed::RoTxn, word: &[u8]) -> ZResult<Option<fst::Set>> {
match self.synonyms.get(reader, word)? {
2019-10-16 17:05:24 +02:00
Some(bytes) => {
let len = bytes.len();
2019-11-17 11:14:01 +01:00
let bytes = Arc::new(bytes.to_owned());
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)))
2019-10-18 13:05:28 +02:00
}
None => Ok(None),
}
}
}