MeiliSearch/meilisearch-http/src/index_controller/updates.rs

269 lines
6.1 KiB
Rust
Raw Normal View History

2021-04-22 10:14:29 +02:00
use std::path::{Path, PathBuf};
2021-03-15 18:11:10 +01:00
use chrono::{DateTime, Utc};
2021-04-22 10:14:29 +02:00
use milli::update::{DocumentAdditionResult, IndexDocumentsMethod, UpdateFormat};
2021-03-15 18:11:10 +01:00
use serde::{Deserialize, Serialize};
2021-01-28 14:12:34 +01:00
2021-05-10 17:30:09 +02:00
use crate::index::{Checked, Settings};
2021-04-22 10:14:29 +02:00
pub type UpdateError = String;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UpdateResult {
DocumentsAddition(DocumentAdditionResult),
DocumentDeletion { deleted: u64 },
Other,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum UpdateMeta {
DocumentsAddition {
method: IndexDocumentsMethod,
format: UpdateFormat,
primary_key: Option<String>,
},
ClearDocuments,
DeleteDocuments,
2021-05-10 17:30:09 +02:00
Settings(Settings<Checked>),
2021-04-22 10:14:29 +02:00
}
#[derive(Debug, Serialize, Deserialize, Clone)]
2021-02-18 17:48:37 +01:00
#[serde(rename_all = "camelCase")]
2021-04-22 10:14:29 +02:00
pub struct Enqueued {
2021-01-28 20:55:29 +01:00
pub update_id: u64,
2021-04-22 10:14:29 +02:00
pub meta: UpdateMeta,
2021-01-28 20:55:29 +01:00
pub enqueued_at: DateTime<Utc>,
2021-04-22 10:14:29 +02:00
pub content: Option<PathBuf>,
2021-01-28 14:12:34 +01:00
}
2021-04-22 10:14:29 +02:00
impl Enqueued {
pub fn new(meta: UpdateMeta, update_id: u64, content: Option<PathBuf>) -> Self {
2021-01-28 14:12:34 +01:00
Self {
enqueued_at: Utc::now(),
meta,
update_id,
2021-04-22 10:14:29 +02:00
content,
2021-01-28 14:12:34 +01:00
}
}
2021-04-22 10:14:29 +02:00
pub fn processing(self) -> Processing {
2021-01-28 14:12:34 +01:00
Processing {
from: self,
started_processing_at: Utc::now(),
}
}
2021-04-22 10:14:29 +02:00
pub fn abort(self) -> Aborted {
2021-01-28 14:12:34 +01:00
Aborted {
from: self,
aborted_at: Utc::now(),
}
}
2021-04-22 10:14:29 +02:00
pub fn meta(&self) -> &UpdateMeta {
2021-01-28 14:12:34 +01:00
&self.meta
}
pub fn id(&self) -> u64 {
self.update_id
}
2021-04-22 10:14:29 +02:00
pub fn content_path(&self) -> Option<&Path> {
self.content.as_deref()
}
pub fn content_path_mut(&mut self) -> Option<&mut PathBuf> {
self.content.as_mut()
}
2021-01-28 14:12:34 +01:00
}
2021-04-22 10:14:29 +02:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2021-02-18 22:10:50 +01:00
#[serde(rename_all = "camelCase")]
2021-04-22 10:14:29 +02:00
pub struct Processed {
pub success: UpdateResult,
2021-01-28 20:55:29 +01:00
pub processed_at: DateTime<Utc>,
2021-01-28 14:12:34 +01:00
#[serde(flatten)]
2021-04-22 10:14:29 +02:00
pub from: Processing,
2021-01-28 14:12:34 +01:00
}
2021-04-22 10:14:29 +02:00
impl Processed {
2021-01-28 14:12:34 +01:00
pub fn id(&self) -> u64 {
self.from.id()
}
pub fn content_path(&self) -> Option<&Path> {
self.from.content_path()
}
pub fn content_path_mut(&mut self) -> Option<&mut PathBuf> {
self.from.content_path_mut()
}
2021-01-28 14:12:34 +01:00
}
2021-04-22 10:14:29 +02:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2021-02-18 22:10:50 +01:00
#[serde(rename_all = "camelCase")]
2021-04-22 10:14:29 +02:00
pub struct Processing {
2021-01-28 14:12:34 +01:00
#[serde(flatten)]
2021-04-22 10:14:29 +02:00
pub from: Enqueued,
2021-01-28 20:55:29 +01:00
pub started_processing_at: DateTime<Utc>,
2021-01-28 14:12:34 +01:00
}
2021-04-22 10:14:29 +02:00
impl Processing {
2021-01-28 14:12:34 +01:00
pub fn id(&self) -> u64 {
self.from.id()
}
2021-04-22 10:14:29 +02:00
pub fn meta(&self) -> &UpdateMeta {
2021-01-28 14:12:34 +01:00
self.from.meta()
}
pub fn content_path(&self) -> Option<&Path> {
self.from.content_path()
}
pub fn content_path_mut(&mut self) -> Option<&mut PathBuf> {
self.from.content_path_mut()
}
2021-04-22 10:14:29 +02:00
pub fn process(self, success: UpdateResult) -> Processed {
2021-01-28 14:12:34 +01:00
Processed {
2021-04-22 10:14:29 +02:00
success,
2021-01-28 14:12:34 +01:00
from: self,
processed_at: Utc::now(),
}
}
2021-04-22 10:14:29 +02:00
pub fn fail(self, error: UpdateError) -> Failed {
2021-01-28 14:12:34 +01:00
Failed {
from: self,
error,
failed_at: Utc::now(),
}
}
}
2021-04-22 10:14:29 +02:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2021-02-18 22:10:50 +01:00
#[serde(rename_all = "camelCase")]
2021-04-22 10:14:29 +02:00
pub struct Aborted {
2021-01-28 14:12:34 +01:00
#[serde(flatten)]
2021-04-22 10:14:29 +02:00
from: Enqueued,
2021-01-28 14:12:34 +01:00
aborted_at: DateTime<Utc>,
}
2021-04-22 10:14:29 +02:00
impl Aborted {
2021-01-28 14:12:34 +01:00
pub fn id(&self) -> u64 {
self.from.id()
}
pub fn content_path(&self) -> Option<&Path> {
self.from.content_path()
}
pub fn content_path_mut(&mut self) -> Option<&mut PathBuf> {
self.from.content_path_mut()
}
2021-01-28 14:12:34 +01:00
}
2021-04-22 10:14:29 +02:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2021-02-18 22:10:50 +01:00
#[serde(rename_all = "camelCase")]
2021-04-22 10:14:29 +02:00
pub struct Failed {
2021-01-28 14:12:34 +01:00
#[serde(flatten)]
2021-04-22 10:14:29 +02:00
from: Processing,
error: UpdateError,
2021-01-28 14:12:34 +01:00
failed_at: DateTime<Utc>,
}
2021-04-22 10:14:29 +02:00
impl Failed {
2021-01-28 14:12:34 +01:00
pub fn id(&self) -> u64 {
self.from.id()
}
pub fn content_path(&self) -> Option<&Path> {
self.from.content_path()
}
pub fn content_path_mut(&mut self) -> Option<&mut PathBuf> {
self.from.content_path_mut()
}
2021-01-28 14:12:34 +01:00
}
2021-04-22 10:14:29 +02:00
#[derive(Debug, Serialize, Deserialize)]
2021-02-18 17:48:37 +01:00
#[serde(tag = "status", rename_all = "camelCase")]
2021-04-22 10:14:29 +02:00
pub enum UpdateStatus {
Processing(Processing),
Enqueued(Enqueued),
Processed(Processed),
Aborted(Aborted),
Failed(Failed),
2021-01-28 14:12:34 +01:00
}
2021-04-22 10:14:29 +02:00
impl UpdateStatus {
2021-01-28 14:12:34 +01:00
pub fn id(&self) -> u64 {
match self {
UpdateStatus::Processing(u) => u.id(),
2021-04-07 19:46:36 +02:00
UpdateStatus::Enqueued(u) => u.id(),
2021-01-28 14:12:34 +01:00
UpdateStatus::Processed(u) => u.id(),
UpdateStatus::Aborted(u) => u.id(),
UpdateStatus::Failed(u) => u.id(),
}
}
2021-02-03 17:44:20 +01:00
2021-04-22 10:14:29 +02:00
pub fn processed(&self) -> Option<&Processed> {
2021-02-03 17:44:20 +01:00
match self {
UpdateStatus::Processed(p) => Some(p),
_ => None,
}
}
pub fn content_path(&self) -> Option<&Path> {
match self {
UpdateStatus::Processing(u) => u.content_path(),
UpdateStatus::Processed(u) => u.content_path(),
UpdateStatus::Aborted(u) => u.content_path(),
UpdateStatus::Failed(u) => u.content_path(),
UpdateStatus::Enqueued(u) => u.content_path(),
}
}
pub fn content_path_mut(&mut self) -> Option<&mut PathBuf> {
match self {
UpdateStatus::Processing(u) => u.content_path_mut(),
UpdateStatus::Processed(u) => u.content_path_mut(),
UpdateStatus::Aborted(u) => u.content_path_mut(),
UpdateStatus::Failed(u) => u.content_path_mut(),
UpdateStatus::Enqueued(u) => u.content_path_mut(),
}
}
2021-01-28 14:12:34 +01:00
}
2021-04-22 10:14:29 +02:00
impl From<Enqueued> for UpdateStatus {
fn from(other: Enqueued) -> Self {
2021-04-07 19:46:36 +02:00
Self::Enqueued(other)
2021-01-28 14:12:34 +01:00
}
}
2021-04-22 10:14:29 +02:00
impl From<Aborted> for UpdateStatus {
fn from(other: Aborted) -> Self {
2021-01-28 14:12:34 +01:00
Self::Aborted(other)
}
}
2021-04-22 10:14:29 +02:00
impl From<Processed> for UpdateStatus {
fn from(other: Processed) -> Self {
2021-01-28 14:12:34 +01:00
Self::Processed(other)
}
}
2021-04-22 10:14:29 +02:00
impl From<Processing> for UpdateStatus {
fn from(other: Processing) -> Self {
2021-01-28 14:12:34 +01:00
Self::Processing(other)
}
}
2021-04-22 10:14:29 +02:00
impl From<Failed> for UpdateStatus {
fn from(other: Failed) -> Self {
2021-01-28 14:12:34 +01:00
Self::Failed(other)
}
}