nostrdb-rs

nostrdb in rust!
git clone git://jb55.com/nostrdb-rs
Log | Files | Refs | Submodules | README | LICENSE

transaction.rs (2228B)


      1 use crate::bindings;
      2 use crate::error::Error;
      3 use crate::ndb::Ndb;
      4 use crate::result::Result;
      5 
      6 /// A `nostrdb` transaction. Only one is allowed to be active per thread.
      7 #[derive(Debug)]
      8 pub struct Transaction {
      9     txn: bindings::ndb_txn,
     10 }
     11 
     12 impl Transaction {
     13     /// Create a new `nostrdb` transaction. These are reference counted
     14     pub fn new(ndb: &Ndb) -> Result<Self> {
     15         // Initialize your transaction here
     16         let mut txn = bindings::ndb_txn::new();
     17         let res = unsafe { bindings::ndb_begin_query(ndb.as_ptr(), &mut txn) };
     18 
     19         if res == 0 {
     20             return Err(Error::TransactionFailed);
     21         }
     22 
     23         Ok(Transaction { txn })
     24     }
     25 
     26     pub fn as_ptr(&self) -> *const bindings::ndb_txn {
     27         &self.txn
     28     }
     29 
     30     pub fn as_mut_ptr(&self) -> *mut bindings::ndb_txn {
     31         self.as_ptr() as *mut bindings::ndb_txn
     32     }
     33 }
     34 
     35 impl Drop for Transaction {
     36     fn drop(&mut self) {
     37         // End the transaction
     38         unsafe {
     39             // Replace with your actual function
     40             bindings::ndb_end_query(&mut self.txn);
     41         }
     42     }
     43 }
     44 
     45 impl bindings::ndb_txn {
     46     fn new() -> Self {
     47         // just create something uninitialized. ndb_begin_query will initialize it for us
     48         let lmdb: *mut bindings::ndb_lmdb = std::ptr::null_mut();
     49         let mdb_txn: *mut ::std::os::raw::c_void = std::ptr::null_mut();
     50         bindings::ndb_txn {
     51             lmdb: lmdb,
     52             mdb_txn: mdb_txn,
     53         }
     54     }
     55 }
     56 
     57 #[cfg(test)]
     58 mod tests {
     59     use super::*;
     60     use crate::config::Config;
     61     use crate::ndb::Ndb;
     62     use crate::test_util;
     63 
     64     #[test]
     65     fn transaction_inheritence_fails() {
     66         let db = "target/testdbs/txn_inheritence_fails";
     67         // Initialize ndb
     68         {
     69             let cfg = Config::new();
     70             let ndb = Ndb::new(db, &cfg).expect("ndb open failed");
     71 
     72             {
     73                 let _txn = Transaction::new(&ndb).expect("txn1 failed");
     74                 let txn2 = Transaction::new(&ndb).expect_err("tx2");
     75                 assert!(txn2 == Error::TransactionFailed);
     76             }
     77 
     78             {
     79                 let _txn = Transaction::new(&ndb).expect("txn1 failed");
     80             }
     81         }
     82 
     83         test_util::cleanup_db(db);
     84     }
     85 }