feat: Introduce some stats to ease debugging

This commit is contained in:
Clément Renault 2019-05-25 12:12:24 +02:00
parent a147c09b06
commit e08edc2d6b
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
3 changed files with 41 additions and 0 deletions

View File

@ -44,6 +44,26 @@ impl DocumentsIndex {
DocumentFieldsIter(iter, end.to_vec()) DocumentFieldsIter(iter, end.to_vec())
} }
pub fn len(&self) -> Result<usize, rocksdb::Error> {
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<u8>); pub struct DocumentFieldsIter<'a>(rocksdb::DBIterator<'a>, Vec<u8>);

View File

@ -15,6 +15,13 @@ use crate::serde::Deserializer;
use super::{Error, CustomSettings}; use super::{Error, CustomSettings};
use super::{RawIndex, DocumentsAddition, DocumentsDeletion}; 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)] #[derive(Clone)]
pub struct Index(pub ArcSwap<InnerIndex>); pub struct Index(pub ArcSwap<InnerIndex>);
@ -48,6 +55,16 @@ impl Index {
Ok(index) Ok(index)
} }
pub fn stats(&self) -> Result<IndexStats, rocksdb::Error> {
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<IndexLease> { pub fn query_builder(&self) -> QueryBuilder<IndexLease> {
let lease = IndexLease(self.0.lease()); let lease = IndexLease(self.0.lease());
QueryBuilder::new(lease) QueryBuilder::new(lease)

View File

@ -9,6 +9,10 @@ use crate::{SchemaAttr, Number};
pub struct RankedMap(HashMap<(DocumentId, SchemaAttr), Number>); pub struct RankedMap(HashMap<(DocumentId, SchemaAttr), Number>);
impl RankedMap { impl RankedMap {
pub fn len(&self) -> usize {
self.0.len()
}
pub fn insert(&mut self, document: DocumentId, attribute: SchemaAttr, number: Number) { pub fn insert(&mut self, document: DocumentId, attribute: SchemaAttr, number: Number) {
self.0.insert((document, attribute), number); self.0.insert((document, attribute), number);
} }