Skip to content

Commit

Permalink
feat: add abort method to RwTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed May 20, 2024
1 parent 3979adf commit cce2e26
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Active development. The API is not stable yet and may change in the future.
- [**update**](https://docs.rs/native_db/latest/native_db/transaction/struct.RwTransaction.html#method.update) an existing item.
- [**remove**](https://docs.rs/native_db/latest/native_db/transaction/struct.RwTransaction.html#method.remove) an existing item.
- [**commit**](https://docs.rs/native_db/latest/native_db/transaction/struct.RwTransaction.html#method.commit) the transaction.
- [**abort**](https://docs.rs/native_db/latest/native_db/transaction/struct.RwTransaction.html#method.abort) the transaction.
- [**migrate**](https://docs.rs/native_db/latest/native_db/transaction/struct.RwTransaction.html#method.migrate) a model.
- plus all read-only transaction APIs.
- **r_transaction** open a read-only transaction.
Expand Down
5 changes: 5 additions & 0 deletions src/transaction/rw_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ impl<'db, 'txn> RwTransaction<'db> {
watch::push_batch(Arc::clone(&self.watcher), batch)?;
Ok(())
}

/// Abort the transaction.
pub fn abort(self) -> Result<()> {
Ok(self.internal.redb_transaction.abort()?)
}
}

impl<'db, 'txn> RwTransaction<'db> {
Expand Down
25 changes: 25 additions & 0 deletions tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ fn test_transaction_obj_1_and_obj_2() {
assert_eq!(result.id, 2);
}

#[test]
fn test_abort_transaction_obj_1_and_obj_2() {
let tf = TmpFs::new().unwrap();

let mut builder = DatabaseBuilder::new();
builder.define::<Item>().unwrap();
builder.define::<Item2>().unwrap();
let db = builder.create(tf.path("test").as_std_path()).unwrap();

let item_1 = Item {
id: 1,
name: "test".to_string(),
};

let rw = db.rw_transaction().unwrap();
rw.insert(item_1).unwrap();
rw.abort().unwrap();
// After abort, the transaction, the transaction can not be used anymore.
//rw.insert(item_2).unwrap();
//rw.commit().unwrap();

let r = db.r_transaction().unwrap();
assert!(r.get().primary::<Item>(1u32).unwrap().is_none());
}

#[allow(unreachable_code)]
#[test]
fn test_transaction_fail() {
Expand Down

0 comments on commit cce2e26

Please sign in to comment.