Skip to content

Commit

Permalink
Merge pull request #1136 from spacejam/0.34.1
Browse files Browse the repository at this point in the history
Cut 0.34.1 with a new TransactionalTrees::flush method
  • Loading branch information
spacejam authored Jul 28, 2020
2 parents fe13c8f + 9e566e7 commit fd0dcee
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 0.34.1

## New Features

* #1136 Added the `TransactionalTree::flush` method to
flush the underlying database after the transaction
commits and before the transaction returns.

# 0.34

## Improvements
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sled"
version = "0.34.0"
version = "0.34.1"
authors = ["Tyler Neely <[email protected]>"]
description = "a modern embedded database"
license = "MIT/Apache-2.0"
Expand Down
27 changes: 26 additions & 1 deletion src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub struct TransactionalTree {
pub(super) tree: Tree,
pub(super) writes: Rc<RefCell<Map<IVec, Option<IVec>>>>,
pub(super) read_cache: Rc<RefCell<Map<IVec, Option<IVec>>>>,
pub(super) flush_on_commit: Rc<RefCell<bool>>,
}

/// An error type that is returned from the closure
Expand Down Expand Up @@ -329,6 +330,11 @@ impl TransactionalTree {
Ok(())
}

/// Flush the database before returning from the transaction.
pub fn flush(&self) {
*self.flush_on_commit.borrow_mut() = true;
}

fn unstage(&self) {
unimplemented!()
}
Expand All @@ -352,6 +358,7 @@ impl TransactionalTree {
tree: tree.clone(),
writes: Default::default(),
read_cache: Default::default(),
flush_on_commit: Default::default(),
}
}
}
Expand Down Expand Up @@ -392,6 +399,22 @@ impl TransactionalTrees {
// recovered atomically
peg.seal_batch()
}

fn flush_if_configured(&self) -> Result<()> {
let mut should_flush = None;

for tree in &self.inner {
if *tree.flush_on_commit.borrow() {
should_flush = Some(tree);
break;
}
}

if let Some(tree) = should_flush {
tree.tree.flush()?;
}
Ok(())
}
}

/// A simple constructor for `Err(TransactionError::Abort(_))`
Expand Down Expand Up @@ -427,7 +450,7 @@ pub trait Transactional<E = ()> {
let view = Self::view_overlay(&tt);

// NB locks must exist until this function returns.
let _locks = if let Ok(l) = tt.stage() {
let locks = if let Ok(l) = tt.stage() {
l
} else {
tt.unstage();
Expand All @@ -442,6 +465,8 @@ pub trait Transactional<E = ()> {
Ok(r) => {
let guard = pin();
tt.commit(&guard)?;
drop(locks);
tt.flush_if_configured()?;
return Ok(r);
}
Err(ConflictableTransactionError::Abort(e)) => {
Expand Down
15 changes: 15 additions & 0 deletions tests/test_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,21 @@ fn concurrent_tree_transactions() -> TransactionResult<()> {
Ok(())
}

#[test]
fn tree_flush_in_transaction() {
let config = sled::Config::new().temporary(true);
let db = config.open().unwrap();
let tree = db.open_tree(b"a").unwrap();

tree.transaction::<_, _, sled::transaction::TransactionError>(|tree| {
tree.insert(b"k1", b"cats")?;
tree.insert(b"k2", b"dogs")?;
tree.flush();
Ok(())
})
.unwrap();
}

#[test]
fn incorrect_multiple_db_transactions() -> TransactionResult<()> {
common::setup_logger();
Expand Down

0 comments on commit fd0dcee

Please sign in to comment.