meilisearch/src/data/mod.rs

117 lines
3.1 KiB
Rust
Raw Normal View History

2020-12-29 18:11:06 +08:00
mod search;
mod updates;
pub use search::{SearchQuery, SearchResult};
use std::ops::Deref;
use std::sync::Arc;
use sha2::Digest;
use crate::{option::Opt, index_controller::Settings};
use crate::index_controller::{IndexStore, UpdateStore};
2020-12-29 18:11:06 +08:00
#[derive(Clone)]
pub struct Data {
inner: Arc<DataInner<UpdateStore>>,
2020-12-29 18:11:06 +08:00
}
impl Deref for Data {
type Target = DataInner<UpdateStore>;
2020-12-29 18:11:06 +08:00
fn deref(&self) -> &Self::Target {
&self.inner
}
}
#[derive(Clone)]
pub struct DataInner<I> {
pub indexes: Arc<I>,
2020-12-29 18:11:06 +08:00
api_keys: ApiKeys,
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 {
pub fn new(options: Opt) -> anyhow::Result<Data> {
let path = options.db_path.clone();
let index_store = IndexStore::new(&path)?;
let index_controller = UpdateStore::new(index_store);
let indexes = Arc::new(index_controller);
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();
let inner = DataInner { indexes, options, api_keys };
2020-12-29 18:11:06 +08:00
let inner = Arc::new(inner);
Ok(Data { inner })
}
pub fn settings<S: AsRef<str>>(&self, index_uid: S) -> anyhow::Result<Settings> {
let index = self.indexes
.get(&index_uid)?
.ok_or_else(|| anyhow::anyhow!("Index {} does not exist.", index_uid.as_ref()))?;
2021-01-01 23:59:49 +08:00
let displayed_attributes = index
.displayed_fields()?
2021-01-14 00:50:36 +08:00
.map(|fields| fields.into_iter().map(String::from).collect())
2021-01-01 23:59:49 +08:00
.unwrap_or_else(|| vec!["*".to_string()]);
let searchable_attributes = index
.searchable_fields()?
.map(|fields| fields.into_iter().map(String::from).collect())
2021-01-01 23:59:49 +08:00
.unwrap_or_else(|| vec!["*".to_string()]);
let faceted_attributes = index.faceted_fields()?
2021-01-14 00:50:36 +08:00
.into_iter()
.map(|(k, v)| (k, v.to_string()))
.collect();
2021-01-01 23:59:49 +08:00
Ok(Settings {
displayed_attributes: Some(Some(displayed_attributes)),
searchable_attributes: Some(Some(searchable_attributes)),
2021-01-14 00:50:36 +08:00
faceted_attributes: Some(Some(faceted_attributes)),
2021-01-01 23:59:49 +08:00
criteria: None,
})
}
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
}
}