Skip to content

Commit

Permalink
feat(transaction): complete the manual transations
Browse files Browse the repository at this point in the history
Related functions:
* unqlite_begin
* unqlite_commit
* unqlite_rollback

Cc #1
  • Loading branch information
Huo Linhe committed Mar 7, 2016
1 parent a470b94 commit 9645a8e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ impl Drop for UnQlite {

pub use self::config::*;
pub use self::util::*;
pub use self::transaction::*;

mod openmode;
mod config;
mod util;
mod transaction;

#[cfg(test)]
#[cfg(feature = "enable-threads")]
Expand Down
51 changes: 51 additions & 0 deletions src/engine/transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use super::UnQlite;
use ffi::{unqlite_begin, unqlite_commit, unqlite_rollback};

/// Manual Transaction Manager
impl<'transaction> UnQlite {
/// Manually begin a write-transaction on the specified database handle.
///
/// Begin a write-transaction on the specified database handle. If a write-transaction has
/// already been opened, this function is a no-op.
/// Tip: For maximum concurrency, it is preferable to let UnQLite start the transaction for you
/// automatically. An automatic transaction is started each time upper-layers or client code
/// request a store, delete or an append operation.
///
pub fn begin(&mut self) -> ::Result<()> {
error_or!(unsafe { unqlite_begin(self.db) })
}

/// Commit all changes to the database.
///
/// Commit all changes to the database and release the exclusive lock. In other words, make
/// sure that all changes reaches the disk surface.
///
/// **Note**: Normally, a call to this routine is not necessary since transactions are committed
/// automatically by the engine when the database is closed after lifetime end unless the
/// `disable_auto_commit()` option is set. In which case, you should manually call
/// `commit()`. Otherwise, the database is rolled back.
///
/// **Tip**: For maximum concurrency, it is recommended that you commit your transaction manually
/// as soon as you have no more insertions. Also, for very large insertions (More than 20000),
/// you should call `commit()` periodically to free some memory (A new transaction is
/// started automatically in the next insertion).
pub fn commit(&mut self) -> ::Result<()> {
match error_or!(unsafe { unqlite_commit(self.db) }) {
Ok(_) => Ok(()),
Err(err) => {
let _ = self.rollback();
Err(err)
}
}
}

/// Rollback a write-transaction on the specified database handle.
///
/// If a write transaction is open, then all changes made within the transaction are reverted
/// and the current write-transaction is closed (Dropping all exclusive locks on the target
/// database, deletion of the journal file, etc.). Otherwise this routine is a no-op.
///
fn rollback(&mut self) -> ::Result<()> {
error_or!(unsafe { unqlite_rollback(self.db) })
}
}

0 comments on commit 9645a8e

Please sign in to comment.