Skip to content

Commit

Permalink
Use redb's recovery callback API (ordinals#2584)
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner authored Nov 22, 2023
1 parent b7b8033 commit 7b4945b
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ use {
ReadableMultimapTable, ReadableTable, RedbKey, RedbValue, StorageError, Table, TableDefinition,
TableHandle, WriteTransaction,
},
std::collections::{BTreeSet, HashMap},
std::io::{BufWriter, Read, Write},
std::{
collections::{BTreeSet, HashMap},
io::{BufWriter, Write},
sync::Once,
},
};

pub(crate) use self::entry::RuneEntry;
Expand Down Expand Up @@ -204,20 +207,6 @@ impl Index {
}
};

if let Ok(mut file) = fs::OpenOptions::new().read(true).open(&path) {
// use cberner's quick hack to check the redb recovery bit
// https://github.com/cberner/redb/issues/639#issuecomment-1628037591
const MAGICNUMBER: [u8; 9] = [b'r', b'e', b'd', b'b', 0x1A, 0x0A, 0xA9, 0x0D, 0x0A];
const RECOVERY_REQUIRED: u8 = 2;

let mut buffer = [0; MAGICNUMBER.len() + 1];
file.read_exact(&mut buffer).unwrap();

if buffer[MAGICNUMBER.len()] & RECOVERY_REQUIRED != 0 {
println!("Index file {:?} needs recovery. This can take a long time, especially for the --index-sats index.", path);
}
}

log::info!("Setting DB cache size to {} bytes", db_cache_size);

let durability = if cfg!(test) {
Expand All @@ -229,8 +218,15 @@ impl Index {
let index_runes;
let index_sats;

let index_path = path.clone();
let once = Once::new();
let database = match Database::builder()
.set_cache_size(db_cache_size)
.set_repair_callback(move |_| {
once.call_once(|| {
println!("Index file `{}` needs recovery. This can take a long time, especially for the --index-sats index.", index_path.display());
})
})
.open(&path)
{
Ok(database) => {
Expand Down

0 comments on commit 7b4945b

Please sign in to comment.