Skip to content

Commit

Permalink
feature: support command and store function
Browse files Browse the repository at this point in the history
  • Loading branch information
runningwater committed Jul 31, 2024
1 parent ea0a835 commit c09d28b
Show file tree
Hide file tree
Showing 8 changed files with 605 additions and 11 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,6 +9,8 @@ license = "MIT"
[dependencies]
anyhow = "1.0.86"
bytes = "1.6.1"
dashmap = "6.0.1"
enum_dispatch = "0.3.13"
lazy_static = "1.5.0"
# This library provides a convenient derive macro for the standard library’s std::error::Error trait.
thiserror = "1.0.63"
68 changes: 68 additions & 0 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::{ops::Deref, sync::Arc};

use dashmap::DashMap;

use crate::RespFrame;

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

// 使用 DashMap, 实现 Redis 存储
#[derive(Debug)]
pub struct BackendInner {
map: DashMap<String, RespFrame>,
hmap: DashMap<String, DashMap<String, RespFrame>>,
}

impl Deref for Backend {
type Target = BackendInner;
fn deref(&self) -> &Self::Target {
&self.0
}
}

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

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(&mut 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 c09d28b

Please sign in to comment.