use std::sync::Arc;
use std::time::Duration;
use crate::cluster::{Cluster, Node};
use crate::commands::{buffer, Command, SingleCommand};
use crate::errors::{ErrorKind, Result};
use crate::net::Connection;
use crate::policy::WritePolicy;
use crate::{Key, ResultCode};
pub struct ExistsCommand<'a> {
single_command: SingleCommand<'a>,
policy: &'a WritePolicy,
pub exists: bool,
}
impl<'a> ExistsCommand<'a> {
pub fn new(policy: &'a WritePolicy, cluster: Arc<Cluster>, key: &'a Key) -> Self {
ExistsCommand {
single_command: SingleCommand::new(cluster, key),
policy,
exists: false,
}
}
pub fn execute(&mut self) -> Result<()> {
SingleCommand::execute(self.policy, self)
}
}
impl<'a> Command for ExistsCommand<'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_exists(self.policy, self.single_command.key)
}
fn get_node(&self) -> Result<Arc<Node>> {
self.single_command.get_node()
}
fn parse_result(&mut self, conn: &mut Connection) -> Result<()> {
if let Err(err) = conn.read_buffer(buffer::MSG_TOTAL_HEADER_SIZE as usize) {
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 && result_code != ResultCode::KeyNotFoundError {
bail!(ErrorKind::ServerError(result_code));
}
self.exists = result_code == ResultCode::Ok;
SingleCommand::empty_socket(conn)
}
}