Skip to content

Commit

Permalink
Update redb (ordinals#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Nov 24, 2022
1 parent cb4f529 commit bad112f
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 266 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.3"
redb = "0.9.0"
redb = "0.10.0"
regex = "1.6.0"
reqwest = { version = "0.11.10", features = ["blocking"] }
rust-embed = "6.4.0"
Expand Down
141 changes: 0 additions & 141 deletions src/bytes.rs

This file was deleted.

7 changes: 0 additions & 7 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ impl Chain {
}
}

pub(crate) fn default_max_index_size(self) -> Bytes {
match self {
Self::Mainnet | Self::Signet | Self::Testnet => Bytes::TIB,
Self::Regtest => Bytes::MIB * 10,
}
}

pub(crate) fn genesis_block(self) -> Block {
bitcoin::blockdata::constants::genesis_block(self.network())
}
Expand Down
36 changes: 20 additions & 16 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ use {
mod rtx;
mod updater;

const HEIGHT_TO_BLOCK_HASH: TableDefinition<u64, [u8; 32]> =
const HEIGHT_TO_BLOCK_HASH: TableDefinition<u64, &[u8; 32]> =
TableDefinition::new("HEIGHT_TO_BLOCK_HASH");
const ORDINAL_TO_INSCRIPTION_TXID: TableDefinition<u64, [u8; 32]> =
const ORDINAL_TO_INSCRIPTION_TXID: TableDefinition<u64, &[u8; 32]> =
TableDefinition::new("ORDINAL_TO_INSCRIPTION_TXID");
const ORDINAL_TO_SATPOINT: TableDefinition<u64, [u8; 44]> =
const ORDINAL_TO_SATPOINT: TableDefinition<u64, &[u8; 44]> =
TableDefinition::new("ORDINAL_TO_SATPOINT");
const OUTPOINT_TO_ORDINAL_RANGES: TableDefinition<[u8; 36], [u8]> =
const OUTPOINT_TO_ORDINAL_RANGES: TableDefinition<&[u8; 36], [u8]> =
TableDefinition::new("OUTPOINT_TO_ORDINAL_RANGES");
const STATISTIC_TO_COUNT: TableDefinition<u64, u64> = TableDefinition::new("STATISTIC_TO_COUNT");
const TXID_TO_INSCRIPTION: TableDefinition<[u8; 32], str> =
const TXID_TO_INSCRIPTION: TableDefinition<&[u8; 32], str> =
TableDefinition::new("TXID_TO_INSCRIPTION");
const WRITE_TRANSACTION_STARTING_BLOCK_COUNT_TO_TIMESTAMP: TableDefinition<u64, u128> =
TableDefinition::new("WRITE_TRANSACTION_START_BLOCK_COUNT_TO_TIMESTAMP");
Expand Down Expand Up @@ -72,6 +72,12 @@ pub(crate) enum Statistic {
OrdinalRanges = 2,
}

impl Statistic {
fn key(self) -> u64 {
self.into()
}
}

impl From<Statistic> for u64 {
fn from(statistic: Statistic) -> Self {
statistic as u64
Expand All @@ -83,7 +89,6 @@ pub(crate) struct Info {
pub(crate) blocks_indexed: u64,
pub(crate) branch_pages: usize,
pub(crate) fragmented_bytes: usize,
pub(crate) free_pages: usize,
pub(crate) index_file_size: u64,
pub(crate) leaf_pages: usize,
pub(crate) metadata_bytes: usize,
Expand Down Expand Up @@ -163,7 +168,7 @@ impl Index {
} else {
WriteStrategy::TwoPhase
})
.create(&database_path, options.max_index_size().0)?
.create(&database_path)?
},
Err(error) => return Err(error.into()),
};
Expand Down Expand Up @@ -230,15 +235,14 @@ impl Index {
.unwrap_or(0),
branch_pages: stats.branch_pages(),
fragmented_bytes: stats.fragmented_bytes(),
free_pages: stats.free_pages(),
index_file_size: fs::metadata(&self.database_path)?.len(),
leaf_pages: stats.leaf_pages(),
metadata_bytes: stats.metadata_bytes(),
ordinal_ranges: statistic_to_count
.get(&Statistic::OrdinalRanges.into())?
.get(&Statistic::OrdinalRanges.key())?
.unwrap_or(0),
outputs_traversed: statistic_to_count
.get(&Statistic::OutputsTraversed.into())?
.get(&Statistic::OutputsTraversed.key())?
.unwrap_or(0),
page_size: stats.page_size(),
stored_bytes: stats.stored_bytes(),
Expand Down Expand Up @@ -299,8 +303,8 @@ 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.into(),
&(statistic_to_count.get(&(statistic.into()))?.unwrap_or(0) + n),
&statistic.key(),
&(statistic_to_count.get(&(statistic.key()))?.unwrap_or(0) + n),
)?;
Ok(())
}
Expand All @@ -319,7 +323,7 @@ impl Index {
.database
.begin_read()?
.open_table(STATISTIC_TO_COUNT)?
.get(&(statistic.into()))?
.get(&statistic.key())?
.unwrap_or(0),
)
}
Expand Down Expand Up @@ -474,13 +478,13 @@ impl Index {
Ok(None)
}

fn list_inner(&self, outpoint: &[u8]) -> Result<Option<Vec<u8>>> {
fn list_inner(&self, outpoint: [u8; 36]) -> Result<Option<Vec<u8>>> {
Ok(
self
.database
.begin_read()?
.open_table(OUTPOINT_TO_ORDINAL_RANGES)?
.get(outpoint.try_into().unwrap())?
.get(&outpoint)?
.map(|outpoint| outpoint.to_vec()),
)
}
Expand All @@ -490,7 +494,7 @@ impl Index {

let outpoint_encoded = encode_outpoint(outpoint);

let ordinal_ranges = self.list_inner(&outpoint_encoded)?;
let ordinal_ranges = self.list_inner(outpoint_encoded)?;

match ordinal_ranges {
Some(ordinal_ranges) => Ok(Some(List::Unspent(
Expand Down
6 changes: 3 additions & 3 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ impl Updater {
&mut self,
txid: Txid,
tx: &Transaction,
ordinal_to_satpoint: &mut Table<u64, [u8; 44]>,
ordinal_to_inscription_txid: &mut Table<u64, [u8; 32]>,
txid_to_inscription: &mut Table<[u8; 32], str>,
ordinal_to_satpoint: &mut Table<u64, &[u8; 44]>,
ordinal_to_inscription_txid: &mut Table<u64, &[u8; 32]>,
txid_to_inscription: &mut Table<&[u8; 32], str>,
input_ordinal_ranges: &mut VecDeque<(u64, u64)>,
ordinal_ranges_written: &mut u64,
outputs_traversed: &mut u64,
Expand Down
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use {
self::{
arguments::Arguments,
blocktime::Blocktime,
bytes::Bytes,
decimal::Decimal,
degree::Degree,
epoch::Epoch,
Expand Down Expand Up @@ -40,7 +39,7 @@ use {
fmt::{self, Display, Formatter},
fs, io,
net::ToSocketAddrs,
ops::{Add, AddAssign, Mul, Sub},
ops::{Add, AddAssign, Sub},
path::{Path, PathBuf},
process,
str::FromStr,
Expand All @@ -64,7 +63,6 @@ use self::test::*;

mod arguments;
mod blocktime;
mod bytes;
mod chain;
mod decimal;
mod degree;
Expand Down
Loading

0 comments on commit bad112f

Please sign in to comment.