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    const SCHEME: Scheme = Scheme::Redb;
101    type Config = RedbConfig;
102
103    fn build(self) -> Result<impl Access> {
104        let table_name = self.config.table.ok_or_else(|| {
105            Error::new(ErrorKind::ConfigInvalid, "table is required but not set")
106                .with_context("service", Scheme::Redb)
107        })?;
108
109        let (datadir, db) = if let Some(db) = self.database {
110            (None, db)
111        } else {
112            let datadir = self.config.datadir.ok_or_else(|| {
113                Error::new(ErrorKind::ConfigInvalid, "datadir is required but not set")
114                    .with_context("service", Scheme::Redb)
115            })?;
116
117            let db = redb::Database::create(&datadir)
118                .map_err(parse_database_error)?
119                .into();
120
121            (Some(datadir), db)
122        };
123
124        create_table(&db, &table_name)?;
125
126        Ok(RedbBackend::new(Adapter {
127            datadir,
128            table: table_name,
129            db,
130        })
131        .with_root(self.config.root.as_deref().unwrap_or_default()))
132    }
133}
134
135/// Backend for Redb services.
136pub type RedbBackend = kv::Backend<Adapter>;
137
138#[derive(Clone)]
139pub struct Adapter {
140    datadir: Option<String>,
141    table: String,
142    db: Arc<redb::Database>,
143}
144
145impl Debug for Adapter {
146    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
147        let mut ds = f.debug_struct("Adapter");
148        ds.field("path", &self.datadir);
149        ds.finish()
150    }
151}
152
153impl kv::Adapter for Adapter {
154    type Scanner = ();
155
156    fn info(&self) -> kv::Info {
157        kv::Info::new(
158            Scheme::Redb,
159            &self.table,
160            Capability {
161                read: true,
162                write: true,
163                shared: false,
164                ..Default::default()
165            },
166        )
167    }
168
169    async fn get(&self, path: &str) -> Result<Option<Buffer>> {
170        let read_txn = self.db.begin_read().map_err(parse_transaction_error)?;
171
172        let table_define: redb::TableDefinition<&str, &[u8]> =
173            redb::TableDefinition::new(&self.table);
174
175        let table = read_txn
176            .open_table(table_define)
177            .map_err(parse_table_error)?;
178
179        let result = match table.get(path) {
180            Ok(Some(v)) => Ok(Some(v.value().to_vec())),
181            Ok(None) => Ok(None),
182            Err(e) => Err(parse_storage_error(e)),
183        }?;
184        Ok(result.map(Buffer::from))
185    }
186
187    async fn set(&self, path: &str, value: Buffer) -> Result<()> {
188        let write_txn = self.db.begin_write().map_err(parse_transaction_error)?;
189
190        let table_define: redb::TableDefinition<&str, &[u8]> =
191            redb::TableDefinition::new(&self.table);
192
193        {
194            let mut table = write_txn
195                .open_table(table_define)
196                .map_err(parse_table_error)?;
197
198            table
199                .insert(path, &*value.to_vec())
200                .map_err(parse_storage_error)?;
201        }
202
203        write_txn.commit().map_err(parse_commit_error)?;
204        Ok(())
205    }
206
207    async fn delete(&self, path: &str) -> Result<()> {
208        let write_txn = self.db.begin_write().map_err(parse_transaction_error)?;
209
210        let table_define: redb::TableDefinition<&str, &[u8]> =
211            redb::TableDefinition::new(&self.table);
212
213        {
214            let mut table = write_txn
215                .open_table(table_define)
216                .map_err(parse_table_error)?;
217
218            table.remove(path).map_err(parse_storage_error)?;
219        }
220
221        write_txn.commit().map_err(parse_commit_error)?;
222        Ok(())
223    }
224}
225
226fn parse_transaction_error(e: redb::TransactionError) -> Error {
227    Error::new(ErrorKind::Unexpected, "error from redb").set_source(e)
228}
229
230fn parse_table_error(e: redb::TableError) -> Error {
231    match e {
232        redb::TableError::TableDoesNotExist(_) => {
233            Error::new(ErrorKind::NotFound, "error from redb").set_source(e)
234        }
235        _ => Error::new(ErrorKind::Unexpected, "error from redb").set_source(e),
236    }
237}
238
239fn parse_storage_error(e: redb::StorageError) -> Error {
240    Error::new(ErrorKind::Unexpected, "error from redb").set_source(e)
241}
242
243fn parse_database_error(e: redb::DatabaseError) -> Error {
244    Error::new(ErrorKind::Unexpected, "error from redb").set_source(e)
245}
246
247fn parse_commit_error(e: redb::CommitError) -> Error {
248    Error::new(ErrorKind::Unexpected, "error from redb").set_source(e)
249}
250
251/// Check if a table exists, otherwise create it.
252fn create_table(db: &redb::Database, table: &str) -> Result<()> {
253    // Only one `WriteTransaction` is permitted at same time,
254    // applying new one will block until it available.
255    //
256    // So we first try checking table existence via `ReadTransaction`.
257    {
258        let read_txn = db.begin_read().map_err(parse_transaction_error)?;
259
260        let table_define: redb::TableDefinition<&str, &[u8]> = redb::TableDefinition::new(table);
261
262        match read_txn.open_table(table_define) {
263            Ok(_) => return Ok(()),
264            Err(redb::TableError::TableDoesNotExist(_)) => (),
265            Err(e) => return Err(parse_table_error(e)),
266        }
267    }
268
269    {
270        let write_txn = db.begin_write().map_err(parse_transaction_error)?;
271
272        let table_define: redb::TableDefinition<&str, &[u8]> = redb::TableDefinition::new(table);
273
274        write_txn
275            .open_table(table_define)
276            .map_err(parse_table_error)?;
277        write_txn.commit().map_err(parse_commit_error)?;
278    }
279
280    Ok(())
281}