Skip to content

Commit 0e14451

Browse files
authored
Merge pull request #10 from upupnoah/main
refactor: delete NullBulkString and NullArray
2 parents 99fceb0 + c0832d7 commit 0e14451

File tree

3 files changed

+105
-67
lines changed

3 files changed

+105
-67
lines changed

src/resp/array.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ use std::ops::Deref;
33
use bytes::{Buf, BytesMut};
44

55
use super::{
6-
calc_total_length, extract_fixed_data, parse_length, RespDecode, RespEncode, RespError,
7-
RespFrame, BUF_CAP, CRLF_LEN,
6+
calc_total_length, parse_length, RespDecode, RespEncode, RespError, RespFrame, BUF_CAP,
7+
CRLF_LEN,
88
};
99

10+
const NULL_ARRAY: &[u8] = b"*-1\r\n";
11+
1012
#[derive(Debug, Clone, PartialEq)]
1113
pub struct RespArray(pub(crate) Vec<RespFrame>);
1214

@@ -22,6 +24,9 @@ impl RespArray {
2224
// - array: "*<number-of-elements>\r\n<element-1>...<element-n>"
2325
impl RespEncode for RespArray {
2426
fn encode(self) -> Vec<u8> {
27+
if self.is_empty() {
28+
return NULL_ARRAY.to_vec();
29+
}
2530
let mut buf = Vec::with_capacity(BUF_CAP);
2631
buf.extend_from_slice(&format!("*{}\r\n", self.len()).into_bytes());
2732
for frame in self.0 {
@@ -35,6 +40,11 @@ impl RespEncode for RespArray {
3540
impl RespDecode for RespArray {
3641
const PREFIX: &'static str = "*";
3742
fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
43+
if buf.starts_with(NULL_ARRAY) {
44+
buf.advance(NULL_ARRAY.len());
45+
return Ok(RespArray::new(vec![]));
46+
}
47+
3848
let (end, len) = parse_length(buf, Self::PREFIX)?;
3949
let total_len = calc_total_length(buf, end, len, Self::PREFIX)?;
4050

@@ -58,24 +68,25 @@ impl RespDecode for RespArray {
5868
}
5969
}
6070

71+
// NOTE: refactor -> delete RespNullArray, add NULL_ARRAY
6172
// - null array: "*-1\r\n"
62-
impl RespEncode for RespNullArray {
63-
fn encode(self) -> Vec<u8> {
64-
b"*-1\r\n".to_vec()
65-
}
66-
}
67-
68-
impl RespDecode for RespNullArray {
69-
const PREFIX: &'static str = "*";
70-
fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
71-
extract_fixed_data(buf, "*-1\r\n", "NullArray")?;
72-
Ok(RespNullArray)
73-
}
74-
75-
fn expect_length(_buf: &[u8]) -> Result<usize, RespError> {
76-
Ok(4)
77-
}
78-
}
73+
// impl RespEncode for RespNullArray {
74+
// fn encode(self) -> Vec<u8> {
75+
// b"*-1\r\n".to_vec()
76+
// }
77+
// }
78+
79+
// impl RespDecode for RespNullArray {
80+
// const PREFIX: &'static str = "*";
81+
// fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
82+
// extract_fixed_data(buf, "*-1\r\n", "NullArray")?;
83+
// Ok(RespNullArray)
84+
// }
85+
86+
// fn expect_length(_buf: &[u8]) -> Result<usize, RespError> {
87+
// Ok(4)
88+
// }
89+
// }
7990

8091
impl Deref for RespArray {
8192
type Target = Vec<RespFrame>;
@@ -107,7 +118,8 @@ mod tests {
107118

108119
#[test]
109120
fn test_null_array_encode() {
110-
let frame: RespFrame = RespNullArray.into();
121+
// let frame: RespFrame = RespNullArray.into();
122+
let frame: RespFrame = RespArray::new(vec![]).into();
111123
assert_eq!(frame.encode(), b"*-1\r\n");
112124
}
113125

@@ -116,8 +128,9 @@ mod tests {
116128
let mut buf = BytesMut::new();
117129
buf.extend_from_slice(b"*-1\r\n");
118130

119-
let frame = RespNullArray::decode(&mut buf)?;
120-
assert_eq!(frame, RespNullArray);
131+
// let frame = RespNullArray::decode(&mut buf)?;
132+
let frame = RespArray::decode(&mut buf)?;
133+
assert_eq!(frame, RespArray::new(vec![]));
121134

122135
Ok(())
123136
}

src/resp/bulk_string.rs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::ops::Deref;
22

33
use bytes::{Buf, BytesMut};
44

5-
use super::{extract_fixed_data, parse_length, RespDecode, RespEncode, RespError, CRLF_LEN};
5+
use super::{parse_length, RespDecode, RespEncode, RespError, CRLF_LEN};
6+
7+
const NULL_BULK_STRING: &[u8] = b"$-1\r\n";
68

79
#[derive(Debug, Clone, PartialEq, Eq)]
810
pub struct BulkString(pub(crate) Vec<u8>); // 单个二进制字符串, 用于存储二进制数据(最大512MB)
@@ -19,6 +21,9 @@ impl BulkString {
1921
// - bulk string: "$<length>\r\n<data>\r\n"
2022
impl RespEncode for BulkString {
2123
fn encode(self) -> Vec<u8> {
24+
if self.is_empty() {
25+
return NULL_BULK_STRING.to_vec();
26+
}
2227
let mut buf = Vec::with_capacity(self.len() + 16);
2328
buf.extend_from_slice(&format!("${}\r\n", self.len()).into_bytes());
2429
buf.extend_from_slice(&self);
@@ -30,6 +35,11 @@ impl RespEncode for BulkString {
3035
impl RespDecode for BulkString {
3136
const PREFIX: &'static str = "$";
3237
fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
38+
if buf.starts_with(NULL_BULK_STRING) {
39+
buf.advance(NULL_BULK_STRING.len()); // advance the buffer
40+
return Ok(BulkString::new(vec![]));
41+
}
42+
3343
let (end, len) = parse_length(buf, Self::PREFIX)?;
3444
let remained = &buf[end + CRLF_LEN..];
3545
if remained.len() < len + CRLF_LEN {
@@ -48,24 +58,25 @@ impl RespDecode for BulkString {
4858
}
4959
}
5060

61+
// NOTE: refactor -> delete RespNullBulkString, add NULL_BULK_STRING
5162
// - null bulk string: "$-1\r\n"
52-
impl RespEncode for RespNullBulkString {
53-
fn encode(self) -> Vec<u8> {
54-
b"$-1\r\n".to_vec()
55-
}
56-
}
57-
58-
impl RespDecode for RespNullBulkString {
59-
const PREFIX: &'static str = "$";
60-
fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
61-
extract_fixed_data(buf, "$-1\r\n", "NullBulkString")?;
62-
Ok(RespNullBulkString)
63-
}
64-
65-
fn expect_length(_buf: &[u8]) -> Result<usize, RespError> {
66-
Ok(5)
67-
}
68-
}
63+
// impl RespEncode for RespNullBulkString {
64+
// fn encode(self) -> Vec<u8> {
65+
// b"$-1\r\n".to_vec()
66+
// }
67+
// }
68+
69+
// impl RespDecode for RespNullBulkString {
70+
// const PREFIX: &'static str = "$";
71+
// fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
72+
// extract_fixed_data(buf, "$-1\r\n", "NullBulkString")?;
73+
// Ok(RespNullBulkString)
74+
// }
75+
76+
// fn expect_length(_buf: &[u8]) -> Result<usize, RespError> {
77+
// Ok(5)
78+
// }
79+
// }
6980

7081
impl Deref for BulkString {
7182
type Target = Vec<u8>;
@@ -120,7 +131,8 @@ mod tests {
120131

121132
#[test]
122133
fn test_null_bulk_string_encode() {
123-
let frame: RespFrame = RespNullBulkString.into();
134+
// let frame: RespFrame = RespNullBulkString.into();
135+
let frame: RespFrame = BulkString::new(vec![]).into();
124136
assert_eq!(frame.encode(), b"$-1\r\n");
125137
}
126138

@@ -148,8 +160,10 @@ mod tests {
148160
let mut buf = BytesMut::new();
149161
buf.extend_from_slice(b"$-1\r\n");
150162

151-
let frame = RespNullBulkString::decode(&mut buf)?;
152-
assert_eq!(frame, RespNullBulkString);
163+
// let frame = RespNullBulkString::decode(&mut buf)?;
164+
// assert_eq!(frame, RespNullBulkString);
165+
let ret = BulkString::decode(&mut buf)?;
166+
assert_eq!(ret, BulkString::new(vec![]));
153167

154168
Ok(())
155169
}

src/resp/frame.rs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use bytes::BytesMut;
22
use enum_dispatch::enum_dispatch;
33

44
use super::{
5-
BulkString, RespArray, RespDecode, RespError, RespMap, RespNull, RespNullArray,
6-
RespNullBulkString, RespSet, SimpleError, SimpleString,
5+
BulkString, RespArray, RespDecode, RespError, RespMap, RespNull, RespSet, SimpleError,
6+
SimpleString,
77
};
88

99
// 关于 enum 的知识点
@@ -21,9 +21,9 @@ pub enum RespFrame {
2121
Error(SimpleError),
2222
Integer(i64),
2323
BulkString(BulkString),
24-
NullBulkString(RespNullBulkString),
24+
// NullBulkString(RespNullBulkString),
2525
Array(RespArray),
26-
NullArray(RespNullArray),
26+
// NullArray(RespNullArray),
2727
Null(RespNull),
2828
Boolean(bool),
2929
Double(f64),
@@ -49,27 +49,36 @@ impl RespDecode for RespFrame {
4949
let frame = i64::decode(buf)?;
5050
Ok(frame.into())
5151
}
52+
// NOTE: refactor -> delete NullBulkString and NullArray
53+
// Some(b'$') => {
54+
// // try null bulk string first
55+
// match RespNullBulkString::decode(buf) {
56+
// Ok(frame) => Ok(frame.into()),
57+
// Err(RespError::NotComplete) => Err(RespError::NotComplete),
58+
// Err(_) => {
59+
// let frame = BulkString::decode(buf)?;
60+
// Ok(frame.into())
61+
// }
62+
// }
63+
// }
64+
// Some(b'*') => {
65+
// // try null array first
66+
// match RespNullArray::decode(buf) {
67+
// Ok(frame) => Ok(frame.into()),
68+
// Err(RespError::NotComplete) => Err(RespError::NotComplete),
69+
// Err(_) => {
70+
// let frame = RespArray::decode(buf)?;
71+
// Ok(frame.into())
72+
// }
73+
// }
74+
// }
5275
Some(b'$') => {
53-
// try null bulk string first
54-
match RespNullBulkString::decode(buf) {
55-
Ok(frame) => Ok(frame.into()),
56-
Err(RespError::NotComplete) => Err(RespError::NotComplete),
57-
Err(_) => {
58-
let frame = BulkString::decode(buf)?;
59-
Ok(frame.into())
60-
}
61-
}
76+
let frame = BulkString::decode(buf)?;
77+
Ok(frame.into())
6278
}
6379
Some(b'*') => {
64-
// try null array first
65-
match RespNullArray::decode(buf) {
66-
Ok(frame) => Ok(frame.into()),
67-
Err(RespError::NotComplete) => Err(RespError::NotComplete),
68-
Err(_) => {
69-
let frame = RespArray::decode(buf)?;
70-
Ok(frame.into())
71-
}
72-
}
80+
let frame = RespArray::decode(buf)?;
81+
Ok(frame.into())
7382
}
7483
Some(b'_') => {
7584
let frame = RespNull::decode(buf)?;
@@ -161,7 +170,8 @@ mod tests {
161170

162171
buf.extend_from_slice(b"$-1\r\n");
163172
let frame = RespFrame::decode(&mut buf)?;
164-
assert_eq!(frame, RespNullBulkString.into());
173+
// assert_eq!(frame, RespNullBulkString.into());
174+
assert_eq!(frame, BulkString::new(vec![]).into());
165175

166176
buf.extend_from_slice(b"*2\r\n$4\r\necho\r\n$5\r\nhello\r\n");
167177
let frame = RespFrame::decode(&mut buf)?;
@@ -176,7 +186,8 @@ mod tests {
176186

177187
buf.extend_from_slice(b"*-1\r\n");
178188
let frame = RespFrame::decode(&mut buf)?;
179-
assert_eq!(frame, RespNullArray.into());
189+
// assert_eq!(frame, RespNullArray.into());
190+
assert_eq!(frame, RespArray::new(vec![]).into());
180191

181192
buf.extend_from_slice(b"_\r\n");
182193
let frame = RespFrame::decode(&mut buf)?;

0 commit comments

Comments
 (0)