Skip to content

Commit

Permalink
Upgrade to redb 0.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Dec 26, 2022
1 parent c0abc3e commit ef0618f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 38 deletions.
4 changes: 2 additions & 2 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 @@ -34,7 +34,7 @@ log = "0.4.14"
mime = "0.3.16"
mime_guess = "2.0.4"
ord-bitcoincore-rpc = "0.16.0"
redb = "0.10.0"
redb = "0.11.0"
regex = "1.6.0"
reqwest = { version = "0.11.10", features = ["blocking"] }
rust-embed = "6.4.0"
Expand Down
69 changes: 42 additions & 27 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const INSCRIPTION_ID_TO_SATPOINT: TableDefinition<&InscriptionIdArray, &SatPoint
TableDefinition::new("INSCRIPTION_ID_TO_SATPOINT");
const INSCRIPTION_NUMBER_TO_INSCRIPTION_ID: TableDefinition<u64, &InscriptionIdArray> =
TableDefinition::new("INSCRIPTION_NUMBER_TO_INSCRIPTION_ID");
const OUTPOINT_TO_SAT_RANGES: TableDefinition<&OutPointArray, [u8]> =
const OUTPOINT_TO_SAT_RANGES: TableDefinition<&OutPointArray, &[u8]> =
TableDefinition::new("OUTPOINT_TO_SAT_RANGES");
const SATPOINT_TO_INSCRIPTION_ID: TableDefinition<&SatPointArray, &InscriptionIdArray> =
TableDefinition::new("SATPOINT_TO_INSCRIPTION_ID");
Expand Down Expand Up @@ -185,7 +185,7 @@ impl Index {
data_dir.join("index.redb")
};

let database = match unsafe { redb::Database::open(&database_path) } {
let database = match unsafe { redb::Database::builder().open_mmapped(&database_path) } {
Ok(database) => database,
Err(redb::Error::Io(error)) if error.kind() == io::ErrorKind::NotFound => {
let database = unsafe {
Expand All @@ -195,7 +195,7 @@ impl Index {
} else {
WriteStrategy::TwoPhase
})
.create(&database_path)?
.create_mmapped(&database_path)?
};
let tx = database.begin_write()?;

Expand Down Expand Up @@ -267,34 +267,38 @@ impl Index {

let info = {
let statistic_to_count = wtx.open_table(STATISTIC_TO_COUNT)?;
let sat_ranges = statistic_to_count
.get(&Statistic::SatRanges.key())?
.map(|x| x.value())
.unwrap_or(0);
let outputs_traversed = statistic_to_count
.get(&Statistic::OutputsTraversed.key())?
.map(|x| x.value())
.unwrap_or(0);
Info {
blocks_indexed: wtx
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
.map(|(height, _hash)| height + 1)
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0),
branch_pages: stats.branch_pages(),
fragmented_bytes: stats.fragmented_bytes(),
index_file_size: fs::metadata(&self.database_path)?.len(),
leaf_pages: stats.leaf_pages(),
metadata_bytes: stats.metadata_bytes(),
sat_ranges: statistic_to_count
.get(&Statistic::SatRanges.key())?
.unwrap_or(0),
outputs_traversed: statistic_to_count
.get(&Statistic::OutputsTraversed.key())?
.unwrap_or(0),
sat_ranges,
outputs_traversed,
page_size: stats.page_size(),
stored_bytes: stats.stored_bytes(),
transactions: wtx
.open_table(WRITE_TRANSACTION_STARTING_BLOCK_COUNT_TO_TIMESTAMP)?
.range(0..)?
.map(
|(starting_block_count, starting_timestamp)| TransactionInfo {
starting_block_count,
starting_timestamp,
starting_block_count: starting_block_count.value(),
starting_timestamp: starting_timestamp.value(),
},
)
.collect(),
Expand Down Expand Up @@ -347,10 +351,12 @@ impl Index {

fn increment_statistic(wtx: &WriteTransaction, statistic: Statistic, n: u64) -> Result {
let mut statistic_to_count = wtx.open_table(STATISTIC_TO_COUNT)?;
statistic_to_count.insert(
&statistic.key(),
&(statistic_to_count.get(&(statistic.key()))?.unwrap_or(0) + n),
)?;
let value = statistic_to_count
.get(&(statistic.key()))?
.map(|x| x.value())
.unwrap_or(0)
+ n;
statistic_to_count.insert(&statistic.key(), &value)?;
Ok(())
}

Expand All @@ -362,6 +368,7 @@ impl Index {
.begin_read()?
.open_table(STATISTIC_TO_COUNT)?
.get(&statistic.key())?
.map(|x| x.value())
.unwrap_or(0),
)
}
Expand All @@ -384,7 +391,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) {
blocks.push((next.0, BlockHash::from_slice(next.1)?));
blocks.push((next.0.value(), BlockHash::from_slice(next.1.value())?));
}

Ok(blocks)
Expand All @@ -399,7 +406,7 @@ impl Index {
let sat_to_satpoint = rtx.open_table(SAT_TO_SATPOINT)?;

for (sat, satpoint) in sat_to_satpoint.range(0..)? {
result.push((Sat(sat), decode_satpoint(*satpoint)));
result.push((Sat(sat.value()), decode_satpoint(*satpoint.value())));
}

Ok(Some(result))
Expand All @@ -416,7 +423,7 @@ impl Index {
.begin_read()?
.open_table(SAT_TO_SATPOINT)?
.get(&sat.n())?
.map(|satpoint| decode_satpoint(*satpoint)),
.map(|satpoint| decode_satpoint(*satpoint.value())),
)
} else {
Ok(None)
Expand Down Expand Up @@ -459,8 +466,8 @@ impl Index {

Ok(
self
.get_inscription_by_inscription_id(Txid::from_inner(*txid))?
.map(|(inscription, _)| (InscriptionId::from_inner(*txid), inscription)),
.get_inscription_by_inscription_id(Txid::from_inner(*txid.value()))?
.map(|(inscription, _)| (InscriptionId::from_inner(*txid.value()), inscription)),
)
}

Expand All @@ -478,7 +485,8 @@ impl Index {
.begin_read()?
.open_table(INSCRIPTION_ID_TO_SATPOINT)?
.get(txid.as_inner())?
.ok_or_else(|| anyhow!("no satpoint for inscription"))?,
.ok_or_else(|| anyhow!("no satpoint for inscription"))?
.value(),
);

Ok(Some((inscription, satpoint)))
Expand Down Expand Up @@ -520,10 +528,10 @@ impl Index {

for (key, value) in outpoint_to_sat_ranges.range([0; 36]..)? {
let mut offset = 0;
for chunk in value.chunks_exact(11) {
for chunk in value.value().chunks_exact(11) {
let (start, end) = Index::decode_sat_range(chunk.try_into().unwrap());
if start <= sat && sat < end {
let outpoint = decode_outpoint(*key);
let outpoint = decode_outpoint(*key.value());
return Ok(Some(SatPoint {
outpoint,
offset: offset + sat - start,
Expand All @@ -543,7 +551,7 @@ impl Index {
.begin_read()?
.open_table(OUTPOINT_TO_SAT_RANGES)?
.get(&outpoint)?
.map(|outpoint| outpoint.to_vec()),
.map(|outpoint| outpoint.value().to_vec()),
)
}

Expand Down Expand Up @@ -585,6 +593,7 @@ impl Index {
.rev()
.next()
.map(|(height, _hash)| height)
.map(|x| x.value())
.unwrap_or(0);

let expected_blocks = height.checked_sub(current).with_context(|| {
Expand All @@ -608,7 +617,12 @@ impl Index {
.begin_read()?
.open_table(SATPOINT_TO_INSCRIPTION_ID)?
.range([0; 44]..)?
.map(|(satpoint, id)| (decode_satpoint(*satpoint), decode_inscription_id(*id)))
.map(|(satpoint, id)| {
(
decode_satpoint(*satpoint.value()),
decode_inscription_id(*id.value()),
)
})
.take(n.unwrap_or(usize::MAX))
.collect(),
)
Expand All @@ -627,7 +641,7 @@ impl Index {
.iter()?
.rev()
{
let id = decode_inscription_id(*id);
let id = decode_inscription_id(*id.value());

let Some((inscription, _satpoint)) = self.get_inscription_by_inscription_id(id)? else {
continue;
Expand All @@ -649,6 +663,7 @@ impl Index {
.begin_read()?
.open_table(INSCRIPTION_ID_TO_HEIGHT)?
.get(inscription_id.as_inner())?
.map(|x| x.value())
.ok_or_else(|| anyhow!("no height for inscription"))
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/index/rtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Rtx<'_> {
.range(0..)?
.rev()
.next()
.map(|(height, _hash)| Height(height)),
.map(|(height, _hash)| Height(height.value())),
)
}

Expand All @@ -23,7 +23,7 @@ impl Rtx<'_> {
.range(0..)?
.rev()
.next()
.map(|(height, _hash)| height + 1)
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0),
)
}
Expand Down
10 changes: 5 additions & 5 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Updater {
.range(0..)?
.rev()
.next()
.map(|(height, _hash)| height + 1)
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0);

wtx
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Updater {
.range(0..)?
.rev()
.next()
.map(|(height, _hash)| height + 1)
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0);
if height != self.height {
// another update has run between committing and beginning the new
Expand Down Expand Up @@ -268,7 +268,7 @@ impl Updater {
if let Some(prev_height) = self.height.checked_sub(1) {
let prev_hash = height_to_block_hash.get(&prev_height)?.unwrap();

if prev_hash != block.header.prev_blockhash.as_ref() {
if prev_hash.value() != block.header.prev_blockhash.as_ref() {
index.reorged.store(true, Ordering::Relaxed);
return Err(anyhow!("reorg detected at or before {prev_height}"));
}
Expand All @@ -282,7 +282,7 @@ impl Updater {
let mut next_inscription_number = inscription_number_to_inscription_id
.iter()?
.rev()
.map(|(number, _id)| number + 1)
.map(|(number, _id)| number.value() + 1)
.next()
.unwrap_or(0);

Expand Down Expand Up @@ -325,7 +325,7 @@ impl Updater {
None => outpoint_to_sat_ranges
.remove(&key)?
.ok_or_else(|| anyhow!("Could not find outpoint {} in index", input.previous_output))?
.to_value()
.value()
.to_vec(),
};

Expand Down
2 changes: 1 addition & 1 deletion src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
let inscription_ids: Vec<(SatPointArray, InscriptionIdArray)> = self
.satpoint_to_id
.range(start..=end)?
.map(|(satpoint, id)| (*satpoint, *id))
.map(|(satpoint, id)| (*satpoint.value(), *id.value()))
.collect();

for (old_satpoint, inscription_id) in inscription_ids {
Expand Down

0 comments on commit ef0618f

Please sign in to comment.