2023-01-16 16:59:26 +01:00
|
|
|
use core::fmt;
|
2022-10-17 16:30:18 +02:00
|
|
|
use std::collections::HashSet;
|
2022-10-20 18:00:07 +02:00
|
|
|
use std::fmt::{Display, Write};
|
2022-10-18 11:02:46 +02:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2022-10-17 18:57:23 +02:00
|
|
|
use enum_iterator::Sequence;
|
2022-10-12 00:43:24 +02:00
|
|
|
use milli::update::IndexDocumentsMethod;
|
2024-05-10 20:08:05 +02:00
|
|
|
use milli::Object;
|
2022-10-13 11:09:00 +02:00
|
|
|
use roaring::RoaringBitmap;
|
2022-10-12 00:43:24 +02:00
|
|
|
use serde::{Deserialize, Serialize, Serializer};
|
|
|
|
use time::{Duration, OffsetDateTime};
|
2022-10-12 03:21:25 +02:00
|
|
|
use uuid::Uuid;
|
2022-10-12 00:43:24 +02:00
|
|
|
|
2023-01-16 16:59:26 +01:00
|
|
|
use crate::error::ResponseError;
|
2022-10-20 18:00:07 +02:00
|
|
|
use crate::keys::Key;
|
|
|
|
use crate::settings::{Settings, Unchecked};
|
|
|
|
use crate::InstanceUid;
|
2022-10-12 00:43:24 +02:00
|
|
|
|
|
|
|
pub type TaskId = u32;
|
|
|
|
|
2022-10-13 12:48:23 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2022-10-12 00:43:24 +02:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-10-12 03:21:25 +02:00
|
|
|
pub struct Task {
|
2022-10-12 00:43:24 +02:00
|
|
|
pub uid: TaskId,
|
|
|
|
|
|
|
|
#[serde(with = "time::serde::rfc3339")]
|
|
|
|
pub enqueued_at: OffsetDateTime,
|
2022-10-12 03:21:25 +02:00
|
|
|
#[serde(with = "time::serde::rfc3339::option")]
|
2022-10-12 00:43:24 +02:00
|
|
|
pub started_at: Option<OffsetDateTime>,
|
2022-10-12 03:21:25 +02:00
|
|
|
#[serde(with = "time::serde::rfc3339::option")]
|
2022-10-12 00:43:24 +02:00
|
|
|
pub finished_at: Option<OffsetDateTime>,
|
2022-10-12 03:21:25 +02:00
|
|
|
|
|
|
|
pub error: Option<ResponseError>,
|
2022-10-18 13:57:58 +02:00
|
|
|
pub canceled_by: Option<TaskId>,
|
2022-10-12 03:21:25 +02:00
|
|
|
pub details: Option<Details>,
|
|
|
|
|
|
|
|
pub status: Status,
|
|
|
|
pub kind: KindWithContent,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Task {
|
|
|
|
pub fn index_uid(&self) -> Option<&str> {
|
|
|
|
use KindWithContent::*;
|
|
|
|
|
|
|
|
match &self.kind {
|
2022-10-24 19:08:15 +02:00
|
|
|
DumpCreation { .. }
|
2022-10-25 10:44:58 +02:00
|
|
|
| SnapshotCreation
|
2022-10-17 17:19:17 +02:00
|
|
|
| TaskCancelation { .. }
|
2022-10-13 11:09:00 +02:00
|
|
|
| TaskDeletion { .. }
|
2022-10-12 03:21:25 +02:00
|
|
|
| IndexSwap { .. } => None,
|
2022-10-21 18:03:10 +02:00
|
|
|
DocumentAdditionOrUpdate { index_uid, .. }
|
2024-05-08 15:26:21 +02:00
|
|
|
| DocumentEdition { index_uid, .. }
|
2022-10-12 03:21:25 +02:00
|
|
|
| DocumentDeletion { index_uid, .. }
|
2023-03-07 10:02:04 +01:00
|
|
|
| DocumentDeletionByFilter { index_uid, .. }
|
2022-10-12 03:21:25 +02:00
|
|
|
| DocumentClear { index_uid }
|
2022-10-21 18:03:10 +02:00
|
|
|
| SettingsUpdate { index_uid, .. }
|
2022-10-12 03:21:25 +02:00
|
|
|
| IndexCreation { index_uid, .. }
|
|
|
|
| IndexUpdate { index_uid, .. }
|
|
|
|
| IndexDeletion { index_uid } => Some(index_uid),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the list of indexes updated by this tasks.
|
2022-10-25 10:26:51 +02:00
|
|
|
pub fn indexes(&self) -> Vec<&str> {
|
2022-10-17 16:30:18 +02:00
|
|
|
self.kind.indexes()
|
2022-10-12 03:21:25 +02:00
|
|
|
}
|
2022-10-13 15:02:59 +02:00
|
|
|
|
|
|
|
/// Return the content-uuid if there is one
|
2022-10-25 14:09:01 +02:00
|
|
|
pub fn content_uuid(&self) -> Option<Uuid> {
|
2022-10-13 15:02:59 +02:00
|
|
|
match self.kind {
|
2022-10-25 14:09:01 +02:00
|
|
|
KindWithContent::DocumentAdditionOrUpdate { content_file, .. } => Some(content_file),
|
2024-05-08 15:26:21 +02:00
|
|
|
KindWithContent::DocumentEdition { .. }
|
|
|
|
| KindWithContent::DocumentDeletion { .. }
|
2023-03-07 10:02:04 +01:00
|
|
|
| KindWithContent::DocumentDeletionByFilter { .. }
|
2022-10-13 15:02:59 +02:00
|
|
|
| KindWithContent::DocumentClear { .. }
|
2022-10-21 18:03:10 +02:00
|
|
|
| KindWithContent::SettingsUpdate { .. }
|
2022-10-13 15:02:59 +02:00
|
|
|
| KindWithContent::IndexDeletion { .. }
|
|
|
|
| KindWithContent::IndexCreation { .. }
|
|
|
|
| KindWithContent::IndexUpdate { .. }
|
|
|
|
| KindWithContent::IndexSwap { .. }
|
2022-10-17 17:19:17 +02:00
|
|
|
| KindWithContent::TaskCancelation { .. }
|
2022-10-17 15:11:35 +02:00
|
|
|
| KindWithContent::TaskDeletion { .. }
|
2022-10-24 19:08:15 +02:00
|
|
|
| KindWithContent::DumpCreation { .. }
|
2022-10-25 10:44:58 +02:00
|
|
|
| KindWithContent::SnapshotCreation => None,
|
2022-10-13 15:02:59 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-12 00:43:24 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 17:19:17 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
2022-10-12 00:43:24 +02:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-10-12 03:21:25 +02:00
|
|
|
pub enum KindWithContent {
|
2022-10-21 18:03:10 +02:00
|
|
|
DocumentAdditionOrUpdate {
|
2022-10-12 03:21:25 +02:00
|
|
|
index_uid: String,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
method: IndexDocumentsMethod,
|
|
|
|
content_file: Uuid,
|
|
|
|
documents_count: u64,
|
|
|
|
allow_index_creation: bool,
|
|
|
|
},
|
2024-05-08 15:26:21 +02:00
|
|
|
DocumentEdition {
|
|
|
|
index_uid: String,
|
2024-05-08 23:37:57 +02:00
|
|
|
filter_expr: Option<serde_json::Value>,
|
2024-05-10 20:08:05 +02:00
|
|
|
context: Option<milli::Object>,
|
2024-05-10 19:42:44 +02:00
|
|
|
function: String,
|
2024-05-08 15:26:21 +02:00
|
|
|
},
|
2022-10-12 03:21:25 +02:00
|
|
|
DocumentDeletion {
|
|
|
|
index_uid: String,
|
|
|
|
documents_ids: Vec<String>,
|
|
|
|
},
|
2023-03-07 10:02:04 +01:00
|
|
|
DocumentDeletionByFilter {
|
|
|
|
index_uid: String,
|
|
|
|
filter_expr: serde_json::Value,
|
|
|
|
},
|
2022-10-12 03:21:25 +02:00
|
|
|
DocumentClear {
|
|
|
|
index_uid: String,
|
|
|
|
},
|
2022-10-21 18:03:10 +02:00
|
|
|
SettingsUpdate {
|
2022-10-12 03:21:25 +02:00
|
|
|
index_uid: String,
|
2022-10-22 16:35:42 +02:00
|
|
|
new_settings: Box<Settings<Unchecked>>,
|
2022-10-12 03:21:25 +02:00
|
|
|
is_deletion: bool,
|
|
|
|
allow_index_creation: bool,
|
|
|
|
},
|
|
|
|
IndexDeletion {
|
|
|
|
index_uid: String,
|
|
|
|
},
|
|
|
|
IndexCreation {
|
|
|
|
index_uid: String,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
},
|
|
|
|
IndexUpdate {
|
|
|
|
index_uid: String,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
},
|
|
|
|
IndexSwap {
|
2022-10-26 12:57:29 +02:00
|
|
|
swaps: Vec<IndexSwap>,
|
2022-10-12 03:21:25 +02:00
|
|
|
},
|
2022-10-17 17:19:17 +02:00
|
|
|
TaskCancelation {
|
|
|
|
query: String,
|
2022-10-18 13:47:22 +02:00
|
|
|
tasks: RoaringBitmap,
|
2022-10-12 03:21:25 +02:00
|
|
|
},
|
2022-10-13 11:09:00 +02:00
|
|
|
TaskDeletion {
|
2022-10-12 03:21:25 +02:00
|
|
|
query: String,
|
2022-10-13 11:09:00 +02:00
|
|
|
tasks: RoaringBitmap,
|
2022-10-12 03:21:25 +02:00
|
|
|
},
|
2022-10-24 19:08:15 +02:00
|
|
|
DumpCreation {
|
2022-10-13 15:02:59 +02:00
|
|
|
keys: Vec<Key>,
|
|
|
|
instance_uid: Option<InstanceUid>,
|
2022-10-12 03:21:25 +02:00
|
|
|
},
|
2022-10-25 10:44:58 +02:00
|
|
|
SnapshotCreation,
|
2022-10-12 03:21:25 +02:00
|
|
|
}
|
2022-10-12 00:43:24 +02:00
|
|
|
|
2022-10-26 15:14:46 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
2022-10-26 12:57:29 +02:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct IndexSwap {
|
|
|
|
pub indexes: (String, String),
|
|
|
|
}
|
|
|
|
|
2022-10-12 03:21:25 +02:00
|
|
|
impl KindWithContent {
|
|
|
|
pub fn as_kind(&self) -> Kind {
|
|
|
|
match self {
|
2022-10-21 18:03:10 +02:00
|
|
|
KindWithContent::DocumentAdditionOrUpdate { .. } => Kind::DocumentAdditionOrUpdate,
|
2024-05-08 15:26:21 +02:00
|
|
|
KindWithContent::DocumentEdition { .. } => Kind::DocumentEdition,
|
2022-10-12 03:21:25 +02:00
|
|
|
KindWithContent::DocumentDeletion { .. } => Kind::DocumentDeletion,
|
2023-03-07 10:02:04 +01:00
|
|
|
KindWithContent::DocumentDeletionByFilter { .. } => Kind::DocumentDeletion,
|
2022-10-22 16:06:01 +02:00
|
|
|
KindWithContent::DocumentClear { .. } => Kind::DocumentDeletion,
|
2022-10-21 18:03:10 +02:00
|
|
|
KindWithContent::SettingsUpdate { .. } => Kind::SettingsUpdate,
|
2022-10-12 03:21:25 +02:00
|
|
|
KindWithContent::IndexCreation { .. } => Kind::IndexCreation,
|
|
|
|
KindWithContent::IndexDeletion { .. } => Kind::IndexDeletion,
|
|
|
|
KindWithContent::IndexUpdate { .. } => Kind::IndexUpdate,
|
|
|
|
KindWithContent::IndexSwap { .. } => Kind::IndexSwap,
|
2022-10-17 17:19:17 +02:00
|
|
|
KindWithContent::TaskCancelation { .. } => Kind::TaskCancelation,
|
2022-10-13 11:09:00 +02:00
|
|
|
KindWithContent::TaskDeletion { .. } => Kind::TaskDeletion,
|
2022-10-24 19:08:15 +02:00
|
|
|
KindWithContent::DumpCreation { .. } => Kind::DumpCreation,
|
2022-10-25 10:44:58 +02:00
|
|
|
KindWithContent::SnapshotCreation => Kind::SnapshotCreation,
|
2022-10-12 03:21:25 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-12 00:43:24 +02:00
|
|
|
|
2022-10-25 10:26:51 +02:00
|
|
|
pub fn indexes(&self) -> Vec<&str> {
|
2022-10-12 03:21:25 +02:00
|
|
|
use KindWithContent::*;
|
|
|
|
|
|
|
|
match self {
|
2022-10-25 10:44:58 +02:00
|
|
|
DumpCreation { .. }
|
|
|
|
| SnapshotCreation
|
|
|
|
| TaskCancelation { .. }
|
|
|
|
| TaskDeletion { .. } => vec![],
|
2022-10-21 18:03:10 +02:00
|
|
|
DocumentAdditionOrUpdate { index_uid, .. }
|
2024-05-08 15:26:21 +02:00
|
|
|
| DocumentEdition { index_uid, .. }
|
2022-10-12 03:21:25 +02:00
|
|
|
| DocumentDeletion { index_uid, .. }
|
2023-03-07 10:02:04 +01:00
|
|
|
| DocumentDeletionByFilter { index_uid, .. }
|
2022-10-12 03:21:25 +02:00
|
|
|
| DocumentClear { index_uid }
|
2022-10-21 18:03:10 +02:00
|
|
|
| SettingsUpdate { index_uid, .. }
|
2022-10-12 03:21:25 +02:00
|
|
|
| IndexCreation { index_uid, .. }
|
|
|
|
| IndexUpdate { index_uid, .. }
|
2022-10-25 10:26:51 +02:00
|
|
|
| IndexDeletion { index_uid } => vec![index_uid],
|
2022-10-17 16:30:18 +02:00
|
|
|
IndexSwap { swaps } => {
|
|
|
|
let mut indexes = HashSet::<&str>::default();
|
2022-10-26 12:57:29 +02:00
|
|
|
for swap in swaps {
|
|
|
|
indexes.insert(swap.indexes.0.as_str());
|
|
|
|
indexes.insert(swap.indexes.1.as_str());
|
2022-10-17 16:30:18 +02:00
|
|
|
}
|
2022-10-25 10:26:51 +02:00
|
|
|
indexes.into_iter().collect()
|
2022-10-17 16:30:18 +02:00
|
|
|
}
|
2022-10-12 03:21:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the default `Details` that correspond to this `KindWithContent`,
|
|
|
|
/// `None` if it cannot be generated.
|
|
|
|
pub fn default_details(&self) -> Option<Details> {
|
|
|
|
match self {
|
2022-10-21 18:03:10 +02:00
|
|
|
KindWithContent::DocumentAdditionOrUpdate { documents_count, .. } => {
|
|
|
|
Some(Details::DocumentAdditionOrUpdate {
|
2022-10-20 18:00:07 +02:00
|
|
|
received_documents: *documents_count,
|
|
|
|
indexed_documents: None,
|
|
|
|
})
|
|
|
|
}
|
2024-05-10 20:08:05 +02:00
|
|
|
KindWithContent::DocumentEdition { index_uid: _, filter_expr, context, function } => {
|
2024-05-08 15:26:21 +02:00
|
|
|
Some(Details::DocumentEdition {
|
|
|
|
edited_documents: None,
|
2024-05-08 23:37:57 +02:00
|
|
|
original_filter: filter_expr.as_ref().map(|v| v.to_string()),
|
2024-05-10 20:08:05 +02:00
|
|
|
context: context.clone(),
|
2024-05-10 19:42:44 +02:00
|
|
|
function: function.clone(),
|
2024-05-08 15:26:21 +02:00
|
|
|
})
|
|
|
|
}
|
2022-10-20 18:00:07 +02:00
|
|
|
KindWithContent::DocumentDeletion { index_uid: _, documents_ids } => {
|
|
|
|
Some(Details::DocumentDeletion {
|
2022-11-28 16:27:41 +01:00
|
|
|
provided_ids: documents_ids.len(),
|
2022-10-20 18:00:07 +02:00
|
|
|
deleted_documents: None,
|
|
|
|
})
|
|
|
|
}
|
2023-03-07 10:02:04 +01:00
|
|
|
KindWithContent::DocumentDeletionByFilter { index_uid: _, filter_expr } => {
|
|
|
|
Some(Details::DocumentDeletionByFilter {
|
|
|
|
original_filter: filter_expr.to_string(),
|
|
|
|
deleted_documents: None,
|
|
|
|
})
|
|
|
|
}
|
2022-11-28 16:27:41 +01:00
|
|
|
KindWithContent::DocumentClear { .. } | KindWithContent::IndexDeletion { .. } => {
|
2022-10-20 18:00:07 +02:00
|
|
|
Some(Details::ClearAll { deleted_documents: None })
|
|
|
|
}
|
2022-10-21 18:03:10 +02:00
|
|
|
KindWithContent::SettingsUpdate { new_settings, .. } => {
|
|
|
|
Some(Details::SettingsUpdate { settings: new_settings.clone() })
|
2022-10-20 18:00:07 +02:00
|
|
|
}
|
2022-10-12 03:21:25 +02:00
|
|
|
KindWithContent::IndexCreation { primary_key, .. }
|
2022-10-20 18:00:07 +02:00
|
|
|
| KindWithContent::IndexUpdate { primary_key, .. } => {
|
|
|
|
Some(Details::IndexInfo { primary_key: primary_key.clone() })
|
|
|
|
}
|
|
|
|
KindWithContent::IndexSwap { swaps } => {
|
|
|
|
Some(Details::IndexSwap { swaps: swaps.clone() })
|
|
|
|
}
|
2022-10-18 17:47:47 +02:00
|
|
|
KindWithContent::TaskCancelation { query, tasks } => Some(Details::TaskCancelation {
|
|
|
|
matched_tasks: tasks.len(),
|
|
|
|
canceled_tasks: None,
|
2022-11-28 16:27:41 +01:00
|
|
|
original_filter: query.clone(),
|
2022-10-18 17:47:47 +02:00
|
|
|
}),
|
2022-10-13 11:09:00 +02:00
|
|
|
KindWithContent::TaskDeletion { query, tasks } => Some(Details::TaskDeletion {
|
2022-10-17 19:24:06 +02:00
|
|
|
matched_tasks: tasks.len(),
|
2022-10-12 03:21:25 +02:00
|
|
|
deleted_tasks: None,
|
2022-11-28 16:27:41 +01:00
|
|
|
original_filter: query.clone(),
|
2022-10-19 16:44:42 +02:00
|
|
|
}),
|
2022-11-28 16:27:41 +01:00
|
|
|
KindWithContent::DumpCreation { .. } => Some(Details::Dump { dump_uid: None }),
|
2022-10-25 10:44:58 +02:00
|
|
|
KindWithContent::SnapshotCreation => None,
|
2022-10-19 16:44:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default_finished_details(&self) -> Option<Details> {
|
|
|
|
match self {
|
2022-10-21 18:03:10 +02:00
|
|
|
KindWithContent::DocumentAdditionOrUpdate { documents_count, .. } => {
|
|
|
|
Some(Details::DocumentAdditionOrUpdate {
|
2022-10-20 18:00:07 +02:00
|
|
|
received_documents: *documents_count,
|
|
|
|
indexed_documents: Some(0),
|
|
|
|
})
|
|
|
|
}
|
2024-05-10 20:08:05 +02:00
|
|
|
KindWithContent::DocumentEdition { index_uid: _, filter_expr, context, function } => {
|
2024-05-08 15:26:21 +02:00
|
|
|
Some(Details::DocumentEdition {
|
|
|
|
edited_documents: Some(0),
|
2024-05-08 23:37:57 +02:00
|
|
|
original_filter: filter_expr.as_ref().map(|v| v.to_string()),
|
2024-05-10 20:08:05 +02:00
|
|
|
context: context.clone(),
|
2024-05-10 19:42:44 +02:00
|
|
|
function: function.clone(),
|
2024-05-08 15:26:21 +02:00
|
|
|
})
|
|
|
|
}
|
2022-10-20 18:00:07 +02:00
|
|
|
KindWithContent::DocumentDeletion { index_uid: _, documents_ids } => {
|
|
|
|
Some(Details::DocumentDeletion {
|
2022-11-28 16:27:41 +01:00
|
|
|
provided_ids: documents_ids.len(),
|
2022-10-20 18:00:07 +02:00
|
|
|
deleted_documents: Some(0),
|
|
|
|
})
|
|
|
|
}
|
2023-03-07 10:02:04 +01:00
|
|
|
KindWithContent::DocumentDeletionByFilter { index_uid: _, filter_expr } => {
|
|
|
|
Some(Details::DocumentDeletionByFilter {
|
|
|
|
original_filter: filter_expr.to_string(),
|
|
|
|
deleted_documents: Some(0),
|
|
|
|
})
|
|
|
|
}
|
2022-10-20 18:00:07 +02:00
|
|
|
KindWithContent::DocumentClear { .. } => {
|
|
|
|
Some(Details::ClearAll { deleted_documents: None })
|
|
|
|
}
|
2022-10-21 18:03:10 +02:00
|
|
|
KindWithContent::SettingsUpdate { new_settings, .. } => {
|
|
|
|
Some(Details::SettingsUpdate { settings: new_settings.clone() })
|
2022-10-20 18:00:07 +02:00
|
|
|
}
|
2022-10-19 16:44:42 +02:00
|
|
|
KindWithContent::IndexDeletion { .. } => None,
|
|
|
|
KindWithContent::IndexCreation { primary_key, .. }
|
2022-10-20 18:00:07 +02:00
|
|
|
| KindWithContent::IndexUpdate { primary_key, .. } => {
|
|
|
|
Some(Details::IndexInfo { primary_key: primary_key.clone() })
|
|
|
|
}
|
2022-10-19 16:44:42 +02:00
|
|
|
KindWithContent::IndexSwap { .. } => {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
KindWithContent::TaskCancelation { query, tasks } => Some(Details::TaskCancelation {
|
|
|
|
matched_tasks: tasks.len(),
|
|
|
|
canceled_tasks: Some(0),
|
2022-11-28 16:27:41 +01:00
|
|
|
original_filter: query.clone(),
|
2022-10-19 16:44:42 +02:00
|
|
|
}),
|
|
|
|
KindWithContent::TaskDeletion { query, tasks } => Some(Details::TaskDeletion {
|
|
|
|
matched_tasks: tasks.len(),
|
|
|
|
deleted_tasks: Some(0),
|
2022-11-28 16:27:41 +01:00
|
|
|
original_filter: query.clone(),
|
2022-10-12 03:21:25 +02:00
|
|
|
}),
|
2022-11-28 16:27:41 +01:00
|
|
|
KindWithContent::DumpCreation { .. } => Some(Details::Dump { dump_uid: None }),
|
2022-10-25 10:44:58 +02:00
|
|
|
KindWithContent::SnapshotCreation => None,
|
2022-10-12 03:21:25 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-12 00:43:24 +02:00
|
|
|
}
|
|
|
|
|
2022-10-13 15:02:59 +02:00
|
|
|
impl From<&KindWithContent> for Option<Details> {
|
|
|
|
fn from(kind: &KindWithContent) -> Self {
|
|
|
|
match kind {
|
2022-10-21 18:03:10 +02:00
|
|
|
KindWithContent::DocumentAdditionOrUpdate { documents_count, .. } => {
|
|
|
|
Some(Details::DocumentAdditionOrUpdate {
|
2022-10-20 18:00:07 +02:00
|
|
|
received_documents: *documents_count,
|
|
|
|
indexed_documents: None,
|
|
|
|
})
|
|
|
|
}
|
2024-05-08 15:53:40 +02:00
|
|
|
KindWithContent::DocumentEdition { .. } => None,
|
2022-10-13 15:02:59 +02:00
|
|
|
KindWithContent::DocumentDeletion { .. } => None,
|
2023-03-07 10:02:04 +01:00
|
|
|
KindWithContent::DocumentDeletionByFilter { .. } => None,
|
2022-10-13 15:02:59 +02:00
|
|
|
KindWithContent::DocumentClear { .. } => None,
|
2022-10-21 18:03:10 +02:00
|
|
|
KindWithContent::SettingsUpdate { new_settings, .. } => {
|
|
|
|
Some(Details::SettingsUpdate { settings: new_settings.clone() })
|
2022-10-20 18:00:07 +02:00
|
|
|
}
|
2022-10-13 15:02:59 +02:00
|
|
|
KindWithContent::IndexDeletion { .. } => None,
|
2022-10-20 18:00:07 +02:00
|
|
|
KindWithContent::IndexCreation { primary_key, .. } => {
|
|
|
|
Some(Details::IndexInfo { primary_key: primary_key.clone() })
|
|
|
|
}
|
|
|
|
KindWithContent::IndexUpdate { primary_key, .. } => {
|
|
|
|
Some(Details::IndexInfo { primary_key: primary_key.clone() })
|
|
|
|
}
|
2022-10-13 15:02:59 +02:00
|
|
|
KindWithContent::IndexSwap { .. } => None,
|
2022-10-18 17:47:47 +02:00
|
|
|
KindWithContent::TaskCancelation { query, tasks } => Some(Details::TaskCancelation {
|
|
|
|
matched_tasks: tasks.len(),
|
|
|
|
canceled_tasks: None,
|
2022-11-28 16:27:41 +01:00
|
|
|
original_filter: query.clone(),
|
2022-10-18 17:47:47 +02:00
|
|
|
}),
|
2022-10-17 19:24:06 +02:00
|
|
|
KindWithContent::TaskDeletion { query, tasks } => Some(Details::TaskDeletion {
|
|
|
|
matched_tasks: tasks.len(),
|
|
|
|
deleted_tasks: None,
|
2022-11-28 16:27:41 +01:00
|
|
|
original_filter: query.clone(),
|
2022-10-17 19:24:06 +02:00
|
|
|
}),
|
2022-11-28 16:27:41 +01:00
|
|
|
KindWithContent::DumpCreation { .. } => Some(Details::Dump { dump_uid: None }),
|
2022-10-25 10:44:58 +02:00
|
|
|
KindWithContent::SnapshotCreation => None,
|
2022-10-13 15:02:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:57:23 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Sequence)]
|
2022-10-12 00:43:24 +02:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum Status {
|
|
|
|
Enqueued,
|
|
|
|
Processing,
|
|
|
|
Succeeded,
|
|
|
|
Failed,
|
2022-10-17 14:02:14 +02:00
|
|
|
Canceled,
|
2022-10-12 00:43:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Status {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Status::Enqueued => write!(f, "enqueued"),
|
|
|
|
Status::Processing => write!(f, "processing"),
|
|
|
|
Status::Succeeded => write!(f, "succeeded"),
|
|
|
|
Status::Failed => write!(f, "failed"),
|
2022-10-17 14:02:14 +02:00
|
|
|
Status::Canceled => write!(f, "canceled"),
|
2022-10-12 00:43:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Status {
|
2023-01-16 16:59:26 +01:00
|
|
|
type Err = ParseTaskStatusError;
|
2022-10-12 00:43:24 +02:00
|
|
|
|
2022-10-17 18:57:23 +02:00
|
|
|
fn from_str(status: &str) -> Result<Self, Self::Err> {
|
|
|
|
if status.eq_ignore_ascii_case("enqueued") {
|
|
|
|
Ok(Status::Enqueued)
|
|
|
|
} else if status.eq_ignore_ascii_case("processing") {
|
|
|
|
Ok(Status::Processing)
|
|
|
|
} else if status.eq_ignore_ascii_case("succeeded") {
|
|
|
|
Ok(Status::Succeeded)
|
|
|
|
} else if status.eq_ignore_ascii_case("failed") {
|
|
|
|
Ok(Status::Failed)
|
2022-10-17 14:02:14 +02:00
|
|
|
} else if status.eq_ignore_ascii_case("canceled") {
|
|
|
|
Ok(Status::Canceled)
|
2022-10-17 18:57:23 +02:00
|
|
|
} else {
|
2023-01-16 16:59:26 +01:00
|
|
|
Err(ParseTaskStatusError(status.to_owned()))
|
2022-10-12 00:43:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 16:59:26 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ParseTaskStatusError(pub String);
|
|
|
|
impl fmt::Display for ParseTaskStatusError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"`{}` is not a valid task status. Available statuses are {}.",
|
|
|
|
self.0,
|
|
|
|
enum_iterator::all::<Status>()
|
|
|
|
.map(|s| format!("`{s}`"))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::error::Error for ParseTaskStatusError {}
|
|
|
|
|
2022-10-17 18:57:23 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Sequence)]
|
2022-10-12 00:43:24 +02:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum Kind {
|
2022-10-21 18:03:10 +02:00
|
|
|
DocumentAdditionOrUpdate,
|
2024-05-08 15:26:21 +02:00
|
|
|
DocumentEdition,
|
2022-10-12 00:43:24 +02:00
|
|
|
DocumentDeletion,
|
2022-10-21 18:03:10 +02:00
|
|
|
SettingsUpdate,
|
2022-10-12 00:43:24 +02:00
|
|
|
IndexCreation,
|
|
|
|
IndexDeletion,
|
|
|
|
IndexUpdate,
|
|
|
|
IndexSwap,
|
2022-10-17 17:19:17 +02:00
|
|
|
TaskCancelation,
|
2022-10-13 11:09:00 +02:00
|
|
|
TaskDeletion,
|
2022-10-24 19:08:15 +02:00
|
|
|
DumpCreation,
|
2022-10-25 10:44:58 +02:00
|
|
|
SnapshotCreation,
|
2022-10-12 00:43:24 +02:00
|
|
|
}
|
|
|
|
|
2022-10-27 18:00:04 +02:00
|
|
|
impl Kind {
|
|
|
|
pub fn related_to_one_index(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Kind::DocumentAdditionOrUpdate
|
2024-05-08 15:26:21 +02:00
|
|
|
| Kind::DocumentEdition
|
2022-10-27 18:00:04 +02:00
|
|
|
| Kind::DocumentDeletion
|
|
|
|
| Kind::SettingsUpdate
|
|
|
|
| Kind::IndexCreation
|
|
|
|
| Kind::IndexDeletion
|
|
|
|
| Kind::IndexUpdate => true,
|
|
|
|
Kind::IndexSwap
|
|
|
|
| Kind::TaskCancelation
|
|
|
|
| Kind::TaskDeletion
|
|
|
|
| Kind::DumpCreation
|
|
|
|
| Kind::SnapshotCreation => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-28 16:27:41 +01:00
|
|
|
impl Display for Kind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Kind::DocumentAdditionOrUpdate => write!(f, "documentAdditionOrUpdate"),
|
2024-05-08 15:26:21 +02:00
|
|
|
Kind::DocumentEdition => write!(f, "documentEdition"),
|
2022-11-28 16:27:41 +01:00
|
|
|
Kind::DocumentDeletion => write!(f, "documentDeletion"),
|
|
|
|
Kind::SettingsUpdate => write!(f, "settingsUpdate"),
|
|
|
|
Kind::IndexCreation => write!(f, "indexCreation"),
|
|
|
|
Kind::IndexDeletion => write!(f, "indexDeletion"),
|
|
|
|
Kind::IndexUpdate => write!(f, "indexUpdate"),
|
|
|
|
Kind::IndexSwap => write!(f, "indexSwap"),
|
|
|
|
Kind::TaskCancelation => write!(f, "taskCancelation"),
|
|
|
|
Kind::TaskDeletion => write!(f, "taskDeletion"),
|
|
|
|
Kind::DumpCreation => write!(f, "dumpCreation"),
|
|
|
|
Kind::SnapshotCreation => write!(f, "snapshotCreation"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 00:43:24 +02:00
|
|
|
impl FromStr for Kind {
|
2023-01-16 16:59:26 +01:00
|
|
|
type Err = ParseTaskKindError;
|
2022-10-12 00:43:24 +02:00
|
|
|
|
2022-10-17 18:57:23 +02:00
|
|
|
fn from_str(kind: &str) -> Result<Self, Self::Err> {
|
|
|
|
if kind.eq_ignore_ascii_case("indexCreation") {
|
|
|
|
Ok(Kind::IndexCreation)
|
|
|
|
} else if kind.eq_ignore_ascii_case("indexUpdate") {
|
|
|
|
Ok(Kind::IndexUpdate)
|
2022-10-26 12:57:29 +02:00
|
|
|
} else if kind.eq_ignore_ascii_case("indexSwap") {
|
|
|
|
Ok(Kind::IndexSwap)
|
2022-10-17 18:57:23 +02:00
|
|
|
} else if kind.eq_ignore_ascii_case("indexDeletion") {
|
|
|
|
Ok(Kind::IndexDeletion)
|
|
|
|
} else if kind.eq_ignore_ascii_case("documentAdditionOrUpdate") {
|
2022-10-21 18:03:10 +02:00
|
|
|
Ok(Kind::DocumentAdditionOrUpdate)
|
2024-05-08 15:26:21 +02:00
|
|
|
} else if kind.eq_ignore_ascii_case("documentEdition") {
|
|
|
|
Ok(Kind::DocumentEdition)
|
2022-10-17 18:57:23 +02:00
|
|
|
} else if kind.eq_ignore_ascii_case("documentDeletion") {
|
|
|
|
Ok(Kind::DocumentDeletion)
|
|
|
|
} else if kind.eq_ignore_ascii_case("settingsUpdate") {
|
2022-10-21 18:03:10 +02:00
|
|
|
Ok(Kind::SettingsUpdate)
|
2022-10-18 13:47:22 +02:00
|
|
|
} else if kind.eq_ignore_ascii_case("taskCancelation") {
|
2022-10-17 17:19:17 +02:00
|
|
|
Ok(Kind::TaskCancelation)
|
2022-10-18 13:47:22 +02:00
|
|
|
} else if kind.eq_ignore_ascii_case("taskDeletion") {
|
2022-10-17 17:19:17 +02:00
|
|
|
Ok(Kind::TaskDeletion)
|
2022-10-17 18:57:23 +02:00
|
|
|
} else if kind.eq_ignore_ascii_case("dumpCreation") {
|
2022-10-24 19:08:15 +02:00
|
|
|
Ok(Kind::DumpCreation)
|
2022-10-25 10:44:58 +02:00
|
|
|
} else if kind.eq_ignore_ascii_case("snapshotCreation") {
|
|
|
|
Ok(Kind::SnapshotCreation)
|
2022-10-17 18:57:23 +02:00
|
|
|
} else {
|
2023-01-16 16:59:26 +01:00
|
|
|
Err(ParseTaskKindError(kind.to_owned()))
|
2022-10-12 00:43:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 16:59:26 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ParseTaskKindError(pub String);
|
|
|
|
impl fmt::Display for ParseTaskKindError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"`{}` is not a valid task type. Available types are {}.",
|
|
|
|
self.0,
|
|
|
|
enum_iterator::all::<Kind>()
|
|
|
|
.map(|k| format!(
|
|
|
|
"`{}`",
|
|
|
|
// by default serde is going to insert `"` around the value.
|
|
|
|
serde_json::to_string(&k).unwrap().trim_matches('"')
|
|
|
|
))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::error::Error for ParseTaskKindError {}
|
|
|
|
|
2022-10-26 15:14:46 +02:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
|
2022-10-12 00:43:24 +02:00
|
|
|
pub enum Details {
|
2024-05-08 23:37:57 +02:00
|
|
|
DocumentAdditionOrUpdate {
|
|
|
|
received_documents: u64,
|
|
|
|
indexed_documents: Option<u64>,
|
|
|
|
},
|
|
|
|
DocumentEdition {
|
|
|
|
edited_documents: Option<u64>,
|
|
|
|
original_filter: Option<String>,
|
2024-05-10 20:08:05 +02:00
|
|
|
context: Option<Object>,
|
2024-05-10 19:42:44 +02:00
|
|
|
function: String,
|
2024-05-08 23:37:57 +02:00
|
|
|
},
|
|
|
|
SettingsUpdate {
|
|
|
|
settings: Box<Settings<Unchecked>>,
|
|
|
|
},
|
|
|
|
IndexInfo {
|
|
|
|
primary_key: Option<String>,
|
|
|
|
},
|
|
|
|
DocumentDeletion {
|
|
|
|
provided_ids: usize,
|
|
|
|
deleted_documents: Option<u64>,
|
|
|
|
},
|
|
|
|
DocumentDeletionByFilter {
|
|
|
|
original_filter: String,
|
|
|
|
deleted_documents: Option<u64>,
|
|
|
|
},
|
|
|
|
ClearAll {
|
|
|
|
deleted_documents: Option<u64>,
|
|
|
|
},
|
|
|
|
TaskCancelation {
|
|
|
|
matched_tasks: u64,
|
|
|
|
canceled_tasks: Option<u64>,
|
|
|
|
original_filter: String,
|
|
|
|
},
|
|
|
|
TaskDeletion {
|
|
|
|
matched_tasks: u64,
|
|
|
|
deleted_tasks: Option<u64>,
|
|
|
|
original_filter: String,
|
|
|
|
},
|
|
|
|
Dump {
|
|
|
|
dump_uid: Option<String>,
|
|
|
|
},
|
|
|
|
IndexSwap {
|
|
|
|
swaps: Vec<IndexSwap>,
|
|
|
|
},
|
2022-10-12 00:43:24 +02:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:27:41 +01:00
|
|
|
impl Details {
|
|
|
|
pub fn to_failed(&self) -> Self {
|
|
|
|
let mut details = self.clone();
|
|
|
|
match &mut details {
|
|
|
|
Self::DocumentAdditionOrUpdate { indexed_documents, .. } => {
|
|
|
|
*indexed_documents = Some(0)
|
|
|
|
}
|
2024-05-08 15:26:21 +02:00
|
|
|
Self::DocumentEdition { edited_documents, .. } => *edited_documents = Some(0),
|
2022-11-28 16:27:41 +01:00
|
|
|
Self::DocumentDeletion { deleted_documents, .. } => *deleted_documents = Some(0),
|
2023-03-07 10:02:04 +01:00
|
|
|
Self::DocumentDeletionByFilter { deleted_documents, .. } => {
|
|
|
|
*deleted_documents = Some(0)
|
|
|
|
}
|
2022-11-28 16:27:41 +01:00
|
|
|
Self::ClearAll { deleted_documents } => *deleted_documents = Some(0),
|
|
|
|
Self::TaskCancelation { canceled_tasks, .. } => *canceled_tasks = Some(0),
|
|
|
|
Self::TaskDeletion { deleted_tasks, .. } => *deleted_tasks = Some(0),
|
|
|
|
Self::SettingsUpdate { .. }
|
|
|
|
| Self::IndexInfo { .. }
|
|
|
|
| Self::Dump { .. }
|
|
|
|
| Self::IndexSwap { .. } => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
details
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 00:43:24 +02:00
|
|
|
/// Serialize a `time::Duration` as a best effort ISO 8601 while waiting for
|
|
|
|
/// https://github.com/time-rs/time/issues/378.
|
|
|
|
/// This code is a port of the old code of time that was removed in 0.2.
|
2022-10-12 03:21:25 +02:00
|
|
|
pub fn serialize_duration<S: Serializer>(
|
2022-10-12 00:43:24 +02:00
|
|
|
duration: &Option<Duration>,
|
|
|
|
serializer: S,
|
|
|
|
) -> Result<S::Ok, S::Error> {
|
|
|
|
match duration {
|
|
|
|
Some(duration) => {
|
|
|
|
// technically speaking, negative duration is not valid ISO 8601
|
|
|
|
if duration.is_negative() {
|
|
|
|
return serializer.serialize_none();
|
|
|
|
}
|
|
|
|
|
|
|
|
const SECS_PER_DAY: i64 = Duration::DAY.whole_seconds();
|
|
|
|
let secs = duration.whole_seconds();
|
|
|
|
let days = secs / SECS_PER_DAY;
|
|
|
|
let secs = secs - days * SECS_PER_DAY;
|
|
|
|
let hasdate = days != 0;
|
|
|
|
let nanos = duration.subsec_nanoseconds();
|
|
|
|
let hastime = (secs != 0 || nanos != 0) || !hasdate;
|
|
|
|
|
|
|
|
// all the following unwrap can't fail
|
|
|
|
let mut res = String::new();
|
|
|
|
write!(&mut res, "P").unwrap();
|
|
|
|
|
|
|
|
if hasdate {
|
|
|
|
write!(&mut res, "{}D", days).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
const NANOS_PER_MILLI: i32 = Duration::MILLISECOND.subsec_nanoseconds();
|
|
|
|
const NANOS_PER_MICRO: i32 = Duration::MICROSECOND.subsec_nanoseconds();
|
|
|
|
|
|
|
|
if hastime {
|
|
|
|
if nanos == 0 {
|
|
|
|
write!(&mut res, "T{}S", secs).unwrap();
|
|
|
|
} else if nanos % NANOS_PER_MILLI == 0 {
|
|
|
|
write!(&mut res, "T{}.{:03}S", secs, nanos / NANOS_PER_MILLI).unwrap();
|
|
|
|
} else if nanos % NANOS_PER_MICRO == 0 {
|
|
|
|
write!(&mut res, "T{}.{:06}S", secs, nanos / NANOS_PER_MICRO).unwrap();
|
|
|
|
} else {
|
|
|
|
write!(&mut res, "T{}.{:09}S", secs, nanos).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serializer.serialize_str(&res)
|
|
|
|
}
|
|
|
|
None => serializer.serialize_none(),
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 03:21:25 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Details;
|
2022-10-20 18:00:07 +02:00
|
|
|
use crate::heed::types::SerdeJson;
|
|
|
|
use crate::heed::{BytesDecode, BytesEncode};
|
2022-10-12 03:21:25 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bad_deser() {
|
2022-10-13 11:09:00 +02:00
|
|
|
let details = Details::TaskDeletion {
|
2022-10-12 03:21:25 +02:00
|
|
|
matched_tasks: 1,
|
|
|
|
deleted_tasks: None,
|
2022-11-28 16:27:41 +01:00
|
|
|
original_filter: "hello".to_owned(),
|
2022-10-12 03:21:25 +02:00
|
|
|
};
|
|
|
|
let serialised = SerdeJson::<Details>::bytes_encode(&details).unwrap();
|
|
|
|
let deserialised = SerdeJson::<Details>::bytes_decode(&serialised).unwrap();
|
2022-11-28 16:27:41 +01:00
|
|
|
meili_snap::snapshot!(format!("{:?}", details), @r###"TaskDeletion { matched_tasks: 1, deleted_tasks: None, original_filter: "hello" }"###);
|
|
|
|
meili_snap::snapshot!(format!("{:?}", deserialised), @r###"TaskDeletion { matched_tasks: 1, deleted_tasks: None, original_filter: "hello" }"###);
|
2022-10-12 03:21:25 +02:00
|
|
|
}
|
|
|
|
}
|