Skip to content

Commit 61efc40

Browse files
committed
Update Store and TTL implementation
1 parent 36802bf commit 61efc40

File tree

9 files changed

+149
-124
lines changed

9 files changed

+149
-124
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
test:
1+
.PHONY: test
2+
test:
23
cargo test -- \
34
--nocapture \
45
--color=always

src/commands/decr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Decr {
1414

1515
impl Executable for Decr {
1616
fn exec(self, store: Store) -> Result<Frame, Error> {
17+
let mut store = store.lock();
1718
let res = store.incr_by(&self.key, -1);
1819
match res {
1920
Ok(val) => Ok(Frame::Integer(val)),

src/commands/decrby.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct DecrBy {
1515

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

2021
match res {

src/commands/incr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Incr {
1414

1515
impl Executable for Incr {
1616
fn exec(self, store: Store) -> Result<Frame, Error> {
17+
let mut store = store.lock();
1718
let res = store.incr_by(&self.key, 1);
1819
match res {
1920
Ok(val) => Ok(Frame::Integer(val)),

src/commands/incrby.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct IncrBy {
1515

1616
impl Executable for IncrBy {
1717
fn exec(self, store: Store) -> Result<Frame, Error> {
18+
let mut store = store.lock();
1819
let res = store.incr_by(&self.key, self.increment);
1920
match res {
2021
Ok(value) => Ok(Frame::Integer(value)),

src/commands/incrbyfloat.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct IncrByFloat {
2626

2727
impl Executable for IncrByFloat {
2828
fn exec(self, store: Store) -> Result<Frame, Error> {
29+
let mut store = store.lock();
2930
let res = store.incr_by(&self.key, self.increment);
3031
match res {
3132
Ok(res) => Ok(Frame::Simple(res.to_string())),

src/commands/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,6 @@ impl CommandParser {
343343
}),
344344
}
345345
}
346-
347-
// TODO: !!!
348-
fn has_more(&mut self) -> bool {
349-
self.parts.clone().peekable().peek().is_some()
350-
}
351346
}
352347

353348
#[derive(Debug, ThisError, PartialEq)]

src/commands/set.rs

+28-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bytes::Bytes;
2+
use tokio::time::Duration;
23

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

2223
#[derive(Debug, PartialEq)]
2324
pub enum SetBehavior {
24-
Nx, // Only set the key if it does not already exist.
25-
Xx, // Only set the key if it already exists.
25+
/// Only set the key if it does not already exist.
26+
Nx,
27+
/// Only set the key if it already exists.
28+
Xx,
2629
}
2730

2831
#[derive(Debug, PartialEq)]
@@ -34,10 +37,18 @@ pub enum Ttl {
3437
KeepTtl, // Retain the time to live associated with the key.
3538
}
3639

40+
impl Ttl {
41+
pub fn duration(&self) -> Duration {
42+
match self {
43+
Ttl::Ex(seconds) => Duration::from_secs(*seconds),
44+
_ => Duration::from_secs(1),
45+
}
46+
}
47+
}
48+
3749
impl Executable for Set {
3850
fn exec(self, store: Store) -> Result<Frame, Error> {
3951
let mut store = store.lock();
40-
4152
let value = store.get(&self.key);
4253

4354
match self.behavior {
@@ -46,7 +57,10 @@ impl Executable for Set {
4657
_ => {}
4758
}
4859

49-
store.set(self.key, self.value);
60+
match self.ttl {
61+
Some(ttl) => store.set_with_ttl(self.key, self.value, ttl.duration()),
62+
None => store.set(self.key, self.value),
63+
};
5064

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

72-
while parser.has_more() {
73-
let opt = parser.next_string()?;
86+
loop {
87+
let option = match parser.next_string() {
88+
Ok(option) => option,
89+
Err(CommandParserError::EndOfStream) => {
90+
break;
91+
}
92+
Err(err) => return Err(err.into()),
93+
};
7494

75-
match opt.as_str() {
95+
match option.as_str() {
7696
// TTL options
7797
"EX" if ttl.is_none() => {
7898
let val = parser.next_integer()?;
@@ -111,7 +131,7 @@ impl TryFrom<&mut CommandParser> for Set {
111131
_ => {
112132
return Err(CommandParserError::InvalidCommandArgument {
113133
command: "SET".to_string(),
114-
argument: opt,
134+
argument: option,
115135
}
116136
.into())
117137
}

0 commit comments

Comments
 (0)