use std::sync::Arc;
use crate::cluster::{Cluster, Node};
use crate::commands::{Command, SingleCommand};
use crate::errors::{Error, Result};
use crate::net::Connection;
use crate::operations::OperationType;
use crate::policy::{Policy, WritePolicy};
use crate::{Bin, Key, ResultCode};
pub struct WriteCommand<'a> {
single_command: SingleCommand<'a>,
policy: &'a WritePolicy,
bins: &'a [Bin],
operation: OperationType,
}
impl<'a> WriteCommand<'a> {
pub fn new(
policy: &'a WritePolicy,
cluster: Arc<Cluster>,
key: &'a Key,
bins: &'a [Bin],
operation: OperationType,
) -> Self {
WriteCommand {
single_command: SingleCommand::new(cluster, key, crate::policy::Replica::Sequence),
bins,
policy,
operation,
}
}
pub async fn execute(&mut self) -> Result<()> {
SingleCommand::execute(self.policy, self).await
}
}
#[async_trait::async_trait]
impl Command for WriteCommand<'_> {
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_write(
self.policy,
self.operation,
self.single_command.key,
self.bins,
)
}
async fn get_node(&mut self) -> Result<Arc<Node>> {
self.single_command.get_node()
}
fn hint(&self) -> u8 {
self.single_command.hint()
}
fn can_retry(&mut self) -> bool {
true
}
fn can_recover_connection(&mut self) -> bool {
true
}
async fn parse_result(&mut self, conn: &mut Connection) -> Result<()> {
if let Err(err) = conn.read_header().await {
warn!("Parse result error: {err}");
return Err(err);
}
conn.buffer.reset_offset();
let result_code = ResultCode::from(conn.buffer.read_u8(Some(13)));
if result_code != ResultCode::Ok {
return Err(Error::ServerError(result_code, false, conn.addr.clone()));
}
SingleCommand::empty_socket(conn).await
}
}