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

79 lines
2.3 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-11-26 11:06:55 +01:00
use meilisearch_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());
2019-10-31 15:48:29 +01:00
self.documents_fields.delete_range(writer, &(start..=end))
2019-10-03 11:49:13 +02:00
}
2019-10-23 16:32:11 +02:00
pub fn clear(self, writer: &mut heed::RwTxn) -> ZResult<()> {
self.documents_fields.clear(writer)
}
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());
2019-10-31 15:48:29 +01:00
let iter = self.documents_fields.range(reader, &(start..=end))?;
2019-10-16 17:05:24 +02:00
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,
}
}
}