Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate service dashmap #2225

Merged
merged 8 commits into from
May 8, 2023
7 changes: 7 additions & 0 deletions core/src/raw/adapters/typed_kv/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ pub trait Adapter: Send + Sync + Debug + Unpin + 'static {

/// Delete a value from adapter.
fn blocking_delete(&self, path: &str) -> Result<()>;

/// Scan a key prefix to get all keys that start with this key.
async fn scan(&self, path: &str) -> Result<Vec<String>>;

/// Scan a key prefix to get all keys that start with this key
/// in blocking way.
fn blocking_scan(&self, path: &str) -> Result<Vec<String>>;
}

/// Value is the typed value stored in adapter.
Expand Down
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) {
PsiACE marked this conversation as resolved.
Show resolved Hide resolved
(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