Files
archive/src/commands/m_get.rs

61 lines
1.3 KiB
Rust

use std::io::Cursor;
use crate::{
Result,
buffer::{ArchiveBuf as _, ArchiveBufMut},
connection::Connection,
database::Database,
errors::AppError,
};
use bytes::BytesMut;
use super::get::Get;
#[derive(Debug, Clone)]
pub struct MGet {
gets: Vec<Get>,
}
impl MGet {
pub fn new(gets: Vec<Get>) -> Self {
Self { gets }
}
pub async fn execute(self, db: &Database, connection: &mut Connection) -> Result<()> {
let mut values = Vec::with_capacity(self.gets.len());
for get in self.gets {
values.push(db.get(&get.key).await);
}
let mut buf = BytesMut::new();
buf.try_put_vec(values, |data, buf| {
buf.put_option(data.as_deref(), ArchiveBufMut::put_bytes_with_length);
Ok::<(), AppError>(())
})?;
connection.write(buf.into()).await?;
Ok(())
}
pub fn parse(buf: &mut Cursor<&[u8]>) -> Result<Self> {
let gets = buf.try_get_vec(Get::parse)?;
Ok(Self { gets })
}
pub fn put(&self, buf: &mut BytesMut) -> Result<()> {
buf.put_short_string("mget")?;
self.put_without_cmd_name(buf)
}
pub fn put_without_cmd_name(&self, buf: &mut BytesMut) -> Result<()> {
buf.try_put_vec(&self.gets, Get::put_without_cmd_name)?;
Ok(())
}
}