2020-05-22 15:00:50 +02:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2019-10-21 12:05:53 +02:00
|
|
|
use heed::Result as ZResult;
|
2020-05-22 15:00:50 +02:00
|
|
|
use heed::types::{ByteSlice, OwnedType};
|
|
|
|
|
|
|
|
use crate::database::MainT;
|
|
|
|
use crate::{DocumentId, FstSetCow};
|
|
|
|
use super::BEU32;
|
2019-10-03 15:04:11 +02:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct DocsWords {
|
2020-05-19 13:53:31 +02:00
|
|
|
pub(crate) docs_words: heed::Database<OwnedType<BEU32>, ByteSlice>,
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocsWords {
|
|
|
|
pub fn put_doc_words(
|
2019-10-18 13:21:41 +02:00
|
|
|
self,
|
2019-11-26 16:12:06 +01:00
|
|
|
writer: &mut heed::RwTxn<MainT>,
|
2019-10-03 15:04:11 +02:00
|
|
|
document_id: DocumentId,
|
2020-05-22 15:00:50 +02:00
|
|
|
words: &FstSetCow,
|
2019-10-18 13:05:28 +02:00
|
|
|
) -> ZResult<()> {
|
2020-05-19 13:53:31 +02:00
|
|
|
let document_id = BEU32::new(document_id.0);
|
2019-10-03 16:13:09 +02:00
|
|
|
let bytes = words.as_fst().as_bytes();
|
2019-10-16 17:05:24 +02:00
|
|
|
self.docs_words.put(writer, &document_id, bytes)
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
pub fn del_doc_words(self, writer: &mut heed::RwTxn<MainT>, document_id: DocumentId) -> ZResult<bool> {
|
2020-05-19 13:53:31 +02:00
|
|
|
let document_id = BEU32::new(document_id.0);
|
2019-10-16 17:05:24 +02:00
|
|
|
self.docs_words.delete(writer, &document_id)
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|
2019-10-03 16:13:09 +02:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
pub fn clear(self, writer: &mut heed::RwTxn<MainT>) -> ZResult<()> {
|
2019-10-23 16:32:11 +02:00
|
|
|
self.docs_words.clear(writer)
|
|
|
|
}
|
|
|
|
|
2020-12-09 11:27:38 +01:00
|
|
|
pub fn doc_words<'a>(self, reader: &'a heed::RoTxn<'a, MainT>, document_id: DocumentId) -> ZResult<FstSetCow> {
|
2020-05-19 13:53:31 +02:00
|
|
|
let document_id = BEU32::new(document_id.0);
|
2019-10-16 17:05:24 +02:00
|
|
|
match self.docs_words.get(reader, &document_id)? {
|
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-03 16:13:09 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|