use std::str;
use std::sync::Arc;
use crate::cluster::{Cluster, Node};
use crate::commands::{Command, ReadCommand, SingleCommand};
use crate::errors::Result;
use crate::net::Connection;
use crate::policy::WritePolicy;
use crate::{Bins, Key, Policy, Value};
pub struct ExecuteUDFCommand<'a> {
pub read_command: ReadCommand<'a>,
policy: &'a WritePolicy,
package_name: &'a str,
function_name: &'a str,
args: Option<&'a [Value]>,
}
impl<'a> ExecuteUDFCommand<'a> {
pub fn new(
policy: &'a WritePolicy,
cluster: Arc<Cluster>,
key: &'a Key,
package_name: &'a str,
function_name: &'a str,
args: Option<&'a [Value]>,
) -> Self {
ExecuteUDFCommand {
read_command: ReadCommand::new(
&policy.base_policy,
cluster,
key,
Bins::All,
crate::policy::Replica::Master,
),
policy,
package_name,
function_name,
args,
}
}
pub async fn execute(&mut self) -> Result<()> {
SingleCommand::execute(self.policy, self).await
}
}
#[async_trait::async_trait]
impl Command for ExecuteUDFCommand<'_> {
async fn write_timeout(&mut self, conn: &mut Connection) -> Result<()> {
conn.buffer.write_timeout(self.policy.server_timeout());
Ok(())
}
async fn write_buffer(&mut self, conn: &mut Connection) -> Result<()> {
conn.flush().await
}
async fn prepare_buffer(&mut self, conn: &mut Connection) -> Result<()> {
conn.buffer.set_udf(
self.policy,
self.read_command.single_command.key,
self.package_name,
self.function_name,
self.args,
)
}
fn can_retry(&mut self) -> bool {
true
}
fn can_recover_connection(&mut self) -> bool {
true
}
async fn get_node(&mut self) -> Result<Arc<Node>> {
self.read_command.get_node().await
}
fn hint(&self) -> u8 {
self.read_command.single_command.hint()
}
async fn parse_result(&mut self, conn: &mut Connection) -> Result<()> {
self.read_command.parse_result(conn).await
}
}