Skip to content

Add swap indexes for v0.30.0 #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
110 changes: 109 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
tasks::{Task, TasksCancelQuery, TasksDeleteQuery, TasksResults, TasksSearchQuery},
utils::async_sleep,
};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::{collections::HashMap, time::Duration};
use time::OffsetDateTime;
Expand All @@ -19,6 +19,11 @@ pub struct Client {
pub(crate) api_key: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SwapIndexes {
pub indexes: (String, String),
}

impl Client {
/// Create a client using the specified server.
/// Don't put a '/' at the end of the host.
Expand Down Expand Up @@ -329,6 +334,56 @@ impl Client {
self.list_all_indexes_raw_with(indexes_query).await
}

/// Swaps a list of two [Index]'es.
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*};
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # futures::executor::block_on(async move {
/// // Create the client
/// let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
///
/// let task_index_1 = client.create_index("swap_index_1", None).await.unwrap();
/// let task_index_2 = client.create_index("swap_index_2", None).await.unwrap();
///
/// // Wait for the task to complete
/// task_index_2.wait_for_completion(&client, None, None).await.unwrap();
///
/// let task = client
/// .swap_indexes([&SwapIndexes {
/// indexes: (
/// "swap_index_1".to_string(),
/// "swap_index_2".to_string(),
/// ),
/// }])
/// .await
/// .unwrap();
///
/// # client.index("swap_index_1").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # client.index("swap_index_2").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn swap_indexes(
&self,
indexes: impl IntoIterator<Item = &SwapIndexes>,
) -> Result<TaskInfo, Error> {
request::<(), Vec<&SwapIndexes>, TaskInfo>(
&format!("{}/swap-indexes", self.host),
&self.api_key,
Method::Post {
query: (),
body: indexes.into_iter().collect(),
},
202,
)
.await
}

/// Get stats of all indexes.
///
/// # Example
Expand Down Expand Up @@ -962,6 +1017,59 @@ mod tests {
use std::mem;
use time::OffsetDateTime;

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Document {
id: String,
}

#[meilisearch_test]
async fn test_swapping_two_indexes(client: Client) {
let index_1 = client.index("test_swapping_two_indexes_1");
let index_2 = client.index("test_swapping_two_indexes_2");

let t0 = index_1
.add_documents(
&[Document {
id: "1".to_string(),
}],
None,
)
.await
.unwrap();

index_2
.add_documents(
&[Document {
id: "2".to_string(),
}],
None,
)
.await
.unwrap();

t0.wait_for_completion(&client, None, None).await.unwrap();

let task = client
.swap_indexes([&SwapIndexes {
indexes: (
"test_swapping_two_indexes_1".to_string(),
"test_swapping_two_indexes_2".to_string(),
),
}])
.await
.unwrap();
task.wait_for_completion(&client, None, None).await.unwrap();

let document = index_1.get_document("2").await.unwrap();

assert_eq!(
Document {
id: "2".to_string()
},
document
);
}

#[meilisearch_test]
async fn test_methods_has_qualified_version_as_header() {
let mock_server_url = &mockito::server_url();
Expand Down
11 changes: 10 additions & 1 deletion src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use time::OffsetDateTime;

use crate::{
client::Client, errors::Error, errors::MeilisearchError, indexes::Index, settings::Settings,
task_info::TaskInfo,
task_info::TaskInfo, SwapIndexes,
};

#[derive(Debug, Clone, Deserialize)]
Expand Down Expand Up @@ -32,6 +32,9 @@ pub enum TaskType {
DumpCreation {
details: Option<DumpCreation>,
},
IndexSwap {
details: Option<IndexSwap>,
},
TaskCancelation {
details: Option<TaskCancelation>,
},
Expand Down Expand Up @@ -85,6 +88,12 @@ pub struct DumpCreation {
pub dump_uid: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IndexSwap {
pub swaps: Vec<SwapIndexes>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TaskCancelation {
Expand Down