use crate::error::Result;
use crate::executor::CommandExecutor;
use crate::value;
use async_trait::async_trait;
use bytes::Bytes;
use redis::{Cmd, ToRedisArgs, Value};
#[async_trait]
pub trait FtCommands: CommandExecutor {
async fn ft_create<I: ToRedisArgs + Send, A: ToRedisArgs + Send + Sync>(
&self,
index: I,
args: &[A],
) -> Result<()> {
let mut cmd = Cmd::new();
cmd.arg("FT.CREATE").arg(index);
for a in args {
cmd.arg(a);
}
value::to_unit(self.execute_command(cmd, None).await?)
}
async fn ft_dropindex<I: ToRedisArgs + Send>(&self, index: I, delete_docs: bool) -> Result<()> {
let mut cmd = Cmd::new();
cmd.arg("FT.DROPINDEX").arg(index);
if delete_docs {
cmd.arg("DD");
}
value::to_unit(self.execute_command(cmd, None).await?)
}
async fn ft_info<I: ToRedisArgs + Send>(&self, index: I) -> Result<Value> {
let mut cmd = Cmd::new();
cmd.arg("FT.INFO").arg(index);
self.execute_command(cmd, None).await
}
async fn ft_list(&self) -> Result<Vec<Bytes>> {
let mut cmd = Cmd::new();
cmd.arg("FT._LIST");
match self.execute_command(cmd, None).await? {
Value::Array(items) => items.into_iter().map(value::to_bytes).collect(),
Value::Set(items) => items.into_iter().map(value::to_bytes).collect(),
Value::Nil => Ok(Vec::new()),
other => Ok(vec![value::to_bytes(other)?]),
}
}
async fn ft_search<
I: ToRedisArgs + Send,
Q: ToRedisArgs + Send,
A: ToRedisArgs + Send + Sync,
>(
&self,
index: I,
query: Q,
args: &[A],
) -> Result<Value> {
let mut cmd = Cmd::new();
cmd.arg("FT.SEARCH").arg(index).arg(query);
for a in args {
cmd.arg(a);
}
self.execute_command(cmd, None).await
}
async fn ft_aggregate<
I: ToRedisArgs + Send,
Q: ToRedisArgs + Send,
A: ToRedisArgs + Send + Sync,
>(
&self,
index: I,
query: Q,
args: &[A],
) -> Result<Value> {
let mut cmd = Cmd::new();
cmd.arg("FT.AGGREGATE").arg(index).arg(query);
for a in args {
cmd.arg(a);
}
self.execute_command(cmd, None).await
}
async fn ft_explain<I: ToRedisArgs + Send, Q: ToRedisArgs + Send>(
&self,
index: I,
query: Q,
) -> Result<Bytes> {
let mut cmd = Cmd::new();
cmd.arg("FT.EXPLAIN").arg(index).arg(query);
value::to_bytes(self.execute_command(cmd, None).await?)
}
async fn ft_explaincli<I: ToRedisArgs + Send, Q: ToRedisArgs + Send>(
&self,
index: I,
query: Q,
) -> Result<Value> {
let mut cmd = Cmd::new();
cmd.arg("FT.EXPLAINCLI").arg(index).arg(query);
self.execute_command(cmd, None).await
}
async fn ft_aliasadd<A: ToRedisArgs + Send, I: ToRedisArgs + Send>(
&self,
alias: A,
index: I,
) -> Result<()> {
let mut cmd = Cmd::new();
cmd.arg("FT.ALIASADD").arg(alias).arg(index);
value::to_unit(self.execute_command(cmd, None).await?)
}
async fn ft_aliasdel<A: ToRedisArgs + Send>(&self, alias: A) -> Result<()> {
let mut cmd = Cmd::new();
cmd.arg("FT.ALIASDEL").arg(alias);
value::to_unit(self.execute_command(cmd, None).await?)
}
async fn ft_aliasupdate<A: ToRedisArgs + Send, I: ToRedisArgs + Send>(
&self,
alias: A,
index: I,
) -> Result<()> {
let mut cmd = Cmd::new();
cmd.arg("FT.ALIASUPDATE").arg(alias).arg(index);
value::to_unit(self.execute_command(cmd, None).await?)
}
async fn ft_aliaslist(&self) -> Result<Value> {
let mut cmd = Cmd::new();
cmd.arg("FT._ALIASLIST");
self.execute_command(cmd, None).await
}
async fn ft_profile<I: ToRedisArgs + Send, A: ToRedisArgs + Send + Sync>(
&self,
index: I,
query_type: &str,
limited: bool,
args: &[A],
) -> Result<Value> {
let mut cmd = Cmd::new();
cmd.arg("FT.PROFILE").arg(index).arg(query_type);
if limited {
cmd.arg("LIMITED");
}
cmd.arg("QUERY");
for a in args {
cmd.arg(a);
}
self.execute_command(cmd, None).await
}
}
impl<T: CommandExecutor + ?Sized> FtCommands for T {}