From 86862a299a7bcc96c0a133cc4b6a647b2bfcc745 Mon Sep 17 00:00:00 2001 From: Anurag Naik <120748645+AnuRage-git@users.noreply.github.com> Date: Sun, 10 Mar 2024 23:56:24 +0530 Subject: [PATCH 1/9] feat : Implement config for module in services Requesting a review of the PR. I have added the functionalities as I have understood from the described issue. --- backend.rs | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++ mod.rs | 20 ++++++ 2 files changed, 197 insertions(+) create mode 100644 backend.rs create mode 100644 mod.rs diff --git a/backend.rs b/backend.rs new file mode 100644 index 00000000000..da84313d0e5 --- /dev/null +++ b/backend.rs @@ -0,0 +1,177 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::collections::BTreeMap; +use std::collections::HashMap; +use std::fmt::Debug; +use std::sync::Arc; +use std::sync::Mutex; + +use async_trait::async_trait; + +use crate::raw::adapters::typed_kv; +use crate::*; + +use serde::Deserialize; + +use self::raw::ConfigDeserializer; + +//Config for memory. +#[derive(Default, Deserialize)] +#[serde(default)] +#[non_exhaustive] + +pub struct MemoryConfig{ + //root of the backend. + pub root: Option, +} + + +/// In memory service support. (BTreeMap Based) +#[doc = include_str!("docs.md")] +#[derive(Default)] +pub struct MemoryBuilder { + config: MemoryConfig, +} + +impl MemoryBuilder { + /// Set the root for BTreeMap. + pub fn root(&mut self, path: &str) -> &mut Self { + self.config.root = Some(path.into()); + self + } +} + +impl Builder for MemoryBuilder { + const SCHEME: Scheme = Scheme::Memory; + type Accessor = MemoryBackend; + + fn from_map(map: HashMap) -> Self { + MemoryBuilder{ + config: MemoryConfig::deserialize(ConfigDeserializer::new(map)) + .expect("config deserialize must succeed"), + } + } + + fn build(&mut self) -> Result { + let adapter = Adapter { + inner: Arc::new(Mutex::new(BTreeMap::default())), + }; + + Ok(MemoryBackend::new(adapter).with_root(self.config.root.as_deref().unwrap_or_default())) + } +} + +/// Backend is used to serve `Accessor` support in memory. +pub type MemoryBackend = typed_kv::Backend; + +#[derive(Clone)] +pub struct Adapter { + inner: Arc>>, +} + +impl Debug for Adapter { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("MemoryBackend").finish_non_exhaustive() + } +} + +#[async_trait] +impl typed_kv::Adapter for Adapter { + fn info(&self) -> typed_kv::Info { + typed_kv::Info::new( + Scheme::Memory, + &format!("{:?}", &self.inner as *const _), + typed_kv::Capability { + get: true, + set: true, + delete: true, + scan: true, + }, + ) + } + + async fn get(&self, path: &str) -> Result> { + self.blocking_get(path) + } + + fn blocking_get(&self, path: &str) -> Result> { + match self.inner.lock().unwrap().get(path) { + None => Ok(None), + Some(bs) => Ok(Some(bs.to_owned())), + } + } + + async fn set(&self, path: &str, value: typed_kv::Value) -> Result<()> { + self.blocking_set(path, value) + } + + fn blocking_set(&self, path: &str, value: typed_kv::Value) -> Result<()> { + self.inner.lock().unwrap().insert(path.to_string(), value); + + Ok(()) + } + + async fn delete(&self, path: &str) -> Result<()> { + self.blocking_delete(path) + } + + fn blocking_delete(&self, path: &str) -> Result<()> { + self.inner.lock().unwrap().remove(path); + + Ok(()) + } + + async fn scan(&self, path: &str) -> Result> { + self.blocking_scan(path) + } + + fn blocking_scan(&self, path: &str) -> Result> { + let inner = self.inner.lock().unwrap(); + let keys: Vec<_> = if path.is_empty() { + inner.keys().cloned().collect() + } else { + let right_range = if let Some(path) = path.strip_suffix('/') { + format!("{}0", path) + } else { + format!("{}{}", path, std::char::MAX) + }; + inner + .range(path.to_string()..right_range) + .filter(|(k, _)| k.as_str() != path) + .map(|(k, _)| k.to_string()) + .collect() + }; + + Ok(keys) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::raw::*; + + #[test] + fn test_accessor_metadata_name() { + let b1 = MemoryBuilder::default().build().unwrap(); + assert_eq!(b1.info().name(), b1.info().name()); + + let b2 = MemoryBuilder::default().build().unwrap(); + assert_ne!(b1.info().name(), b2.info().name()) + } +} diff --git a/mod.rs b/mod.rs new file mode 100644 index 00000000000..b8d63eff8fe --- /dev/null +++ b/mod.rs @@ -0,0 +1,20 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +mod backend; +pub use backend::MemoryBuilder as Memory; +pub use backend::MemoryConfig; \ No newline at end of file From e88f52eb1a456c6588fff70e9150af674e2d182c Mon Sep 17 00:00:00 2001 From: Anurag Naik <120748645+AnuRage-git@users.noreply.github.com> Date: Tue, 12 Mar 2024 09:14:20 +0530 Subject: [PATCH 2/9] Delete mod.rs --- mod.rs | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 mod.rs diff --git a/mod.rs b/mod.rs deleted file mode 100644 index b8d63eff8fe..00000000000 --- a/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -mod backend; -pub use backend::MemoryBuilder as Memory; -pub use backend::MemoryConfig; \ No newline at end of file From afb05fdf48d1881849d053a5f88c907c37ec8d4a Mon Sep 17 00:00:00 2001 From: Anurag Naik <120748645+AnuRage-git@users.noreply.github.com> Date: Tue, 12 Mar 2024 09:14:35 +0530 Subject: [PATCH 3/9] Delete backend.rs --- backend.rs | 177 ----------------------------------------------------- 1 file changed, 177 deletions(-) delete mode 100644 backend.rs diff --git a/backend.rs b/backend.rs deleted file mode 100644 index da84313d0e5..00000000000 --- a/backend.rs +++ /dev/null @@ -1,177 +0,0 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -use std::collections::BTreeMap; -use std::collections::HashMap; -use std::fmt::Debug; -use std::sync::Arc; -use std::sync::Mutex; - -use async_trait::async_trait; - -use crate::raw::adapters::typed_kv; -use crate::*; - -use serde::Deserialize; - -use self::raw::ConfigDeserializer; - -//Config for memory. -#[derive(Default, Deserialize)] -#[serde(default)] -#[non_exhaustive] - -pub struct MemoryConfig{ - //root of the backend. - pub root: Option, -} - - -/// In memory service support. (BTreeMap Based) -#[doc = include_str!("docs.md")] -#[derive(Default)] -pub struct MemoryBuilder { - config: MemoryConfig, -} - -impl MemoryBuilder { - /// Set the root for BTreeMap. - pub fn root(&mut self, path: &str) -> &mut Self { - self.config.root = Some(path.into()); - self - } -} - -impl Builder for MemoryBuilder { - const SCHEME: Scheme = Scheme::Memory; - type Accessor = MemoryBackend; - - fn from_map(map: HashMap) -> Self { - MemoryBuilder{ - config: MemoryConfig::deserialize(ConfigDeserializer::new(map)) - .expect("config deserialize must succeed"), - } - } - - fn build(&mut self) -> Result { - let adapter = Adapter { - inner: Arc::new(Mutex::new(BTreeMap::default())), - }; - - Ok(MemoryBackend::new(adapter).with_root(self.config.root.as_deref().unwrap_or_default())) - } -} - -/// Backend is used to serve `Accessor` support in memory. -pub type MemoryBackend = typed_kv::Backend; - -#[derive(Clone)] -pub struct Adapter { - inner: Arc>>, -} - -impl Debug for Adapter { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("MemoryBackend").finish_non_exhaustive() - } -} - -#[async_trait] -impl typed_kv::Adapter for Adapter { - fn info(&self) -> typed_kv::Info { - typed_kv::Info::new( - Scheme::Memory, - &format!("{:?}", &self.inner as *const _), - typed_kv::Capability { - get: true, - set: true, - delete: true, - scan: true, - }, - ) - } - - async fn get(&self, path: &str) -> Result> { - self.blocking_get(path) - } - - fn blocking_get(&self, path: &str) -> Result> { - match self.inner.lock().unwrap().get(path) { - None => Ok(None), - Some(bs) => Ok(Some(bs.to_owned())), - } - } - - async fn set(&self, path: &str, value: typed_kv::Value) -> Result<()> { - self.blocking_set(path, value) - } - - fn blocking_set(&self, path: &str, value: typed_kv::Value) -> Result<()> { - self.inner.lock().unwrap().insert(path.to_string(), value); - - Ok(()) - } - - async fn delete(&self, path: &str) -> Result<()> { - self.blocking_delete(path) - } - - fn blocking_delete(&self, path: &str) -> Result<()> { - self.inner.lock().unwrap().remove(path); - - Ok(()) - } - - async fn scan(&self, path: &str) -> Result> { - self.blocking_scan(path) - } - - fn blocking_scan(&self, path: &str) -> Result> { - let inner = self.inner.lock().unwrap(); - let keys: Vec<_> = if path.is_empty() { - inner.keys().cloned().collect() - } else { - let right_range = if let Some(path) = path.strip_suffix('/') { - format!("{}0", path) - } else { - format!("{}{}", path, std::char::MAX) - }; - inner - .range(path.to_string()..right_range) - .filter(|(k, _)| k.as_str() != path) - .map(|(k, _)| k.to_string()) - .collect() - }; - - Ok(keys) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::raw::*; - - #[test] - fn test_accessor_metadata_name() { - let b1 = MemoryBuilder::default().build().unwrap(); - assert_eq!(b1.info().name(), b1.info().name()); - - let b2 = MemoryBuilder::default().build().unwrap(); - assert_ne!(b1.info().name(), b2.info().name()) - } -} From c6032d69eec51de7d5a9ace409fb1498b6ac62e9 Mon Sep 17 00:00:00 2001 From: Anurag Naik <120748645+AnuRage-git@users.noreply.github.com> Date: Tue, 12 Mar 2024 09:15:28 +0530 Subject: [PATCH 4/9] Update backend.rs --- core/src/services/memory/backend.rs | 30 +++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/core/src/services/memory/backend.rs b/core/src/services/memory/backend.rs index 6549d0dbe88..b7f850333a1 100644 --- a/core/src/services/memory/backend.rs +++ b/core/src/services/memory/backend.rs @@ -26,17 +26,32 @@ use async_trait::async_trait; use crate::raw::adapters::typed_kv; use crate::*; +use serde::Deserialize; + +use self::raw::ConfigDeserializer; + +//Config for memory. +#[derive(Default, Deserialize)] +#[serde(default)] +#[non_exhaustive] + +pub struct MemoryConfig{ + //root of the backend. + pub root: Option, +} + + /// In memory service support. (BTreeMap Based) #[doc = include_str!("docs.md")] #[derive(Default)] pub struct MemoryBuilder { - root: Option, + config: MemoryConfig, } impl MemoryBuilder { /// Set the root for BTreeMap. pub fn root(&mut self, path: &str) -> &mut Self { - self.root = Some(path.into()); + self.config.root = Some(path.into()); self } } @@ -46,11 +61,10 @@ impl Builder for MemoryBuilder { type Accessor = MemoryBackend; fn from_map(map: HashMap) -> Self { - let mut builder = Self::default(); - - map.get("root").map(|v| builder.root(v)); - - builder + MemoryBuilder{ + config: MemoryConfig::deserialize(ConfigDeserializer::new(map)) + .expect("config deserialize must succeed"), + } } fn build(&mut self) -> Result { @@ -58,7 +72,7 @@ impl Builder for MemoryBuilder { inner: Arc::new(Mutex::new(BTreeMap::default())), }; - Ok(MemoryBackend::new(adapter).with_root(self.root.as_deref().unwrap_or_default())) + Ok(MemoryBackend::new(adapter).with_root(self.config.root.as_deref().unwrap_or_default())) } } From c434545eedd5dab61621f222419357c1f29d2e96 Mon Sep 17 00:00:00 2001 From: Anurag Naik <120748645+AnuRage-git@users.noreply.github.com> Date: Tue, 12 Mar 2024 09:16:06 +0530 Subject: [PATCH 5/9] Update mod.rs --- core/src/services/memory/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/services/memory/mod.rs b/core/src/services/memory/mod.rs index 2539e665860..6ba9c2bac33 100644 --- a/core/src/services/memory/mod.rs +++ b/core/src/services/memory/mod.rs @@ -17,3 +17,4 @@ mod backend; pub use backend::MemoryBuilder as Memory; +pub use backend::MemoryConfig; From a83b5dad3133e5422e393b13661ba89dd7293535 Mon Sep 17 00:00:00 2001 From: Anurag Naik <120748645+AnuRage-git@users.noreply.github.com> Date: Tue, 12 Mar 2024 17:35:17 +0530 Subject: [PATCH 6/9] Update mod.rs --- core/src/services/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs index e98245e1249..3bcca4402b6 100644 --- a/core/src/services/mod.rs +++ b/core/src/services/mod.rs @@ -135,7 +135,9 @@ pub use memcached::MemcachedConfig; #[cfg(feature = "services-memory")] mod memory; #[cfg(feature = "services-memory")] -pub use memory::Memory; +pub use self::memory::MemoryConfig; +#[cfg(feature= "services-memory")] +pub use self::memory::Memory; #[cfg(feature = "services-mini-moka")] mod mini_moka; From 2c95d0988d01fed2440e8756e98cdea925b61df4 Mon Sep 17 00:00:00 2001 From: Anurag Naik <120748645+AnuRage-git@users.noreply.github.com> Date: Tue, 12 Mar 2024 17:43:16 +0530 Subject: [PATCH 7/9] formatted backend.rs as suggested --- core/src/services/memory/backend.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/core/src/services/memory/backend.rs b/core/src/services/memory/backend.rs index b7f850333a1..ad9c6dfddf8 100644 --- a/core/src/services/memory/backend.rs +++ b/core/src/services/memory/backend.rs @@ -30,17 +30,15 @@ use serde::Deserialize; use self::raw::ConfigDeserializer; -//Config for memory. +///Config for memory. #[derive(Default, Deserialize)] #[serde(default)] #[non_exhaustive] - -pub struct MemoryConfig{ +pub struct MemoryConfig { //root of the backend. pub root: Option, } - /// In memory service support. (BTreeMap Based) #[doc = include_str!("docs.md")] #[derive(Default)] @@ -61,9 +59,9 @@ impl Builder for MemoryBuilder { type Accessor = MemoryBackend; fn from_map(map: HashMap) -> Self { - MemoryBuilder{ + MemoryBuilder { config: MemoryConfig::deserialize(ConfigDeserializer::new(map)) - .expect("config deserialize must succeed"), + .expect("config deserialize must succeed"), } } From eb4b1b6d7c0d54fc9fafe7f7bcffedb8618eaa83 Mon Sep 17 00:00:00 2001 From: Anurag Naik Date: Wed, 13 Mar 2024 12:25:33 +0530 Subject: [PATCH 8/9] fix linting errors with cargo fmt --- core/src/services/memory/backend.rs | 4 ++++ core/src/services/mod.rs | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/core/src/services/memory/backend.rs b/core/src/services/memory/backend.rs index ad9c6dfddf8..a6c39c261be 100644 --- a/core/src/services/memory/backend.rs +++ b/core/src/services/memory/backend.rs @@ -35,7 +35,11 @@ use self::raw::ConfigDeserializer; #[serde(default)] #[non_exhaustive] pub struct MemoryConfig { +<<<<<<< Updated upstream //root of the backend. +======= + ///root of the backend. +>>>>>>> Stashed changes pub root: Option, } diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs index 3bcca4402b6..013123a662a 100644 --- a/core/src/services/mod.rs +++ b/core/src/services/mod.rs @@ -135,9 +135,15 @@ pub use memcached::MemcachedConfig; #[cfg(feature = "services-memory")] mod memory; #[cfg(feature = "services-memory")] +<<<<<<< Updated upstream pub use self::memory::MemoryConfig; #[cfg(feature= "services-memory")] pub use self::memory::Memory; +======= +pub use self::memory::Memory; +#[cfg(feature = "services-memory")] +pub use self::memory::MemoryConfig; +>>>>>>> Stashed changes #[cfg(feature = "services-mini-moka")] mod mini_moka; From f0e8fdaa63af2dcd818d01a926a24a748fd34f48 Mon Sep 17 00:00:00 2001 From: Anurag Naik Date: Wed, 13 Mar 2024 12:27:51 +0530 Subject: [PATCH 9/9] fix linting errors with cargo fmt --- core/src/services/memory/backend.rs | 4 ---- core/src/services/mod.rs | 6 ------ 2 files changed, 10 deletions(-) diff --git a/core/src/services/memory/backend.rs b/core/src/services/memory/backend.rs index a6c39c261be..03c59ee297d 100644 --- a/core/src/services/memory/backend.rs +++ b/core/src/services/memory/backend.rs @@ -35,11 +35,7 @@ use self::raw::ConfigDeserializer; #[serde(default)] #[non_exhaustive] pub struct MemoryConfig { -<<<<<<< Updated upstream - //root of the backend. -======= ///root of the backend. ->>>>>>> Stashed changes pub root: Option, } diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs index 013123a662a..a8fb10026c5 100644 --- a/core/src/services/mod.rs +++ b/core/src/services/mod.rs @@ -135,15 +135,9 @@ pub use memcached::MemcachedConfig; #[cfg(feature = "services-memory")] mod memory; #[cfg(feature = "services-memory")] -<<<<<<< Updated upstream -pub use self::memory::MemoryConfig; -#[cfg(feature= "services-memory")] -pub use self::memory::Memory; -======= pub use self::memory::Memory; #[cfg(feature = "services-memory")] pub use self::memory::MemoryConfig; ->>>>>>> Stashed changes #[cfg(feature = "services-mini-moka")] mod mini_moka;