2019-10-21 12:05:53 +02:00
|
|
|
use heed::types::{ByteSlice, OwnedType};
|
2019-11-26 16:12:06 +01:00
|
|
|
use crate::database::MainT;
|
2019-10-21 12:05:53 +02:00
|
|
|
use heed::Result as ZResult;
|
2020-01-13 19:10:58 +01:00
|
|
|
use meilisearch_schema::FieldId;
|
2019-10-16 17:05:24 +02:00
|
|
|
|
2020-01-13 19:10:58 +01:00
|
|
|
use super::DocumentFieldStoredKey;
|
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 {
|
2020-01-13 19:10:58 +01:00
|
|
|
pub(crate) documents_fields: heed::Database<OwnedType<DocumentFieldStoredKey>, 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-11-26 16:12:06 +01:00
|
|
|
writer: &mut heed::RwTxn<MainT>,
|
2019-10-03 15:04:11 +02:00
|
|
|
document_id: DocumentId,
|
2020-01-29 18:30:21 +01:00
|
|
|
field: FieldId,
|
2019-10-03 16:13:09 +02:00
|
|
|
value: &[u8],
|
2019-10-18 13:05:28 +02:00
|
|
|
) -> ZResult<()> {
|
2020-01-29 18:30:21 +01:00
|
|
|
let key = DocumentFieldStoredKey::new(document_id, field);
|
2019-10-16 17:05:24 +02:00
|
|
|
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-11-26 16:12:06 +01:00
|
|
|
writer: &mut heed::RwTxn<MainT>,
|
2019-10-03 11:49:13 +02:00
|
|
|
document_id: DocumentId,
|
2019-10-18 13:05:28 +02:00
|
|
|
) -> ZResult<usize> {
|
2020-01-13 19:10:58 +01:00
|
|
|
let start = DocumentFieldStoredKey::new(document_id, FieldId::min());
|
|
|
|
let end = DocumentFieldStoredKey::new(document_id, FieldId::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-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.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-11-26 16:12:06 +01:00
|
|
|
reader: &'txn heed::RoTxn<MainT>,
|
2019-10-03 11:49:13 +02:00
|
|
|
document_id: DocumentId,
|
2020-01-29 18:30:21 +01:00
|
|
|
field: FieldId,
|
2019-10-18 13:05:28 +02:00
|
|
|
) -> ZResult<Option<&'txn [u8]>> {
|
2020-01-29 18:30:21 +01:00
|
|
|
let key = DocumentFieldStoredKey::new(document_id, field);
|
2019-10-16 17:05:24 +02:00
|
|
|
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-11-26 16:12:06 +01:00
|
|
|
reader: &'txn heed::RoTxn<MainT>,
|
2019-10-03 11:49:13 +02:00
|
|
|
document_id: DocumentId,
|
2019-10-18 13:05:28 +02:00
|
|
|
) -> ZResult<DocumentFieldsIter<'txn>> {
|
2020-01-13 19:10:58 +01:00
|
|
|
let start = DocumentFieldStoredKey::new(document_id, FieldId::min());
|
|
|
|
let end = DocumentFieldStoredKey::new(document_id, FieldId::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> {
|
2020-01-13 19:10:58 +01:00
|
|
|
iter: heed::RoRange<'txn, OwnedType<DocumentFieldStoredKey>, ByteSlice>,
|
2019-10-03 11:49:13 +02:00
|
|
|
}
|
|
|
|
|
2019-10-16 17:05:24 +02:00
|
|
|
impl<'txn> Iterator for DocumentFieldsIter<'txn> {
|
2020-01-13 19:10:58 +01:00
|
|
|
type Item = ZResult<(FieldId, &'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))) => {
|
2020-01-13 19:10:58 +01:00
|
|
|
let field_id = FieldId(key.field_id.get());
|
|
|
|
Some(Ok((field_id, 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|