list settings

This commit is contained in:
mpostma 2021-03-04 12:38:55 +01:00
parent 47138c7632
commit 17b84691f2
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A
5 changed files with 70 additions and 47 deletions

View File

@ -78,35 +78,8 @@ impl Data {
Ok(Data { inner })
}
pub fn settings<S: AsRef<str>>(&self, index_uid: S) -> anyhow::Result<Settings> {
let index = self.index_controller
.index(index_uid.as_ref().to_string())?
.ok_or_else(|| anyhow::anyhow!("Index {} does not exist.", index_uid.as_ref()))?;
let txn = index.read_txn()?;
let displayed_attributes = index
.displayed_fields(&txn)?
.map(|fields| fields.into_iter().map(String::from).collect())
.unwrap_or_else(|| vec!["*".to_string()]);
let searchable_attributes = index
.searchable_fields(&txn)?
.map(|fields| fields.into_iter().map(String::from).collect())
.unwrap_or_else(|| vec!["*".to_string()]);
let faceted_attributes = index
.faceted_fields(&txn)?
.into_iter()
.map(|(k, v)| (k, v.to_string()))
.collect();
Ok(Settings {
displayed_attributes: Some(Some(displayed_attributes)),
searchable_attributes: Some(Some(searchable_attributes)),
faceted_attributes: Some(Some(faceted_attributes)),
criteria: None,
})
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 fn list_indexes(&self) -> anyhow::Result<Vec<IndexMetadata>> {

View File

@ -17,3 +17,32 @@ impl Deref for Index {
self.0.as_ref()
}
}
impl Index {
pub fn settings(&self) -> anyhow::Result<Settings> {
let txn = self.read_txn()?;
let displayed_attributes = self
.displayed_fields(&txn)?
.map(|fields| fields.into_iter().map(String::from).collect())
.unwrap_or_else(|| vec!["*".to_string()]);
let searchable_attributes = self
.searchable_fields(&txn)?
.map(|fields| fields.into_iter().map(String::from).collect())
.unwrap_or_else(|| vec!["*".to_string()]);
let faceted_attributes = self
.faceted_fields(&txn)?
.into_iter()
.map(|(k, v)| (k, v.to_string()))
.collect();
Ok(Settings {
displayed_attributes: Some(Some(displayed_attributes)),
searchable_attributes: Some(Some(searchable_attributes)),
faceted_attributes: Some(Some(faceted_attributes)),
criteria: None,
})
}
}

View File

@ -16,7 +16,7 @@ use super::update_handler::UpdateHandler;
use crate::index_controller::{IndexMetadata, UpdateMeta, updates::{Processed, Failed, Processing}};
use crate::index::UpdateResult as UResult;
use crate::option::IndexerOpts;
use crate::index::{Index, SearchQuery, SearchResult};
use crate::index::{Index, SearchQuery, SearchResult, Settings};
pub type Result<T> = std::result::Result<T, IndexError>;
type AsyncMap<K, V> = Arc<RwLock<HashMap<K, V>>>;
@ -26,6 +26,7 @@ enum IndexMsg {
CreateIndex { uuid: Uuid, primary_key: Option<String>, ret: oneshot::Sender<Result<IndexMetadata>> },
Update { meta: Processing<UpdateMeta>, data: std::fs::File, ret: oneshot::Sender<UpdateResult>},
Search { uuid: Uuid, query: SearchQuery, ret: oneshot::Sender<anyhow::Result<SearchResult>> },
Settings { uuid: Uuid, ret: oneshot::Sender<Result<Settings>> },
}
struct IndexActor<S> {
@ -75,6 +76,7 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
IndexMsg::CreateIndex { uuid, primary_key, ret } => self.handle_create_index(uuid, primary_key, ret).await,
IndexMsg::Update { ret, meta, data } => self.handle_update(meta, data, ret).await,
IndexMsg::Search { ret, query, uuid } => self.handle_search(uuid, query, ret).await,
IndexMsg::Settings { ret, uuid } => self.handle_settings(uuid, ret).await,
}
});
@ -100,9 +102,19 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
let uuid = meta.index_uuid().clone();
let index = self.store.get_or_create(uuid).await.unwrap();
let update_handler = self.update_handler.clone();
let result = tokio::task::spawn_blocking(move || update_handler.handle_update(meta, data, index)).await;
let result = result.unwrap();
let _ = ret.send(result);
tokio::task::spawn_blocking(move || {
let result = update_handler.handle_update(meta, data, index);
let _ = ret.send(result);
}).await;
}
async fn handle_settings(&self, uuid: Uuid, ret: oneshot::Sender<Result<Settings>>) {
let index = self.store.get(uuid).await.unwrap().unwrap();
tokio::task::spawn_blocking(move || {
let result = index.settings()
.map_err(|e| IndexError::Error(e));
let _ = ret.send(result);
}).await;
}
}
@ -141,6 +153,13 @@ impl IndexActorHandle {
let _ = self.sender.send(msg).await;
Ok(receiver.await.expect("IndexActor has been killed")?)
}
pub async fn settings(&self, uuid: Uuid) -> Result<Settings> {
let (ret, receiver) = oneshot::channel();
let msg = IndexMsg::Settings { uuid, ret };
let _ = self.sender.send(msg).await;
Ok(receiver.await.expect("IndexActor has been killed")?)
}
}
struct MapIndexStore {

View File

@ -7,14 +7,15 @@ mod update_handler;
use std::path::Path;
use tokio::sync::{mpsc, oneshot};
use futures::stream::StreamExt;
use actix_web::web::Payload;
use crate::index::{SearchResult, SearchQuery};
use actix_web::web::Bytes;
use actix_web::web::Payload;
use anyhow::Context;
use chrono::{DateTime, Utc};
use crate::index::{SearchResult, SearchQuery};
use futures::stream::StreamExt;
use milli::update::{IndexDocumentsMethod, UpdateFormat};
use serde::{Serialize, Deserialize};
use tokio::sync::{mpsc, oneshot};
use uuid::Uuid;
pub use updates::{Processed, Processing, Failed};
@ -135,14 +136,6 @@ impl IndexController {
todo!()
}
fn swap_indices(&self, index1_uid: String, index2_uid: String) -> anyhow::Result<()> {
todo!()
}
pub fn index(&self, name: String) -> anyhow::Result<Option<std::sync::Arc<milli::Index>>> {
todo!()
}
fn update_status(&self, index: String, id: u64) -> anyhow::Result<Option<UpdateStatus>> {
todo!()
}
@ -155,6 +148,15 @@ impl IndexController {
todo!()
}
pub async fn settings(&self, index: String) -> anyhow::Result<Settings> {
let uuid = self.uuid_resolver
.resolve(index.clone())
.await?
.with_context(|| format!("Index {:?} doesn't exist", index))?;
let settings = self.index_handle.settings(uuid).await?;
Ok(settings)
}
fn update_index(&self, name: String, index_settings: IndexSettings) -> anyhow::Result<IndexMetadata> {
todo!()
}

View File

@ -64,7 +64,7 @@ macro_rules! make_setting_route {
data: actix_web::web::Data<data::Data>,
index_uid: actix_web::web::Path<String>,
) -> std::result::Result<HttpResponse, ResponseError> {
match data.settings(index_uid.as_ref()) {
match data.settings(index_uid.as_ref()).await {
Ok(settings) => {
let setting = settings.$attr;
let json = serde_json::to_string(&setting).unwrap();
@ -153,7 +153,7 @@ async fn get_all(
data: web::Data<Data>,
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
match data.settings(index_uid.as_ref()) {
match data.settings(index_uid.as_ref()).await {
Ok(settings) => {
let json = serde_json::to_string(&settings).unwrap();
Ok(HttpResponse::Ok().body(json))