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

75 lines
2.2 KiB
Rust
Raw Normal View History

2019-10-21 12:05:53 +02:00
use heed::types::{ByteSlice, OwnedType};
use heed::Result as ZResult;
2019-10-03 11:49:13 +02:00
use meilidb_schema::SchemaAttr;
2019-10-16 17:05:24 +02:00
use super::DocumentAttrKey;
2019-10-18 13:05:28 +02:00
use crate::DocumentId;
2019-10-03 11:49:13 +02:00
2019-10-03 15:04:11 +02:00
#[derive(Copy, Clone)]
2019-10-03 11:49:13 +02:00
pub struct DocumentsFields {
2019-10-21 12:05:53 +02:00
pub(crate) documents_fields: heed::Database<OwnedType<DocumentAttrKey>, ByteSlice>,
2019-10-03 11:49:13 +02:00
}
impl DocumentsFields {
2019-10-03 16:13:09 +02:00
pub fn put_document_field(
2019-10-18 13:21:41 +02:00
self,
2019-10-21 12:05:53 +02:00
writer: &mut heed::RwTxn,
2019-10-03 15:04:11 +02:00
document_id: DocumentId,
2019-10-03 16:13:09 +02:00
attribute: SchemaAttr,
value: &[u8],
2019-10-18 13:05:28 +02:00
) -> ZResult<()> {
2019-10-16 17:05:24 +02:00
let key = DocumentAttrKey::new(document_id, attribute);
self.documents_fields.put(writer, &key, value)
2019-10-03 15:04:11 +02:00
}
2019-10-03 16:13:09 +02:00
pub fn del_all_document_fields(
2019-10-18 13:21:41 +02:00
self,
2019-10-21 12:05:53 +02:00
writer: &mut heed::RwTxn,
2019-10-03 11:49:13 +02:00
document_id: DocumentId,
2019-10-18 13:05:28 +02:00
) -> ZResult<usize> {
2019-10-16 17:05:24 +02:00
let start = DocumentAttrKey::new(document_id, SchemaAttr::min());
let end = DocumentAttrKey::new(document_id, SchemaAttr::max());
self.documents_fields.delete_range(writer, start..=end)
2019-10-03 11:49:13 +02:00
}
2019-10-16 17:05:24 +02:00
pub fn document_attribute<'txn>(
2019-10-18 13:21:41 +02:00
self,
2019-10-21 12:05:53 +02:00
reader: &'txn heed::RoTxn,
2019-10-03 11:49:13 +02:00
document_id: DocumentId,
attribute: SchemaAttr,
2019-10-18 13:05:28 +02:00
) -> ZResult<Option<&'txn [u8]>> {
2019-10-16 17:05:24 +02:00
let key = DocumentAttrKey::new(document_id, attribute);
self.documents_fields.get(reader, &key)
2019-10-03 11:49:13 +02:00
}
2019-10-16 17:05:24 +02:00
pub fn document_fields<'txn>(
2019-10-18 13:21:41 +02:00
self,
2019-10-21 12:05:53 +02:00
reader: &'txn heed::RoTxn,
2019-10-03 11:49:13 +02:00
document_id: DocumentId,
2019-10-18 13:05:28 +02:00
) -> ZResult<DocumentFieldsIter<'txn>> {
2019-10-16 17:05:24 +02:00
let start = DocumentAttrKey::new(document_id, SchemaAttr::min());
let end = DocumentAttrKey::new(document_id, SchemaAttr::max());
let iter = self.documents_fields.range(reader, start..=end)?;
Ok(DocumentFieldsIter { iter })
2019-10-03 11:49:13 +02:00
}
}
2019-10-16 17:05:24 +02:00
pub struct DocumentFieldsIter<'txn> {
2019-10-21 12:05:53 +02:00
iter: heed::RoRange<'txn, OwnedType<DocumentAttrKey>, ByteSlice>,
2019-10-03 11:49:13 +02:00
}
2019-10-16 17:05:24 +02:00
impl<'txn> Iterator for DocumentFieldsIter<'txn> {
type Item = ZResult<(SchemaAttr, &'txn [u8])>;
2019-10-03 11:49:13 +02:00
fn next(&mut self) -> Option<Self::Item> {
match self.iter.next() {
2019-10-16 17:05:24 +02:00
Some(Ok((key, bytes))) => {
let attr = SchemaAttr(key.attr.get());
2019-10-03 11:49:13 +02:00
Some(Ok((attr, bytes)))
2019-10-18 13:05:28 +02:00
}
2019-10-18 13:21:41 +02:00
Some(Err(e)) => Some(Err(e)),
2019-10-03 11:49:13 +02:00
None => None,
}
}
}