diff --git a/meilidb-data/src/database/index/main_index.rs b/meilidb-data/src/database/index/main_index.rs index f11637c85..285aba868 100644 --- a/meilidb-data/src/database/index/main_index.rs +++ b/meilidb-data/src/database/index/main_index.rs @@ -1,14 +1,16 @@ use std::sync::Arc; +use std::convert::TryInto; use meilidb_schema::Schema; use crate::ranked_map::RankedMap; use crate::database::Error; -const SCHEMA_KEY: &str = "schema"; -const WORDS_KEY: &str = "words"; -const SYNONYMS_KEY: &str = "synonyms"; -const RANKED_MAP_KEY: &str = "ranked-map"; +const SCHEMA_KEY: &str = "schema"; +const WORDS_KEY: &str = "words"; +const SYNONYMS_KEY: &str = "synonyms"; +const RANKED_MAP_KEY: &str = "ranked-map"; +const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents"; #[derive(Clone)] pub struct MainIndex(pub(crate) crate::CfTree); @@ -79,4 +81,22 @@ impl MainIndex { self.0.insert(RANKED_MAP_KEY, bytes)?; Ok(()) } + + pub fn number_of_documents(&self) -> Result { + match self.0.get(NUMBER_OF_DOCUMENTS_KEY)? { + Some(bytes) => { + let array = (*bytes).try_into().unwrap(); + Ok(u64::from_be_bytes(array)) + }, + None => Ok(0), + } + } + + pub fn set_number_of_documents(&self, f: F) -> Result + where F: FnOnce(u64) -> u64, + { + let new = self.number_of_documents().map(f)?; + self.0.insert(NUMBER_OF_DOCUMENTS_KEY, new.to_be_bytes())?; + Ok(new) + } } diff --git a/meilidb-data/src/database/update/documents_addition.rs b/meilidb-data/src/database/update/documents_addition.rs index 397727fba..3daeb278c 100644 --- a/meilidb-data/src/database/update/documents_addition.rs +++ b/meilidb-data/src/database/update/documents_addition.rs @@ -123,6 +123,9 @@ pub fn apply_documents_addition( main.set_words_set(&words)?; main.set_ranked_map(&ranked_map)?; + let inserted_documents_len = document_ids.len() as u64; + let number_of_documents = main.set_number_of_documents(|old| old + inserted_documents_len)?; + // update the "consistent" view of the Index let cache = ref_index.cache; let words = Arc::new(words); diff --git a/meilidb-data/src/database/update/documents_deletion.rs b/meilidb-data/src/database/update/documents_deletion.rs index b32fe349f..7c7526a64 100644 --- a/meilidb-data/src/database/update/documents_deletion.rs +++ b/meilidb-data/src/database/update/documents_deletion.rs @@ -134,6 +134,9 @@ pub fn apply_documents_deletion( main.set_words_set(&words)?; main.set_ranked_map(&ranked_map)?; + let deleted_documents_len = deleted_documents.len() as u64; + let number_of_documents = main.set_number_of_documents(|old| old - deleted_documents_len)?; + // update the "consistent" view of the Index let cache = ref_index.cache; let words = Arc::new(words);