From 5727752aeeb2bf18145ba22f79f5657df88d54cd Mon Sep 17 00:00:00 2001 From: meteorgan Date: Fri, 9 Aug 2024 00:05:06 +0800 Subject: [PATCH] feat: support root path for moka and mini-moka (#4984) --- core/src/services/dashmap/backend.rs | 17 +++++++++++++---- core/src/services/mini_moka/backend.rs | 23 +++++++++++++++++++++-- core/src/services/moka/backend.rs | 25 +++++++++++++++++++++++-- core/src/types/builder.rs | 2 +- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/core/src/services/dashmap/backend.rs b/core/src/services/dashmap/backend.rs index ac359ee4c415..9b8bdf55e647 100644 --- a/core/src/services/dashmap/backend.rs +++ b/core/src/services/dashmap/backend.rs @@ -50,7 +50,12 @@ pub struct DashmapBuilder { impl DashmapBuilder { /// Set the root for dashmap. pub fn root(mut self, path: &str) -> Self { - self.config.root = Some(path.into()); + self.config.root = if path.is_empty() { + None + } else { + Some(path.to_string()) + }; + self } } @@ -60,10 +65,14 @@ impl Builder for DashmapBuilder { type Config = DashmapConfig; fn build(self) -> Result { - Ok(DashmapBackend::new(Adapter { + let mut backend = DashmapBackend::new(Adapter { inner: DashMap::default(), - }) - .with_root(self.config.root.as_deref().unwrap_or_default())) + }); + if let Some(v) = self.config.root { + backend = backend.with_root(&v); + } + + Ok(backend) } } diff --git a/core/src/services/mini_moka/backend.rs b/core/src/services/mini_moka/backend.rs index 6373d157f9f7..e84a82c8f38e 100644 --- a/core/src/services/mini_moka/backend.rs +++ b/core/src/services/mini_moka/backend.rs @@ -45,6 +45,9 @@ pub struct MiniMokaConfig { /// /// Refer to [`mini-moka::sync::CacheBuilder::time_to_idle`](https://docs.rs/mini-moka/latest/mini_moka/sync/struct.CacheBuilder.html#method.time_to_idle) pub time_to_idle: Option, + + /// root path of this backend + pub root: Option, } impl Configurator for MiniMokaConfig { @@ -91,6 +94,17 @@ impl MiniMokaBuilder { } self } + + /// Set root path of this backend + pub fn root(mut self, path: &str) -> Self { + self.config.root = if path.is_empty() { + None + } else { + Some(path.to_string()) + }; + + self + } } impl Builder for MiniMokaBuilder { @@ -114,9 +128,14 @@ impl Builder for MiniMokaBuilder { } debug!("backend build finished: {:?}", &self); - Ok(MiniMokaBackend::new(Adapter { + let mut backend = MiniMokaBackend::new(Adapter { inner: builder.build(), - })) + }); + if let Some(v) = self.config.root { + backend = backend.with_root(&v); + } + + Ok(backend) } } diff --git a/core/src/services/moka/backend.rs b/core/src/services/moka/backend.rs index 13e8fc73029b..1f29fd8bcf87 100644 --- a/core/src/services/moka/backend.rs +++ b/core/src/services/moka/backend.rs @@ -52,6 +52,9 @@ pub struct MokaConfig { /// /// Refer to [`moka::sync::CacheBuilder::segments`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.segments) pub num_segments: Option, + + /// root path of this backend + pub root: Option, } impl Debug for MokaConfig { @@ -62,6 +65,7 @@ impl Debug for MokaConfig { .field("time_to_live", &self.time_to_live) .field("time_to_idle", &self.time_to_idle) .field("num_segments", &self.num_segments) + .field("root", &self.root) .finish_non_exhaustive() } } @@ -127,6 +131,17 @@ impl MokaBuilder { self.config.num_segments = Some(v); self } + + /// Set root path of this backend + pub fn root(mut self, path: &str) -> Self { + self.config.root = if path.is_empty() { + None + } else { + Some(path.to_string()) + }; + + self + } } impl Builder for MokaBuilder { @@ -154,9 +169,15 @@ impl Builder for MokaBuilder { } debug!("backend build finished: {:?}", &self); - Ok(MokaBackend::new(Adapter { + + let mut backend = MokaBackend::new(Adapter { inner: builder.build(), - })) + }); + if let Some(v) = self.config.root { + backend = backend.with_root(&v); + } + + Ok(backend) } } diff --git a/core/src/types/builder.rs b/core/src/types/builder.rs index 287122457c2a..1beaec42fe57 100644 --- a/core/src/types/builder.rs +++ b/core/src/types/builder.rs @@ -103,7 +103,7 @@ impl Builder for () { /// use std::collections::HashMap; /// /// use opendal::services::S3Config; -/// use opendal::{Configurator, Operator};/// +/// use opendal::{Configurator, Operator}; /// use opendal::raw::HttpClient; /// /// async fn test() -> Result<()> {