Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cut 0.34.1 with a new TransactionalTrees::flush method #1136

Merged
merged 3 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

* Added the `TransactionalTree::flush` method to
spacejam marked this conversation as resolved.
Show resolved Hide resolved
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 <t@jujit.su>"]
description = "a modern embedded database"
license = "MIT/Apache-2.0"
Expand Down
29 changes: 28 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 @@ -390,7 +397,25 @@ impl TransactionalTrees {
// when the peg drops, it ensures all updates
// written to the log since its creation are
// recovered atomically
peg.seal_batch()
let ret = peg.seal_batch();

ret
}

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(())
}
}

Expand Down Expand Up @@ -442,6 +467,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