Skip to content

Commit

Permalink
mem: Add list len command
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jun 11, 2024
1 parent 0c08341 commit 2f720c3
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 20 deletions.
18 changes: 10 additions & 8 deletions src/cmd/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ use crate::cmd::parse::{ParseCommandError, Parser};

#[derive(Debug, Clone)]
pub enum ListCommand {
LGet(String),
LLen(String),
Len(String),
}

impl ListCommand {
pub(super) fn parse(
cmd_name: &str,
_parser: &Parser,
parser: &mut Parser,
) -> Result<Option<Command>, ParseCommandError> {
match cmd_name {
"lget" => todo!(),
"llen" => todo!(),
_ => Ok(None),
}
let list_cmd = match cmd_name {
"llen" => {
let key = parser.next_string()?;
Self::Len(key)
}
_ => return Ok(None),
};
Ok(Some(Command::List(list_cmd)))
}
}
2 changes: 1 addition & 1 deletion src/cmd/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl TryFrom<Frame> for Command {
// TODO(Shaohua): Add a command hash map.
let mut command: Option<Self> = StringCommand::parse(&cmd_name, &mut parser)?;
if command.is_none() {
command = ListCommand::parse(&cmd_name, &parser)?;
command = ListCommand::parse(&cmd_name, &mut parser)?;
}
if command.is_none() {
log::warn!("Command not found: {cmd_name}");
Expand Down
3 changes: 2 additions & 1 deletion src/mem/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::collections::HashMap;

use crate::cmd::Command;
use crate::cmd::frame::Frame;
use crate::mem::list::ListObject;
use crate::mem::Mem;
use crate::mem::string::StrObject;

Expand All @@ -14,7 +15,7 @@ pub type Db = HashMap<String, MemObject>;
#[derive(Debug, Clone)]
pub enum MemObject {
Str(StrObject),
List(String),
List(ListObject),
}

impl Mem {
Expand Down
14 changes: 14 additions & 0 deletions src/mem/list/len.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by GNU Affero General Public License
// that can be found in the LICENSE file.

use crate::cmd::frame::Frame;
use crate::mem::db::{Db, MemObject};

pub fn len(db: &Db, key: &str) -> Frame {
match db.get(key) {
Some(MemObject::List(list)) => Frame::Integer(list.len() as i64),
Some(_other) => Frame::wrong_type_err(),
None => Frame::null(),
}
}
12 changes: 10 additions & 2 deletions src/mem/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
// Use of this source is governed by GNU Affero General Public License
// that can be found in the LICENSE file.

use std::collections::LinkedList;

use crate::cmd::frame::Frame;
use crate::cmd::list::ListCommand;
use crate::mem::Mem;

mod len;

pub type ListObject = LinkedList<Vec<u8>>;

impl Mem {
#[allow(clippy::needless_pass_by_value)]
pub fn handle_list_command(&mut self, _command: ListCommand) -> Frame {
todo!()
pub fn handle_list_command(&mut self, command: ListCommand) -> Frame {
match command {
ListCommand::Len(key) => len::len(&self.db, &key),
}
}
}
2 changes: 1 addition & 1 deletion src/mem/string/strlen.rs → src/mem/string/len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::cmd::frame::Frame;
use crate::mem::db::{Db, MemObject};

#[allow(clippy::cast_possible_wrap)]
pub fn strlen(db: &Db, key: &str) -> Frame {
pub fn len(db: &Db, key: &str) -> Frame {
match db.get(key) {
Some(MemObject::Str(value)) => Frame::Integer(value.len() as i64),
Some(_other) => Frame::Error(
Expand Down
4 changes: 2 additions & 2 deletions src/mem/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::mem::Mem;

mod get;
mod set;
mod strlen;
mod len;
mod append;
mod get_del;
mod get_set;
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Mem {
StringCommand::GetSet(key, value) => get_set::get_set(&mut self.db, key, value),
StringCommand::Set(key, value) => set::set(&mut self.db, key, value),
StringCommand::SetRange(key, offset, value) => set_range::set_range(&mut self.db, key, offset, value),
StringCommand::StrLen(key) => strlen::strlen(&self.db, &key),
StringCommand::StrLen(key) => len::len(&self.db, &key),
StringCommand::SubStr(key, start, end) => sub_str::sub_str(&self.db, &key, start, end),
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/mem/string/set_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ pub fn set_range(db: &mut Db, key: String, offset: isize, value: Bytes) -> Frame
let old_value = match old_value {
MemObject::Str(s) => match s {
StrObject::Integer(_int) => todo!(),
StrObject::Vec(vec) => {
vec
}
}
StrObject::Vec(vec) => vec,
},
_ => return Frame::wrong_type_err(),
};
return if value.is_empty() {
Expand All @@ -40,6 +38,7 @@ pub fn set_range(db: &mut Db, key: String, offset: isize, value: Bytes) -> Frame
if !check_string_length(offset_usize, value.len()) {
return FrameConst::from_str(STRING_TOO_LONG_ERR);
}
// FIXME(Shaohua): merge two parts of vector
old_value.put_slice(&value);
Frame::Integer(old_value.len() as i64)
};
Expand All @@ -58,4 +57,4 @@ pub fn set_range(db: &mut Db, key: String, offset: isize, value: Bytes) -> Frame
db.insert(key, MemObject::Str(s));
Frame::Integer(len as i64)
}
}
}

0 comments on commit 2f720c3

Please sign in to comment.