Skip to content

Commit

Permalink
Merge pull request #4 from upupnoah/main
Browse files Browse the repository at this point in the history
feat(redis-cli): support redis cli
  • Loading branch information
upupnoah authored Jul 23, 2024
2 parents c619348 + bb506e3 commit f0dc4ed
Show file tree
Hide file tree
Showing 11 changed files with 742 additions and 12 deletions.
166 changes: 166 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ authors = ["Noah <upupqi.cs@gmail.com>"]
[dependencies]
anyhow = "^1.0"
bytes = "^1.6.1"
dashmap = "6.0.1"
enum_dispatch = "^0.3.13"
lazy_static = "^1.5.0"
thiserror = "^1.0.62"
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# simple-redis

A simple redis server implementation.

## Project Layout

1. Small projects use the top-level module name .rs + modulename folder.
2. Use mod.rs for medium to large projects.
1 change: 1 addition & 0 deletions examples/enum_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl DoSomething for B {
println!("B");
}
}

fn main() {
// test enum_dispatch
let apple = Types::Apple(A);
Expand Down
65 changes: 65 additions & 0 deletions src/backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::RespFrame;
use dashmap::DashMap;
use std::ops::Deref;
use std::sync::Arc;

#[derive(Debug, Clone)]
pub struct Backend(Arc<BackendInner>);

#[derive(Debug)]
pub struct BackendInner {
pub(crate) map: DashMap<String, RespFrame>,
pub(crate) hmap: DashMap<String, DashMap<String, RespFrame>>,
}

impl Deref for Backend {
type Target = BackendInner;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl Default for BackendInner {
fn default() -> Self {
Self {
map: DashMap::new(),
hmap: DashMap::new(),
}
}
}

impl Default for Backend {
fn default() -> Self {
Self(Arc::new(BackendInner::default()))
}
}

impl Backend {
pub fn new() -> Self {
Self::default()
}

pub fn get(&self, key: &str) -> Option<RespFrame> {
self.map.get(key).map(|v| v.value().clone())
}

pub fn set(&self, key: String, value: RespFrame) {
self.map.insert(key, value);
}

pub fn hget(&self, key: &str, field: &str) -> Option<RespFrame> {
self.hmap
.get(key)
.and_then(|v| v.get(field).map(|v| v.value().clone()))
}

pub fn hset(&self, key: String, field: String, value: RespFrame) {
let hmap = self.hmap.entry(key).or_default();
hmap.insert(field, value);
}

pub fn hgetall(&self, key: &str) -> Option<DashMap<String, RespFrame>> {
self.hmap.get(key).map(|v| v.clone())
}
}
Loading

0 comments on commit f0dc4ed

Please sign in to comment.