1use libc::c_uint;
2use std::ffi::CString;
3use std::ptr;
4
5use ffi;
6
7use error::{
8 lmdb_result,
9 Result,
10};
11
12#[derive(Clone, Copy, Debug, Eq, PartialEq)]
16pub struct Database {
17 dbi: ffi::MDB_dbi,
18}
19
20impl Database {
21 pub(crate) unsafe fn new(txn: *mut ffi::MDB_txn, name: Option<&str>, flags: c_uint) -> Result<Database> {
26 let c_name = name.map(|n| CString::new(n).unwrap());
27 let name_ptr = if let Some(ref c_name) = c_name {
28 c_name.as_ptr()
29 } else {
30 ptr::null()
31 };
32 let mut dbi: ffi::MDB_dbi = 0;
33 lmdb_result(ffi::mdb_dbi_open(txn, name_ptr, flags, &mut dbi))?;
34 Ok(Database {
35 dbi,
36 })
37 }
38
39 pub(crate) fn freelist_db() -> Database {
40 Database {
41 dbi: 0,
42 }
43 }
44
45 #[allow(clippy::trivially_copy_pass_by_ref)]
50 pub fn dbi(&self) -> ffi::MDB_dbi {
51 self.dbi
52 }
53}
54
55unsafe impl Sync for Database {}
56unsafe impl Send for Database {}