Improve updates JSON format

This commit is contained in:
Clément Renault 2019-11-12 16:18:53 +01:00
parent f355280250
commit a98949ff1d
4 changed files with 43 additions and 46 deletions

View File

@ -68,7 +68,7 @@ fn update_awaiter(receiver: UpdateEvents, env: heed::Env, update_fn: Arc<ArcSwap
let status = break_try!(result, "update task failed");
// commit the nested transaction if the update was successful, abort it otherwise
if status.result.is_ok() {
if status.error.is_none() {
break_try!(nested_writer.commit(), "commit nested transaction failed");
} else {
nested_writer.abort()
@ -323,7 +323,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());
}
#[test]
@ -384,11 +384,11 @@ 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_err());
assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_some());
}
#[test]
fn ignored_words_to_long() {
fn ignored_words_too_long() {
let dir = tempfile::tempdir().unwrap();
let database = Database::open_or_create(dir.path()).unwrap();
@ -434,7 +434,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());
}
#[test]
@ -524,7 +524,7 @@ mod tests {
// check if it has been accepted
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());
reader.abort();
let mut additions = index.documents_addition();
@ -558,7 +558,7 @@ mod tests {
// check if it has been accepted
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());
// even try to search for a document
let results = index.query_builder().query(&reader, "21 ", 0..20).unwrap();
@ -604,7 +604,7 @@ mod tests {
// check if it has been accepted
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_err());
assert_matches!(result, UpdateStatus::Processed { content } if content.error.is_some());
}
#[test]
@ -668,7 +668,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<IgnoredAny> = 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<IgnoredAny> = 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<serde_json::Value> = index
.document(&reader, None, DocumentId(7900334843754999545))

View File

@ -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<OwnedType<BEU64>, SerdeBincode<ProcessedUpdateResult>>,
heed::Database<OwnedType<BEU64>, SerdeJson<ProcessedUpdateResult>>,
}
impl UpdatesResults {

View File

@ -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<String>,
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<UpdateStatus> {
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)

View File

@ -150,19 +150,16 @@ pub async fn get_update_status(ctx: Context<Data>) -> SResult<Response> {
.map_err(ResponseError::internal)?;
let response = match status {
UpdateStatus::Enqueued(data) => {
tide::response::json(json!({ "status": "enqueued", "data": data }))
.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" }))
UpdateStatus::Unknown => {
tide::response::json(json!({ "message": "unknown update id" }))
.with_status(StatusCode::NOT_FOUND)
.into_response(),
.into_response()
}
status => {
tide::response::json(status)
.with_status(StatusCode::OK)
.into_response()
}
};
Ok(response)