Skip to content

Commit

Permalink
Don't recreate db every run (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
terror authored Feb 16, 2022
1 parent 73e5756 commit e5853d7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
11 changes: 5 additions & 6 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 15 additions & 7 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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,
)];
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use {
collections::VecDeque,
fmt::{self, Display, Formatter},
fs::File,
io,
ops::{Add, AddAssign, Deref, Sub},
path::{Path, PathBuf},
process,
Expand Down

0 comments on commit e5853d7

Please sign in to comment.