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

56 lines
1.6 KiB
Rust
Raw Normal View History

2019-10-03 16:13:09 +02:00
use std::sync::Arc;
use rkv::{Value, StoreError};
2019-10-03 17:33:15 +02:00
use crate::{DocumentId, MResult};
2019-10-03 15:04:11 +02:00
#[derive(Copy, Clone)]
pub struct DocsWords {
pub(crate) docs_words: rkv::SingleStore,
}
impl DocsWords {
pub fn put_doc_words(
&self,
writer: &mut rkv::Writer,
document_id: DocumentId,
words: &fst::Set,
) -> Result<(), rkv::StoreError>
{
2019-10-03 16:13:09 +02:00
let document_id_bytes = document_id.0.to_be_bytes();
let bytes = words.as_fst().as_bytes();
self.docs_words.put(writer, document_id_bytes, &Value::Blob(bytes))
2019-10-03 15:04:11 +02:00
}
pub fn del_doc_words(
&self,
writer: &mut rkv::Writer,
document_id: DocumentId,
) -> Result<bool, rkv::StoreError>
2019-10-03 15:04:11 +02:00
{
let document_id_bytes = document_id.0.to_be_bytes();
match self.docs_words.delete(writer, document_id_bytes) {
Ok(()) => Ok(true),
Err(StoreError::LmdbError(lmdb::Error::NotFound)) => Ok(false),
Err(e) => Err(e),
}
2019-10-03 15:04:11 +02:00
}
2019-10-03 16:13:09 +02:00
pub fn doc_words<T: rkv::Readable>(
&self,
reader: &T,
document_id: DocumentId,
2019-10-03 17:33:15 +02:00
) -> MResult<Option<fst::Set>>
2019-10-03 16:13:09 +02:00
{
let document_id_bytes = document_id.0.to_be_bytes();
match self.docs_words.get(reader, document_id_bytes)? {
Some(Value::Blob(bytes)) => {
let len = bytes.len();
let bytes = Arc::from(bytes);
2019-10-03 17:33:15 +02:00
let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len)?;
2019-10-03 16:13:09 +02:00
Ok(Some(fst::Set::from(fst)))
},
Some(value) => panic!("invalid type {:?}", value),
None => Ok(None),
}
}
2019-10-03 15:04:11 +02:00
}