2019-10-08 16:16:30 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use crate::error::MResult;
|
|
|
|
|
2019-10-03 15:04:11 +02:00
|
|
|
#[derive(Copy, Clone)]
|
2019-10-02 17:34:32 +02:00
|
|
|
pub struct Synonyms {
|
|
|
|
pub(crate) synonyms: rkv::SingleStore,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Synonyms {
|
2019-10-08 16:16:30 +02:00
|
|
|
pub fn put_synonyms(
|
2019-10-02 17:34:32 +02:00
|
|
|
&self,
|
2019-10-08 16:16:30 +02:00
|
|
|
writer: &mut rkv::Writer,
|
|
|
|
word: &[u8],
|
|
|
|
synonyms: &fst::Set,
|
|
|
|
) -> Result<(), rkv::StoreError>
|
2019-10-02 17:34:32 +02:00
|
|
|
{
|
2019-10-08 16:16:30 +02:00
|
|
|
let blob = rkv::Value::Blob(synonyms.as_fst().as_bytes());
|
|
|
|
self.synonyms.put(writer, word, &blob)
|
2019-10-02 17:34:32 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 16:16:30 +02:00
|
|
|
pub fn synonyms(
|
2019-10-02 17:34:32 +02:00
|
|
|
&self,
|
2019-10-04 10:21:09 +02:00
|
|
|
reader: &impl rkv::Readable,
|
2019-10-02 17:34:32 +02:00
|
|
|
word: &[u8],
|
2019-10-08 16:16:30 +02:00
|
|
|
) -> MResult<Option<fst::Set>>
|
2019-10-02 17:34:32 +02:00
|
|
|
{
|
2019-10-08 16:16:30 +02:00
|
|
|
match self.synonyms.get(reader, word)? {
|
|
|
|
Some(rkv::Value::Blob(bytes)) => {
|
|
|
|
let len = bytes.len();
|
|
|
|
let bytes = Arc::from(bytes);
|
|
|
|
let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len)?;
|
|
|
|
Ok(Some(fst::Set::from(fst)))
|
|
|
|
},
|
|
|
|
Some(value) => panic!("invalid type {:?}", value),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
2019-10-02 17:34:32 +02:00
|
|
|
}
|
|
|
|
}
|