Skip to content

Commit

Permalink
list: Replace Bytes with Vec<u8>
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jun 11, 2024
1 parent e7fb714 commit 9b12f4e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 24 deletions.
10 changes: 4 additions & 6 deletions src/cmd/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
// Use of this source is governed by GNU Affero General Public License
// that can be found in the LICENSE file.

use bytes::Bytes;

use crate::cmd::Command;
use crate::cmd::parse::{ParseCommandError, Parser};

#[derive(Debug, Clone)]
pub enum ListCommand {
Len(String),
PushBack(String, Vec<Bytes>),
PushBackExist(String, Vec<Bytes>),
PushFront(String, Vec<Bytes>),
PushFrontExist(String, Vec<Bytes>),
PushBack(String, Vec<Vec<u8>>),
PushBackExist(String, Vec<Vec<u8>>),
PushFront(String, Vec<Vec<u8>>),
PushFrontExist(String, Vec<Vec<u8>>),
PopBack(String, Option<usize>),
PopFront(String, Option<usize>),
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ impl Parser {
.ok_or(ParseCommandError::InvalidParameter)
}

pub fn remaining(&mut self) -> Result<Vec<Bytes>, ParseCommandError> {
pub fn remaining(&mut self) -> Result<Vec<Vec<u8>>, ParseCommandError> {
let mut list = Vec::new();
while let Some(frame) = self.iter.next() {
match frame {
Frame::Bulk(frame) => list.push(frame),
Frame::Bulk(frame) => list.push(frame.to_vec()),
frame => {
log::warn!("Protocol error, expected bulk frame, got: {frame:?}");
return Err(ParseCommandError::ProtocolError);
Expand Down
6 changes: 2 additions & 4 deletions src/mem/list/push_back.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

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

use bytes::Bytes;

use crate::cmd::reply_frame::ReplyFrame;
use crate::mem::db::{Db, MemObject};
use crate::mem::list::ListObject;
Expand All @@ -22,13 +20,13 @@ use crate::mem::list::ListObject;
//
// So for instance the command `RPUSH mylist a b c` will result into a list containing
// `a` as first element, `b` as second element and `c` as third element.
pub fn push_back(db: &mut Db, key: String, values: Vec<Bytes>) -> ReplyFrame {
pub fn push_back(db: &mut Db, key: String, values: Vec<Vec<u8>>) -> ReplyFrame {
match db.entry(key) {
Entry::Occupied(mut occupied) => match occupied.get_mut() {
MemObject::Str(_) => ReplyFrame::wrong_type_err(),
MemObject::List(old_list) => {
for value in values {
old_list.push_back(value.to_vec());
old_list.push_back(value);
}
ReplyFrame::Usize(old_list.len())
}
Expand Down
6 changes: 2 additions & 4 deletions src/mem/list/push_back_exist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

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

use bytes::Bytes;

use crate::cmd::reply_frame::ReplyFrame;
use crate::mem::db::{Db, MemObject};

/// Inserts specified values at the tail of the list stored at key, only if key already exists
/// and holds a list.
///
/// In contrary to `RPUSH`, no operation will be performed when key does not yet exist.
pub fn push_back_exist(db: &mut Db, key: String, values: Vec<Bytes>) -> ReplyFrame {
pub fn push_back_exist(db: &mut Db, key: String, values: Vec<Vec<u8>>) -> ReplyFrame {
match db.entry(key) {
Entry::Occupied(mut occupied) => match occupied.get_mut() {
MemObject::Str(_) => ReplyFrame::wrong_type_err(),
MemObject::List(old_list) => {
for value in values {
old_list.push_back(value.to_vec());
old_list.push_back(value);
}
ReplyFrame::Usize(old_list.len())
}
Expand Down
6 changes: 2 additions & 4 deletions src/mem/list/push_front.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

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

use bytes::Bytes;

use crate::cmd::reply_frame::ReplyFrame;
use crate::mem::db::{Db, MemObject};
use crate::mem::list::ListObject;
Expand All @@ -21,13 +19,13 @@ use crate::mem::list::ListObject;
// from the leftmost element to the rightmost element.
// So for instance the command `LPUSH mylist a b c` will result into a list containing
// `c` as first element, `b` as second element and `a` as third element.
pub fn push_front(db: &mut Db, key: String, values: Vec<Bytes>) -> ReplyFrame {
pub fn push_front(db: &mut Db, key: String, values: Vec<Vec<u8>>) -> ReplyFrame {
match db.entry(key) {
Entry::Occupied(mut occupied) => match occupied.get_mut() {
MemObject::Str(_) => ReplyFrame::wrong_type_err(),
MemObject::List(old_list) => {
for value in values {
old_list.push_front(value.to_vec());
old_list.push_front(value);
}
ReplyFrame::Usize(old_list.len())
}
Expand Down
6 changes: 2 additions & 4 deletions src/mem/list/push_front_exist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

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

use bytes::Bytes;

use crate::cmd::reply_frame::ReplyFrame;
use crate::mem::db::{Db, MemObject};

/// Inserts specified values at the head of the list stored at key,
/// only if key already exists and holds a list.
///
/// In contrary to `LPUSH`, no operation will be performed when key does not yet exist.
pub fn push_front_exist(db: &mut Db, key: String, values: Vec<Bytes>) -> ReplyFrame {
pub fn push_front_exist(db: &mut Db, key: String, values: Vec<Vec<u8>>) -> ReplyFrame {
match db.entry(key) {
Entry::Occupied(mut occupied) => match occupied.get_mut() {
MemObject::Str(_) => ReplyFrame::wrong_type_err(),
MemObject::List(old_list) => {
for value in values {
old_list.push_front(value.to_vec());
old_list.push_front(value);
}
ReplyFrame::Usize(old_list.len())
}
Expand Down

0 comments on commit 9b12f4e

Please sign in to comment.