Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(service/sled): introduce tree support #2516

Merged
merged 8 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ OPENDAL_SFTP_KNOWN_HOSTS_STRATEGY=<accept|add|strict>
# sled
OPENDAL_SLED_TEST=false
OPENDAL_SLED_DATADIR=/path/to/database
OPENDAL_SLED_TREE=sled-tree
# moka
OPENDAL_MOKA_TEST=false
# ghac
Expand Down
19 changes: 18 additions & 1 deletion .github/workflows/service_test_sled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- uses: actions/checkout@v3
- name: Setup Rust toolchain
uses: ./.github/actions/setup
- name: Test
- name: Test sled
shell: bash
working-directory: core
run: cargo test sled --features services-sled -j=1
Expand All @@ -53,3 +53,20 @@ jobs:
OPENDAL_SLED_TEST: on
OPENDAL_SLED_ROOT: /
OPENDAL_SLED_DATADIR: /tmp/opendal/sled/
sled-with-tree:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Rust toolchain
uses: ./.github/actions/setup
- name: Test sled with tree
oowl marked this conversation as resolved.
Show resolved Hide resolved
shell: bash
working-directory: core
run: cargo test sled --features services-sled -j=1
env:
RUST_BACKTRACE: full
RUST_LOG: debug
OPENDAL_SLED_TEST: on
OPENDAL_SLED_ROOT: /
OPENDAL_SLED_DATADIR: /tmp/opendal/sled/
OPENDAL_SLED_TREE: sled-tree
oowl marked this conversation as resolved.
Show resolved Hide resolved
42 changes: 36 additions & 6 deletions core/src/services/sled/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::str;

use async_trait::async_trait;

Expand All @@ -28,13 +29,17 @@ use crate::ErrorKind;
use crate::Scheme;
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 {
/// That path to the sled data directory.
datadir: Option<String>,
root: Option<String>,
tree: Option<String>,
}

impl SledBuilder {
Expand All @@ -49,6 +54,12 @@ impl SledBuilder {
self.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
}
}

impl Builder for SledBuilder {
Expand All @@ -60,6 +71,7 @@ impl Builder for SledBuilder {

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
}
Expand All @@ -77,9 +89,23 @@ impl Builder for SledBuilder {
.set_source(e)
})?;

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

let tree = db.open_tree(&tree_name).map_err(|e| {
Error::new(ErrorKind::ConfigInvalid, "open tree")
.with_context("service", Scheme::Sled)
.with_context("datadir", datadir_path.clone())
.with_context("tree", tree_name.clone())
.set_source(e)
})?;

Ok(SledBackend::new(Adapter {
datadir: datadir_path,
db,
tree,
})
.with_root(self.root.as_deref().unwrap_or_default()))
}
Expand All @@ -91,7 +117,7 @@ pub type SledBackend = kv::Backend<Adapter>;
#[derive(Clone)]
pub struct Adapter {
datadir: String,
db: sled::Db,
tree: sled::Tree,
}

impl Debug for Adapter {
Expand Down Expand Up @@ -123,15 +149,19 @@ impl kv::Adapter for Adapter {
}

fn blocking_get(&self, path: &str) -> Result<Option<Vec<u8>>> {
Ok(self.db.get(path).map_err(parse_error)?.map(|v| v.to_vec()))
Ok(self
.tree
.get(path)
.map_err(parse_error)?
.map(|v| v.to_vec()))
}

async fn set(&self, path: &str, value: &[u8]) -> Result<()> {
self.blocking_set(path, value)
}

fn blocking_set(&self, path: &str, value: &[u8]) -> Result<()> {
self.db.insert(path, value).map_err(parse_error)?;
self.tree.insert(path, value).map_err(parse_error)?;

Ok(())
}
Expand All @@ -141,7 +171,7 @@ impl kv::Adapter for Adapter {
}

fn blocking_delete(&self, path: &str) -> Result<()> {
self.db.remove(path).map_err(parse_error)?;
self.tree.remove(path).map_err(parse_error)?;

Ok(())
}
Expand All @@ -151,7 +181,7 @@ impl kv::Adapter for Adapter {
}

fn blocking_scan(&self, path: &str) -> Result<Vec<String>> {
let it = self.db.scan_prefix(path).keys();
let it = self.tree.scan_prefix(path).keys();
let mut res = Vec::default();

for i in it {
Expand Down