From e97e13ce9f31cbd9ae9cc0a4faa8e78c8b8a4a01 Mon Sep 17 00:00:00 2001 From: Quentin de Quelen Date: Tue, 19 Nov 2019 17:38:02 +0100 Subject: [PATCH] Rename index_name to index_uids --- meilidb-core/src/database.rs | 2 +- meilidb-http/src/routes/index.rs | 45 ++++++++++++++++++++++++++++++- meilidb-http/src/routes/search.rs | 2 +- meilidb-http/src/routes/stats.rs | 2 +- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/meilidb-core/src/database.rs b/meilidb-core/src/database.rs index 8cd3cd2a0..0bdde55cb 100644 --- a/meilidb-core/src/database.rs +++ b/meilidb-core/src/database.rs @@ -251,7 +251,7 @@ impl Database { self.env.copy_to_path(path, CompactionOption::Enabled) } - pub fn indexes_names(&self) -> MResult> { + pub fn indexes_uids(&self) -> MResult> { let indexes = self.indexes.read().unwrap(); Ok(indexes.keys().cloned().collect()) } diff --git a/meilidb-http/src/routes/index.rs b/meilidb-http/src/routes/index.rs index af0e15a70..b31a9b301 100644 --- a/meilidb-http/src/routes/index.rs +++ b/meilidb-http/src/routes/index.rs @@ -29,11 +29,54 @@ pub async fn list_indexes(ctx: Context) -> SResult { let list = ctx .state() .db - .indexes_names() + .indexes_uids() .map_err(ResponseError::internal)?; Ok(tide::response::json(list)) } +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +struct GetIndexResponse { + name: String, + uid: String, + schema: Option, + created_at: DateTime, + updated_at: DateTime, +} + +pub async fn get_index(ctx: Context) -> SResult { + ctx.is_allowed(IndexesRead)?; + + let index = ctx.index()?; + + let env = &ctx.state().db.env; + let mut reader = env.read_txn().map_err(ResponseError::internal)?; + + let uid = ctx.url_param("index")?.to_string(); + let name = index.main.name(&mut reader) + .map_err(ResponseError::internal)? + .ok_or(ResponseError::internal("Name not found"))?; + let schema = index.main.schema(&mut reader) + .map_err(ResponseError::internal)? + .map(|schema| SchemaBody::from(schema)); + let created_at = index.main.created_at(&mut reader) + .map_err(ResponseError::internal)? + .ok_or(ResponseError::internal("Created date not found"))?; + let updated_at = index.main.updated_at(&mut reader) + .map_err(ResponseError::internal)? + .ok_or(ResponseError::internal("Updated date not found"))?; + + let response_body = GetIndexResponse { + name, + uid, + schema, + created_at, + updated_at, + }; + + Ok(tide::response::json(response_body)) +} + pub async fn get_index_schema(ctx: Context) -> SResult { ctx.is_allowed(IndexesRead)?; diff --git a/meilidb-http/src/routes/search.rs b/meilidb-http/src/routes/search.rs index f144e3f32..e4500bc92 100644 --- a/meilidb-http/src/routes/search.rs +++ b/meilidb-http/src/routes/search.rs @@ -158,7 +158,7 @@ pub async fn search_multi_index(mut ctx: Context) -> SResult { index_list = ctx .state() .db - .indexes_names() + .indexes_uids() .map_err(ResponseError::internal)? .into_iter() .collect(); diff --git a/meilidb-http/src/routes/stats.rs b/meilidb-http/src/routes/stats.rs index afc8ceaaf..296f6f578 100644 --- a/meilidb-http/src/routes/stats.rs +++ b/meilidb-http/src/routes/stats.rs @@ -72,7 +72,7 @@ pub async fn get_stats(ctx: Context) -> SResult { let mut index_list = HashMap::new(); - if let Ok(indexes_set) = ctx.state().db.indexes_names() { + if let Ok(indexes_set) = ctx.state().db.indexes_uids() { for index_uid in indexes_set { let db = &ctx.state().db; let env = &db.env;