Move fields frequency from common store to index main store

This commit is contained in:
Quentin de Quelen 2019-11-20 10:47:34 +01:00
parent 394976d330
commit 3286a5213c
2 changed files with 26 additions and 36 deletions

View File

@ -1,3 +1,4 @@
use std::collections::HashMap;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use crate::RankedMap; use crate::RankedMap;
use heed::Result as ZResult; use heed::Result as ZResult;
@ -7,6 +8,7 @@ use std::sync::Arc;
const CREATED_AT: &str = "created-at"; const CREATED_AT: &str = "created-at";
const CUSTOMS_KEY: &str = "customs-key"; const CUSTOMS_KEY: &str = "customs-key";
const FIELDS_FREQUENCY: &str = "fields-frequency";
const NAME: &str = "name"; const NAME: &str = "name";
const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents"; const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents";
const RANKED_MAP_KEY: &str = "ranked-map"; const RANKED_MAP_KEY: &str = "ranked-map";
@ -16,6 +18,8 @@ const SYNONYMS_KEY: &str = "synonyms";
const UPDATED_AT: &str = "updated-at"; const UPDATED_AT: &str = "updated-at";
const WORDS_KEY: &str = "words"; const WORDS_KEY: &str = "words";
pub type FreqsMap = HashMap<String, usize>;
type SerdeFreqsMap = SerdeBincode<FreqsMap>;
type SerdeDatetime = SerdeBincode<DateTime<Utc>>; type SerdeDatetime = SerdeBincode<DateTime<Utc>>;
#[derive(Copy, Clone)] #[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::<Str, SerdeFreqsMap>(writer, FIELDS_FREQUENCY, fields_frequency)
}
pub fn fields_frequency(&self, reader: &heed::RoTxn) -> ZResult<Option<FreqsMap>> {
match self
.main
.get::<Str, SerdeFreqsMap>(&reader, FIELDS_FREQUENCY)?
{
Some(freqs) => Ok(Some(freqs)),
None => Ok(None),
}
}
pub fn put_customs(self, writer: &mut heed::RwTxn, customs: &[u8]) -> ZResult<()> { pub fn put_customs(self, writer: &mut heed::RwTxn, customs: &[u8]) -> ZResult<()> {
self.main self.main
.put::<Str, ByteSlice>(writer, CUSTOMS_KEY, customs) .put::<Str, ByteSlice>(writer, CUSTOMS_KEY, customs)

View File

@ -5,14 +5,12 @@ use std::sync::Arc;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use heed::types::{SerdeBincode, Str}; use heed::types::{SerdeBincode, Str};
use log::*; use log::*;
use meilidb_core::{Database, MResult}; use meilidb_core::{Database, MResult, Error as MError};
use sysinfo::Pid; use sysinfo::Pid;
use crate::option::Opt; use crate::option::Opt;
use crate::routes::index::index_update_callback; use crate::routes::index::index_update_callback;
pub type FreqsMap = HashMap<String, usize>;
type SerdeFreqsMap = SerdeBincode<FreqsMap>;
type SerdeDatetime = SerdeBincode<DateTime<Utc>>; type SerdeDatetime = SerdeBincode<DateTime<Utc>>;
#[derive(Clone)] #[derive(Clone)]
@ -44,47 +42,25 @@ impl DataInner {
} }
} }
pub fn last_update( pub fn last_update(&self, reader: &heed::RoTxn) -> MResult<Option<DateTime<Utc>>> {
&self,
reader: &heed::RoTxn,
index_uid: &str,
) -> MResult<Option<DateTime<Utc>>> {
let key = format!("last-update-{}", index_uid);
match self match self
.db .db
.common_store() .common_store()
.get::<Str, SerdeDatetime>(&reader, &key)? .get::<Str, SerdeDatetime>(&reader, "last-update")?
{ {
Some(datetime) => Ok(Some(datetime)), Some(datetime) => Ok(Some(datetime)),
None => Ok(None), None => Ok(None),
} }
} }
pub fn set_last_update(&self, writer: &mut heed::RwTxn, index_uid: &str) -> MResult<()> { pub fn set_last_update(&self, writer: &mut heed::RwTxn) -> MResult<()> {
let key = format!("last-update-{}", index_uid);
self.db self.db
.common_store() .common_store()
.put::<Str, SerdeDatetime>(writer, &key, &Utc::now()) .put::<Str, SerdeDatetime>(writer, "last-update", &Utc::now())
.map_err(Into::into) .map_err(Into::into)
} }
pub fn fields_frequency( pub fn compute_stats(&self, mut writer: &mut heed::RwTxn, index_uid: &str) -> MResult<()> {
&self,
reader: &heed::RoTxn,
index_uid: &str,
) -> MResult<Option<FreqsMap>> {
let key = format!("fields-frequency-{}", index_uid);
match self
.db
.common_store()
.get::<Str, SerdeFreqsMap>(&reader, &key)?
{
Some(freqs) => Ok(Some(freqs)),
None => Ok(None),
}
}
pub fn compute_stats(&self, writer: &mut heed::RwTxn, index_uid: &str) -> MResult<()> {
let index = match self.db.open_index(&index_uid) { let index = match self.db.open_index(&index_uid) {
Some(index) => index, Some(index) => index,
None => { None => {
@ -115,12 +91,7 @@ impl DataInner {
.map(|(a, c)| (schema.attribute_name(a).to_owned(), c)) .map(|(a, c)| (schema.attribute_name(a).to_owned(), c))
.collect(); .collect();
let key = format!("fields-frequency-{}", index_uid); index.main.put_fields_frequency(&mut writer, &frequency).map_err(MError::Zlmdb)
self.db
.common_store()
.put::<Str, SerdeFreqsMap>(writer, &key, &frequency)?;
Ok(())
} }
} }