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

143 lines
4.4 KiB
Rust
Raw Normal View History

2019-10-18 13:05:28 +02:00
use super::DocumentAttrKey;
use crate::DocumentId;
2019-10-21 12:05:53 +02:00
use heed::types::OwnedType;
use heed::Result as ZResult;
2019-11-26 11:06:55 +01:00
use meilisearch_schema::SchemaAttr;
#[derive(Copy, Clone)]
pub struct DocumentsFieldsCounts {
2019-10-21 12:05:53 +02:00
pub(crate) documents_fields_counts: heed::Database<OwnedType<DocumentAttrKey>, OwnedType<u64>>,
}
impl DocumentsFieldsCounts {
pub fn put_document_field_count(
2019-10-18 13:21:41 +02:00
self,
2019-10-21 12:05:53 +02:00
writer: &mut heed::RwTxn,
document_id: DocumentId,
attribute: SchemaAttr,
value: u64,
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_counts.put(writer, &key, &value)
}
pub fn del_all_document_fields_counts(
2019-10-18 13:21:41 +02:00
self,
2019-10-21 12:05:53 +02:00
writer: &mut heed::RwTxn,
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-18 13:05:28 +02:00
self.documents_fields_counts
2019-10-31 15:48:29 +01:00
.delete_range(writer, &(start..=end))
}
2019-10-23 16:32:11 +02:00
pub fn clear(self, writer: &mut heed::RwTxn) -> ZResult<()> {
self.documents_fields_counts.clear(writer)
}
pub fn document_field_count(
2019-10-18 13:21:41 +02:00
self,
2019-10-21 12:05:53 +02:00
reader: &heed::RoTxn,
document_id: DocumentId,
attribute: SchemaAttr,
2019-10-18 13:05:28 +02:00
) -> ZResult<Option<u64>> {
2019-10-16 17:05:24 +02:00
let key = DocumentAttrKey::new(document_id, attribute);
match self.documents_fields_counts.get(reader, &key)? {
Some(count) => Ok(Some(count)),
None => Ok(None),
}
}
2019-10-16 17:05:24 +02:00
pub fn document_fields_counts<'txn>(
2019-10-18 13:21:41 +02:00
self,
2019-10-21 12:05:53 +02:00
reader: &'txn heed::RoTxn,
document_id: DocumentId,
2019-10-18 13:05:28 +02:00
) -> ZResult<DocumentFieldsCountsIter<'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_counts.range(reader, &(start..=end))?;
2019-10-16 17:05:24 +02:00
Ok(DocumentFieldsCountsIter { iter })
}
2019-10-21 12:05:53 +02:00
pub fn documents_ids<'txn>(self, reader: &'txn heed::RoTxn) -> ZResult<DocumentsIdsIter<'txn>> {
2019-10-16 17:05:24 +02:00
let iter = self.documents_fields_counts.iter(reader)?;
2019-10-18 13:05:28 +02:00
Ok(DocumentsIdsIter {
last_seen_id: None,
iter,
})
}
2019-10-16 17:05:24 +02:00
pub fn all_documents_fields_counts<'txn>(
2019-10-18 13:21:41 +02:00
self,
2019-10-21 12:05:53 +02:00
reader: &'txn heed::RoTxn,
2019-10-18 13:05:28 +02:00
) -> ZResult<AllDocumentsFieldsCountsIter<'txn>> {
2019-10-16 17:05:24 +02:00
let iter = self.documents_fields_counts.iter(reader)?;
Ok(AllDocumentsFieldsCountsIter { iter })
}
}
2019-10-16 17:05:24 +02:00
pub struct DocumentFieldsCountsIter<'txn> {
2019-10-21 12:05:53 +02:00
iter: heed::RoRange<'txn, OwnedType<DocumentAttrKey>, OwnedType<u64>>,
}
impl Iterator for DocumentFieldsCountsIter<'_> {
2019-10-16 17:05:24 +02:00
type Item = ZResult<(SchemaAttr, u64)>;
fn next(&mut self) -> Option<Self::Item> {
match self.iter.next() {
2019-10-16 17:05:24 +02:00
Some(Ok((key, count))) => {
let attr = SchemaAttr(key.attr.get());
Some(Ok((attr, count)))
2019-10-18 13:05:28 +02:00
}
2019-10-18 13:21:41 +02:00
Some(Err(e)) => Some(Err(e)),
None => None,
}
}
}
2019-10-16 17:05:24 +02:00
pub struct DocumentsIdsIter<'txn> {
last_seen_id: Option<DocumentId>,
2019-10-21 12:05:53 +02:00
iter: heed::RoIter<'txn, OwnedType<DocumentAttrKey>, OwnedType<u64>>,
}
impl Iterator for DocumentsIdsIter<'_> {
2019-10-16 17:05:24 +02:00
type Item = ZResult<DocumentId>;
fn next(&mut self) -> Option<Self::Item> {
for result in &mut self.iter {
match result {
Ok((key, _)) => {
2019-10-16 17:05:24 +02:00
let document_id = DocumentId(key.docid.get());
if Some(document_id) != self.last_seen_id {
self.last_seen_id = Some(document_id);
2019-10-18 13:05:28 +02:00
return Some(Ok(document_id));
}
2019-10-18 13:05:28 +02:00
}
2019-10-18 13:21:41 +02:00
Err(e) => return Some(Err(e)),
}
}
None
}
}
2019-10-16 17:05:24 +02:00
pub struct AllDocumentsFieldsCountsIter<'txn> {
2019-10-21 12:05:53 +02:00
iter: heed::RoIter<'txn, OwnedType<DocumentAttrKey>, OwnedType<u64>>,
}
impl Iterator for AllDocumentsFieldsCountsIter<'_> {
2019-10-16 17:05:24 +02:00
type Item = ZResult<(DocumentId, SchemaAttr, u64)>;
fn next(&mut self) -> Option<Self::Item> {
match self.iter.next() {
2019-10-16 17:05:24 +02:00
Some(Ok((key, count))) => {
let docid = DocumentId(key.docid.get());
let attr = SchemaAttr(key.attr.get());
Some(Ok((docid, attr, count)))
2019-10-18 13:05:28 +02:00
}
2019-10-18 13:21:41 +02:00
Some(Err(e)) => Some(Err(e)),
None => None,
}
}
}