2021-02-27 17:19:05 +08:00
|
|
|
pub mod search;
|
2020-12-29 18:11:06 +08:00
|
|
|
mod updates;
|
|
|
|
|
2021-02-02 02:51:47 +08:00
|
|
|
use std::fs::create_dir_all;
|
2020-12-29 18:11:06 +08:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use sha2::Digest;
|
|
|
|
|
2021-03-04 18:56:32 +08:00
|
|
|
use crate::index_controller::{IndexMetadata, IndexSettings};
|
2021-03-04 19:03:06 +08:00
|
|
|
use crate::index_controller::IndexController;
|
2021-03-04 18:56:32 +08:00
|
|
|
use crate::index::Settings;
|
2021-02-04 00:44:20 +08:00
|
|
|
use crate::option::Opt;
|
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 {
|
|
|
|
pub fn new(options: Opt) -> anyhow::Result<Data> {
|
2021-01-16 22:09:48 +08:00
|
|
|
let path = options.db_path.clone();
|
2021-03-03 21:39:44 +08:00
|
|
|
|
2021-02-26 16:10:04 +08:00
|
|
|
//let indexer_opts = options.indexer_options.clone();
|
2021-03-03 21:39:44 +08:00
|
|
|
|
2021-01-28 21:12:34 +08:00
|
|
|
create_dir_all(&path)?;
|
2021-03-03 18:43:51 +08:00
|
|
|
let index_controller = IndexController::new(&path);
|
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-02-02 02:51:47 +08:00
|
|
|
let inner = DataInner { index_controller, options, api_keys };
|
2020-12-29 18:11:06 +08:00
|
|
|
let inner = Arc::new(inner);
|
|
|
|
|
|
|
|
Ok(Data { inner })
|
|
|
|
}
|
|
|
|
|
2021-01-16 22:09:48 +08:00
|
|
|
pub fn settings<S: AsRef<str>>(&self, index_uid: S) -> anyhow::Result<Settings> {
|
2021-01-28 21:12:34 +08:00
|
|
|
let index = self.index_controller
|
2021-02-26 16:10:04 +08:00
|
|
|
.index(index_uid.as_ref().to_string())?
|
2021-01-16 22:09:48 +08:00
|
|
|
.ok_or_else(|| anyhow::anyhow!("Index {} does not exist.", index_uid.as_ref()))?;
|
2021-01-01 23:59:49 +08:00
|
|
|
|
2021-01-28 21:12:34 +08:00
|
|
|
let txn = index.read_txn()?;
|
|
|
|
|
2021-01-16 22:09:48 +08:00
|
|
|
let displayed_attributes = index
|
2021-01-28 21:12:34 +08:00
|
|
|
.displayed_fields(&txn)?
|
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()]);
|
|
|
|
|
2021-01-16 22:09:48 +08:00
|
|
|
let searchable_attributes = index
|
2021-01-28 21:12:34 +08:00
|
|
|
.searchable_fields(&txn)?
|
2021-01-16 22:09:48 +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()]);
|
|
|
|
|
2021-01-28 21:12:34 +08:00
|
|
|
let faceted_attributes = index
|
|
|
|
.faceted_fields(&txn)?
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-04 00:44:20 +08:00
|
|
|
pub fn list_indexes(&self) -> anyhow::Result<Vec<IndexMetadata>> {
|
|
|
|
self.index_controller.list_indexes()
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:34:12 +08:00
|
|
|
pub fn index(&self, name: impl AsRef<str>) -> anyhow::Result<Option<IndexMetadata>> {
|
2021-02-26 16:10:04 +08:00
|
|
|
todo!()
|
|
|
|
//Ok(self
|
|
|
|
//.list_indexes()?
|
|
|
|
//.into_iter()
|
|
|
|
//.find(|i| i.uid == name.as_ref()))
|
2021-02-04 19:34:12 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 16:10:04 +08:00
|
|
|
pub async fn create_index(&self, name: impl AsRef<str>, primary_key: Option<impl AsRef<str>>) -> anyhow::Result<IndexMetadata> {
|
2021-02-09 18:41:26 +08:00
|
|
|
let settings = IndexSettings {
|
|
|
|
name: Some(name.as_ref().to_string()),
|
|
|
|
primary_key: primary_key.map(|s| s.as_ref().to_string()),
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|