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