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

feat: update redb to v2 #2120

Merged
merged 17 commits into from
Apr 9, 2024
85 changes: 15 additions & 70 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion iroh-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ data-encoding = { version = "2.3.3", optional = true }
hex = "0.4.3"
multibase = { version = "0.9.1", optional = true }
postcard = { version = "1", default-features = false, features = ["alloc", "use-std", "experimental-derive"], optional = true }
redb = { version = "1.5.1", optional = true }
redb = { version = "2.0.0", optional = true }
serde = { version = "1", features = ["derive"] }
serde-error = "0.1.2"
thiserror = "1"
Expand Down
6 changes: 3 additions & 3 deletions iroh-base/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub struct HashAndFormat {
mod redb_support {
use super::{Hash, HashAndFormat};
use postcard::experimental::max_size::MaxSize;
use redb::{RedbKey, RedbValue};
use redb::{Key as RedbKey, Value as RedbValue};

impl RedbValue for Hash {
type SelfType<'a> = Self;
Expand Down Expand Up @@ -421,7 +421,7 @@ mod tests {
#[cfg(feature = "redb")]
#[test]
fn hash_redb() {
use redb::RedbValue;
use redb::Value as RedbValue;
let bytes: [u8; 32] = (0..32).collect::<Vec<_>>().as_slice().try_into().unwrap();
let hash = Hash::from(bytes);
assert_eq!(<Hash as RedbValue>::fixed_width(), Some(32));
Expand All @@ -446,7 +446,7 @@ mod tests {
#[cfg(feature = "redb")]
#[test]
fn hash_and_format_redb() {
use redb::RedbValue;
use redb::Value as RedbValue;
let hash_bytes: [u8; 32] = (0..32).collect::<Vec<_>>().as_slice().try_into().unwrap();
let hash = Hash::from(hash_bytes);
let haf = HashAndFormat::raw(hash);
Expand Down
6 changes: 4 additions & 2 deletions iroh-bytes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ postcard = { version = "1", default-features = false, features = ["alloc", "use-
quinn = "0.10"
rand = "0.8"
range-collections = "0.4.0"
redb = { version = "1.5.1", optional = true }
redb = { version = "2.0.0", optional = true }
redb_v1 = { package = "redb", version = "1.5.1", optional = true }
reflink-copy = { version = "0.1.8", optional = true }
self_cell = "1.0.1"
serde = { version = "1", features = ["derive"] }
serde-error = "0.1.2"
smallvec = { version = "1.10.0", features = ["serde", "const_new"] }
tempfile = { version = "3.10.0", optional = true }
thiserror = "1"
tokio = { version = "1", features = ["fs"] }
tokio-util = { version = "0.7", features = ["io-util", "io", "rt"] }
Expand All @@ -64,7 +66,7 @@ tempfile = "3.10.0"

[features]
default = ["fs-store"]
fs-store = ["reflink-copy", "redb"]
fs-store = ["reflink-copy", "redb", "redb_v1", "tempfile"]
downloader = ["iroh-net", "parking_lot", "tokio-util/time"]
metrics = ["iroh-metrics"]

Expand Down
16 changes: 13 additions & 3 deletions iroh-bytes/src/store/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ use futures::{channel::oneshot, Stream, StreamExt};

use iroh_base::hash::{BlobFormat, Hash, HashAndFormat};
use iroh_io::AsyncSliceReader;
use redb::{AccessGuard, ReadableTable, StorageError};
use redb::{AccessGuard, DatabaseError, ReadableTable, StorageError};
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use tokio::io::AsyncWriteExt;
use tracing::trace_span;

mod import_flat_store;
mod migrate_redb_v1_v2;
mod tables;
#[doc(hidden)]
pub mod test_support;
Expand Down Expand Up @@ -307,7 +308,7 @@ impl EntryState {
}
}

impl redb::RedbValue for EntryState {
impl redb::Value for EntryState {
type SelfType<'a> = EntryState;

type AsBytes<'a> = SmallVec<[u8; 128]>;
Expand Down Expand Up @@ -1218,6 +1219,8 @@ pub(crate) enum ActorError {
Io(#[from] io::Error),
#[error("inconsistent database state: {0}")]
Inconsistent(String),
#[error("error during database migration: {0}")]
Migration(#[source] anyhow::Error),
}

impl From<ActorError> for io::Error {
Expand Down Expand Up @@ -1435,7 +1438,14 @@ impl Actor {
temp: Arc<RwLock<TempCounterMap>>,
rt: tokio::runtime::Handle,
) -> ActorResult<(Self, flume::Sender<ActorMessage>)> {
let db = redb::Database::create(path)?;
let db = match redb::Database::create(path) {
Ok(db) => db,
Err(DatabaseError::UpgradeRequired(1)) => {
migrate_redb_v1_v2::run(path).map_err(ActorError::Migration)?
}
Err(err) => return Err(err.into()),
};

let txn = db.begin_write()?;
// create tables and drop them just to create them.
let mut t = Default::default();
Expand Down
Loading
Loading