libmdbx_remote/
database.rs1use 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 {
37 c_name.as_ptr()
38 } else {
39 ptr::null()
40 };
41 let mut dbi: ffi::MDBX_dbi = 0;
42 txn.txn_execute(|txn_ptr| {
43 mdbx_result(unsafe { ffi::mdbx_dbi_open(txn_ptr, name_ptr, flags, &mut dbi) })
44 })??;
45 Ok(Self::new_from_ptr(dbi, txn.env().clone()))
46 }
47
48 pub(crate) const fn new_from_ptr(dbi: ffi::MDBX_dbi, env: Environment) -> Self {
49 Self {
50 dbi,
51 _env: Some(env),
52 }
53 }
54
55 pub const fn freelist_db() -> Self {
57 Self { dbi: 0, _env: None }
58 }
59
60 pub const fn dbi(&self) -> ffi::MDBX_dbi {
65 self.dbi
66 }
67}
68
69unsafe impl Send for Database {}
70unsafe impl Sync for Database {}