2022-10-24 20:49:39 +08:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
use index_scheduler::IndexScheduler;
|
|
|
|
use meilisearch_types::error::ResponseError;
|
2022-10-26 18:57:29 +08:00
|
|
|
use meilisearch_types::tasks::{IndexSwap, KindWithContent};
|
2022-10-24 20:49:39 +08:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
2022-10-24 23:51:30 +08:00
|
|
|
use crate::error::MeilisearchHttpError;
|
2022-10-24 20:49:39 +08:00
|
|
|
use crate::extractors::authentication::GuardedData;
|
2022-10-26 19:30:37 +08:00
|
|
|
use crate::extractors::authentication::{policies::*, AuthenticationError};
|
2022-10-24 20:49:39 +08:00
|
|
|
use crate::extractors::sequential_extractor::SeqHandler;
|
|
|
|
use crate::routes::tasks::TaskView;
|
|
|
|
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(web::resource("").route(web::post().to(SeqHandler(swap_indexes))));
|
|
|
|
}
|
|
|
|
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
pub struct SwapIndexesPayload {
|
2022-10-26 18:57:29 +08:00
|
|
|
indexes: Vec<String>,
|
2022-10-24 20:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn swap_indexes(
|
|
|
|
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_SWAP }>, Data<IndexScheduler>>,
|
|
|
|
params: web::Json<Vec<SwapIndexesPayload>>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let search_rules = &index_scheduler.filters().search_rules;
|
|
|
|
|
|
|
|
let mut swaps = vec![];
|
|
|
|
let mut indexes_set = HashSet::<String>::default();
|
2022-10-26 19:30:37 +08:00
|
|
|
let mut unauthorized_indexes = HashSet::new();
|
2022-10-24 20:49:39 +08:00
|
|
|
let mut unknown_indexes = HashSet::new();
|
|
|
|
let mut duplicate_indexes = HashSet::new();
|
2022-10-26 18:57:29 +08:00
|
|
|
for SwapIndexesPayload { indexes } in params.into_inner().into_iter() {
|
|
|
|
let (lhs, rhs) = match indexes.as_slice() {
|
|
|
|
[lhs, rhs] => (lhs, rhs),
|
|
|
|
_ => {
|
|
|
|
return Err(MeilisearchHttpError::SwapIndexPayloadWrongLength(indexes).into());
|
|
|
|
}
|
|
|
|
};
|
2022-10-24 20:49:39 +08:00
|
|
|
if !search_rules.is_index_authorized(&lhs) {
|
2022-10-26 19:30:37 +08:00
|
|
|
unauthorized_indexes.insert(lhs.clone());
|
2022-10-24 20:49:39 +08:00
|
|
|
}
|
|
|
|
if !search_rules.is_index_authorized(&rhs) {
|
2022-10-26 19:30:37 +08:00
|
|
|
unauthorized_indexes.insert(rhs.clone());
|
2022-10-24 20:49:39 +08:00
|
|
|
}
|
2022-10-26 18:57:29 +08:00
|
|
|
match index_scheduler.index(&lhs) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(index_scheduler::Error::IndexNotFound(_)) => {
|
|
|
|
unknown_indexes.insert(lhs.clone());
|
|
|
|
}
|
|
|
|
Err(e) => return Err(e.into()),
|
|
|
|
}
|
|
|
|
match index_scheduler.index(&rhs) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(index_scheduler::Error::IndexNotFound(_)) => {
|
|
|
|
unknown_indexes.insert(rhs.clone());
|
|
|
|
}
|
|
|
|
Err(e) => return Err(e.into()),
|
|
|
|
}
|
2022-10-24 20:49:39 +08:00
|
|
|
|
2022-10-26 18:57:29 +08:00
|
|
|
swaps.push(IndexSwap { indexes: (lhs.clone(), rhs.clone()) });
|
2022-10-24 20:49:39 +08:00
|
|
|
|
|
|
|
let is_unique_index_lhs = indexes_set.insert(lhs.clone());
|
|
|
|
if !is_unique_index_lhs {
|
2022-10-26 18:57:29 +08:00
|
|
|
duplicate_indexes.insert(lhs.clone());
|
2022-10-24 20:49:39 +08:00
|
|
|
}
|
|
|
|
let is_unique_index_rhs = indexes_set.insert(rhs.clone());
|
|
|
|
if !is_unique_index_rhs {
|
2022-10-26 18:57:29 +08:00
|
|
|
duplicate_indexes.insert(rhs.clone());
|
2022-10-24 20:49:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !duplicate_indexes.is_empty() {
|
2022-10-24 23:51:30 +08:00
|
|
|
let duplicate_indexes: Vec<_> = duplicate_indexes.into_iter().collect();
|
|
|
|
if let [index] = duplicate_indexes.as_slice() {
|
|
|
|
return Err(MeilisearchHttpError::SwapDuplicateIndexFound(index.clone()).into());
|
|
|
|
} else {
|
|
|
|
return Err(MeilisearchHttpError::SwapDuplicateIndexesFound(duplicate_indexes).into());
|
2022-10-24 20:49:39 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-26 19:30:37 +08:00
|
|
|
if !unauthorized_indexes.is_empty() {
|
|
|
|
return Err(AuthenticationError::InvalidToken.into());
|
|
|
|
}
|
2022-10-24 20:49:39 +08:00
|
|
|
if !unknown_indexes.is_empty() {
|
2022-10-24 23:51:30 +08:00
|
|
|
let unknown_indexes: Vec<_> = unknown_indexes.into_iter().collect();
|
|
|
|
if let [index] = unknown_indexes.as_slice() {
|
|
|
|
return Err(index_scheduler::Error::IndexNotFound(index.clone()).into());
|
|
|
|
} else {
|
|
|
|
return Err(MeilisearchHttpError::IndexesNotFound(unknown_indexes).into());
|
|
|
|
}
|
2022-10-24 20:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let task = KindWithContent::IndexSwap { swaps };
|
|
|
|
|
|
|
|
let task = index_scheduler.register(task)?;
|
|
|
|
let task_view = TaskView::from_task(&task);
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(task_view))
|
|
|
|
}
|