Skip to content

Commit bb506e3

Browse files
committed
feat(redis-cli): support redis cli
1 parent 58d29a6 commit bb506e3

File tree

11 files changed

+742
-12
lines changed

11 files changed

+742
-12
lines changed

Cargo.lock

Lines changed: 166 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ authors = ["Noah <[email protected]>"]
99
[dependencies]
1010
anyhow = "^1.0"
1111
bytes = "^1.6.1"
12+
dashmap = "6.0.1"
1213
enum_dispatch = "^0.3.13"
14+
lazy_static = "^1.5.0"
1315
thiserror = "^1.0.62"

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
# simple-redis
22

33
A simple redis server implementation.
4+
5+
## Project Layout
6+
7+
1. Small projects use the top-level module name .rs + modulename folder.
8+
2. Use mod.rs for medium to large projects.

examples/enum_dispatch.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ impl DoSomething for B {
2525
println!("B");
2626
}
2727
}
28+
2829
fn main() {
2930
// test enum_dispatch
3031
let apple = Types::Apple(A);

src/backend.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use crate::RespFrame;
2+
use dashmap::DashMap;
3+
use std::ops::Deref;
4+
use std::sync::Arc;
5+
6+
#[derive(Debug, Clone)]
7+
pub struct Backend(Arc<BackendInner>);
8+
9+
#[derive(Debug)]
10+
pub struct BackendInner {
11+
pub(crate) map: DashMap<String, RespFrame>,
12+
pub(crate) hmap: DashMap<String, DashMap<String, RespFrame>>,
13+
}
14+
15+
impl Deref for Backend {
16+
type Target = BackendInner;
17+
18+
fn deref(&self) -> &Self::Target {
19+
&self.0
20+
}
21+
}
22+
23+
impl Default for BackendInner {
24+
fn default() -> Self {
25+
Self {
26+
map: DashMap::new(),
27+
hmap: DashMap::new(),
28+
}
29+
}
30+
}
31+
32+
impl Default for Backend {
33+
fn default() -> Self {
34+
Self(Arc::new(BackendInner::default()))
35+
}
36+
}
37+
38+
impl Backend {
39+
pub fn new() -> Self {
40+
Self::default()
41+
}
42+
43+
pub fn get(&self, key: &str) -> Option<RespFrame> {
44+
self.map.get(key).map(|v| v.value().clone())
45+
}
46+
47+
pub fn set(&self, key: String, value: RespFrame) {
48+
self.map.insert(key, value);
49+
}
50+
51+
pub fn hget(&self, key: &str, field: &str) -> Option<RespFrame> {
52+
self.hmap
53+
.get(key)
54+
.and_then(|v| v.get(field).map(|v| v.value().clone()))
55+
}
56+
57+
pub fn hset(&self, key: String, field: String, value: RespFrame) {
58+
let hmap = self.hmap.entry(key).or_default();
59+
hmap.insert(field, value);
60+
}
61+
62+
pub fn hgetall(&self, key: &str) -> Option<DashMap<String, RespFrame>> {
63+
self.hmap.get(key).map(|v| v.clone())
64+
}
65+
}

0 commit comments

Comments
 (0)