Skip to content

Commit

Permalink
feat : Implement config for services/foundationdb (#4355)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AnuRage-git authored Mar 19, 2024
1 parent 9a9e69c commit 4bb1d76
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
46 changes: 34 additions & 12 deletions core/src/services/foundationdb/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
///config_path for the backend.
pub config_path: Option<String>,
}

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<String>,
config_path: Option<String>,
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
}
}
Expand All @@ -59,18 +82,16 @@ impl Builder for FoundationdbBuilder {
type Accessor = FoundationdbBackend;

fn from_map(map: HashMap<String, String>) -> 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<Self::Accessor> {
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)
Expand All @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions core/src/services/foundationdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@

mod backend;

pub use backend::FoundationConfig;
pub use backend::FoundationdbBuilder as Foundationdb;
2 changes: 2 additions & 0 deletions core/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 4bb1d76

Please sign in to comment.