restructure project

This commit is contained in:
mpostma
2021-03-10 13:46:49 +01:00
parent 8061a04661
commit 5ecf514d28
75 changed files with 4377 additions and 323 deletions

View File

@ -0,0 +1,110 @@
pub mod search;
mod updates;
use std::fs::create_dir_all;
use std::ops::Deref;
use std::sync::Arc;
use sha2::Digest;
use crate::index_controller::{IndexMetadata, IndexSettings};
use crate::index_controller::IndexController;
use crate::index::Settings;
use crate::option::Opt;
#[derive(Clone)]
pub struct Data {
inner: Arc<DataInner>,
}
impl Deref for Data {
type Target = DataInner;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
pub struct DataInner {
pub index_controller: IndexController,
pub 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();
create_dir_all(&path)?;
let index_controller = IndexController::new(&path)?;
let mut api_keys = ApiKeys {
master: options.clone().master_key,
private: None,
public: None,
};
api_keys.generate_missing_api_keys();
let inner = DataInner { index_controller, options, api_keys };
let inner = Arc::new(inner);
Ok(Data { inner })
}
pub async fn settings<S: AsRef<str>>(&self, index_uid: S) -> anyhow::Result<Settings> {
self.index_controller.settings(index_uid.as_ref().to_string()).await
}
pub async fn list_indexes(&self) -> anyhow::Result<Vec<IndexMetadata>> {
self.index_controller.list_indexes().await
}
pub async fn index(&self, name: impl AsRef<str>) -> anyhow::Result<Option<IndexMetadata>> {
self.index_controller.get_index(name.as_ref().to_string()).await
}
pub async fn create_index(&self, name: impl AsRef<str>, primary_key: Option<impl AsRef<str>>) -> anyhow::Result<IndexMetadata> {
let settings = IndexSettings {
name: Some(name.as_ref().to_string()),
primary_key: primary_key.map(|s| s.as_ref().to_string()),
};
let meta = self.index_controller.create_index(settings).await?;
Ok(meta)
}
#[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
}
}

View File

@ -0,0 +1,34 @@
use serde_json::{Map, Value};
use crate::index::{SearchQuery, SearchResult};
use super::Data;
impl Data {
pub async fn search<S: AsRef<str>>(
&self,
index: S,
search_query: SearchQuery,
) -> anyhow::Result<SearchResult> {
self.index_controller.search(index.as_ref().to_string(), search_query).await
}
pub async fn retrieve_documents(
&self,
index: String,
offset: usize,
limit: usize,
attributes_to_retrieve: Option<Vec<String>>,
) -> anyhow::Result<Vec<Map<String, Value>>> {
self.index_controller.documents(index, offset, limit, attributes_to_retrieve).await
}
pub async fn retrieve_document(
&self,
index: impl AsRef<str> + Sync + Send + 'static,
document_id: impl AsRef<str> + Sync + Send + 'static,
attributes_to_retrieve: Option<Vec<String>>,
) -> anyhow::Result<Map<String, Value>>
{
self.index_controller.document(index.as_ref().to_string(), document_id.as_ref().to_string(), attributes_to_retrieve).await
}
}

View File

@ -0,0 +1,82 @@
//use async_compression::tokio_02::write::GzipEncoder;
//use futures_util::stream::StreamExt;
use milli::update::{IndexDocumentsMethod, UpdateFormat};
//use tokio::io::AsyncWriteExt;
use actix_web::web::Payload;
use crate::index_controller::{UpdateStatus, IndexMetadata};
use crate::index::Settings;
use super::Data;
impl Data {
pub async fn add_documents(
&self,
index: impl AsRef<str> + Send + Sync + 'static,
method: IndexDocumentsMethod,
format: UpdateFormat,
stream: Payload,
primary_key: Option<String>,
) -> anyhow::Result<UpdateStatus>
{
let update_status = self.index_controller.add_documents(index.as_ref().to_string(), method, format, stream, primary_key).await?;
Ok(update_status)
}
pub async fn update_settings(
&self,
index: String,
settings: Settings
) -> anyhow::Result<UpdateStatus> {
let update = self.index_controller.update_settings(index, settings).await?;
Ok(update.into())
}
pub async fn clear_documents(
&self,
index: impl AsRef<str> + Sync + Send + 'static,
) -> anyhow::Result<UpdateStatus> {
let update = self.index_controller.clear_documents(index.as_ref().to_string()).await?;
Ok(update)
}
pub async fn delete_documents(
&self,
index: impl AsRef<str> + Sync + Send + 'static,
document_ids: Vec<String>,
) -> anyhow::Result<UpdateStatus> {
let update = self.index_controller.delete_documents(index.as_ref().to_string(), document_ids).await?;
Ok(update.into())
}
pub async fn delete_index(
&self,
index: impl AsRef<str> + Send + Sync + 'static,
) -> anyhow::Result<()> {
self.index_controller.delete_index(index.as_ref().to_owned()).await?;
Ok(())
}
pub async fn get_update_status(&self, index: impl AsRef<str>, uid: u64) -> anyhow::Result<Option<UpdateStatus>> {
self.index_controller.update_status(index.as_ref().to_string(), uid).await
}
pub async fn get_updates_status(&self, index: impl AsRef<str>) -> anyhow::Result<Vec<UpdateStatus>> {
self.index_controller.all_update_status(index.as_ref().to_string()).await
}
pub fn update_index(
&self,
name: impl AsRef<str>,
primary_key: Option<impl AsRef<str>>,
new_name: Option<impl AsRef<str>>
) -> anyhow::Result<IndexMetadata> {
todo!()
//let settings = IndexSettings {
//name: new_name.map(|s| s.as_ref().to_string()),
//primary_key: primary_key.map(|s| s.as_ref().to_string()),
//};
//self.index_controller.update_index(name, settings)
}
}