From 4bb1d7614ed62a6739fea6757555d37ccc35e5b7 Mon Sep 17 00:00:00 2001 From: Anurag Naik <120748645+AnuRage-git@users.noreply.github.com> Date: Tue, 19 Mar 2024 16:10:15 +0530 Subject: [PATCH] feat : Implement config for services/foundationdb (#4355) * 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. * Delete mod.rs * Delete backend.rs * Update backend.rs * Update mod.rs * Update mod.rs * formatted backend.rs as suggested * fix linting errors with cargo fmt * fix linting errors with cargo fmt * feat : add config for services/foundationdb * add Debug method for FoundatonConfig * Update backend.rs * update backend.rs * Update mod.rs * formatted using cargo fmt --- core/src/services/foundationdb/backend.rs | 46 +++++++++++++++++------ core/src/services/foundationdb/mod.rs | 1 + core/src/services/mod.rs | 2 + 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/core/src/services/foundationdb/backend.rs b/core/src/services/foundationdb/backend.rs index f7686ddee96f..c95d9f929795 100644 --- a/core/src/services/foundationdb/backend.rs +++ b/core/src/services/foundationdb/backend.rs @@ -25,31 +25,54 @@ use foundationdb::api::NetworkAutoStop; use foundationdb::Database; use crate::raw::adapters::kv; -use crate::raw::normalize_root; +use crate::raw::*; use crate::Builder; use crate::Error; use crate::ErrorKind; use crate::Scheme; use crate::*; +use serde::Deserialize; + /// [foundationdb](https://www.foundationdb.org/) service support. +///Config for FoundationDB. +#[derive(Default, Deserialize)] +#[serde(default)] +#[non_exhaustive] +pub struct FoundationConfig { + ///root of the backend. + pub root: Option, + ///config_path for the backend. + pub config_path: Option, +} + +impl Debug for FoundationConfig { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("FoundationConfig"); + + ds.field("root", &self.root); + ds.field("config_path", &self.config_path); + + ds.finish() + } +} + #[doc = include_str!("docs.md")] #[derive(Default)] pub struct FoundationdbBuilder { - root: Option, - config_path: Option, + config: FoundationConfig, } impl FoundationdbBuilder { /// Set the root for Foundationdb. pub fn root(&mut self, path: &str) -> &mut Self { - self.root = Some(path.into()); + self.config.root = Some(path.into()); self } /// Set the config path for Foundationdb. If not set, will fallback to use default pub fn config_path(&mut self, path: &str) -> &mut Self { - self.config_path = Some(path.into()); + self.config.config_path = Some(path.into()); self } } @@ -59,18 +82,16 @@ impl Builder for FoundationdbBuilder { type Accessor = FoundationdbBackend; fn from_map(map: HashMap) -> Self { - let mut builder = FoundationdbBuilder::default(); - - map.get("root").map(|v| builder.root(v)); - map.get("config_path").map(|v| builder.config_path(v)); + let config = FoundationConfig::deserialize(ConfigDeserializer::new(map)) + .expect("config deserialize must succeed"); - builder + Self { config } } fn build(&mut self) -> Result { let _network = Arc::new(unsafe { foundationdb::boot() }); let db; - if let Some(cfg_path) = &self.config_path { + if let Some(cfg_path) = &self.config.config_path { db = Database::from_path(cfg_path).map_err(|e| { Error::new(ErrorKind::ConfigInvalid, "open foundation db") .with_context("service", Scheme::Foundationdb) @@ -87,7 +108,8 @@ impl Builder for FoundationdbBuilder { let db = Arc::new(db); let root = normalize_root( - self.root + self.config + .root .clone() .unwrap_or_else(|| "/".to_string()) .as_str(), diff --git a/core/src/services/foundationdb/mod.rs b/core/src/services/foundationdb/mod.rs index 1c63d128261c..10d196dff737 100644 --- a/core/src/services/foundationdb/mod.rs +++ b/core/src/services/foundationdb/mod.rs @@ -17,4 +17,5 @@ mod backend; +pub use backend::FoundationConfig; pub use backend::FoundationdbBuilder as Foundationdb; diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs index 1cdb39f01a33..71cd71b8084f 100644 --- a/core/src/services/mod.rs +++ b/core/src/services/mod.rs @@ -269,6 +269,8 @@ pub use self::tikv::TikvConfig; #[cfg(feature = "services-foundationdb")] mod foundationdb; #[cfg(feature = "services-foundationdb")] +pub use self::foundationdb::FoundationConfig; +#[cfg(feature = "services-foundationdb")] pub use self::foundationdb::Foundationdb; #[cfg(feature = "services-postgresql")]