diff --git a/src/data/mod.rs b/src/data/mod.rs index 76effde58..1dec00766 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -78,35 +78,8 @@ impl Data { Ok(Data { inner }) } - pub fn settings>(&self, index_uid: S) -> anyhow::Result { - 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>(&self, index_uid: S) -> anyhow::Result { + self.index_controller.settings(index_uid.as_ref().to_string()).await } pub fn list_indexes(&self) -> anyhow::Result> { diff --git a/src/index/mod.rs b/src/index/mod.rs index f35e6b3dd..a68f983e9 100644 --- a/src/index/mod.rs +++ b/src/index/mod.rs @@ -17,3 +17,32 @@ impl Deref for Index { self.0.as_ref() } } + +impl Index { + pub fn settings(&self) -> anyhow::Result { + 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, + }) + } +} diff --git a/src/index_controller/index_actor.rs b/src/index_controller/index_actor.rs index 96e5010db..655a1e2d5 100644 --- a/src/index_controller/index_actor.rs +++ b/src/index_controller/index_actor.rs @@ -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 = std::result::Result; type AsyncMap = Arc>>; @@ -26,6 +26,7 @@ enum IndexMsg { CreateIndex { uuid: Uuid, primary_key: Option, ret: oneshot::Sender> }, Update { meta: Processing, data: std::fs::File, ret: oneshot::Sender}, Search { uuid: Uuid, query: SearchQuery, ret: oneshot::Sender> }, + Settings { uuid: Uuid, ret: oneshot::Sender> }, } struct IndexActor { @@ -75,6 +76,7 @@ impl IndexActor { 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 IndexActor { 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>) { + 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 { + 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 { diff --git a/src/index_controller/mod.rs b/src/index_controller/mod.rs index dd2dbd7cf..f1bc922d6 100644 --- a/src/index_controller/mod.rs +++ b/src/index_controller/mod.rs @@ -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>> { - todo!() - } - fn update_status(&self, index: String, id: u64) -> anyhow::Result> { todo!() } @@ -155,6 +148,15 @@ impl IndexController { todo!() } + pub async fn settings(&self, index: String) -> anyhow::Result { + 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 { todo!() } diff --git a/src/routes/settings/mod.rs b/src/routes/settings/mod.rs index 93e10674d..0f904f572 100644 --- a/src/routes/settings/mod.rs +++ b/src/routes/settings/mod.rs @@ -64,7 +64,7 @@ macro_rules! make_setting_route { data: actix_web::web::Data, index_uid: actix_web::web::Path, ) -> std::result::Result { - 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, index_uid: web::Path, ) -> Result { - 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))