Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: project layout #5

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/backend.rs → src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use dashmap::DashMap;
use std::ops::Deref;
use std::sync::Arc;

// region: --- Enums and Structs
#[derive(Debug, Clone)]
pub struct Backend(Arc<BackendInner>);

Expand All @@ -11,7 +12,9 @@ pub struct BackendInner {
pub(crate) map: DashMap<String, RespFrame>,
pub(crate) hmap: DashMap<String, DashMap<String, RespFrame>>,
}
// endregion: --- Enums and Structs

// region: --- impls
impl Deref for Backend {
type Target = BackendInner;

Expand Down Expand Up @@ -63,3 +66,4 @@ impl Backend {
self.hmap.get(key).map(|v| v.clone())
}
}
// endregion: --- impls
4 changes: 4 additions & 0 deletions src/cmd/hmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use super::{
extract_args, validate_command, CommandError, CommandExecutor, HGet, HGetAll, HSet, RESP_OK,
};

// region: --- impls

// endregion: --- impls

impl CommandExecutor for HGet {
fn execute(self, backend: &crate::Backend) -> RespFrame {
match backend.hget(&self.key, &self.field) {
Expand Down
4 changes: 4 additions & 0 deletions src/cmd.rs → src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ mod hmap;
mod map;

// you could also use once_cell instead of lazy_static
// lazy_static:
// 1. init in runtime
// 2. thread safe
// 3. improve performance
lazy_static! {
static ref RESP_OK: RespFrame = SimpleString::new("OK").into();
}
Expand Down
2 changes: 2 additions & 0 deletions src/resp/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::{
calc_total_length, extract_fixed_data, extract_simple_frame_data, parse_length, CRLF_LEN,
};

// region: --- impls
impl RespDecode for RespFrame {
const PREFIX: &'static str = "";
fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
Expand Down Expand Up @@ -324,6 +325,7 @@ impl RespDecode for RespSet {
calc_total_length(buf, end, len, Self::PREFIX)
}
}
// endregion: --- impls

#[cfg(test)]
mod tests {
Expand Down
2 changes: 2 additions & 0 deletions src/resp/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{

const BUF_CAP: usize = 4096;

// region: --- impls
// - simple string: "+<string>\r\n"
impl RespEncode for SimpleString {
fn encode(self) -> Vec<u8> {
Expand Down Expand Up @@ -202,6 +203,7 @@ impl Deref for RespSet {
&self.0
}
}
// endregion: --- impls

// enum_dispatch
// 这里因为 enum_dispatch 的原因, 会自动为变体类型生成, From<xxx> for Enum_Name
Expand Down
10 changes: 10 additions & 0 deletions src/resp.rs → src/resp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod encode;
const CRLF: &[u8] = b"\r\n";
const CRLF_LEN: usize = CRLF.len();

// region: --- Traits
#[enum_dispatch]
pub trait RespEncode {
fn encode(self) -> Vec<u8>;
Expand All @@ -21,7 +22,9 @@ pub trait RespDecode: Sized {
fn decode(buf: &mut BytesMut) -> Result<Self, RespError>;
fn expect_length(buf: &[u8]) -> Result<usize, RespError>;
}
// endregion: --- Traits

// region: --- Enum and Structs
#[derive(Error, Debug, PartialEq, Eq)]
pub enum RespError {
// region: --- thiserror format usage
Expand Down Expand Up @@ -99,6 +102,9 @@ pub struct RespMap(pub(crate) BTreeMap<String, RespFrame>);
#[derive(Debug, Clone, PartialEq)]
pub struct RespSet(pub(crate) Vec<RespFrame>); // 改为 Vec, 用于有序的集合数据

// endregion: --- Enum and Structs

// region: --- impls
impl SimpleString {
pub fn new(s: impl Into<String>) -> Self {
SimpleString(s.into())
Expand Down Expand Up @@ -176,7 +182,9 @@ impl<const N: usize> From<&[u8; N]> for BulkString {
BulkString(s.to_vec())
}
}
// endregion: --- impls

// region: --- Functions
// utility functions
fn extract_fixed_data(
buf: &mut BytesMut,
Expand Down Expand Up @@ -265,3 +273,5 @@ fn calc_total_length(buf: &[u8], end: usize, len: usize, prefix: &str) -> Result
_ => Ok(len + CRLF_LEN),
}
}

// endregion: --- Functions