Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kindywu committed May 6, 2024
2 parents 8a36102 + 5f3ab8e commit 05d7eac
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions src/resp/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RespFrame>);

// const BUF_CAP: usize = 4096;
const NULL_ARRAY_STRING: &[u8; 5] = b"*-1\r\n";

// - array: "*<number-of-elements>\r\n<element-1>...<element-n>"
Expand All @@ -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());
Expand Down
3 changes: 1 addition & 2 deletions src/resp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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()?))
}
Expand Down

0 comments on commit 05d7eac

Please sign in to comment.