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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use std::ops::Deref; use std::ptr; use lmdb_sys as ffi; use crate::lmdb_error::lmdb_result; use crate::Result; pub struct RoTxn { pub(crate) txn: *mut ffi::MDB_txn, } impl RoTxn { pub(crate) fn new(env: *mut ffi::MDB_env) -> Result<RoTxn> { let mut txn: *mut ffi::MDB_txn = ptr::null_mut(); unsafe { lmdb_result(ffi::mdb_txn_begin( env, ptr::null_mut(), ffi::MDB_RDONLY, &mut txn, ))? }; Ok(RoTxn { txn }) } pub fn abort(self) { drop(self) } pub fn commit(mut self) -> Result<()> { let result = unsafe { lmdb_result(ffi::mdb_txn_commit(self.txn)) }; self.txn = ptr::null_mut(); result.map_err(Into::into) } } impl Drop for RoTxn { fn drop(&mut self) { if !self.txn.is_null() { unsafe { ffi::mdb_txn_abort(self.txn) } } } } pub struct RwTxn { pub(crate) txn: RoTxn, } impl RwTxn { pub(crate) fn new(env: *mut ffi::MDB_env) -> Result<RwTxn> { let mut txn: *mut ffi::MDB_txn = ptr::null_mut(); unsafe { lmdb_result(ffi::mdb_txn_begin(env, ptr::null_mut(), 0, &mut txn))? }; Ok(RwTxn { txn: RoTxn { txn } }) } pub fn commit(self) -> Result<()> { self.txn.commit() } pub fn abort(self) { drop(self.txn) } } impl Deref for RwTxn { type Target = RoTxn; fn deref(&self) -> &Self::Target { &self.txn } }