diff --git a/Cargo.lock b/Cargo.lock index d037c8a543..8c9f3917ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,9 +95,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.72" +version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" [[package]] name = "cfg-if" @@ -401,9 +401,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.117" +version = "0.2.118" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" +checksum = "06e509672465a0504304aa87f9f176f2b2b716ed8fb105ebe5c02dc6dce96a94" [[package]] name = "log" @@ -582,8 +582,7 @@ dependencies = [ [[package]] name = "redb" version = "0.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "203f19c79e67a9514966531c381db94b9f235989bd504613215cdc80ce30efaa" +source = "git+https://github.com/cberner/redb.git#1d93dbe103f287622c62a915eb7d22321892f3d6" dependencies = [ "libc", "memmap2", diff --git a/Cargo.toml b/Cargo.toml index 6268abd62e..37ae1f4430 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ integer-cbrt = "0.1.2" integer-sqrt = "0.1.5" log = "0.4.14" memmap2 = "0.5.3" -redb = "0.0.4" +redb = { version = "0.0.4", git = "https://github.com/cberner/redb.git" } structopt = "0.3.25" tempfile = "3.2.0" unindent = "0.1.7" diff --git a/src/index.rs b/src/index.rs index 7ccdcf0447..2958d7f8b5 100644 --- a/src/index.rs +++ b/src/index.rs @@ -29,8 +29,18 @@ impl Index { .join(".bitcoin/blocks") }; + let result = unsafe { Database::open("index.redb") }; + + let database = match result { + Ok(database) => database, + Err(redb::Error::Io(error)) if error.kind() == io::ErrorKind::NotFound => unsafe { + Database::create("index.redb", index_size.unwrap_or(1 << 20))? + }, + Err(error) => return Err(error.into()), + }; + let index = Self { - database: unsafe { Database::open("index.redb", index_size.unwrap_or(1 << 20))? }, + database, blocksdir, }; @@ -71,7 +81,7 @@ impl Index { .get(key.as_slice())? .ok_or("Could not find outpoint in index")?; - for chunk in ordinal_ranges.to_value().chunks_exact(16) { + for chunk in ordinal_ranges.chunks_exact(16) { let start = u64::from_le_bytes(chunk[0..8].try_into().unwrap()); let end = u64::from_le_bytes(chunk[8..16].try_into().unwrap()); input_ordinal_ranges.push_back((start, end)); @@ -260,7 +270,6 @@ impl Index { height_to_hash .get(&0)? .ok_or("Could not find genesis block in index")? - .to_value() .to_vec(), 0, )]; @@ -300,14 +309,13 @@ impl Index { match heights_to_hash.get(&height)? { None => Ok(None), Some(guard) => { - let hash = guard.to_value(); + let hash = guard; let hash_to_location: ReadOnlyTable<[u8], u64> = tx.open_table(Self::HASH_TO_LOCATION)?; let location = hash_to_location .get(hash)? - .ok_or("Could not find block location in index")? - .to_value(); + .ok_or("Could not find block location in index")?; let path = self.blockfile_path(location >> 32); @@ -335,7 +343,7 @@ impl Index { .ok_or("Could not find outpoint in index")?; let mut output = Vec::new(); - for chunk in ordinal_ranges.to_value().chunks_exact(16) { + for chunk in ordinal_ranges.chunks_exact(16) { let start = u64::from_le_bytes(chunk[0..8].try_into().unwrap()); let end = u64::from_le_bytes(chunk[8..16].try_into().unwrap()); output.push((start, end)); diff --git a/src/main.rs b/src/main.rs index 2c93dc5d53..920e53362b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ use { collections::VecDeque, fmt::{self, Display, Formatter}, fs::File, + io, ops::{Add, AddAssign, Deref, Sub}, path::{Path, PathBuf}, process,