diff --git a/meilidb-core/src/store/main.rs b/meilidb-core/src/store/main.rs index 55d5f10b0..c89cd23a1 100644 --- a/meilidb-core/src/store/main.rs +++ b/meilidb-core/src/store/main.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use chrono::{DateTime, Utc}; use crate::RankedMap; use heed::Result as ZResult; @@ -7,6 +8,7 @@ use std::sync::Arc; const CREATED_AT: &str = "created-at"; const CUSTOMS_KEY: &str = "customs-key"; +const FIELDS_FREQUENCY: &str = "fields-frequency"; const NAME: &str = "name"; const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents"; const RANKED_MAP_KEY: &str = "ranked-map"; @@ -16,6 +18,8 @@ const SYNONYMS_KEY: &str = "synonyms"; const UPDATED_AT: &str = "updated-at"; const WORDS_KEY: &str = "words"; +pub type FreqsMap = HashMap; +type SerdeFreqsMap = SerdeBincode; type SerdeDatetime = SerdeBincode>; #[derive(Copy, Clone)] @@ -144,6 +148,21 @@ impl Main { } } + pub fn put_fields_frequency(self, writer: &mut heed::RwTxn, fields_frequency: &FreqsMap) -> ZResult<()> { + self.main + .put::(writer, FIELDS_FREQUENCY, fields_frequency) + } + + pub fn fields_frequency(&self, reader: &heed::RoTxn) -> ZResult> { + match self + .main + .get::(&reader, FIELDS_FREQUENCY)? + { + Some(freqs) => Ok(Some(freqs)), + None => Ok(None), + } + } + pub fn put_customs(self, writer: &mut heed::RwTxn, customs: &[u8]) -> ZResult<()> { self.main .put::(writer, CUSTOMS_KEY, customs) diff --git a/meilidb-http/src/data.rs b/meilidb-http/src/data.rs index 9a2617191..06016b9e7 100644 --- a/meilidb-http/src/data.rs +++ b/meilidb-http/src/data.rs @@ -5,14 +5,12 @@ use std::sync::Arc; use chrono::{DateTime, Utc}; use heed::types::{SerdeBincode, Str}; use log::*; -use meilidb_core::{Database, MResult}; +use meilidb_core::{Database, MResult, Error as MError}; use sysinfo::Pid; use crate::option::Opt; use crate::routes::index::index_update_callback; -pub type FreqsMap = HashMap; -type SerdeFreqsMap = SerdeBincode; type SerdeDatetime = SerdeBincode>; #[derive(Clone)] @@ -44,47 +42,25 @@ impl DataInner { } } - pub fn last_update( - &self, - reader: &heed::RoTxn, - index_uid: &str, - ) -> MResult>> { - let key = format!("last-update-{}", index_uid); + pub fn last_update(&self, reader: &heed::RoTxn) -> MResult>> { match self .db .common_store() - .get::(&reader, &key)? + .get::(&reader, "last-update")? { Some(datetime) => Ok(Some(datetime)), None => Ok(None), } } - pub fn set_last_update(&self, writer: &mut heed::RwTxn, index_uid: &str) -> MResult<()> { - let key = format!("last-update-{}", index_uid); + pub fn set_last_update(&self, writer: &mut heed::RwTxn) -> MResult<()> { self.db .common_store() - .put::(writer, &key, &Utc::now()) + .put::(writer, "last-update", &Utc::now()) .map_err(Into::into) } - pub fn fields_frequency( - &self, - reader: &heed::RoTxn, - index_uid: &str, - ) -> MResult> { - let key = format!("fields-frequency-{}", index_uid); - match self - .db - .common_store() - .get::(&reader, &key)? - { - Some(freqs) => Ok(Some(freqs)), - None => Ok(None), - } - } - - pub fn compute_stats(&self, writer: &mut heed::RwTxn, index_uid: &str) -> MResult<()> { + pub fn compute_stats(&self, mut writer: &mut heed::RwTxn, index_uid: &str) -> MResult<()> { let index = match self.db.open_index(&index_uid) { Some(index) => index, None => { @@ -115,12 +91,7 @@ impl DataInner { .map(|(a, c)| (schema.attribute_name(a).to_owned(), c)) .collect(); - let key = format!("fields-frequency-{}", index_uid); - self.db - .common_store() - .put::(writer, &key, &frequency)?; - - Ok(()) + index.main.put_fields_frequency(&mut writer, &frequency).map_err(MError::Zlmdb) } }