Skip to content

Commit

Permalink
feat(services/dashmap): migrate service dashmap
Browse files Browse the repository at this point in the history
Signed-off-by: Chojan Shang <[email protected]>
  • Loading branch information
PsiACE committed May 7, 2023
1 parent d45acbc commit 5e3e40e
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions core/src/services/dashmap/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::fmt::Debug;
use async_trait::async_trait;
use dashmap::DashMap;

use crate::raw::adapters::kv;
use crate::raw::adapters::typed_kv;
use crate::*;

/// [dashmap](https://github.com/xacrimon/dashmap) backend support.
Expand Down Expand Up @@ -70,45 +70,36 @@ impl Builder for DashmapBuilder {
}

/// Backend is used to serve `Accessor` support in dashmap.
pub type DashmapBackend = kv::Backend<Adapter>;
pub type DashmapBackend = typed_kv::Backend<Adapter>;

#[derive(Debug, Clone)]
pub struct Adapter {
inner: DashMap<String, Vec<u8>>,
inner: DashMap<String, typed_kv::Value>,
}

#[async_trait]
impl kv::Adapter for Adapter {
fn metadata(&self) -> kv::Metadata {
kv::Metadata::new(
Scheme::Dashmap,
&format!("{:?}", &self.inner as *const _),
Capability {
read: true,
write: true,
scan: true,
..Default::default()
},
)
impl typed_kv::Adapter for Adapter {
fn metadata(&self) -> (Scheme, String) {
(Scheme::Dashmap, "dashmap".to_string())
}

async fn get(&self, path: &str) -> Result<Option<Vec<u8>>> {
async fn get(&self, path: &str) -> Result<Option<typed_kv::Value>> {
self.blocking_get(path)
}

fn blocking_get(&self, path: &str) -> Result<Option<Vec<u8>>> {
fn blocking_get(&self, path: &str) -> Result<Option<typed_kv::Value>> {
match self.inner.get(path) {
None => Ok(None),
Some(bs) => Ok(Some(bs.to_vec())),
Some(bs) => Ok(Some(bs.value().to_owned())),
}
}

async fn set(&self, path: &str, value: &[u8]) -> Result<()> {
async fn set(&self, path: &str, value: typed_kv::Value) -> Result<()> {
self.blocking_set(path, value)
}

fn blocking_set(&self, path: &str, value: &[u8]) -> Result<()> {
self.inner.insert(path.to_string(), value.to_vec());
fn blocking_set(&self, path: &str, value: typed_kv::Value) -> Result<()> {
self.inner.insert(path.to_string(), value);

Ok(())
}
Expand Down

0 comments on commit 5e3e40e

Please sign in to comment.