use std::sync::Arc;
use std::time::Duration;
use crate::cluster::{Cluster, Node};
use crate::commands::{Command, ReadCommand, SingleCommand};
use crate::errors::Result;
use crate::net::Connection;
use crate::operations::Operation;
use crate::policy::WritePolicy;
use crate::{Bins, Key};
pub struct OperateCommand<'a> {
pub read_command: ReadCommand<'a>,
policy: &'a WritePolicy,
operations: &'a [Operation<'a>],
}
impl<'a> OperateCommand<'a> {
pub fn new(
policy: &'a WritePolicy,
cluster: Arc<Cluster>,
key: &'a Key,
operations: &'a [Operation<'a>],
) -> Self {
OperateCommand {
read_command: ReadCommand::new(&policy.base_policy, cluster, key, Bins::All),
policy,
operations,
}
}
pub fn execute(&mut self) -> Result<()> {
SingleCommand::execute(self.policy, self)
}
}
impl<'a> Command for OperateCommand<'a> {
fn write_timeout(&mut self, conn: &mut Connection, timeout: Option<Duration>) -> Result<()> {
conn.buffer.write_timeout(timeout);
Ok(())
}
fn write_buffer(&mut self, conn: &mut Connection) -> Result<()> {
conn.flush()
}
fn prepare_buffer(&mut self, conn: &mut Connection) -> Result<()> {
conn.buffer.set_operate(
self.policy,
self.read_command.single_command.key,
self.operations,
)
}
fn get_node(&self) -> Result<Arc<Node>> {
self.read_command.get_node()
}
fn parse_result(&mut self, conn: &mut Connection) -> Result<()> {
self.read_command.parse_result(conn)
}
}