Skip to main content

opendal_service_sled/
backend.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::sync::Arc;
19
20use super::SLED_SCHEME;
21use super::config::SledConfig;
22use super::core::*;
23use super::deleter::SledDeleter;
24use super::lister::SledLister;
25use super::reader::*;
26use super::writer::SledWriter;
27use opendal_core::raw::*;
28use opendal_core::*;
29
30// https://github.com/spacejam/sled/blob/69294e59c718289ab3cb6bd03ac3b9e1e072a1e7/src/db.rs#L5
31const DEFAULT_TREE_ID: &str = r#"__sled__default"#;
32
33/// Sled services support.
34#[doc = include_str!("docs.md")]
35#[derive(Debug, Default)]
36pub struct SledBuilder {
37    pub(super) config: SledConfig,
38}
39
40impl SledBuilder {
41    /// Set the path to the sled data directory. Will create if not exists.
42    pub fn datadir(mut self, path: &str) -> Self {
43        self.config.datadir = Some(path.into());
44        self
45    }
46
47    /// Set the tree for sled.
48    pub fn tree(mut self, tree: &str) -> Self {
49        self.config.tree = Some(tree.into());
50        self
51    }
52
53    /// Set the root for sled.
54    pub fn root(mut self, root: &str) -> Self {
55        self.config.root = if root.is_empty() {
56            None
57        } else {
58            Some(root.to_string())
59        };
60
61        self
62    }
63}
64
65impl Builder for SledBuilder {
66    type Config = SledConfig;
67
68    fn build(self) -> Result<impl Service> {
69        let datadir_path = self.config.datadir.ok_or_else(|| {
70            Error::new(ErrorKind::ConfigInvalid, "datadir is required but not set")
71                .with_context("service", SLED_SCHEME)
72        })?;
73
74        let db = sled::open(&datadir_path).map_err(|e| {
75            Error::new(ErrorKind::ConfigInvalid, "open db")
76                .with_context("service", SLED_SCHEME)
77                .with_context("datadir", datadir_path.clone())
78                .set_source(e)
79        })?;
80
81        // use "default" tree if not set
82        let tree_name = self
83            .config
84            .tree
85            .unwrap_or_else(|| DEFAULT_TREE_ID.to_string());
86
87        let tree = db.open_tree(&tree_name).map_err(|e| {
88            Error::new(ErrorKind::ConfigInvalid, "open tree")
89                .with_context("service", SLED_SCHEME)
90                .with_context("datadir", datadir_path.clone())
91                .with_context("tree", tree_name.clone())
92                .set_source(e)
93        })?;
94
95        let root = normalize_root(&self.config.root.unwrap_or_default());
96
97        Ok(SledBackend::new(SledCore {
98            datadir: datadir_path,
99            tree,
100        })
101        .with_normalized_root(root))
102    }
103}
104
105/// Backend for sled services.
106#[derive(Clone, Debug)]
107pub struct SledBackend {
108    pub(crate) core: Arc<SledCore>,
109    pub(crate) root: String,
110    pub(crate) info: ServiceInfo,
111    pub(crate) capability: Capability,
112}
113
114impl SledBackend {
115    pub fn new(core: SledCore) -> Self {
116        let info = ServiceInfo::new(SLED_SCHEME, "/", &core.datadir);
117        let capability = Capability {
118            read: true,
119            stat: true,
120            write: true,
121            write_can_empty: true,
122            delete: true,
123            list: true,
124            list_with_recursive: true,
125            ..Default::default()
126        };
127
128        Self {
129            core: Arc::new(core),
130            root: "/".to_string(),
131            info,
132            capability,
133        }
134    }
135
136    fn with_normalized_root(mut self, root: String) -> Self {
137        self.info = self.info.with_root(&root);
138        self.root = root;
139        self
140    }
141}
142
143impl Service for SledBackend {
144    type Reader = oio::StreamReader<SledReader>;
145    type Writer = SledWriter;
146    type Lister = oio::HierarchyLister<SledLister>;
147    type Deleter = oio::OneShotDeleter<SledDeleter>;
148    type Copier = ();
149    type Composer = ();
150
151    fn info(&self) -> ServiceInfo {
152        self.info.clone()
153    }
154
155    fn capability(&self) -> Capability {
156        self.capability
157    }
158
159    async fn create_dir(
160        &self,
161        _ctx: &OperationContext,
162        _path: &str,
163        _args: OpCreateDir,
164    ) -> Result<RpCreateDir> {
165        Err(Error::new(
166            ErrorKind::Unsupported,
167            "operation is not supported",
168        ))
169    }
170
171    async fn stat(&self, _ctx: &OperationContext, path: &str, _: OpStat) -> Result<RpStat> {
172        let p = build_abs_path(&self.root, path);
173
174        if p == build_abs_path(&self.root, "") {
175            Ok(RpStat::new(MetadataBuilder::dir().build()))
176        } else {
177            let bs = self.core.get(&p)?;
178            match bs {
179                Some(bs) => Ok(RpStat::new({
180                    let metadata = MetadataBuilder::file(bs.len() as u64);
181                    metadata.build()
182                })),
183                None => Err(Error::new(ErrorKind::NotFound, "kv not found in sled")),
184            }
185        }
186    }
187    fn read(&self, _ctx: &OperationContext, path: &str, args: OpRead) -> Result<Self::Reader> {
188        let output: oio::StreamReader<SledReader> = {
189            Ok(oio::StreamReader::new(SledReader::new(
190                self.clone(),
191                path,
192                args,
193            )))
194        }?;
195
196        Ok(output)
197    }
198
199    fn write(&self, _ctx: &OperationContext, path: &str, _: OpWrite) -> Result<Self::Writer> {
200        let output: SledWriter = {
201            let p = build_abs_path(&self.root, path);
202            let writer = SledWriter::new(self.core.clone(), p);
203            Ok(writer)
204        }?;
205
206        Ok(output)
207    }
208
209    fn delete(&self, _ctx: &OperationContext) -> Result<Self::Deleter> {
210        let output: oio::OneShotDeleter<SledDeleter> = {
211            let deleter = SledDeleter::new(self.core.clone(), self.root.clone());
212            Ok(oio::OneShotDeleter::new(deleter))
213        }?;
214
215        Ok(output)
216    }
217
218    fn list(&self, _ctx: &OperationContext, path: &str, args: OpList) -> Result<Self::Lister> {
219        let output: oio::HierarchyLister<SledLister> = {
220            let p = build_abs_path(&self.root, path);
221            let lister = SledLister::new(self.core.clone(), self.root.clone(), p)?;
222            Ok(oio::HierarchyLister::new(lister, path, args.recursive()))
223        }?;
224
225        Ok(output)
226    }
227
228    fn copy(
229        &self,
230        _ctx: &OperationContext,
231        _from: &str,
232        _to: &str,
233        _args: OpCopy,
234    ) -> Result<Self::Copier> {
235        Err(Error::new(
236            ErrorKind::Unsupported,
237            "operation is not supported",
238        ))
239    }
240
241    async fn rename(
242        &self,
243        _ctx: &OperationContext,
244        _from: &str,
245        _to: &str,
246        _args: OpRename,
247    ) -> Result<RpRename> {
248        Err(Error::new(
249            ErrorKind::Unsupported,
250            "operation is not supported",
251        ))
252    }
253
254    async fn presign(
255        &self,
256        _ctx: &OperationContext,
257        _path: &str,
258        _args: OpPresign,
259    ) -> Result<RpPresign> {
260        Err(Error::new(
261            ErrorKind::Unsupported,
262            "operation is not supported",
263        ))
264    }
265}