reth_libmdbx/
database.rs
1use crate::{
2 error::{mdbx_result, Result},
3 transaction::TransactionKind,
4 Environment, Transaction,
5};
6use ffi::MDBX_db_flags_t;
7use std::{ffi::CStr, ptr};
8
9#[derive(Debug)]
13pub struct Database {
14 dbi: ffi::MDBX_dbi,
15 _env: Option<Environment>,
18}
19
20impl Database {
21 pub(crate) fn new<K: TransactionKind>(
26 txn: &Transaction<K>,
27 name: Option<&str>,
28 flags: MDBX_db_flags_t,
29 ) -> Result<Self> {
30 let mut c_name_buf = smallvec::SmallVec::<[u8; 32]>::new();
31 let c_name = name.map(|n| {
32 c_name_buf.extend_from_slice(n.as_bytes());
33 c_name_buf.push(0);
34 CStr::from_bytes_with_nul(&c_name_buf).unwrap()
35 });
36 let name_ptr = if let Some(c_name) = c_name { c_name.as_ptr() } else { ptr::null() };
37 let mut dbi: ffi::MDBX_dbi = 0;
38 txn.txn_execute(|txn_ptr| {
39 mdbx_result(unsafe { ffi::mdbx_dbi_open(txn_ptr, name_ptr, flags, &mut dbi) })
40 })??;
41 Ok(Self::new_from_ptr(dbi, txn.env().clone()))
42 }
43
44 pub(crate) const fn new_from_ptr(dbi: ffi::MDBX_dbi, env: Environment) -> Self {
45 Self { dbi, _env: Some(env) }
46 }
47
48 pub const fn freelist_db() -> Self {
50 Self { dbi: 0, _env: None }
51 }
52
53 pub const fn dbi(&self) -> ffi::MDBX_dbi {
58 self.dbi
59 }
60}
61
62unsafe impl Send for Database {}
63unsafe impl Sync for Database {}