Skip to content

Commit 1fd62a7

Browse files
committed
Merge #575: Remove database flush
5ff8320 add private function ivcec_to_u32 in keyvalue (KaFai Choi) e68d3b9 remove Database::flush (KaFai Choi) Pull request description: <!-- You can erase any parts of this template not applicable to your Pull Request. --> ### Description This PR is to remove Database::flush. See this issue for detail #567 ### Notes to the reviewers The 2nd commit is a small refactoring of adding a new private ivec_to_u32 to avoid too much code duplication. Please let me know if it's ok to include this in this PR or I should make it into a separate PR Currently existing test cases are shared across for all Databaes implementation so I am not sure if we should add specific test cases for keyvalue(Tree) for this auto-flush behaviour?(and I feel like it's more a implementation detail). Please let me know how should I proceed for test case in this PR ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### New Features: * [ ] I've added tests for the new feature * [ ] I've added docs for the new feature * [x] I've updated `CHANGELOG.md` #### Bugfixes: * [ ] This pull request breaks the existing API * [ ] I've added tests to reproduce the issue which are now passing * [ ] I'm linking the issue being fixed by this PR ACKs for top commit: danielabrozzoni: re-ACK 5ff8320 Tree-SHA512: eb37de8217efeb89d3ae346da36d0fb55aa67554d591b4759500f793bcf6aa7601c3d717fd473136c88e76aa72dbb6008ecf62b1d4ccf5ba3cbd1598f758522a
2 parents 063d51f + 5ff8320 commit 1fd62a7

File tree

6 files changed

+14
-50
lines changed

6 files changed

+14
-50
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ To decouple the `Wallet` from the `Blockchain` we've made major changes:
5353
- Stop making a request for the block height when calling `Wallet:new`.
5454
- Added `SyncOptions` to capture extra (future) arguments to `Wallet::sync`.
5555
- Removed `max_addresses` sync parameter which determined how many addresses to cache before syncing since this can just be done with `ensure_addresses_cached`.
56+
- remove `flush` method from the `Database` trait.
5657

5758
## [v0.16.1] - [v0.16.0]
5859

src/database/any.rs

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

264260
impl BatchOperations for AnyBatch {

src/database/keyvalue.rs

+13-31
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,9 @@ macro_rules! impl_batch_operations {
166166
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
167167
let key = MapKey::LastIndex(keychain).as_map_key();
168168
let res = self.remove(key);
169-
let res = $process_delete!(res);
170-
171-
match res {
172-
None => Ok(None),
173-
Some(b) => {
174-
let array: [u8; 4] = b.as_ref().try_into().map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
175-
let val = u32::from_be_bytes(array);
176-
Ok(Some(val))
177-
}
178-
}
169+
$process_delete!(res)
170+
.map(ivec_to_u32)
171+
.transpose()
179172
}
180173

181174
fn del_sync_time(&mut self) -> Result<Option<SyncTime>, Error> {
@@ -357,16 +350,7 @@ impl Database for Tree {
357350

358351
fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
359352
let key = MapKey::LastIndex(keychain).as_map_key();
360-
self.get(key)?
361-
.map(|b| -> Result<_, Error> {
362-
let array: [u8; 4] = b
363-
.as_ref()
364-
.try_into()
365-
.map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
366-
let val = u32::from_be_bytes(array);
367-
Ok(val)
368-
})
369-
.transpose()
353+
self.get(key)?.map(ivec_to_u32).transpose()
370354
}
371355

372356
fn get_sync_time(&self) -> Result<Option<SyncTime>, Error> {
@@ -393,19 +377,17 @@ impl Database for Tree {
393377

394378
Some(new.to_be_bytes().to_vec())
395379
})?
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-
})
380+
.map_or(Ok(0), ivec_to_u32)
404381
}
382+
}
405383

406-
fn flush(&mut self) -> Result<(), Error> {
407-
Ok(Tree::flush(self).map(|_| ())?)
408-
}
384+
fn ivec_to_u32(b: sled::IVec) -> Result<u32, Error> {
385+
let array: [u8; 4] = b
386+
.as_ref()
387+
.try_into()
388+
.map_err(|_| Error::InvalidU32Bytes(b.to_vec()))?;
389+
let val = u32::from_be_bytes(array);
390+
Ok(val)
409391
}
410392

411393
impl BatchDatabase for Tree {

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

-7
Original file line numberDiff line numberDiff line change
@@ -158,13 +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-
#[deprecated(
163-
since = "0.18.0",
164-
note = "The flush function is only needed for the sled database on mobile, instead for mobile use the sqlite database."
165-
)]
166-
/// Force changes to be written to disk
167-
fn flush(&mut self) -> Result<(), Error>;
168161
}
169162

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

src/database/sqlite.rs

-4
Original file line numberDiff line numberDiff line change
@@ -891,10 +891,6 @@ impl Database for SqliteDatabase {
891891
}
892892
}
893893
}
894-
895-
fn flush(&mut self) -> Result<(), Error> {
896-
Ok(())
897-
}
898894
}
899895

900896
impl BatchDatabase for SqliteDatabase {

0 commit comments

Comments
 (0)