Skip to content

Commit 52f2061

Browse files
committed
refactor(store)!: change Store's method and error names
The changes in this commit were motivated due to a bug in the `StoreFile` which caused old data to be lost if the file was `open` instead of created and new data was appended. The bugfix later motivated a general name cleanup in StoreFile's methods and errors and some minor changes in their signatures. FileError was renamed to StoreError, which now includes the IterError variants, allowing the remplacement of the former form. The new StoreFile methods are: - create: create file in write only mode or fail if file exists. - load: open existing file, check integrity of content and retrieve Store. - append: add new changesets to Store. Do nothing if changeset is empty. - dump: aggregate and retrieve all stored changesets in store. - load_or_create: load if file exists, create if not, and retrieve Store.
1 parent fc76d66 commit 52f2061

File tree

8 files changed

+392
-261
lines changed

8 files changed

+392
-261
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ The project is split up into several crates in the `/crates` directory:
4242
- [`wallet`](./crates/wallet): Contains the central high level `Wallet` type that is built from the low-level mechanisms provided by the other components
4343
- [`chain`](./crates/chain): Tools for storing and indexing chain data
4444
- [`persist`](./crates/persist): Types that define data persistence of a BDK wallet
45-
- [`file_store`](./crates/file_store): A (experimental) persistence backend for storing chain data in a single file.
45+
- [`file_store`](./crates/file_store): Persistence backend for storing chain data in a single file. Intended for testing and development purposes, not for production.
4646
- [`esplora`](./crates/esplora): Extends the [`esplora-client`] crate with methods to fetch chain data from an esplora HTTP server in the form that [`bdk_chain`] and `Wallet` can consume.
4747
- [`electrum`](./crates/electrum): Extends the [`electrum-client`] crate with methods to fetch chain data from an electrum server in the form that [`bdk_chain`] and `Wallet` can consume.
4848

crates/file_store/src/entry_iter.rs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::StoreError;
12
use bincode::Options;
23
use std::{
34
fs::File,
@@ -37,7 +38,7 @@ impl<T> Iterator for EntryIter<'_, T>
3738
where
3839
T: serde::de::DeserializeOwned,
3940
{
40-
type Item = Result<T, IterError>;
41+
type Item = Result<T, StoreError>;
4142

4243
fn next(&mut self) -> Option<Self::Item> {
4344
if self.finished {
@@ -63,7 +64,7 @@ where
6364
}
6465
}
6566
self.db_file.seek(io::SeekFrom::Start(pos_before_read))?;
66-
Err(IterError::Bincode(*e))
67+
Err(StoreError::Bincode(*e))
6768
}
6869
}
6970
})()
@@ -80,29 +81,3 @@ impl<T> Drop for EntryIter<'_, T> {
8081
}
8182
}
8283
}
83-
84-
/// Error type for [`EntryIter`].
85-
#[derive(Debug)]
86-
pub enum IterError {
87-
/// Failure to read from the file.
88-
Io(io::Error),
89-
/// Failure to decode data from the file.
90-
Bincode(bincode::ErrorKind),
91-
}
92-
93-
impl core::fmt::Display for IterError {
94-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
95-
match self {
96-
IterError::Io(e) => write!(f, "io error trying to read entry {}", e),
97-
IterError::Bincode(e) => write!(f, "bincode error while reading entry {}", e),
98-
}
99-
}
100-
}
101-
102-
impl From<io::Error> for IterError {
103-
fn from(value: io::Error) -> Self {
104-
IterError::Io(value)
105-
}
106-
}
107-
108-
impl std::error::Error for IterError {}

crates/file_store/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) fn bincode_options() -> impl bincode::Options {
1313

1414
/// Error that occurs due to problems encountered with the file.
1515
#[derive(Debug)]
16-
pub enum FileError {
16+
pub enum StoreError {
1717
/// IO error, this may mean that the file is too short.
1818
Io(io::Error),
1919
/// Magic bytes do not match what is expected.
@@ -22,7 +22,7 @@ pub enum FileError {
2222
Bincode(bincode::ErrorKind),
2323
}
2424

25-
impl core::fmt::Display for FileError {
25+
impl core::fmt::Display for StoreError {
2626
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2727
match self {
2828
Self::Io(e) => write!(f, "io error trying to read file: {}", e),
@@ -36,10 +36,10 @@ impl core::fmt::Display for FileError {
3636
}
3737
}
3838

39-
impl From<io::Error> for FileError {
39+
impl From<io::Error> for StoreError {
4040
fn from(value: io::Error) -> Self {
4141
Self::Io(value)
4242
}
4343
}
4444

45-
impl std::error::Error for FileError {}
45+
impl std::error::Error for StoreError {}

0 commit comments

Comments
 (0)