Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Store and TTL implementation #52

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test:
.PHONY: test
test:
cargo test -- \
--nocapture \
--color=always
1 change: 1 addition & 0 deletions src/commands/decr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Decr {

impl Executable for Decr {
fn exec(self, store: Store) -> Result<Frame, Error> {
let mut store = store.lock();
let res = store.incr_by(&self.key, -1);
match res {
Ok(val) => Ok(Frame::Integer(val)),
Expand Down
1 change: 1 addition & 0 deletions src/commands/decrby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct DecrBy {

impl Executable for DecrBy {
fn exec(self, store: Store) -> Result<Frame, Error> {
let mut store = store.lock();
let res = store.incr_by(&self.key, -self.decrement);

match res {
Expand Down
1 change: 1 addition & 0 deletions src/commands/incr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Incr {

impl Executable for Incr {
fn exec(self, store: Store) -> Result<Frame, Error> {
let mut store = store.lock();
let res = store.incr_by(&self.key, 1);
match res {
Ok(val) => Ok(Frame::Integer(val)),
Expand Down
1 change: 1 addition & 0 deletions src/commands/incrby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct IncrBy {

impl Executable for IncrBy {
fn exec(self, store: Store) -> Result<Frame, Error> {
let mut store = store.lock();
let res = store.incr_by(&self.key, self.increment);
match res {
Ok(value) => Ok(Frame::Integer(value)),
Expand Down
1 change: 1 addition & 0 deletions src/commands/incrbyfloat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct IncrByFloat {

impl Executable for IncrByFloat {
fn exec(self, store: Store) -> Result<Frame, Error> {
let mut store = store.lock();
let res = store.incr_by(&self.key, self.increment);
match res {
Ok(res) => Ok(Frame::Simple(res.to_string())),
Expand Down
5 changes: 0 additions & 5 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,6 @@ impl CommandParser {
}),
}
}

// TODO: !!!
fn has_more(&mut self) -> bool {
self.parts.clone().peekable().peek().is_some()
}
}

#[derive(Debug, ThisError, PartialEq)]
Expand Down
38 changes: 30 additions & 8 deletions src/commands/set.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bytes::Bytes;
use tokio::time::Duration;

use crate::commands::executable::Executable;
use crate::commands::{CommandParser, CommandParserError};
Expand All @@ -21,8 +22,10 @@ pub struct Set {

#[derive(Debug, PartialEq)]
pub enum SetBehavior {
Nx, // Only set the key if it does not already exist.
Xx, // Only set the key if it already exists.
/// Only set the key if it does not already exist.
Nx,
/// Only set the key if it already exists.
Xx,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can have these on cargo docs.

}

#[derive(Debug, PartialEq)]
Expand All @@ -34,10 +37,20 @@ pub enum Ttl {
KeepTtl, // Retain the time to live associated with the key.
}

impl Ttl {
pub fn duration(&self) -> Duration {
match self {
Ttl::Ex(seconds) => Duration::from_secs(*seconds),
Ttl::Px(millis) => Duration::from_millis(*millis),
// TODO: EXAT, PXAT and KeepTtl.
_ => Duration::from_secs(1),
}
}
}

impl Executable for Set {
fn exec(self, store: Store) -> Result<Frame, Error> {
let mut store = store.lock();

let value = store.get(&self.key);

match self.behavior {
Expand All @@ -46,7 +59,10 @@ impl Executable for Set {
_ => {}
}

store.set(self.key, self.value);
match self.ttl {
Some(ttl) => store.set_with_ttl(self.key, self.value, ttl.duration()),
None => store.set(self.key, self.value),
};

let res = if self.get {
value.map_or(Frame::NullBulkString, Frame::Bulk)
Expand All @@ -69,10 +85,16 @@ impl TryFrom<&mut CommandParser> for Set {
let mut behavior = None;
let mut get = false;

while parser.has_more() {
let opt = parser.next_string()?;
loop {
let option = match parser.next_string() {
Ok(option) => option,
Err(CommandParserError::EndOfStream) => {
break;
}
Err(err) => return Err(err.into()),
};

match opt.as_str() {
match option.as_str() {
// TTL options
"EX" if ttl.is_none() => {
let val = parser.next_integer()?;
Expand Down Expand Up @@ -111,7 +133,7 @@ impl TryFrom<&mut CommandParser> for Set {
_ => {
return Err(CommandParserError::InvalidCommandArgument {
command: "SET".to_string(),
argument: opt,
argument: option,
}
.into())
}
Expand Down
Loading
Loading