2021-04-09 15:41:24 +03:00
|
|
|
use std::collections::HashMap;
|
2020-12-29 11:11:06 +01:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-04-09 15:41:24 +03:00
|
|
|
use chrono::{DateTime, Utc};
|
2020-12-29 11:11:06 +01:00
|
|
|
use sha2::Digest;
|
|
|
|
|
2021-03-04 11:56:32 +01:00
|
|
|
use crate::index::Settings;
|
2021-04-08 16:35:28 +03:00
|
|
|
use crate::index_controller::{IndexController, IndexStats};
|
2021-03-15 18:11:10 +01:00
|
|
|
use crate::index_controller::{IndexMetadata, IndexSettings};
|
2021-02-03 17:44:20 +01:00
|
|
|
use crate::option::Opt;
|
2020-12-29 11:11:06 +01:00
|
|
|
|
2021-04-01 17:44:42 +03:00
|
|
|
pub mod search;
|
|
|
|
mod updates;
|
|
|
|
|
2020-12-29 11:11:06 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Data {
|
2021-01-28 14:12:34 +01:00
|
|
|
inner: Arc<DataInner>,
|
2020-12-29 11:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Data {
|
2021-01-28 14:12:34 +01:00
|
|
|
type Target = DataInner;
|
2020-12-29 11:11:06 +01:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 14:12:34 +01:00
|
|
|
pub struct DataInner {
|
2021-03-03 11:43:51 +01:00
|
|
|
pub index_controller: IndexController,
|
2021-02-16 15:54:07 +01:00
|
|
|
pub api_keys: ApiKeys,
|
2020-12-29 11:11:06 +01:00
|
|
|
options: Opt,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ApiKeys {
|
|
|
|
pub public: Option<String>,
|
|
|
|
pub private: Option<String>,
|
|
|
|
pub master: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:35:28 +03:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Stats {
|
|
|
|
pub database_size: u64,
|
|
|
|
pub last_update: Option<DateTime<Utc>>,
|
|
|
|
pub indexes: HashMap<String, IndexStats>,
|
|
|
|
}
|
|
|
|
|
2020-12-29 11:11:06 +01:00
|
|
|
impl ApiKeys {
|
|
|
|
pub fn generate_missing_api_keys(&mut self) {
|
|
|
|
if let Some(master_key) = &self.master {
|
|
|
|
if self.private.is_none() {
|
|
|
|
let key = format!("{}-private", master_key);
|
|
|
|
let sha = sha2::Sha256::digest(key.as_bytes());
|
|
|
|
self.private = Some(format!("{:x}", sha));
|
|
|
|
}
|
|
|
|
if self.public.is_none() {
|
|
|
|
let key = format!("{}-public", master_key);
|
|
|
|
let sha = sha2::Sha256::digest(key.as_bytes());
|
|
|
|
self.public = Some(format!("{:x}", sha));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Data {
|
|
|
|
pub fn new(options: Opt) -> anyhow::Result<Data> {
|
2021-01-16 15:09:48 +01:00
|
|
|
let path = options.db_path.clone();
|
2021-03-03 14:39:44 +01:00
|
|
|
|
2021-03-17 12:01:56 +01:00
|
|
|
let index_controller = IndexController::new(&path, &options)?;
|
2020-12-29 11:11:06 +01:00
|
|
|
|
|
|
|
let mut api_keys = ApiKeys {
|
|
|
|
master: options.clone().master_key,
|
|
|
|
private: None,
|
|
|
|
public: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
api_keys.generate_missing_api_keys();
|
|
|
|
|
2021-03-15 18:11:10 +01:00
|
|
|
let inner = DataInner {
|
|
|
|
index_controller,
|
|
|
|
options,
|
|
|
|
api_keys,
|
|
|
|
};
|
2020-12-29 11:11:06 +01:00
|
|
|
let inner = Arc::new(inner);
|
|
|
|
|
|
|
|
Ok(Data { inner })
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:52:05 +01:00
|
|
|
pub async fn settings(&self, uid: String) -> anyhow::Result<Settings> {
|
|
|
|
self.index_controller.settings(uid).await
|
2021-01-01 16:59:49 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 20:12:20 +01:00
|
|
|
pub async fn list_indexes(&self) -> anyhow::Result<Vec<IndexMetadata>> {
|
|
|
|
self.index_controller.list_indexes().await
|
2021-02-03 17:44:20 +01:00
|
|
|
}
|
|
|
|
|
2021-03-15 16:52:05 +01:00
|
|
|
pub async fn index(&self, uid: String) -> anyhow::Result<IndexMetadata> {
|
|
|
|
self.index_controller.get_index(uid).await
|
2021-02-04 12:34:12 +01:00
|
|
|
}
|
|
|
|
|
2021-03-15 18:11:10 +01:00
|
|
|
pub async fn create_index(
|
|
|
|
&self,
|
|
|
|
uid: String,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
) -> anyhow::Result<IndexMetadata> {
|
2021-02-09 11:41:26 +01:00
|
|
|
let settings = IndexSettings {
|
2021-03-15 16:52:05 +01:00
|
|
|
uid: Some(uid),
|
|
|
|
primary_key,
|
2021-02-09 11:41:26 +01:00
|
|
|
};
|
|
|
|
|
2021-02-26 09:10:04 +01:00
|
|
|
let meta = self.index_controller.create_index(settings).await?;
|
2021-02-08 10:47:34 +01:00
|
|
|
Ok(meta)
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:35:28 +03:00
|
|
|
pub async fn get_index_stats(&self, uid: String) -> anyhow::Result<IndexStats> {
|
|
|
|
Ok(self.index_controller.get_stats(uid).await?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_stats(&self) -> anyhow::Result<Stats> {
|
|
|
|
let mut stats = Stats::default();
|
2021-04-09 15:41:24 +03:00
|
|
|
stats.database_size += self.index_controller.get_uuids_size().await?;
|
2021-04-08 16:35:28 +03:00
|
|
|
|
|
|
|
for index in self.index_controller.list_indexes().await? {
|
|
|
|
let index_stats = self.index_controller.get_stats(index.uid.clone()).await?;
|
|
|
|
|
|
|
|
stats.database_size += index_stats.size;
|
2021-04-09 15:41:24 +03:00
|
|
|
stats.database_size += self
|
|
|
|
.index_controller
|
|
|
|
.get_updates_size(index.uid.clone())
|
|
|
|
.await?;
|
|
|
|
|
2021-04-08 16:35:28 +03:00
|
|
|
stats.last_update = Some(match stats.last_update {
|
|
|
|
Some(last_update) => last_update.max(index.meta.updated_at),
|
|
|
|
None => index.meta.updated_at,
|
|
|
|
});
|
2021-04-09 15:41:24 +03:00
|
|
|
|
2021-04-08 16:35:28 +03:00
|
|
|
stats.indexes.insert(index.uid, index_stats);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(stats)
|
|
|
|
}
|
|
|
|
|
2020-12-29 11:11:06 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn http_payload_size_limit(&self) -> usize {
|
|
|
|
self.options.http_payload_size_limit.get_bytes() as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn api_keys(&self) -> &ApiKeys {
|
|
|
|
&self.api_keys
|
|
|
|
}
|
|
|
|
}
|