Skip to content

Commit

Permalink
Update redb from 0.13.0 to 1.0.2 (#2141)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Jun 29, 2023
1 parent 76373c3 commit 402d632
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 67 deletions.
8 changes: 4 additions & 4 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 @@ -41,7 +41,7 @@ mime_guess = "2.0.4"
miniscript = "9.0.1"
mp4 = "0.13.0"
ord-bitcoincore-rpc = "0.16.5"
redb = "0.13.0"
redb = "1.0.1"
regex = "1.6.0"
rss = "2.0.1"
rust-embed = "6.4.0"
Expand Down
132 changes: 72 additions & 60 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use {
log::log_enabled,
redb::{
Database, MultimapTable, MultimapTableDefinition, ReadableMultimapTable, ReadableTable, Table,
TableDefinition, WriteStrategy, WriteTransaction,
TableDefinition, WriteTransaction,
},
std::collections::HashMap,
std::io::{BufWriter, Write},
Expand Down Expand Up @@ -99,19 +99,19 @@ impl From<Statistic> for u64 {
#[derive(Serialize)]
pub(crate) struct Info {
pub(crate) blocks_indexed: u64,
pub(crate) branch_pages: usize,
pub(crate) fragmented_bytes: usize,
pub(crate) branch_pages: u64,
pub(crate) fragmented_bytes: u64,
pub(crate) index_file_size: u64,
pub(crate) index_path: PathBuf,
pub(crate) leaf_pages: usize,
pub(crate) metadata_bytes: usize,
pub(crate) leaf_pages: u64,
pub(crate) metadata_bytes: u64,
pub(crate) outputs_traversed: u64,
pub(crate) page_size: usize,
pub(crate) sat_ranges: u64,
pub(crate) stored_bytes: usize,
pub(crate) stored_bytes: u64,
pub(crate) transactions: Vec<TransactionInfo>,
pub(crate) tree_height: usize,
pub(crate) utxos_indexed: usize,
pub(crate) tree_height: u32,
pub(crate) utxos_indexed: u64,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -159,7 +159,7 @@ impl Index {
data_dir.join("index.redb")
};

let database = match unsafe { Database::builder().open_mmapped(&path) } {
let database = match Database::open(&path) {
Ok(database) => {
let schema_version = database
.begin_read()?
Expand All @@ -185,23 +185,15 @@ impl Index {

database
}
Err(redb::Error::Io(error)) if error.kind() == io::ErrorKind::NotFound => {
let database = unsafe {
Database::builder()
.set_write_strategy(if cfg!(test) {
WriteStrategy::Checksum
} else {
WriteStrategy::TwoPhase
})
.create_mmapped(&path)?
};
let tx = database.begin_write()?;
Err(_) => {
let database = Database::builder().create(&path)?;

let mut tx = database.begin_write()?;

#[cfg(test)]
let tx = {
let mut tx = tx;
if cfg!(test) {
tx.set_durability(redb::Durability::None);
tx
} else {
tx.set_durability(redb::Durability::Immediate);
};

tx.open_table(HEIGHT_TO_BLOCK_HASH)?;
Expand All @@ -227,7 +219,6 @@ impl Index {

database
}
Err(error) => return Err(error.into()),
};

let genesis_block_coinbase_transaction =
Expand Down Expand Up @@ -307,7 +298,7 @@ impl Index {
pub(crate) fn has_sat_index(&self) -> Result<bool> {
match self.begin_read()?.0.open_table(OUTPOINT_TO_SAT_RANGES) {
Ok(_) => Ok(true),
Err(redb::Error::TableDoesNotExist(_)) => Ok(false),
Err(redb::TableError::TableDoesNotExist(_)) => Ok(false),
Err(err) => Err(err.into()),
}
}
Expand Down Expand Up @@ -342,6 +333,7 @@ impl Index {
.range(0..)?
.rev()
.next()
.and_then(|result| result.ok())
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0),
branch_pages: stats.branch_pages(),
Expand All @@ -356,12 +348,14 @@ impl Index {
transactions: wtx
.open_table(WRITE_TRANSACTION_STARTING_BLOCK_COUNT_TO_TIMESTAMP)?
.range(0..)?
.map(
|(starting_block_count, starting_timestamp)| TransactionInfo {
starting_block_count: starting_block_count.value(),
starting_timestamp: starting_timestamp.value(),
},
)
.flat_map(|result| {
result.map(
|(starting_block_count, starting_timestamp)| TransactionInfo {
starting_block_count: starting_block_count.value(),
starting_timestamp: starting_timestamp.value(),
},
)
})
.collect(),
tree_height: stats.tree_height(),
utxos_indexed: wtx.open_table(OUTPOINT_TO_SAT_RANGES)?.len()?,
Expand All @@ -378,12 +372,13 @@ impl Index {
pub(crate) fn export(&self, filename: &String) -> Result {
let mut writer = BufWriter::new(File::create(filename)?);

for (number, id) in self
for result in self
.database
.begin_read()?
.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)?
.iter()?
{
let (number, id) = result?;
writeln!(
writer,
"{}\t{}",
Expand Down Expand Up @@ -461,6 +456,7 @@ impl Index {
let height_to_block_hash = rtx.0.open_table(HEIGHT_TO_BLOCK_HASH)?;

for next in height_to_block_hash.range(0..block_count)?.rev().take(take) {
let next = next?;
blocks.push((next.0.value(), Entry::load(*next.1.value())));
}

Expand All @@ -475,7 +471,8 @@ impl Index {

let sat_to_satpoint = rtx.open_table(SAT_TO_SATPOINT)?;

for (sat, satpoint) in sat_to_satpoint.range(0..)? {
for range in sat_to_satpoint.range(0..)? {
let (sat, satpoint) = range?;
result.push((Sat(sat.value()), Entry::load(*satpoint.value())));
}

Expand Down Expand Up @@ -531,6 +528,7 @@ impl Index {
.open_multimap_table(SAT_TO_INSCRIPTION_ID)?
.get(&sat.n())?
.next()
.and_then(|result| result.ok())
.map(|inscription_id| Entry::load(*inscription_id.value())),
)
}
Expand Down Expand Up @@ -661,7 +659,8 @@ impl Index {

let outpoint_to_sat_ranges = rtx.0.open_table(OUTPOINT_TO_SAT_RANGES)?;

for (key, value) in outpoint_to_sat_ranges.range::<&[u8; 36]>(&[0; 36]..)? {
for range in outpoint_to_sat_ranges.range::<&[u8; 36]>(&[0; 36]..)? {
let (key, value) = range?;
let mut offset = 0;
for chunk in value.value().chunks_exact(11) {
let (start, end) = SatRange::load(chunk.try_into().unwrap());
Expand Down Expand Up @@ -726,6 +725,7 @@ impl Index {
.range(0..)?
.rev()
.next()
.and_then(|result| result.ok())
.map(|(height, _hash)| height)
.map(|x| x.value())
.unwrap_or(0);
Expand All @@ -750,18 +750,25 @@ impl Index {
&self,
n: Option<usize>,
) -> Result<BTreeMap<SatPoint, InscriptionId>> {
Ok(
self
.database
.begin_read()?
.open_multimap_table(SATPOINT_TO_INSCRIPTION_ID)?
.range::<&[u8; 44]>(&[0; 44]..)?
.flat_map(|(satpoint, id_iter)| {
id_iter.map(move |id| (Entry::load(*satpoint.value()), Entry::load(*id.value())))
})
.take(n.unwrap_or(usize::MAX))
.collect(),
)
let rtx = self.database.begin_read()?;

let mut result = BTreeMap::new();

for range_result in rtx
.open_multimap_table(SATPOINT_TO_INSCRIPTION_ID)?
.range::<&[u8; 44]>(&[0; 44]..)?
{
let (satpoint, ids) = range_result?;
for id_result in ids {
let id = id_result?;
result.insert(Entry::load(*satpoint.value()), Entry::load(*id.value()));
}
if result.len() == n.unwrap_or(usize::MAX) {
break;
}
}

Ok(result)
}

pub(crate) fn get_homepage_inscriptions(&self) -> Result<Vec<InscriptionId>> {
Expand All @@ -773,7 +780,7 @@ impl Index {
.iter()?
.rev()
.take(8)
.map(|(_number, id)| Entry::load(*id.value()))
.flat_map(|result| result.map(|(_number, id)| Entry::load(*id.value())))
.collect(),
)
}
Expand All @@ -789,7 +796,8 @@ impl Index {
rtx.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)?;

let latest = match inscription_number_to_inscription_id.iter()?.rev().next() {
Some((number, _id)) => number.value(),
Some(Ok((number, _id))) => number.value(),
Some(Err(_)) => return Ok(Default::default()),
None => return Ok(Default::default()),
};

Expand Down Expand Up @@ -818,7 +826,7 @@ impl Index {
.range(..=from)?
.rev()
.take(n)
.map(|(_number, id)| Entry::load(*id.value()))
.flat_map(|result| result.map(|(_number, id)| Entry::load(*id.value())))
.collect();

Ok((inscriptions, prev, next))
Expand All @@ -833,7 +841,7 @@ impl Index {
.iter()?
.rev()
.take(n)
.map(|(number, id)| (number.value(), Entry::load(*id.value())))
.flat_map(|result| result.map(|(number, id)| (number.value(), Entry::load(*id.value()))))
.collect(),
)
}
Expand Down Expand Up @@ -884,7 +892,7 @@ impl Index {
assert!(satpoint_to_inscription_id
.get(&satpoint.store())
.unwrap()
.any(|id| InscriptionId::load(*id.value()) == inscription_id));
.any(|id| InscriptionId::load(*id.unwrap().value()) == inscription_id));

match sat {
Some(sat) => {
Expand All @@ -896,7 +904,7 @@ impl Index {
.unwrap()
.get(&sat)
.unwrap()
.any(|id| InscriptionId::load(*id.value()) == inscription_id));
.any(|id| InscriptionId::load(*id.unwrap().value()) == inscription_id));

// we do not track common sats (only the sat ranges)
if !Sat(sat).is_common() {
Expand Down Expand Up @@ -939,13 +947,17 @@ impl Index {
}
.store();

Ok(
satpoint_to_id
.range::<&[u8; 44]>(&start..=&end)?
.flat_map(|(satpoint, id_iter)| {
id_iter.map(move |id| (Entry::load(*satpoint.value()), Entry::load(*id.value())))
}),
)
let mut inscriptions = Vec::new();

for range in satpoint_to_id.range::<&[u8; 44]>(&start..=&end)? {
let (satpoint, ids) = range?;
for id_result in ids {
let id = id_result?;
inscriptions.push((Entry::load(*satpoint.value()), Entry::load(*id.value())));
}
}

Ok(inscriptions.into_iter())
}

fn inscriptions_on_output_ordered<'a: 'tx, 'tx>(
Expand Down
3 changes: 3 additions & 0 deletions src/index/rtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Rtx<'_> {
.range(0..)?
.rev()
.next()
.and_then(|result| result.ok())
.map(|(height, _hash)| Height(height.value())),
)
}
Expand All @@ -23,6 +24,7 @@ impl Rtx<'_> {
.range(0..)?
.rev()
.next()
.and_then(|result| result.ok())
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0),
)
Expand All @@ -44,6 +46,7 @@ impl Rtx<'_> {
.range(0..)?
.rev()
.next()
.and_then(|result| result.ok())
.map(|(_height, hash)| BlockHash::load(*hash.value())),
),
}
Expand Down
2 changes: 2 additions & 0 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Updater {
.range(0..)?
.rev()
.next()
.and_then(|result| result.ok())
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0);

Expand Down Expand Up @@ -141,6 +142,7 @@ impl Updater {
.range(0..)?
.rev()
.next()
.and_then(|result| result.ok())
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0);
if height != self.height {
Expand Down
7 changes: 5 additions & 2 deletions src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
) -> Result<Self> {
let next_cursed_number = number_to_id
.iter()?
.map(|(number, _id)| number.value() - 1)
.next()
.and_then(|result| result.ok())
.map(|(number, _id)| number.value() - 1)
.unwrap_or(-1);

let next_number = number_to_id
.iter()?
.rev()
.map(|(number, _id)| number.value() + 1)
.next()
.and_then(|result| result.ok())
.map(|(number, _id)| number.value() + 1)
.unwrap_or(0);

Ok(Self {
Expand Down Expand Up @@ -173,6 +175,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
.iter()?
.rev()
.next()
.and_then(|result| result.ok())
.map(|(_id, number)| number.value() + 1)
.unwrap_or(0);

Expand Down

0 comments on commit 402d632

Please sign in to comment.