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

44 lines
1.4 KiB
Rust
Raw Normal View History

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 {
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,
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<()> {
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
}
pub fn del_doc_words(self, writer: &mut heed::RwTxn<MainT>, document_id: DocumentId) -> ZResult<bool> {
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
pub fn clear(self, writer: &mut heed::RwTxn<MainT>) -> ZResult<()> {
2019-10-23 16:32:11 +02:00
self.docs_words.clear(writer)
}
2020-05-22 15:00:50 +02:00
pub fn doc_words(self, reader: &heed::RoTxn<MainT>, document_id: DocumentId) -> ZResult<FstSetCow> {
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
}