diff --git a/meilidb-data/src/database/documents_index.rs b/meilidb-data/src/database/documents_index.rs index 706fa9d84..60420a8a9 100644 --- a/meilidb-data/src/database/documents_index.rs +++ b/meilidb-data/src/database/documents_index.rs @@ -44,6 +44,26 @@ impl DocumentsIndex { DocumentFieldsIter(iter, end.to_vec()) } + + pub fn len(&self) -> Result { + let mut last_document_id = None; + let mut count = 0; + + let from = rocksdb::IteratorMode::Start; + let iterator = self.0.iterator(from)?; + + for (key, value) in iterator { + let slice = key.as_ref().try_into().unwrap(); + let document_id = DocumentAttrKey::from_be_bytes(slice).document_id; + + if Some(document_id) != last_document_id { + last_document_id = Some(document_id); + count += 1; + } + } + + Ok(count) + } } pub struct DocumentFieldsIter<'a>(rocksdb::DBIterator<'a>, Vec); diff --git a/meilidb-data/src/database/index.rs b/meilidb-data/src/database/index.rs index 5db72e085..fce37ace6 100644 --- a/meilidb-data/src/database/index.rs +++ b/meilidb-data/src/database/index.rs @@ -15,6 +15,13 @@ use crate::serde::Deserializer; use super::{Error, CustomSettings}; use super::{RawIndex, DocumentsAddition, DocumentsDeletion}; +#[derive(Copy, Clone)] +pub struct IndexStats { + pub number_of_words: usize, + pub number_of_documents: usize, + pub number_attrs_in_ranked_map: usize, +} + #[derive(Clone)] pub struct Index(pub ArcSwap); @@ -48,6 +55,16 @@ impl Index { Ok(index) } + pub fn stats(&self) -> Result { + let lease = self.0.lease(); + + Ok(IndexStats { + number_of_words: lease.words.len(), + number_of_documents: lease.raw.documents.len()?, + number_attrs_in_ranked_map: lease.ranked_map.len(), + }) + } + pub fn query_builder(&self) -> QueryBuilder { let lease = IndexLease(self.0.lease()); QueryBuilder::new(lease) diff --git a/meilidb-data/src/ranked_map.rs b/meilidb-data/src/ranked_map.rs index 7c10a0649..609cee954 100644 --- a/meilidb-data/src/ranked_map.rs +++ b/meilidb-data/src/ranked_map.rs @@ -9,6 +9,10 @@ use crate::{SchemaAttr, Number}; pub struct RankedMap(HashMap<(DocumentId, SchemaAttr), Number>); impl RankedMap { + pub fn len(&self) -> usize { + self.0.len() + } + pub fn insert(&mut self, document: DocumentId, attribute: SchemaAttr, number: Number) { self.0.insert((document, attribute), number); }