Skip to content

Commit fd0dcee

Browse files
authored
Merge pull request #1136 from spacejam/0.34.1
Cut 0.34.1 with a new TransactionalTrees::flush method
2 parents fe13c8f + 9e566e7 commit fd0dcee

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# 0.34.1
2+
3+
## New Features
4+
5+
* #1136 Added the `TransactionalTree::flush` method to
6+
flush the underlying database after the transaction
7+
commits and before the transaction returns.
8+
19
# 0.34
210

311
## Improvements

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sled"
3-
version = "0.34.0"
3+
version = "0.34.1"
44
authors = ["Tyler Neely <[email protected]>"]
55
description = "a modern embedded database"
66
license = "MIT/Apache-2.0"

src/transaction.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub struct TransactionalTree {
9696
pub(super) tree: Tree,
9797
pub(super) writes: Rc<RefCell<Map<IVec, Option<IVec>>>>,
9898
pub(super) read_cache: Rc<RefCell<Map<IVec, Option<IVec>>>>,
99+
pub(super) flush_on_commit: Rc<RefCell<bool>>,
99100
}
100101

101102
/// An error type that is returned from the closure
@@ -329,6 +330,11 @@ impl TransactionalTree {
329330
Ok(())
330331
}
331332

333+
/// Flush the database before returning from the transaction.
334+
pub fn flush(&self) {
335+
*self.flush_on_commit.borrow_mut() = true;
336+
}
337+
332338
fn unstage(&self) {
333339
unimplemented!()
334340
}
@@ -352,6 +358,7 @@ impl TransactionalTree {
352358
tree: tree.clone(),
353359
writes: Default::default(),
354360
read_cache: Default::default(),
361+
flush_on_commit: Default::default(),
355362
}
356363
}
357364
}
@@ -392,6 +399,22 @@ impl TransactionalTrees {
392399
// recovered atomically
393400
peg.seal_batch()
394401
}
402+
403+
fn flush_if_configured(&self) -> Result<()> {
404+
let mut should_flush = None;
405+
406+
for tree in &self.inner {
407+
if *tree.flush_on_commit.borrow() {
408+
should_flush = Some(tree);
409+
break;
410+
}
411+
}
412+
413+
if let Some(tree) = should_flush {
414+
tree.tree.flush()?;
415+
}
416+
Ok(())
417+
}
395418
}
396419

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

429452
// NB locks must exist until this function returns.
430-
let _locks = if let Ok(l) = tt.stage() {
453+
let locks = if let Ok(l) = tt.stage() {
431454
l
432455
} else {
433456
tt.unstage();
@@ -442,6 +465,8 @@ pub trait Transactional<E = ()> {
442465
Ok(r) => {
443466
let guard = pin();
444467
tt.commit(&guard)?;
468+
drop(locks);
469+
tt.flush_if_configured()?;
445470
return Ok(r);
446471
}
447472
Err(ConflictableTransactionError::Abort(e)) => {

tests/test_tree.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,21 @@ fn concurrent_tree_transactions() -> TransactionResult<()> {
504504
Ok(())
505505
}
506506

507+
#[test]
508+
fn tree_flush_in_transaction() {
509+
let config = sled::Config::new().temporary(true);
510+
let db = config.open().unwrap();
511+
let tree = db.open_tree(b"a").unwrap();
512+
513+
tree.transaction::<_, _, sled::transaction::TransactionError>(|tree| {
514+
tree.insert(b"k1", b"cats")?;
515+
tree.insert(b"k2", b"dogs")?;
516+
tree.flush();
517+
Ok(())
518+
})
519+
.unwrap();
520+
}
521+
507522
#[test]
508523
fn incorrect_multiple_db_transactions() -> TransactionResult<()> {
509524
common::setup_logger();

0 commit comments

Comments
 (0)