2020-05-22 15:00:50 +02:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
use heed::Result as ZResult;
|
2019-10-21 12:05:53 +02:00
|
|
|
use heed::types::ByteSlice;
|
2020-05-22 15:00:50 +02:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
use crate::database::MainT;
|
2020-05-22 12:03:57 +02:00
|
|
|
use crate::{FstSetCow, MResult};
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2019-10-03 15:04:11 +02:00
|
|
|
#[derive(Copy, Clone)]
|
2019-10-02 17:34:32 +02:00
|
|
|
pub struct Synonyms {
|
2019-10-21 12:05:53 +02:00
|
|
|
pub(crate) synonyms: heed::Database<ByteSlice, ByteSlice>,
|
2019-10-02 17:34:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Synonyms {
|
2020-05-22 15:00:50 +02:00
|
|
|
pub fn put_synonyms<A>(self, writer: &mut heed::RwTxn<MainT>, word: &[u8], synonyms: &fst::Set<A>) -> ZResult<()>
|
|
|
|
where A: AsRef<[u8]>,
|
|
|
|
{
|
2019-10-16 17:05:24 +02:00
|
|
|
let bytes = synonyms.as_fst().as_bytes();
|
|
|
|
self.synonyms.put(writer, word, bytes)
|
2019-10-02 17:34:32 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
pub fn del_synonyms(self, writer: &mut heed::RwTxn<MainT>, 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-26 16:12:06 +01:00
|
|
|
pub fn clear(self, writer: &mut heed::RwTxn<MainT>) -> ZResult<()> {
|
2019-11-06 10:49:13 +01:00
|
|
|
self.synonyms.clear(writer)
|
|
|
|
}
|
|
|
|
|
2020-05-22 12:03:57 +02:00
|
|
|
pub fn synonyms_fst<'txn>(self, reader: &'txn heed::RoTxn<MainT>, word: &[u8]) -> ZResult<FstSetCow<'txn>> {
|
2019-10-08 16:16:30 +02:00
|
|
|
match self.synonyms.get(reader, word)? {
|
2020-05-22 15:00:50 +02:00
|
|
|
Some(bytes) => Ok(fst::Set::new(bytes).unwrap().map_data(Cow::Borrowed).unwrap()),
|
|
|
|
None => Ok(fst::Set::default().map_data(Cow::Owned).unwrap()),
|
2019-10-08 16:16:30 +02:00
|
|
|
}
|
2019-10-02 17:34:32 +02:00
|
|
|
}
|
2020-05-22 12:03:57 +02:00
|
|
|
|
|
|
|
pub fn synonyms(self, reader: &heed::RoTxn<MainT>, word: &[u8]) -> MResult<Option<Vec<String>>> {
|
|
|
|
let synonyms = self
|
|
|
|
.synonyms_fst(&reader, word)?
|
|
|
|
.map(|list| list.stream().into_strs())
|
|
|
|
.transpose()?;
|
|
|
|
Ok(synonyms)
|
|
|
|
}
|
2019-10-02 17:34:32 +02:00
|
|
|
}
|
2020-05-22 12:03:57 +02:00
|
|
|
|