Skip to main content

opendal_service_redb/
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::fmt::Debug;
19use std::sync::Arc;
20
21use super::REDB_SCHEME;
22use super::config::RedbConfig;
23use super::core::*;
24use super::deleter::RedbDeleter;
25use super::lister::RedbLister;
26use super::reader::*;
27use super::writer::RedbWriter;
28use opendal_core::raw::*;
29use opendal_core::*;
30
31/// Redb service support.
32#[doc = include_str!("docs.md")]
33#[derive(Default)]
34pub struct RedbBuilder {
35    pub(super) config: RedbConfig,
36
37    pub(super) database: Option<Arc<redb::Database>>,
38}
39
40impl Debug for RedbBuilder {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        f.debug_struct("RedbBuilder")
43            .field("config", &self.config)
44            .finish_non_exhaustive()
45    }
46}
47
48impl RedbBuilder {
49    /// Set the database for Redb.
50    ///
51    /// This method should be called when you want to
52    /// use multiple tables of one database because
53    /// Redb doesn't allow opening a database that have been opened.
54    ///
55    /// <div class="warning">
56    ///
57    /// `datadir` and `database` should not be set simultaneously.
58    /// If both are set, `database` will take precedence.
59    ///
60    /// </div>
61    pub fn database(mut self, db: Arc<redb::Database>) -> Self {
62        self.database = Some(db);
63        self
64    }
65
66    /// Set the path to the redb data directory. Will create if not exists.
67    ///
68    ///
69    /// <div class="warning">
70    ///
71    /// Opening redb database via `datadir` takes away the ability to access multiple redb tables.
72    /// If you need to access multiple redb tables, the correct solution is to
73    /// create an `Arc<redb::database>` beforehand and then share it via [`database`]
74    /// with multiple builders where every builder will open one redb table.
75    ///
76    /// </div>
77    ///
78    /// [`database`]: RedbBuilder::database
79    pub fn datadir(mut self, path: &str) -> Self {
80        self.config.datadir = Some(path.into());
81        self
82    }
83
84    /// Set the table name for Redb. Will create if not exists.
85    pub fn table(mut self, table: &str) -> Self {
86        self.config.table = Some(table.into());
87        self
88    }
89
90    /// Set the root for Redb.
91    pub fn root(mut self, path: &str) -> Self {
92        self.config.root = Some(path.into());
93        self
94    }
95}
96
97impl Builder for RedbBuilder {
98    type Config = RedbConfig;
99
100    fn build(self) -> Result<impl Service> {
101        let table_name = self.config.table.ok_or_else(|| {
102            Error::new(ErrorKind::ConfigInvalid, "table is required but not set")
103                .with_context("service", REDB_SCHEME)
104        })?;
105
106        let (datadir, db) = if let Some(db) = self.database {
107            (None, db)
108        } else {
109            let datadir = self.config.datadir.ok_or_else(|| {
110                Error::new(ErrorKind::ConfigInvalid, "datadir is required but not set")
111                    .with_context("service", REDB_SCHEME)
112            })?;
113
114            let db = redb::Database::create(&datadir)
115                .map_err(parse_database_error)?
116                .into();
117
118            (Some(datadir), db)
119        };
120
121        create_table(&db, &table_name)?;
122
123        let root = normalize_root(&self.config.root.unwrap_or_default());
124
125        Ok(RedbBackend::new(RedbCore {
126            datadir,
127            table: table_name,
128            db,
129        })
130        .with_normalized_root(root))
131    }
132}
133
134/// Backend for Redb services.
135#[derive(Clone, Debug)]
136pub struct RedbBackend {
137    pub(crate) core: Arc<RedbCore>,
138    pub(crate) root: String,
139    pub(crate) info: ServiceInfo,
140    pub(crate) capability: Capability,
141}
142
143impl RedbBackend {
144    pub fn new(core: RedbCore) -> Self {
145        let info = ServiceInfo::new(REDB_SCHEME, "/", &core.table);
146        let capability = Capability {
147            read: true,
148            stat: true,
149            write: true,
150            write_can_empty: true,
151            delete: true,
152            list: true,
153            list_with_recursive: true,
154            ..Default::default()
155        };
156
157        Self {
158            core: Arc::new(core),
159            root: "/".to_string(),
160            info,
161            capability,
162        }
163    }
164
165    fn with_normalized_root(mut self, root: String) -> Self {
166        self.info = self.info.with_root(&root);
167        self.root = root;
168        self
169    }
170}
171
172impl Service for RedbBackend {
173    type Reader = oio::StreamReader<RedbReader>;
174    type Writer = RedbWriter;
175    type Lister = oio::HierarchyLister<RedbLister>;
176    type Deleter = oio::OneShotDeleter<RedbDeleter>;
177    type Copier = ();
178    type Composer = ();
179
180    fn info(&self) -> ServiceInfo {
181        self.info.clone()
182    }
183
184    fn capability(&self) -> Capability {
185        self.capability
186    }
187
188    async fn create_dir(
189        &self,
190        _ctx: &OperationContext,
191        _path: &str,
192        _args: OpCreateDir,
193    ) -> Result<RpCreateDir> {
194        Err(Error::new(
195            ErrorKind::Unsupported,
196            "operation is not supported",
197        ))
198    }
199
200    async fn stat(&self, _ctx: &OperationContext, path: &str, _: OpStat) -> Result<RpStat> {
201        let p = build_abs_path(&self.root, path);
202
203        if p == build_abs_path(&self.root, "") {
204            Ok(RpStat::new(MetadataBuilder::dir().build()))
205        } else {
206            let bs = self.core.get(&p)?;
207            match bs {
208                Some(bs) => Ok(RpStat::new({
209                    let metadata = MetadataBuilder::file(bs.len() as u64);
210                    metadata.build()
211                })),
212                None => Err(Error::new(ErrorKind::NotFound, "kv not found in redb")),
213            }
214        }
215    }
216    fn read(&self, _ctx: &OperationContext, path: &str, args: OpRead) -> Result<Self::Reader> {
217        let output: oio::StreamReader<RedbReader> = {
218            Ok(oio::StreamReader::new(RedbReader::new(
219                self.clone(),
220                path,
221                args,
222            )))
223        }?;
224
225        Ok(output)
226    }
227
228    fn write(&self, _ctx: &OperationContext, path: &str, _: OpWrite) -> Result<Self::Writer> {
229        let output: RedbWriter = {
230            let p = build_abs_path(&self.root, path);
231            Ok(RedbWriter::new(self.core.clone(), p))
232        }?;
233
234        Ok(output)
235    }
236
237    fn delete(&self, _ctx: &OperationContext) -> Result<Self::Deleter> {
238        let output: oio::OneShotDeleter<RedbDeleter> = {
239            Ok(oio::OneShotDeleter::new(RedbDeleter::new(
240                self.core.clone(),
241                self.root.clone(),
242            )))
243        }?;
244
245        Ok(output)
246    }
247
248    fn list(&self, _ctx: &OperationContext, path: &str, args: OpList) -> Result<Self::Lister> {
249        let prefix = build_abs_path(&self.root, path);
250        let range = self.core.scan(&prefix)?;
251        let lister = RedbLister::new(self.root.clone(), prefix, range);
252        Ok(oio::HierarchyLister::new(lister, path, args.recursive()))
253    }
254
255    fn copy(
256        &self,
257        _ctx: &OperationContext,
258        _from: &str,
259        _to: &str,
260        _args: OpCopy,
261    ) -> Result<Self::Copier> {
262        Err(Error::new(
263            ErrorKind::Unsupported,
264            "operation is not supported",
265        ))
266    }
267
268    async fn rename(
269        &self,
270        _ctx: &OperationContext,
271        _from: &str,
272        _to: &str,
273        _args: OpRename,
274    ) -> Result<RpRename> {
275        Err(Error::new(
276            ErrorKind::Unsupported,
277            "operation is not supported",
278        ))
279    }
280
281    async fn presign(
282        &self,
283        _ctx: &OperationContext,
284        _path: &str,
285        _args: OpPresign,
286    ) -> Result<RpPresign> {
287        Err(Error::new(
288            ErrorKind::Unsupported,
289            "operation is not supported",
290        ))
291    }
292}