diff --git a/README.md b/README.md index 0b7733a..0b36d89 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,14 @@ - pre-commit install - git remote add origin https://github.com/kindywu/03-simple-redis.git +# RESP + +- Redis serialization protocol specification + +# 技术架构 + +- tokio-util::codec::Framed -> bytes -> encode/decode frame + # 设置日志级别(windows) - $env:RUST_LOG="info" diff --git a/src/resp/array.rs b/src/resp/array.rs index 0e4be98..2914369 100644 --- a/src/resp/array.rs +++ b/src/resp/array.rs @@ -4,11 +4,12 @@ use bytes::{Buf, BytesMut}; use crate::{RespDecode, RespEncode, RespError, RespFrame}; -use super::{calc_total_length, parse_length, BUF_CAP, CRLF_LEN}; +use super::{calc_total_length, parse_length, CRLF_LEN}; #[derive(Debug, Clone, PartialEq, PartialOrd)] pub struct RespArray(pub(crate) Vec); +// const BUF_CAP: usize = 4096; const NULL_ARRAY_STRING: &[u8; 5] = b"*-1\r\n"; // - array: "*\r\n..." @@ -17,7 +18,8 @@ impl RespEncode for RespArray { if self.0.is_empty() { NULL_ARRAY_STRING.to_vec() } else { - let mut buf = Vec::with_capacity(BUF_CAP); + // let mut buf = Vec::with_capacity(BUF_CAP); + let mut buf = Vec::new(); buf.extend_from_slice(&format!("*{}\r\n", self.0.len()).into_bytes()); for frame in self.0 { buf.extend_from_slice(&frame.encode()); diff --git a/src/resp/mod.rs b/src/resp/mod.rs index eabd15d..33edd61 100644 --- a/src/resp/mod.rs +++ b/src/resp/mod.rs @@ -12,7 +12,6 @@ pub use resp_frame::*; pub use simple_error::*; pub use simple_string::*; -const BUF_CAP: usize = 4096; const CRLF: &[u8] = b"\r\n"; const CRLF_LEN: usize = CRLF.len(); @@ -74,7 +73,7 @@ fn parse_length(buf: &[u8], prefix: &str) -> Result<(usize, usize), RespError> { let end = extract_simple_frame_data(buf, prefix)?; let s = String::from_utf8_lossy(&buf[prefix.len()..end]); if s == "-1" { - Ok((end, 0usize)) + Ok((end, 0)) } else { Ok((end, s.parse()?)) }