mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-06-28 15:22:59 +02:00
194 lines
5.6 KiB
Rust
194 lines
5.6 KiB
Rust
use serde::Deserialize;
|
|
use time::OffsetDateTime;
|
|
use uuid::Uuid;
|
|
|
|
use super::{
|
|
errors::ResponseError,
|
|
meta::IndexUid,
|
|
settings::{Settings, Unchecked},
|
|
};
|
|
|
|
pub type TaskId = u32;
|
|
pub type BatchId = u32;
|
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq)]
|
|
#[cfg_attr(test, derive(serde::Serialize))]
|
|
pub struct Task {
|
|
pub id: TaskId,
|
|
pub index_uid: IndexUid,
|
|
pub content: TaskContent,
|
|
pub events: Vec<TaskEvent>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq)]
|
|
#[cfg_attr(test, derive(serde::Serialize))]
|
|
#[allow(clippy::large_enum_variant)]
|
|
pub enum TaskContent {
|
|
DocumentAddition {
|
|
content_uuid: Uuid,
|
|
merge_strategy: IndexDocumentsMethod,
|
|
primary_key: Option<String>,
|
|
documents_count: usize,
|
|
allow_index_creation: bool,
|
|
},
|
|
DocumentDeletion(DocumentDeletion),
|
|
SettingsUpdate {
|
|
settings: Settings<Unchecked>,
|
|
/// Indicates whether the task was a deletion
|
|
is_deletion: bool,
|
|
allow_index_creation: bool,
|
|
},
|
|
IndexDeletion,
|
|
IndexCreation {
|
|
primary_key: Option<String>,
|
|
},
|
|
IndexUpdate {
|
|
primary_key: Option<String>,
|
|
},
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize)]
|
|
#[cfg_attr(test, derive(serde::Serialize))]
|
|
pub enum IndexDocumentsMethod {
|
|
/// Replace the previous document with the new one,
|
|
/// removing all the already known attributes.
|
|
ReplaceDocuments,
|
|
|
|
/// Merge the previous version of the document with the new version,
|
|
/// replacing old attributes values with the new ones and add the new attributes.
|
|
UpdateDocuments,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
|
|
#[cfg_attr(test, derive(serde::Serialize))]
|
|
pub enum DocumentDeletion {
|
|
Clear,
|
|
Ids(Vec<String>),
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
|
|
#[cfg_attr(test, derive(serde::Serialize))]
|
|
pub enum TaskEvent {
|
|
Created(#[serde(with = "time::serde::rfc3339")] OffsetDateTime),
|
|
Batched {
|
|
#[serde(with = "time::serde::rfc3339")]
|
|
timestamp: OffsetDateTime,
|
|
batch_id: BatchId,
|
|
},
|
|
Processing(#[serde(with = "time::serde::rfc3339")] OffsetDateTime),
|
|
Succeded {
|
|
result: TaskResult,
|
|
#[serde(with = "time::serde::rfc3339")]
|
|
timestamp: OffsetDateTime,
|
|
},
|
|
Failed {
|
|
error: ResponseError,
|
|
#[serde(with = "time::serde::rfc3339")]
|
|
timestamp: OffsetDateTime,
|
|
},
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
|
|
#[cfg_attr(test, derive(serde::Serialize))]
|
|
pub enum TaskResult {
|
|
DocumentAddition { indexed_documents: u64 },
|
|
DocumentDeletion { deleted_documents: u64 },
|
|
ClearAll { deleted_documents: u64 },
|
|
Other,
|
|
}
|
|
|
|
impl Task {
|
|
/// Return true when a task is finished.
|
|
/// A task is finished when its last state is either `Succeeded` or `Failed`.
|
|
pub fn is_finished(&self) -> bool {
|
|
self.events.last().map_or(false, |event| {
|
|
matches!(event, TaskEvent::Succeded { .. } | TaskEvent::Failed { .. })
|
|
})
|
|
}
|
|
|
|
/// Return the content_uuid of the `Task` if there is one.
|
|
pub fn get_content_uuid(&self) -> Option<Uuid> {
|
|
match self {
|
|
Task {
|
|
content: TaskContent::DocumentAddition { content_uuid, .. },
|
|
..
|
|
} => Some(*content_uuid),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl IndexUid {
|
|
pub fn into_inner(self) -> String {
|
|
self.0
|
|
}
|
|
|
|
/// Return a reference over the inner str.
|
|
pub fn as_str(&self) -> &str {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl std::ops::Deref for IndexUid {
|
|
type Target = str;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
/// 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.
|
|
#[cfg(test)]
|
|
fn serialize_duration<S: serde::Serializer>(
|
|
duration: &Option<time::Duration>,
|
|
serializer: S,
|
|
) -> Result<S::Ok, S::Error> {
|
|
use std::fmt::Write;
|
|
use time::Duration;
|
|
|
|
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(),
|
|
}
|
|
}
|