meilisearch/meilisearch-http/src/data/mod.rs

134 lines
3.3 KiB
Rust
Raw Normal View History

2020-12-29 18:11:06 +08:00
use std::ops::Deref;
use std::sync::Arc;
use sha2::Digest;
2021-05-10 23:30:09 +08:00
use crate::index::{Checked, Settings};
2021-05-31 22:03:39 +08:00
use crate::index_controller::{
2021-06-15 23:39:07 +08:00
error::Result, DumpInfo, IndexController, IndexMetadata, IndexSettings, IndexStats, Stats,
2021-05-31 22:03:39 +08:00
};
2021-02-04 00:44:20 +08:00
use crate::option::Opt;
2020-12-29 18:11:06 +08:00
2021-04-01 22:44:42 +08:00
pub mod search;
mod updates;
2020-12-29 18:11:06 +08:00
#[derive(Clone)]
pub struct Data {
2021-01-28 21:12:34 +08:00
inner: Arc<DataInner>,
2020-12-29 18:11:06 +08:00
}
impl Deref for Data {
2021-01-28 21:12:34 +08:00
type Target = DataInner;
2020-12-29 18:11:06 +08:00
fn deref(&self) -> &Self::Target {
&self.inner
}
}
2021-01-28 21:12:34 +08:00
pub struct DataInner {
2021-03-03 18:43:51 +08:00
pub index_controller: IndexController,
2021-02-16 22:54:07 +08:00
pub api_keys: ApiKeys,
2020-12-29 18:11:06 +08:00
options: Opt,
}
#[derive(Clone)]
pub struct ApiKeys {
pub public: Option<String>,
pub private: Option<String>,
pub master: Option<String>,
}
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 {
2021-06-15 23:39:07 +08:00
pub fn new(options: Opt) -> anyhow::Result<Data> {
let path = options.db_path.clone();
2021-04-29 20:45:08 +08:00
let index_controller = IndexController::new(&path, &options)?;
2020-12-29 18:11:06 +08:00
let mut api_keys = ApiKeys {
master: options.clone().master_key,
private: None,
public: None,
};
api_keys.generate_missing_api_keys();
2021-05-31 22:03:39 +08:00
let inner = DataInner {
index_controller,
api_keys,
options,
};
2020-12-29 18:11:06 +08:00
let inner = Arc::new(inner);
Ok(Data { inner })
}
pub async fn settings(&self, uid: String) -> Result<Settings<Checked>> {
2021-03-15 23:52:05 +08:00
self.index_controller.settings(uid).await
2021-01-01 23:59:49 +08:00
}
pub async fn list_indexes(&self) -> Result<Vec<IndexMetadata>> {
2021-03-07 03:12:20 +08:00
self.index_controller.list_indexes().await
2021-02-04 00:44:20 +08:00
}
pub async fn index(&self, uid: String) -> Result<IndexMetadata> {
2021-03-15 23:52:05 +08:00
self.index_controller.get_index(uid).await
2021-02-04 19:34:12 +08:00
}
2021-03-16 01:11:10 +08:00
pub async fn create_index(
&self,
uid: String,
primary_key: Option<String>,
) -> Result<IndexMetadata> {
let settings = IndexSettings {
2021-03-15 23:52:05 +08:00
uid: Some(uid),
primary_key,
};
2021-02-26 16:10:04 +08:00
let meta = self.index_controller.create_index(settings).await?;
2021-02-08 17:47:34 +08:00
Ok(meta)
}
pub async fn get_index_stats(&self, uid: String) -> Result<IndexStats> {
2021-04-15 00:55:04 +08:00
Ok(self.index_controller.get_index_stats(uid).await?)
2021-04-08 21:35:28 +08:00
}
pub async fn get_all_stats(&self) -> Result<Stats> {
2021-04-15 00:55:04 +08:00
Ok(self.index_controller.get_all_stats().await?)
2021-04-08 21:35:28 +08:00
}
pub async fn create_dump(&self) -> Result<DumpInfo> {
2021-05-11 02:25:09 +08:00
Ok(self.index_controller.create_dump().await?)
}
pub async fn dump_status(&self, uid: String) -> Result<DumpInfo> {
2021-05-11 02:25:09 +08:00
Ok(self.index_controller.dump_info(uid).await?)
2021-05-05 20:11:56 +08:00
}
2020-12-29 18:11:06 +08: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
}
}