Skip to content

Commit

Permalink
string: Add get-del command
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jun 9, 2024
1 parent 4b103ca commit 598a8f3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/cmd/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::cmd::parse::{Parser, ParsingCommandError};
pub enum StringCommand {
Append(String, Bytes),
Get(String),
GetDel(String),
Set(String, Bytes),
StrLen(String),
}
Expand All @@ -30,6 +31,10 @@ impl StringCommand {
let key = parser.next_string()?;
Self::Get(key)
}
"getdel" => {
let key = parser.next_string()?;
Self::GetDel(key)
}
"set" => {
let key = parser.next_string()?;
let value = parser.next_bytes()?;
Expand Down
4 changes: 3 additions & 1 deletion src/mem/string/append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source is governed by GNU Affero General Public License
// that can be found in the LICENSE file.

#![allow(clippy::cast_possible_wrap)]

use std::collections::hash_map::Entry;

use bytes::Bytes;
Expand All @@ -19,7 +21,7 @@ pub fn append(db: &mut Db, key: String, value: Bytes) -> Frame {
match db.entry(key) {
Entry::Occupied(mut occupied) => match occupied.get_mut() {
MemObject::Str(old_str) => {
old_str.append(value);
old_str.append(&value);
let len = old_str.len();
Frame::Integer(len as i64)
}
Expand Down
18 changes: 18 additions & 0 deletions src/mem/string/get_del.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 get_del(db: &mut Db, key: &String) -> Frame {
match db.get(key) {
Some(MemObject::Str(value)) => {
let frame = Frame::Bulk(value.to_bytes());
db.remove(key);
frame
}
Some(_other) => Frame::wrong_type_err(),
None => Frame::null(),
}
}
19 changes: 11 additions & 8 deletions src/mem/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod get;
mod set;
mod strlen;
mod append;
mod get_del;

#[derive(Debug, Clone)]
pub enum StrObject {
Expand All @@ -22,14 +23,15 @@ pub enum StrObject {
impl StrObject {
#[must_use]
#[inline]
#[allow(clippy::needless_pass_by_value)]
pub fn from_bytes(bytes: Bytes) -> Self {
Self::Vec(bytes.to_vec())
}

pub fn append(&mut self, bytes: Bytes) {
pub fn append(&mut self, bytes: &Bytes) {
match self {
StrObject::Integer(_integer) => todo!(),
StrObject::Vec(vec) => {
Self::Integer(_integer) => todo!(),
Self::Vec(vec) => {
let mut bytes_vec = bytes.to_vec();
vec.append(&mut bytes_vec);
}
Expand All @@ -38,9 +40,9 @@ impl StrObject {

pub fn to_bytes(&self) -> Bytes {
match self {
StrObject::Integer(_integer) => todo!(),
StrObject::Vec(vec) => {
Bytes::copy_from_slice(&vec)
Self::Integer(_integer) => todo!(),
Self::Vec(vec) => {
Bytes::copy_from_slice(vec)
}
}
}
Expand All @@ -56,8 +58,8 @@ impl StrObject {
pub fn len(&self) -> usize {
match self {
// TODO(Shaohua):
StrObject::Integer(_) => 8,
StrObject::Vec(vec) => vec.len(),
Self::Integer(_) => 8,
Self::Vec(vec) => vec.len(),
}
}
}
Expand All @@ -67,6 +69,7 @@ impl Mem {
match command {
StringCommand::Append(key, value) => append::append(&mut self.db, key, value),
StringCommand::Get(key) => get::get(&self.db, &key),
StringCommand::GetDel(key) => get_del::get_del(&mut self.db, &key),
StringCommand::Set(key, value) => set::set(&mut self.db, key, value),
StringCommand::StrLen(key) => strlen::strlen(&self.db, &key),
}
Expand Down

0 comments on commit 598a8f3

Please sign in to comment.