Skip to content

Commit 4962711

Browse files
committed
remove Database::flush and implicitly call it in keyvalue::Databaes impl functions
1 parent 3334c8d commit 4962711

File tree

5 files changed

+30
-36
lines changed

5 files changed

+30
-36
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ To decouple the `Wallet` from the `Blockchain` we've made major changes:
2525
- Stop making a request for the block height when calling `Wallet:new`.
2626
- Added `SyncOptions` to capture extra (future) arguments to `Wallet::sync`.
2727
- Removed `max_addresses` sync parameter which determined how many addresses to cache before syncing since this can just be done with `ensure_addresses_cached`.
28+
- remove `flush` method from the `Database` trait and implicitly call it in keyvalue::Database
29+
2830

2931
## [v0.16.1] - [v0.16.0]
3032

@@ -437,4 +439,4 @@ final transaction is created by calling `finish` on the builder.
437439
[v0.16.0]: https://github.com/bitcoindevkit/bdk/compare/v0.15.0...v0.16.0
438440
[v0.16.1]: https://github.com/bitcoindevkit/bdk/compare/v0.16.0...v0.16.1
439441
[v0.17.0]: https://github.com/bitcoindevkit/bdk/compare/v0.16.1...v0.17.0
440-
[unreleased]: https://github.com/bitcoindevkit/bdk/compare/v0.17.0...HEAD
442+
[unreleased]: https://github.com/bitcoindevkit/bdk/compare/v0.17.0...HEAD

src/database/any.rs

-4
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,6 @@ impl Database for AnyDatabase {
254254
fn increment_last_index(&mut self, keychain: KeychainKind) -> Result<u32, Error> {
255255
impl_inner_method!(AnyDatabase, self, increment_last_index, keychain)
256256
}
257-
258-
fn flush(&mut self) -> Result<(), Error> {
259-
impl_inner_method!(AnyDatabase, self, flush)
260-
}
261257
}
262258

263259
impl BatchOperations for AnyBatch {

src/database/keyvalue.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ impl BatchOperations for Batch {
207207
impl_batch_operations!({}, process_delete_batch);
208208
}
209209

210+
/// Implementation of Database using [`sled::Tree`]. All write operations will be
211+
/// auto-flushed.
210212
impl Database for Tree {
211213
fn check_descriptor_checksum<B: AsRef<[u8]>>(
212214
&mut self,
@@ -224,6 +226,7 @@ impl Database for Tree {
224226
}
225227
} else {
226228
self.insert(&key, bytes.as_ref())?;
229+
Tree::flush(self)?;
227230
Ok(())
228231
}
229232
}
@@ -380,31 +383,31 @@ impl Database for Tree {
380383
// inserts 0 if not present
381384
fn increment_last_index(&mut self, keychain: KeychainKind) -> Result<u32, Error> {
382385
let key = MapKey::LastIndex(keychain).as_map_key();
383-
self.update_and_fetch(key, |prev| {
384-
let new = match prev {
385-
Some(b) => {
386-
let array: [u8; 4] = b.try_into().unwrap_or([0; 4]);
387-
let val = u32::from_be_bytes(array);
388-
389-
val + 1
390-
}
391-
None => 0,
392-
};
393-
394-
Some(new.to_be_bytes().to_vec())
395-
})?
396-
.map_or(Ok(0), |b| -> Result<_, Error> {
397-
let array: [u8; 4] = b
398-
.as_ref()
399-
.try_into()
400-
.map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
401-
let val = u32::from_be_bytes(array);
402-
Ok(val)
403-
})
404-
}
386+
let last_index = self
387+
.update_and_fetch(key, |prev| {
388+
let new = match prev {
389+
Some(b) => {
390+
let array: [u8; 4] = b.try_into().unwrap_or([0; 4]);
391+
let val = u32::from_be_bytes(array);
392+
393+
val + 1
394+
}
395+
None => 0,
396+
};
397+
398+
Some(new.to_be_bytes().to_vec())
399+
})?
400+
.map_or(Ok(0), |b| -> Result<_, Error> {
401+
let array: [u8; 4] = b
402+
.as_ref()
403+
.try_into()
404+
.map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
405+
let val = u32::from_be_bytes(array);
406+
Ok(val)
407+
})?;
405408

406-
fn flush(&mut self) -> Result<(), Error> {
407-
Ok(Tree::flush(self).map(|_| ())?)
409+
Tree::flush(self).map(|_| ())?;
410+
Ok(last_index)
408411
}
409412
}
410413

src/database/memory.rs

-4
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,6 @@ impl Database for MemoryDatabase {
449449

450450
Ok(*value)
451451
}
452-
453-
fn flush(&mut self) -> Result<(), Error> {
454-
Ok(())
455-
}
456452
}
457453

458454
impl BatchDatabase for MemoryDatabase {

src/database/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ pub trait Database: BatchOperations {
158158
///
159159
/// It should insert and return `0` if not present in the database
160160
fn increment_last_index(&mut self, keychain: KeychainKind) -> Result<u32, Error>;
161-
162-
/// Force changes to be written to disk
163-
fn flush(&mut self) -> Result<(), Error>;
164161
}
165162

166163
/// Trait for a database that supports batch operations

0 commit comments

Comments
 (0)