From a98949ff1d1b6f7bd809c93236b26cbfe75be306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Tue, 12 Nov 2019 16:18:53 +0100 Subject: [PATCH] Improve updates JSON format --- meilidb-core/src/database.rs | 22 +++++------ meilidb-core/src/store/updates_results.rs | 4 +- meilidb-core/src/update/mod.rs | 46 +++++++++++------------ meilidb-http/src/routes/index.rs | 17 ++++----- 4 files changed, 43 insertions(+), 46 deletions(-) diff --git a/meilidb-core/src/database.rs b/meilidb-core/src/database.rs index 07d015632..790a74c7d 100644 --- a/meilidb-core/src/database.rs +++ b/meilidb-core/src/database.rs @@ -68,7 +68,7 @@ fn update_awaiter(receiver: UpdateEvents, env: heed::Env, update_fn: Arc = index.document(&reader, None, DocumentId(25)).unwrap(); assert!(document.is_none()); @@ -748,7 +748,7 @@ mod tests { let reader = env.read_txn().unwrap(); let result = index.update_status(&reader, update_id).unwrap(); - assert_matches!(result, UpdateStatus::Processed(status) if status.result.is_ok()); + assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_none()); let document: Option = index.document(&reader, None, DocumentId(25)).unwrap(); assert!(document.is_none()); @@ -791,7 +791,7 @@ mod tests { let reader = env.read_txn().unwrap(); let result = index.update_status(&reader, update_id).unwrap(); - assert_matches!(result, UpdateStatus::Processed(status) if status.result.is_ok()); + assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_none()); let document: Option = index .document(&reader, None, DocumentId(7900334843754999545)) diff --git a/meilidb-core/src/store/updates_results.rs b/meilidb-core/src/store/updates_results.rs index cf99c2acf..d85a38f6d 100644 --- a/meilidb-core/src/store/updates_results.rs +++ b/meilidb-core/src/store/updates_results.rs @@ -1,12 +1,12 @@ use super::BEU64; use crate::update::ProcessedUpdateResult; -use heed::types::{OwnedType, SerdeBincode}; +use heed::types::{OwnedType, SerdeJson}; use heed::Result as ZResult; #[derive(Copy, Clone)] pub struct UpdatesResults { pub(crate) updates_results: - heed::Database, SerdeBincode>, + heed::Database, SerdeJson>, } impl UpdatesResults { diff --git a/meilidb-core/src/update/mod.rs b/meilidb-core/src/update/mod.rs index 8d00498f0..6bb4317e0 100644 --- a/meilidb-core/src/update/mod.rs +++ b/meilidb-core/src/update/mod.rs @@ -22,7 +22,7 @@ pub use self::synonyms_deletion::{apply_synonyms_deletion, SynonymsDeletion}; use std::cmp; use std::collections::{BTreeMap, BTreeSet, HashMap}; -use std::time::{Duration, Instant}; +use std::time::Instant; use heed::Result as ZResult; use log::debug; @@ -49,9 +49,7 @@ impl Update { pub fn update_type(&self) -> UpdateType { match self { Update::ClearAll => UpdateType::ClearAll, - Update::Schema(schema) => UpdateType::Schema { - schema: schema.clone(), - }, + Update::Schema(_) => UpdateType::Schema, Update::Customs(_) => UpdateType::Customs, Update::DocumentsAddition(addition) => UpdateType::DocumentsAddition { number: addition.len(), @@ -79,9 +77,10 @@ impl Update { } #[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "name")] pub enum UpdateType { ClearAll, - Schema { schema: Schema }, + Schema, Customs, DocumentsAddition { number: usize }, DocumentsPartial { number: usize }, @@ -92,17 +91,14 @@ pub enum UpdateType { StopWordsDeletion { number: usize }, } -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct DetailedDuration { - pub main: Duration, -} - #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ProcessedUpdateResult { pub update_id: u64, + #[serde(rename = "type")] pub update_type: UpdateType, - pub result: Result<(), String>, - pub detailed_duration: DetailedDuration, + #[serde(skip_serializing_if = "Option::is_none")] + pub error: Option, + pub duration: f64, // in seconds } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -112,9 +108,16 @@ pub struct EnqueuedUpdateResult { } #[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "status")] pub enum UpdateStatus { - Enqueued(EnqueuedUpdateResult), - Processed(ProcessedUpdateResult), + Enqueued { + #[serde(flatten)] + content: EnqueuedUpdateResult + }, + Processed { + #[serde(flatten)] + content: ProcessedUpdateResult + }, Unknown, } @@ -125,13 +128,13 @@ pub fn update_status( update_id: u64, ) -> MResult { match updates_results_store.update_result(reader, update_id)? { - Some(result) => Ok(UpdateStatus::Processed(result)), + Some(result) => Ok(UpdateStatus::Processed { content: result }), None => { if let Some(update) = updates_store.get(reader, update_id)? { - Ok(UpdateStatus::Enqueued(EnqueuedUpdateResult { + Ok(UpdateStatus::Enqueued { content: EnqueuedUpdateResult { update_id, update_type: update.update_type(), - })) + }}) } else { Ok(UpdateStatus::Unknown) } @@ -183,9 +186,7 @@ pub fn update_task<'a, 'b>( Update::Schema(schema) => { let start = Instant::now(); - let update_type = UpdateType::Schema { - schema: schema.clone(), - }; + let update_type = UpdateType::Schema; let result = apply_schema_update( writer, &schema, @@ -323,12 +324,11 @@ pub fn update_task<'a, 'b>( update_id, update_type, result ); - let detailed_duration = DetailedDuration { main: duration }; let status = ProcessedUpdateResult { update_id, update_type, - result: result.map_err(|e| e.to_string()), - detailed_duration, + error: result.map_err(|e| e.to_string()).err(), + duration: duration.as_secs_f64(), }; Ok(status) diff --git a/meilidb-http/src/routes/index.rs b/meilidb-http/src/routes/index.rs index 320c0ff48..51adf3ba3 100644 --- a/meilidb-http/src/routes/index.rs +++ b/meilidb-http/src/routes/index.rs @@ -150,19 +150,16 @@ pub async fn get_update_status(ctx: Context) -> SResult { .map_err(ResponseError::internal)?; let response = match status { - UpdateStatus::Enqueued(data) => { - tide::response::json(json!({ "status": "enqueued", "data": data })) + UpdateStatus::Unknown => { + tide::response::json(json!({ "message": "unknown update id" })) + .with_status(StatusCode::NOT_FOUND) + .into_response() + } + status => { + tide::response::json(status) .with_status(StatusCode::OK) .into_response() } - UpdateStatus::Processed(data) => { - tide::response::json(json!({ "status": "processed", "data": data })) - .with_status(StatusCode::OK) - .into_response() - } - UpdateStatus::Unknown => tide::response::json(json!({ "message": "unknown update id" })) - .with_status(StatusCode::NOT_FOUND) - .into_response(), }; Ok(response)