Skip to content

Commit

Permalink
feature: support get/set, hget/hset/hgetall command
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrchen committed Mar 31, 2024
1 parent a3d46d7 commit ee67e56
Show file tree
Hide file tree
Showing 9 changed files with 692 additions and 17 deletions.
152 changes: 152 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 @@ -8,5 +8,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.81"
bytes = "1.6.0"
dashmap = "5.5.3"
enum_dispatch = "0.3.13"
lazy_static = "1.4.0"
thiserror = "1.0.58"
Binary file added dump.rdb
Binary file not shown.
65 changes: 65 additions & 0 deletions src/backend/mod.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 Backend {
fn default() -> Self {
Self(Arc::new(BackendInner::default()))
}
}

impl Default for BackendInner {
fn default() -> Self {
Self {
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(&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 ee67e56

Please sign in to comment.