refactor set command
This commit is contained in:
@@ -1,24 +1,18 @@
|
||||
use std::{io::Cursor, time::Duration};
|
||||
use std::io::Cursor;
|
||||
|
||||
use crate::{Result, connection::Connection, database::Database, errors::AppError};
|
||||
use bytes::{Buf as _, Bytes};
|
||||
use tokio::time::Instant;
|
||||
|
||||
use crate::{
|
||||
Result,
|
||||
connection::Connection,
|
||||
database::{Database, Value},
|
||||
errors::AppError,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Set {
|
||||
key: String,
|
||||
value: Value,
|
||||
data: Box<[u8]>,
|
||||
expiration: Option<u64>,
|
||||
}
|
||||
|
||||
impl Set {
|
||||
pub async fn execute(self, db: &Database, connection: &mut Connection) -> Result<()> {
|
||||
db.set(self.key, self.value).await?;
|
||||
db.set(self.key, self.data, self.expiration).await?;
|
||||
|
||||
connection.write(Bytes::from_static(&[1])).await?;
|
||||
|
||||
@@ -42,15 +36,16 @@ impl Set {
|
||||
|
||||
let data = bytes.copy_to_bytes(value_length);
|
||||
|
||||
let expiration: Option<Instant> = match bytes.try_get_u8()? {
|
||||
1 => Some(Instant::now() + Duration::from_secs(bytes.try_get_u64()?)),
|
||||
let expiration: Option<u64> = match bytes.try_get_u8()? {
|
||||
1 => Some(bytes.try_get_u64()?),
|
||||
0 => None,
|
||||
_ => return Err(AppError::UnexpectedCommandData),
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
key,
|
||||
value: Value::new(data, expiration),
|
||||
data: (*data).into(),
|
||||
expiration,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user