2022-10-24 14:49:39 +02:00
|
|
|
use actix_web::web::Data;
|
2022-11-28 16:27:41 +01:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
2023-02-14 13:12:42 +01:00
|
|
|
use deserr::actix_web::AwebJson;
|
2023-02-13 18:45:13 +01:00
|
|
|
use deserr::Deserr;
|
2022-10-24 14:49:39 +02:00
|
|
|
use index_scheduler::IndexScheduler;
|
2023-01-16 16:59:26 +01:00
|
|
|
use meilisearch_types::deserr::DeserrJsonError;
|
2023-01-11 12:33:56 +01:00
|
|
|
use meilisearch_types::error::deserr_codes::InvalidSwapIndexes;
|
2023-01-16 16:59:26 +01:00
|
|
|
use meilisearch_types::error::ResponseError;
|
2023-01-17 13:51:07 +01:00
|
|
|
use meilisearch_types::index_uid::IndexUid;
|
2022-10-26 12:57:29 +02:00
|
|
|
use meilisearch_types::tasks::{IndexSwap, KindWithContent};
|
2022-11-28 16:27:41 +01:00
|
|
|
use serde_json::json;
|
2022-10-24 14:49:39 +02:00
|
|
|
|
2024-02-21 11:21:26 +01:00
|
|
|
use super::{get_task_id, is_dry_run, SummarizedTaskView};
|
2022-11-28 16:27:41 +01:00
|
|
|
use crate::analytics::Analytics;
|
2022-10-24 17:51:30 +02:00
|
|
|
use crate::error::MeilisearchHttpError;
|
2022-10-26 13:43:57 +02:00
|
|
|
use crate::extractors::authentication::policies::*;
|
|
|
|
use crate::extractors::authentication::{AuthenticationError, GuardedData};
|
2022-10-24 14:49:39 +02:00
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
2024-02-20 11:24:44 +01:00
|
|
|
use crate::Opt;
|
2022-10-24 14:49:39 +02:00
|
|
|
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(web::resource("").route(web::post().to(SeqHandler(swap_indexes))));
|
|
|
|
}
|
2022-12-14 13:00:43 +01:00
|
|
|
|
2023-02-13 18:45:13 +01:00
|
|
|
#[derive(Deserr, Debug, Clone, PartialEq, Eq)]
|
2023-01-12 13:55:53 +01:00
|
|
|
#[deserr(error = DeserrJsonError, rename_all = camelCase, deny_unknown_fields)]
|
2022-10-24 14:49:39 +02:00
|
|
|
pub struct SwapIndexesPayload {
|
2023-01-16 16:59:26 +01:00
|
|
|
#[deserr(error = DeserrJsonError<InvalidSwapIndexes>, missing_field_error = DeserrJsonError::missing_swap_indexes)]
|
2023-01-17 13:51:07 +01:00
|
|
|
indexes: Vec<IndexUid>,
|
2022-10-24 14:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn swap_indexes(
|
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_SWAP }>, Data<IndexScheduler>>,
|
2023-02-14 13:12:42 +01:00
|
|
|
params: AwebJson<Vec<SwapIndexesPayload>, DeserrJsonError>,
|
2022-11-28 16:27:41 +01:00
|
|
|
req: HttpRequest,
|
2024-02-20 11:24:44 +01:00
|
|
|
opt: web::Data<Opt>,
|
2022-11-28 16:27:41 +01:00
|
|
|
analytics: web::Data<dyn Analytics>,
|
2022-10-24 14:49:39 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-12-14 13:00:43 +01:00
|
|
|
let params = params.into_inner();
|
2022-11-28 16:27:41 +01:00
|
|
|
analytics.publish(
|
|
|
|
"Indexes Swapped".to_string(),
|
|
|
|
json!({
|
|
|
|
"swap_operation_number": params.len(),
|
|
|
|
}),
|
|
|
|
Some(&req),
|
|
|
|
);
|
2023-02-20 09:25:29 +01:00
|
|
|
let filters = index_scheduler.filters();
|
2022-10-24 14:49:39 +02:00
|
|
|
|
|
|
|
let mut swaps = vec![];
|
2022-12-14 13:00:43 +01:00
|
|
|
for SwapIndexesPayload { indexes } in params.into_iter() {
|
2023-01-12 13:55:53 +01:00
|
|
|
// TODO: switch to deserr
|
2022-10-26 12:57:29 +02:00
|
|
|
let (lhs, rhs) = match indexes.as_slice() {
|
|
|
|
[lhs, rhs] => (lhs, rhs),
|
|
|
|
_ => {
|
|
|
|
return Err(MeilisearchHttpError::SwapIndexPayloadWrongLength(indexes).into());
|
|
|
|
}
|
|
|
|
};
|
2023-02-20 09:25:29 +01:00
|
|
|
if !filters.is_index_authorized(lhs) || !filters.is_index_authorized(rhs) {
|
2022-10-27 09:41:32 +02:00
|
|
|
return Err(AuthenticationError::InvalidToken.into());
|
2022-10-24 14:49:39 +02:00
|
|
|
}
|
2023-01-17 13:51:07 +01:00
|
|
|
swaps.push(IndexSwap { indexes: (lhs.to_string(), rhs.to_string()) });
|
2022-10-26 13:30:37 +02:00
|
|
|
}
|
2022-10-24 14:49:39 +02:00
|
|
|
|
|
|
|
let task = KindWithContent::IndexSwap { swaps };
|
2024-02-20 11:24:44 +01:00
|
|
|
let uid = get_task_id(&req, &opt)?;
|
2024-02-21 11:21:26 +01:00
|
|
|
let dry_run = is_dry_run(&req, &opt)?;
|
2023-09-07 11:37:02 +02:00
|
|
|
let task: SummarizedTaskView =
|
2024-02-21 11:21:26 +01:00
|
|
|
tokio::task::spawn_blocking(move || index_scheduler.register(task, uid, dry_run))
|
|
|
|
.await??
|
|
|
|
.into();
|
2022-10-27 00:56:34 +02:00
|
|
|
Ok(HttpResponse::Accepted().json(task))
|
2022-10-24 14:49:39 +02:00
|
|
|
}
|