db/storage/kvs/redb/
mod.rs1pub mod tx;
2pub mod ty;
3
4use async_trait::async_trait;
5use redb::Database;
6pub use tx::*;
7pub use ty::*;
8
9use crate::{
10 util::{generate_path, get_absolute_path},
11 DBTransaction, DatastoreAdapter, Error, StorageAdapter, StorageAdapterName, StorageVariant,
12};
13pub struct ReDBAdapter(StorageAdapter<DBType>);
14
15#[cfg(feature = "test-suite")]
16crate::full_adapter_test_impl!(ReDBAdapter::default());
17
18impl ReDBAdapter {
19 impl_new_type_adapter!(DBType);
20
21 pub fn new(path: &str) -> Result<ReDBAdapter, Error> {
22 let path = &path["redb:".len()..];
23 let abs_path = get_absolute_path(path);
24 let db_instance = unsafe { Database::create(abs_path.as_str()).unwrap() };
25
26 Ok(ReDBAdapter(StorageAdapter::<DBType>::new(
27 StorageAdapterName::ReDB,
28 path.to_string(),
29 db_instance,
30 StorageVariant::KeyValueStore,
31 )?))
32 }
33}
34
35#[async_trait]
36impl DatastoreAdapter for ReDBAdapter {
37 type Transaction = ReDBTransaction;
38
39 fn default() -> Self {
40 let path = &generate_path("redb", None);
41 ReDBAdapter::new(path).unwrap()
42 }
43
44 fn spawn(&self) -> Self {
45 ReDBAdapter::default()
46 }
47
48 fn path(&self) -> &str {
49 &self.0.path
50 }
51
52 async fn transaction(&self, w: bool) -> Result<Self::Transaction, Error> {
53 let inner = self.get_initialized_inner().unwrap();
54 let db = &inner.db_instance;
55 let tx = db.begin_write().unwrap();
56
57 let tx = unsafe { extend_tx_lifetime(tx) };
58
59 Ok(DBTransaction::<DBType, TxType>::new(tx, db.clone(), w).unwrap())
60 }
61}
62
63unsafe fn extend_tx_lifetime(tx: redb::WriteTransaction<'_>) -> redb::WriteTransaction<'static> {
64 std::mem::transmute::<redb::WriteTransaction<'_>, redb::WriteTransaction<'static>>(tx)
65}