2019-10-31 15:00:36 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use heed::types::{SerdeBincode, Str};
|
|
|
|
use log::*;
|
2019-11-20 10:47:34 +01:00
|
|
|
use meilidb_core::{Database, MResult, Error as MError};
|
2019-10-31 15:00:36 +01:00
|
|
|
use sysinfo::Pid;
|
|
|
|
|
|
|
|
use crate::option::Opt;
|
|
|
|
use crate::routes::index::index_update_callback;
|
|
|
|
|
|
|
|
type SerdeDatetime = SerdeBincode<DateTime<Utc>>;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Data {
|
|
|
|
inner: Arc<DataInner>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Data {
|
|
|
|
type Target = DataInner;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DataInner {
|
|
|
|
pub db: Arc<Database>,
|
|
|
|
pub db_path: String,
|
2019-11-19 17:09:06 +01:00
|
|
|
pub api_key: Option<String>,
|
2019-10-31 15:00:36 +01:00
|
|
|
pub server_pid: Pid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DataInner {
|
|
|
|
pub fn is_indexing(&self, reader: &heed::RoTxn, index: &str) -> MResult<Option<bool>> {
|
|
|
|
match self.db.open_index(&index) {
|
|
|
|
Some(index) => index.current_update_id(&reader).map(|u| Some(u.is_some())),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 10:47:34 +01:00
|
|
|
pub fn last_update(&self, reader: &heed::RoTxn) -> MResult<Option<DateTime<Utc>>> {
|
2019-10-31 15:00:36 +01:00
|
|
|
match self
|
|
|
|
.db
|
|
|
|
.common_store()
|
2019-11-20 10:47:34 +01:00
|
|
|
.get::<Str, SerdeDatetime>(&reader, "last-update")?
|
2019-10-31 15:00:36 +01:00
|
|
|
{
|
|
|
|
Some(datetime) => Ok(Some(datetime)),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 10:47:34 +01:00
|
|
|
pub fn set_last_update(&self, writer: &mut heed::RwTxn) -> MResult<()> {
|
2019-10-31 15:00:36 +01:00
|
|
|
self.db
|
|
|
|
.common_store()
|
2019-11-20 10:47:34 +01:00
|
|
|
.put::<Str, SerdeDatetime>(writer, "last-update", &Utc::now())
|
2019-10-31 15:00:36 +01:00
|
|
|
.map_err(Into::into)
|
|
|
|
}
|
|
|
|
|
2019-11-20 10:47:34 +01:00
|
|
|
pub fn compute_stats(&self, mut writer: &mut heed::RwTxn, index_uid: &str) -> MResult<()> {
|
2019-11-19 16:15:49 +01:00
|
|
|
let index = match self.db.open_index(&index_uid) {
|
2019-10-31 15:00:36 +01:00
|
|
|
Some(index) => index,
|
|
|
|
None => {
|
2019-11-19 16:15:49 +01:00
|
|
|
error!("Impossible to retrieve index {}", index_uid);
|
2019-10-31 15:00:36 +01:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let schema = match index.main.schema(&writer)? {
|
|
|
|
Some(schema) => schema,
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let all_documents_fields = index
|
|
|
|
.documents_fields_counts
|
|
|
|
.all_documents_fields_counts(&writer)?;
|
|
|
|
|
|
|
|
// count fields frequencies
|
|
|
|
let mut fields_frequency = HashMap::<_, usize>::new();
|
|
|
|
for result in all_documents_fields {
|
|
|
|
let (_, attr, _) = result?;
|
|
|
|
*fields_frequency.entry(attr).or_default() += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert attributes to their names
|
|
|
|
let frequency: HashMap<_, _> = fields_frequency
|
|
|
|
.into_iter()
|
|
|
|
.map(|(a, c)| (schema.attribute_name(a).to_owned(), c))
|
|
|
|
.collect();
|
|
|
|
|
2019-11-20 10:47:34 +01:00
|
|
|
index.main.put_fields_frequency(&mut writer, &frequency).map_err(MError::Zlmdb)
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Data {
|
|
|
|
pub fn new(opt: Opt) -> Data {
|
2019-11-19 17:09:06 +01:00
|
|
|
let db_path = opt.db_path.clone();
|
|
|
|
let api_key = opt.api_key.clone();
|
2019-10-31 15:00:36 +01:00
|
|
|
let server_pid = sysinfo::get_current_pid().unwrap();
|
|
|
|
|
2019-11-19 17:09:06 +01:00
|
|
|
let db = Arc::new(Database::open_or_create(opt.db_path.clone()).unwrap());
|
2019-10-31 15:00:36 +01:00
|
|
|
|
|
|
|
let inner_data = DataInner {
|
|
|
|
db: db.clone(),
|
|
|
|
db_path,
|
2019-11-19 17:09:06 +01:00
|
|
|
api_key,
|
2019-10-31 15:00:36 +01:00
|
|
|
server_pid,
|
|
|
|
};
|
|
|
|
|
|
|
|
let data = Data {
|
|
|
|
inner: Arc::new(inner_data),
|
|
|
|
};
|
|
|
|
|
2019-11-15 17:33:06 +01:00
|
|
|
let callback_context = data.clone();
|
2019-11-19 16:15:49 +01:00
|
|
|
db.set_update_callback(Box::new(move |index_uid, status| {
|
|
|
|
index_update_callback(&index_uid, &callback_context, status);
|
2019-11-15 17:33:06 +01:00
|
|
|
}));
|
2019-10-31 15:00:36 +01:00
|
|
|
|
|
|
|
data
|
|
|
|
}
|
|
|
|
}
|