use crate::cluster::{Cluster, Node};
use crate::errors::{Error, Result};
use crate::task::{Status, Task};
use crate::{AdminPolicy, Version};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct DropIndexTask {
cluster: Arc<Cluster>,
namespace: String,
index_name: String,
}
static SUCCESS_PATTERN: &str = "false";
impl DropIndexTask {
pub const fn new(cluster: Arc<Cluster>, namespace: String, index_name: String) -> Self {
DropIndexTask {
cluster,
namespace,
index_name,
}
}
fn build_command(node: &Arc<Node>, namespace: &str, index_name: &str) -> String {
let node_version = node.version();
if node_version >= &Version::new(8, 1, 0, 0) {
format!("sindex-exists:namespace={namespace};indexname={index_name}")
} else {
format!("sindex-exists:ns={namespace};indexname={index_name}")
}
}
fn parse_response(response: &str) -> Status {
if response.to_lowercase() == SUCCESS_PATTERN {
Status::Complete
} else {
Status::InProgress
}
}
}
#[async_trait::async_trait]
impl Task for DropIndexTask {
async fn query_status(&self) -> Result<Status> {
let nodes = self.cluster.nodes();
if nodes.is_empty() {
return Err(Error::Connection("No connected node".to_string()));
}
let admin_policy = AdminPolicy { timeout: 3_000 };
for node in &nodes {
let command = &DropIndexTask::build_command(node, &self.namespace, &self.index_name);
let response = node.info(&admin_policy, &[&command[..]]).await?;
if !response.contains_key(command) {
return Ok(Status::NotFound);
}
match DropIndexTask::parse_response(&response[command]) {
Status::Complete => {}
status => return Ok(status),
}
}
Ok(Status::Complete)
}
}