Skip to content

Commit

Permalink
feat(services/sled): add SledConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
yufan022 committed Mar 12, 2024
1 parent 605cfe0 commit 81108e0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
2 changes: 2 additions & 0 deletions core/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ pub use sftp::SftpConfig;
mod sled;
#[cfg(feature = "services-sled")]
pub use self::sled::Sled;
#[cfg(feature = "services-sled")]
pub use self::sled::SledConfig;

#[cfg(feature = "services-supabase")]
mod supabase;
Expand Down
65 changes: 45 additions & 20 deletions core/src/services/sled/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::fmt::Formatter;
use std::str;

use async_trait::async_trait;
use serde::Deserialize;
use tokio::task;

use crate::raw::adapters::kv;
Expand All @@ -34,32 +35,58 @@ use crate::*;
// https://github.com/spacejam/sled/blob/69294e59c718289ab3cb6bd03ac3b9e1e072a1e7/src/db.rs#L5
const DEFAULT_TREE_ID: &str = r#"__sled__default"#;

/// Sled service support.
#[doc = include_str!("docs.md")]
#[derive(Default)]
pub struct SledBuilder {
/// Config for Sled services support.
#[derive(Default, Deserialize)]
#[serde(default)]
#[non_exhaustive]
pub struct SledConfig {
/// That path to the sled data directory.
datadir: Option<String>,
root: Option<String>,
tree: Option<String>,
}

impl Debug for SledConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SledConfig")
.field("datadir", &self.datadir)
.field("root", &self.root)
.field("tree", &self.tree)
.finish()
}
}

/// Sled services support.
#[doc = include_str!("docs.md")]
#[derive(Default)]
pub struct SledBuilder {
config: SledConfig,
}

impl Debug for SledBuilder {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SledBuilder")
.field("config", &self.config)
.finish()
}
}

impl SledBuilder {
/// Set the path to the sled data directory. Will create if not exists.
pub fn datadir(&mut self, path: &str) -> &mut Self {
self.datadir = Some(path.into());
self.config.datadir = Some(path.into());
self
}

/// Set the root for sled.
pub fn root(&mut self, path: &str) -> &mut Self {
self.root = Some(path.into());
self.config.root = Some(path.into());
self
}

/// Set the tree for sled.
pub fn tree(&mut self, tree: &str) -> &mut Self {
self.tree = Some(tree.into());
self.config.tree = Some(tree.into());
self
}
}
Expand All @@ -69,17 +96,14 @@ impl Builder for SledBuilder {
type Accessor = SledBackend;

fn from_map(map: HashMap<String, String>) -> Self {
let mut builder = SledBuilder::default();

map.get("datadir").map(|v| builder.datadir(v));
map.get("root").map(|v| builder.root(v));
map.get("tree").map(|v| builder.tree(v));

builder
SledBuilder {
config: SledConfig::deserialize(ConfigDeserializer::new(map))
.expect("config deserialize must succeed"),
}
}

fn build(&mut self) -> Result<Self::Accessor> {
let datadir_path = self.datadir.take().ok_or_else(|| {
let datadir_path = self.config.datadir.take().ok_or_else(|| {
Error::new(ErrorKind::ConfigInvalid, "datadir is required but not set")
.with_context("service", Scheme::Sled)
})?;
Expand All @@ -92,10 +116,11 @@ impl Builder for SledBuilder {
})?;

// use "default" tree if not set
let tree_name = match self.tree.take() {
Some(tree) => tree,
None => DEFAULT_TREE_ID.to_string(),
};
let tree_name = self
.config
.tree
.take()
.unwrap_or_else(|| DEFAULT_TREE_ID.to_string());

let tree = db.open_tree(&tree_name).map_err(|e| {
Error::new(ErrorKind::ConfigInvalid, "open tree")
Expand All @@ -109,7 +134,7 @@ impl Builder for SledBuilder {
datadir: datadir_path,
tree,
})
.with_root(self.root.as_deref().unwrap_or_default()))
.with_root(self.config.root.as_deref().unwrap_or_default()))
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/services/sled/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
mod backend;

pub use backend::SledBuilder as Sled;
pub use backend::SledConfig;

0 comments on commit 81108e0

Please sign in to comment.