mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-27 04:25:06 +08:00
feat: Update the number of documents in the KV
This commit is contained in:
parent
8d8aed36a8
commit
707e2f4d77
@ -1,14 +1,16 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
use meilidb_schema::Schema;
|
use meilidb_schema::Schema;
|
||||||
|
|
||||||
use crate::ranked_map::RankedMap;
|
use crate::ranked_map::RankedMap;
|
||||||
use crate::database::Error;
|
use crate::database::Error;
|
||||||
|
|
||||||
const SCHEMA_KEY: &str = "schema";
|
const SCHEMA_KEY: &str = "schema";
|
||||||
const WORDS_KEY: &str = "words";
|
const WORDS_KEY: &str = "words";
|
||||||
const SYNONYMS_KEY: &str = "synonyms";
|
const SYNONYMS_KEY: &str = "synonyms";
|
||||||
const RANKED_MAP_KEY: &str = "ranked-map";
|
const RANKED_MAP_KEY: &str = "ranked-map";
|
||||||
|
const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents";
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct MainIndex(pub(crate) crate::CfTree);
|
pub struct MainIndex(pub(crate) crate::CfTree);
|
||||||
@ -79,4 +81,22 @@ impl MainIndex {
|
|||||||
self.0.insert(RANKED_MAP_KEY, bytes)?;
|
self.0.insert(RANKED_MAP_KEY, bytes)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn number_of_documents(&self) -> Result<u64, Error> {
|
||||||
|
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<F>(&self, f: F) -> Result<u64, Error>
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,6 +123,9 @@ pub fn apply_documents_addition(
|
|||||||
main.set_words_set(&words)?;
|
main.set_words_set(&words)?;
|
||||||
main.set_ranked_map(&ranked_map)?;
|
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
|
// update the "consistent" view of the Index
|
||||||
let cache = ref_index.cache;
|
let cache = ref_index.cache;
|
||||||
let words = Arc::new(words);
|
let words = Arc::new(words);
|
||||||
|
@ -134,6 +134,9 @@ pub fn apply_documents_deletion(
|
|||||||
main.set_words_set(&words)?;
|
main.set_words_set(&words)?;
|
||||||
main.set_ranked_map(&ranked_map)?;
|
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
|
// update the "consistent" view of the Index
|
||||||
let cache = ref_index.cache;
|
let cache = ref_index.cache;
|
||||||
let words = Arc::new(words);
|
let words = Arc::new(words);
|
||||||
|
Loading…
Reference in New Issue
Block a user