opendal/services/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::fmt::Formatter;
20use std::sync::Arc;
21
22use crate::raw::adapters::kv;
23use crate::raw::*;
24use crate::services::RedbConfig;
25use crate::Builder;
26use crate::Error;
27use crate::ErrorKind;
28use crate::Scheme;
29use crate::*;
30
31impl Configurator for RedbConfig {
32    type Builder = RedbBuilder;
33    fn into_builder(self) -> Self::Builder {
34        RedbBuilder {
35            config: self,
36            database: None,
37        }
38    }
39}
40
41/// Redb service support.
42#[doc = include_str!("docs.md")]
43#[derive(Default, Debug)]
44pub struct RedbBuilder {
45    config: RedbConfig,
46
47    database: Option<Arc<redb::Database>>,
48}
49
50impl RedbBuilder {
51    /// Set the database for Redb.
52    ///
53    /// This method should be called when you want to
54    /// use multiple tables of one database because
55    /// Redb doesn't allow opening a database that have been opened.
56    ///
57    /// <div class="warning">
58    ///
59    /// `datadir` and `database` should not be set simultaneously.
60    /// If both are set, `database` will take precedence.
61    ///
62    /// </div>
63    pub fn database(mut self, db: Arc<redb::Database>) -> Self {
64        self.database = Some(db);
65        self
66    }
67
68    /// Set the path to the redb data directory. Will create if not exists.
69    ///
70    ///
71    /// <div class="warning">
72    ///
73    /// Opening redb database via `datadir` takes away the ability to access multiple redb tables.
74    /// If you need to access multiple redb tables, the correct solution is to
75    /// create an `Arc<redb::database>` beforehand and then share it via [`database`]
76    /// with multiple builders where every builder will open one redb table.
77    ///
78    /// </div>
79    ///
80    /// [`database`]: RedbBuilder::database
81    pub fn datadir(mut self, path: &str) -> Self {
82        self.config.datadir = Some(path.into());
83        self
84    }
85
86    /// Set the table name for Redb. Will create if not exists.
87    pub fn table(mut self, table: &str) -> Self {
88        self.config.table = Some(table.into());
89        self
90    }
91
92    /// Set the root for Redb.
93    pub fn root(mut self, path: &str) -> Self {
94        self.config.root = Some(path.into());
95        self
96    }
97}
98
99impl Builder for RedbBuilder {
100    type Config = RedbConfig;
101
102    fn build(self) -> Result<impl Access> {
103        let table_name = self.config.table.ok_or_else(|| {
104            Error::new(ErrorKind::ConfigInvalid, "table is required but not set")
105                .with_context("service", Scheme::Redb)
106        })?;
107
108        let (datadir, db) = if let Some(db) = self.database {
109            (None, db)
110        } else {
111            let datadir = self.config.datadir.ok_or_else(|| {
112                Error::new(ErrorKind::ConfigInvalid, "datadir is required but not set")
113                    .with_context("service", Scheme::Redb)
114            })?;
115
116            let db = redb::Database::create(&datadir)
117                .map_err(parse_database_error)?
118                .into();
119
120            (Some(datadir), db)
121        };
122
123        create_table(&db, &table_name)?;
124
125        Ok(RedbBackend::new(Adapter {
126            datadir,
127            table: table_name,
128            db,
129        })
130        .with_root(self.config.root.as_deref().unwrap_or_default()))
131    }
132}
133
134/// Backend for Redb services.
135pub type RedbBackend = kv::Backend<Adapter>;
136
137#[derive(Clone)]
138pub struct Adapter {
139    datadir: Option<String>,
140    table: String,
141    db: Arc<redb::Database>,
142}
143
144impl Debug for Adapter {
145    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
146        let mut ds = f.debug_struct("Adapter");
147        ds.field("path", &self.datadir);
148        ds.finish()
149    }
150}
151
152impl kv::Adapter for Adapter {
153    type Scanner = ();
154
155    fn info(&self) -> kv::Info {
156        kv::Info::new(
157            Scheme::Redb,
158            &self.table,
159            Capability {
160                read: true,
161                write: true,
162                shared: false,
163                ..Default::default()
164            },
165        )
166    }
167
168    async fn get(&self, path: &str) -> Result<Option<Buffer>> {
169        let read_txn = self.db.begin_read().map_err(parse_transaction_error)?;
170
171        let table_define: redb::TableDefinition<&str, &[u8]> =
172            redb::TableDefinition::new(&self.table);
173
174        let table = read_txn
175            .open_table(table_define)
176            .map_err(parse_table_error)?;
177
178        let result = match table.get(path) {
179            Ok(Some(v)) => Ok(Some(v.value().to_vec())),
180            Ok(None) => Ok(None),
181            Err(e) => Err(parse_storage_error(e)),
182        }?;
183        Ok(result.map(Buffer::from))
184    }
185
186    async fn set(&self, path: &str, value: Buffer) -> Result<()> {
187        let write_txn = self.db.begin_write().map_err(parse_transaction_error)?;
188
189        let table_define: redb::TableDefinition<&str, &[u8]> =
190            redb::TableDefinition::new(&self.table);
191
192        {
193            let mut table = write_txn
194                .open_table(table_define)
195                .map_err(parse_table_error)?;
196
197            table
198                .insert(path, &*value.to_vec())
199                .map_err(parse_storage_error)?;
200        }
201
202        write_txn.commit().map_err(parse_commit_error)?;
203        Ok(())
204    }
205
206    async fn delete(&self, path: &str) -> Result<()> {
207        let write_txn = self.db.begin_write().map_err(parse_transaction_error)?;
208
209        let table_define: redb::TableDefinition<&str, &[u8]> =
210            redb::TableDefinition::new(&self.table);
211
212        {
213            let mut table = write_txn
214                .open_table(table_define)
215                .map_err(parse_table_error)?;
216
217            table.remove(path).map_err(parse_storage_error)?;
218        }
219
220        write_txn.commit().map_err(parse_commit_error)?;
221        Ok(())
222    }
223}
224
225fn parse_transaction_error(e: redb::TransactionError) -> Error {
226    Error::new(ErrorKind::Unexpected, "error from redb").set_source(e)
227}
228
229fn parse_table_error(e: redb::TableError) -> Error {
230    match e {
231        redb::TableError::TableDoesNotExist(_) => {
232            Error::new(ErrorKind::NotFound, "error from redb").set_source(e)
233        }
234        _ => Error::new(ErrorKind::Unexpected, "error from redb").set_source(e),
235    }
236}
237
238fn parse_storage_error(e: redb::StorageError) -> Error {
239    Error::new(ErrorKind::Unexpected, "error from redb").set_source(e)
240}
241
242fn parse_database_error(e: redb::DatabaseError) -> Error {
243    Error::new(ErrorKind::Unexpected, "error from redb").set_source(e)
244}
245
246fn parse_commit_error(e: redb::CommitError) -> Error {
247    Error::new(ErrorKind::Unexpected, "error from redb").set_source(e)
248}
249
250/// Check if a table exists, otherwise create it.
251fn create_table(db: &redb::Database, table: &str) -> Result<()> {
252    // Only one `WriteTransaction` is permitted at same time,
253    // applying new one will block until it available.
254    //
255    // So we first try checking table existence via `ReadTransaction`.
256    {
257        let read_txn = db.begin_read().map_err(parse_transaction_error)?;
258
259        let table_define: redb::TableDefinition<&str, &[u8]> = redb::TableDefinition::new(table);
260
261        match read_txn.open_table(table_define) {
262            Ok(_) => return Ok(()),
263            Err(redb::TableError::TableDoesNotExist(_)) => (),
264            Err(e) => return Err(parse_table_error(e)),
265        }
266    }
267
268    {
269        let write_txn = db.begin_write().map_err(parse_transaction_error)?;
270
271        let table_define: redb::TableDefinition<&str, &[u8]> = redb::TableDefinition::new(table);
272
273        write_txn
274            .open_table(table_define)
275            .map_err(parse_table_error)?;
276        write_txn.commit().map_err(parse_commit_error)?;
277    }
278
279    Ok(())
280}