-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support get/set, hget/hset/hgetall command
- Loading branch information
Showing
9 changed files
with
692 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
Oops, something went wrong.