2020-05-19 13:53:31 +02:00
|
|
|
use super::BEU32;
|
2019-11-26 16:12:06 +01:00
|
|
|
use crate::database::MainT;
|
2019-10-18 13:05:28 +02:00
|
|
|
use crate::DocumentId;
|
2019-10-21 12:05:53 +02:00
|
|
|
use heed::types::{ByteSlice, OwnedType};
|
|
|
|
use heed::Result as ZResult;
|
2019-10-03 16:13:09 +02:00
|
|
|
use std::sync::Arc;
|
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,
|
|
|
|
words: &fst::Set,
|
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)
|
|
|
|
}
|
|
|
|
|
2019-10-16 17:05:24 +02:00
|
|
|
pub fn doc_words(
|
2019-10-18 13:21:41 +02:00
|
|
|
self,
|
2019-11-26 16:12:06 +01:00
|
|
|
reader: &heed::RoTxn<MainT>,
|
2019-10-03 16:13:09 +02:00
|
|
|
document_id: DocumentId,
|
2019-10-18 13:05:28 +02:00
|
|
|
) -> ZResult<Option<fst::Set>> {
|
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)? {
|
|
|
|
Some(bytes) => {
|
2019-10-03 16:13:09 +02:00
|
|
|
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();
|
2019-10-03 16:13:09 +02:00
|
|
|
Ok(Some(fst::Set::from(fst)))
|
2019-10-18 13:05:28 +02:00
|
|
|
}
|
2019-10-03 16:13:09 +02:00
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
2019-10-03 15:04:11 +02:00
|
|
|
}
|