1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::path::Path;

use redb::{DatabaseError, TransactionError};

use super::tx::{ReadTransaction, WriteTransaction};
use crate::tx;

#[derive(Debug)]
pub struct Database(redb::Database);

impl Database {
    pub fn create(path: impl AsRef<Path>) -> Result<Database, DatabaseError> {
        Ok(Self(redb::Database::create(path)?))
    }

    pub fn open(path: impl AsRef<Path>) -> Result<Database, DatabaseError> {
        Ok(Self(redb::Database::open(path)?))
    }

    pub fn begin_read(&self) -> Result<tx::ReadTransaction, TransactionError> {
        Ok(ReadTransaction::from(self.0.begin_read()?))
    }

    pub fn begin_write(&self) -> Result<tx::WriteTransaction, TransactionError> {
        Ok(WriteTransaction::from(self.0.begin_write()?))
    }
}

impl From<redb::Database> for Database {
    fn from(value: redb::Database) -> Self {
        Self(value)
    }
}