computer: big refactor

This commit is contained in:
nym21
2025-12-03 19:33:08 +01:00
parent fcc74ba212
commit d27cc02e8c
34 changed files with 2343 additions and 7340 deletions

View File

@@ -14,9 +14,12 @@ use vecdb::{
LazyVecFrom1, LazyVecFrom2, PAGE_SIZE, PcoVec, TypedVecIterator, VecIndex, unlikely,
};
use crate::grouped::{
ComputedValueVecsFromHeight, ComputedValueVecsFromTxindex, ComputedVecsFromDateIndex,
ComputedVecsFromHeight, ComputedVecsFromTxindex, Source, VecBuilderOptions,
use crate::{
grouped::{
ComputedValueVecsFromHeight, ComputedValueVecsFromTxindex, ComputedVecsFromDateIndex,
ComputedVecsFromHeight, ComputedVecsFromTxindex, Source, VecBuilderOptions,
},
utils::OptionExt,
};
use super::{Indexes, indexes, price};
@@ -154,9 +157,48 @@ impl Vecs {
let version = parent_version + Version::ZERO;
let compute_dollars = price.is_some();
let v0 = Version::ZERO;
let v2 = Version::TWO;
let v4 = Version::new(4);
let v5 = Version::new(5);
let txinindex_to_value: EagerVec<PcoVec<TxInIndex, Sats>> =
EagerVec::forced_import(&db, "value", version + Version::ZERO)?;
// Helper macros for common patterns
macro_rules! eager {
($name:expr) => {
EagerVec::forced_import(&db, $name, version + v0)?
};
($name:expr, $v:expr) => {
EagerVec::forced_import(&db, $name, version + $v)?
};
}
macro_rules! computed_h {
($name:expr, $source:expr, $opts:expr) => {
ComputedVecsFromHeight::forced_import(&db, $name, $source, version + v0, indexes, $opts)?
};
($name:expr, $source:expr, $v:expr, $opts:expr) => {
ComputedVecsFromHeight::forced_import(&db, $name, $source, version + $v, indexes, $opts)?
};
}
macro_rules! computed_di {
($name:expr, $opts:expr) => {
ComputedVecsFromDateIndex::forced_import(&db, $name, Source::Compute, version + v0, indexes, $opts)?
};
($name:expr, $v:expr, $opts:expr) => {
ComputedVecsFromDateIndex::forced_import(&db, $name, Source::Compute, version + $v, indexes, $opts)?
};
}
macro_rules! computed_tx {
($name:expr, $source:expr, $opts:expr) => {
ComputedVecsFromTxindex::forced_import(&db, $name, $source, version + v0, indexes, $opts)?
};
}
let last = || VecBuilderOptions::default().add_last();
let sum = || VecBuilderOptions::default().add_sum();
let sum_cum = || VecBuilderOptions::default().add_sum().add_cumulative();
let stats = || VecBuilderOptions::default().add_average().add_minmax().add_percentiles();
let full_stats = || VecBuilderOptions::default().add_average().add_minmax().add_percentiles().add_sum().add_cumulative();
let txinindex_to_value: EagerVec<PcoVec<TxInIndex, Sats>> = eager!("value");
let txindex_to_weight = LazyVecFrom2::init(
"weight",
@@ -200,16 +242,10 @@ impl Vecs {
},
);
let txindex_to_input_value =
EagerVec::forced_import(&db, "input_value", version + Version::ZERO)?;
let txindex_to_output_value =
EagerVec::forced_import(&db, "output_value", version + Version::ZERO)?;
let txindex_to_fee = EagerVec::forced_import(&db, "fee", version + Version::ZERO)?;
let txindex_to_fee_rate =
EagerVec::forced_import(&db, "fee_rate", version + Version::ZERO)?;
let txindex_to_input_value = eager!("input_value");
let txindex_to_output_value = eager!("output_value");
let txindex_to_fee = eager!("fee");
let txindex_to_fee_rate = eager!("fee_rate");
let dateindex_to_block_count_target = LazyVecFrom1::init(
"block_count_target",
@@ -262,221 +298,34 @@ impl Vecs {
semesterindex_to_block_count_target,
yearindex_to_block_count_target,
decadeindex_to_block_count_target,
height_to_interval: EagerVec::forced_import(&db, "interval", version + Version::ZERO)?,
timeindexes_to_timestamp: ComputedVecsFromDateIndex::forced_import(
&db,
"timestamp",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_first(),
)?,
indexes_to_block_interval: ComputedVecsFromHeight::forced_import(
&db,
"block_interval",
Source::None,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_percentiles()
.add_minmax()
.add_average(),
)?,
indexes_to_block_count: ComputedVecsFromHeight::forced_import(
&db,
"block_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_sum().add_cumulative(),
)?,
indexes_to_1w_block_count: ComputedVecsFromDateIndex::forced_import(
&db,
"1w_block_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_1m_block_count: ComputedVecsFromDateIndex::forced_import(
&db,
"1m_block_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_1y_block_count: ComputedVecsFromDateIndex::forced_import(
&db,
"1y_block_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_block_weight: ComputedVecsFromHeight::forced_import(
&db,
"block_weight",
Source::None,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_sum()
.add_minmax()
.add_average()
.add_percentiles()
.add_cumulative(),
)?,
indexes_to_block_size: ComputedVecsFromHeight::forced_import(
&db,
"block_size",
Source::None,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_sum()
.add_minmax()
.add_average()
.add_percentiles()
.add_cumulative(),
)?,
height_to_vbytes: EagerVec::forced_import(&db, "vbytes", version + Version::ZERO)?,
height_to_24h_block_count: EagerVec::forced_import(
&db,
"24h_block_count",
version + Version::ZERO,
)?,
height_to_24h_coinbase_sum: EagerVec::forced_import(
&db,
"24h_coinbase_sum",
version + Version::ZERO,
)?,
height_to_24h_coinbase_usd_sum: EagerVec::forced_import(
&db,
"24h_coinbase_usd_sum",
version + Version::ZERO,
)?,
indexes_to_block_vbytes: ComputedVecsFromHeight::forced_import(
&db,
"block_vbytes",
Source::None,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_sum()
.add_minmax()
.add_average()
.add_percentiles()
.add_cumulative(),
)?,
difficultyepoch_to_timestamp: EagerVec::forced_import(
&db,
"timestamp",
version + Version::ZERO,
)?,
halvingepoch_to_timestamp: EagerVec::forced_import(
&db,
"timestamp",
version + Version::ZERO,
)?,
height_to_interval: eager!("interval"),
timeindexes_to_timestamp: computed_di!("timestamp", VecBuilderOptions::default().add_first()),
indexes_to_block_interval: computed_h!("block_interval", Source::None, stats()),
indexes_to_block_count: computed_h!("block_count", Source::Compute, sum_cum()),
indexes_to_1w_block_count: computed_di!("1w_block_count", last()),
indexes_to_1m_block_count: computed_di!("1m_block_count", last()),
indexes_to_1y_block_count: computed_di!("1y_block_count", last()),
indexes_to_block_weight: computed_h!("block_weight", Source::None, full_stats()),
indexes_to_block_size: computed_h!("block_size", Source::None, full_stats()),
height_to_vbytes: eager!("vbytes"),
height_to_24h_block_count: eager!("24h_block_count"),
height_to_24h_coinbase_sum: eager!("24h_coinbase_sum"),
height_to_24h_coinbase_usd_sum: eager!("24h_coinbase_usd_sum"),
indexes_to_block_vbytes: computed_h!("block_vbytes", Source::None, full_stats()),
difficultyepoch_to_timestamp: eager!("timestamp"),
halvingepoch_to_timestamp: eager!("timestamp"),
dateindex_to_fee_dominance: EagerVec::forced_import(
&db,
"fee_dominance",
version + Version::ZERO,
)?,
dateindex_to_subsidy_dominance: EagerVec::forced_import(
&db,
"subsidy_dominance",
version + Version::ZERO,
)?,
indexes_to_difficulty: ComputedVecsFromHeight::forced_import(
&db,
"difficulty",
Source::None,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_difficultyepoch: ComputedVecsFromDateIndex::forced_import(
&db,
"difficultyepoch",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_halvingepoch: ComputedVecsFromDateIndex::forced_import(
&db,
"halvingepoch",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_tx_count: ComputedVecsFromHeight::forced_import(
&db,
"tx_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_input_count: ComputedVecsFromTxindex::forced_import(
&db,
"input_count",
Source::None,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_output_count: ComputedVecsFromTxindex::forced_import(
&db,
"output_count",
Source::None,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_tx_v1: ComputedVecsFromHeight::forced_import(
&db,
"tx_v1",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_sum().add_cumulative(),
)?,
indexes_to_tx_v2: ComputedVecsFromHeight::forced_import(
&db,
"tx_v2",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_sum().add_cumulative(),
)?,
indexes_to_tx_v3: ComputedVecsFromHeight::forced_import(
&db,
"tx_v3",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_sum().add_cumulative(),
)?,
dateindex_to_fee_dominance: eager!("fee_dominance"),
dateindex_to_subsidy_dominance: eager!("subsidy_dominance"),
indexes_to_difficulty: computed_h!("difficulty", Source::None, last()),
indexes_to_difficultyepoch: computed_di!("difficultyepoch", last()),
indexes_to_halvingepoch: computed_di!("halvingepoch", last()),
indexes_to_tx_count: computed_h!("tx_count", Source::Compute, full_stats()),
indexes_to_input_count: computed_tx!("input_count", Source::None, full_stats()),
indexes_to_output_count: computed_tx!("output_count", Source::None, full_stats()),
indexes_to_tx_v1: computed_h!("tx_v1", Source::Compute, sum_cum()),
indexes_to_tx_v2: computed_h!("tx_v2", Source::Compute, sum_cum()),
indexes_to_tx_v3: computed_h!("tx_v3", Source::Compute, sum_cum()),
indexes_to_sent: ComputedValueVecsFromHeight::forced_import(
&db,
"sent",
@@ -501,39 +350,9 @@ impl Vecs {
.add_minmax()
.add_average(),
)?,
indexes_to_fee_rate: ComputedVecsFromTxindex::forced_import(
&db,
"fee_rate",
Source::None,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_percentiles()
.add_minmax()
.add_average(),
)?,
indexes_to_tx_vsize: ComputedVecsFromTxindex::forced_import(
&db,
"tx_vsize",
Source::None,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_percentiles()
.add_minmax()
.add_average(),
)?,
indexes_to_tx_weight: ComputedVecsFromTxindex::forced_import(
&db,
"tx_weight",
Source::None,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_percentiles()
.add_minmax()
.add_average(),
)?,
indexes_to_fee_rate: computed_tx!("fee_rate", Source::None, stats()),
indexes_to_tx_vsize: computed_tx!("tx_vsize", Source::None, stats()),
indexes_to_tx_weight: computed_tx!("tx_weight", Source::None, stats()),
indexes_to_subsidy: ComputedValueVecsFromHeight::forced_import(
&db,
"subsidy",
@@ -571,434 +390,55 @@ impl Vecs {
compute_dollars,
indexes,
)?,
indexes_to_p2a_count: ComputedVecsFromHeight::forced_import(
&db,
"p2a_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_p2ms_count: ComputedVecsFromHeight::forced_import(
&db,
"p2ms_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_p2pk33_count: ComputedVecsFromHeight::forced_import(
&db,
"p2pk33_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_p2pk65_count: ComputedVecsFromHeight::forced_import(
&db,
"p2pk65_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_p2pkh_count: ComputedVecsFromHeight::forced_import(
&db,
"p2pkh_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_p2sh_count: ComputedVecsFromHeight::forced_import(
&db,
"p2sh_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_p2tr_count: ComputedVecsFromHeight::forced_import(
&db,
"p2tr_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_p2wpkh_count: ComputedVecsFromHeight::forced_import(
&db,
"p2wpkh_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_p2wsh_count: ComputedVecsFromHeight::forced_import(
&db,
"p2wsh_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_opreturn_count: ComputedVecsFromHeight::forced_import(
&db,
"opreturn_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_unknownoutput_count: ComputedVecsFromHeight::forced_import(
&db,
"unknownoutput_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_emptyoutput_count: ComputedVecsFromHeight::forced_import(
&db,
"emptyoutput_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default()
.add_average()
.add_minmax()
.add_percentiles()
.add_sum()
.add_cumulative(),
)?,
indexes_to_exact_utxo_count: ComputedVecsFromHeight::forced_import(
&db,
"exact_utxo_count",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_subsidy_usd_1y_sma: compute_dollars.then(|| {
ComputedVecsFromDateIndex::forced_import(
&db,
"subsidy_usd_1y_sma",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)
.unwrap()
}),
indexes_to_puell_multiple: compute_dollars.then(|| {
ComputedVecsFromDateIndex::forced_import(
&db,
"puell_multiple",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)
.unwrap()
}),
indexes_to_hash_rate: ComputedVecsFromHeight::forced_import(
&db,
"hash_rate",
Source::Compute,
version + Version::new(5),
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_rate_1w_sma: ComputedVecsFromDateIndex::forced_import(
&db,
"hash_rate_1w_sma",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_rate_1m_sma: ComputedVecsFromDateIndex::forced_import(
&db,
"hash_rate_1m_sma",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_rate_2m_sma: ComputedVecsFromDateIndex::forced_import(
&db,
"hash_rate_2m_sma",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_rate_1y_sma: ComputedVecsFromDateIndex::forced_import(
&db,
"hash_rate_1y_sma",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_difficulty_as_hash: ComputedVecsFromHeight::forced_import(
&db,
"difficulty_as_hash",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_difficulty_adjustment: ComputedVecsFromHeight::forced_import(
&db,
"difficulty_adjustment",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_sum(),
)?,
indexes_to_blocks_before_next_difficulty_adjustment:
ComputedVecsFromHeight::forced_import(
&db,
"blocks_before_next_difficulty_adjustment",
Source::Compute,
version + Version::TWO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_days_before_next_difficulty_adjustment:
ComputedVecsFromHeight::forced_import(
&db,
"days_before_next_difficulty_adjustment",
Source::Compute,
version + Version::TWO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_blocks_before_next_halving: ComputedVecsFromHeight::forced_import(
&db,
"blocks_before_next_halving",
Source::Compute,
version + Version::TWO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_days_before_next_halving: ComputedVecsFromHeight::forced_import(
&db,
"days_before_next_halving",
Source::Compute,
version + Version::TWO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_price_ths: ComputedVecsFromHeight::forced_import(
&db,
"hash_price_ths",
Source::Compute,
version + Version::new(4),
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_price_phs: ComputedVecsFromHeight::forced_import(
&db,
"hash_price_phs",
Source::Compute,
version + Version::new(4),
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_value_ths: ComputedVecsFromHeight::forced_import(
&db,
"hash_value_ths",
Source::Compute,
version + Version::new(4),
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_value_phs: ComputedVecsFromHeight::forced_import(
&db,
"hash_value_phs",
Source::Compute,
version + Version::new(4),
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_price_ths_min: ComputedVecsFromHeight::forced_import(
&db,
"hash_price_ths_min",
Source::Compute,
version + Version::new(4),
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_price_phs_min: ComputedVecsFromHeight::forced_import(
&db,
"hash_price_phs_min",
Source::Compute,
version + Version::new(4),
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_price_rebound: ComputedVecsFromHeight::forced_import(
&db,
"hash_price_rebound",
Source::Compute,
version + Version::new(4),
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_value_ths_min: ComputedVecsFromHeight::forced_import(
&db,
"hash_value_ths_min",
Source::Compute,
version + Version::new(4),
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_value_phs_min: ComputedVecsFromHeight::forced_import(
&db,
"hash_value_phs_min",
Source::Compute,
version + Version::new(4),
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_hash_value_rebound: ComputedVecsFromHeight::forced_import(
&db,
"hash_value_rebound",
Source::Compute,
version + Version::new(4),
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_inflation_rate: ComputedVecsFromDateIndex::forced_import(
&db,
"inflation_rate",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_annualized_volume: ComputedVecsFromDateIndex::forced_import(
&db,
"annualized_volume",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_annualized_volume_btc: ComputedVecsFromDateIndex::forced_import(
&db,
"annualized_volume_btc",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_annualized_volume_usd: ComputedVecsFromDateIndex::forced_import(
&db,
"annualized_volume_usd",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_tx_btc_velocity: ComputedVecsFromDateIndex::forced_import(
&db,
"tx_btc_velocity",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_tx_usd_velocity: ComputedVecsFromDateIndex::forced_import(
&db,
"tx_usd_velocity",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_tx_per_sec: ComputedVecsFromDateIndex::forced_import(
&db,
"tx_per_sec",
Source::Compute,
version + Version::TWO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_outputs_per_sec: ComputedVecsFromDateIndex::forced_import(
&db,
"outputs_per_sec",
Source::Compute,
version + Version::TWO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_inputs_per_sec: ComputedVecsFromDateIndex::forced_import(
&db,
"inputs_per_sec",
Source::Compute,
version + Version::TWO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_p2a_count: computed_h!("p2a_count", Source::Compute, full_stats()),
indexes_to_p2ms_count: computed_h!("p2ms_count", Source::Compute, full_stats()),
indexes_to_p2pk33_count: computed_h!("p2pk33_count", Source::Compute, full_stats()),
indexes_to_p2pk65_count: computed_h!("p2pk65_count", Source::Compute, full_stats()),
indexes_to_p2pkh_count: computed_h!("p2pkh_count", Source::Compute, full_stats()),
indexes_to_p2sh_count: computed_h!("p2sh_count", Source::Compute, full_stats()),
indexes_to_p2tr_count: computed_h!("p2tr_count", Source::Compute, full_stats()),
indexes_to_p2wpkh_count: computed_h!("p2wpkh_count", Source::Compute, full_stats()),
indexes_to_p2wsh_count: computed_h!("p2wsh_count", Source::Compute, full_stats()),
indexes_to_opreturn_count: computed_h!("opreturn_count", Source::Compute, full_stats()),
indexes_to_unknownoutput_count: computed_h!("unknownoutput_count", Source::Compute, full_stats()),
indexes_to_emptyoutput_count: computed_h!("emptyoutput_count", Source::Compute, full_stats()),
indexes_to_exact_utxo_count: computed_h!("exact_utxo_count", Source::Compute, last()),
indexes_to_subsidy_usd_1y_sma: compute_dollars
.then(|| ComputedVecsFromDateIndex::forced_import(&db, "subsidy_usd_1y_sma", Source::Compute, version + v0, indexes, last()))
.transpose()?,
indexes_to_puell_multiple: compute_dollars
.then(|| ComputedVecsFromDateIndex::forced_import(&db, "puell_multiple", Source::Compute, version + v0, indexes, last()))
.transpose()?,
indexes_to_hash_rate: computed_h!("hash_rate", Source::Compute, v5, last()),
indexes_to_hash_rate_1w_sma: computed_di!("hash_rate_1w_sma", last()),
indexes_to_hash_rate_1m_sma: computed_di!("hash_rate_1m_sma", last()),
indexes_to_hash_rate_2m_sma: computed_di!("hash_rate_2m_sma", last()),
indexes_to_hash_rate_1y_sma: computed_di!("hash_rate_1y_sma", last()),
indexes_to_difficulty_as_hash: computed_h!("difficulty_as_hash", Source::Compute, last()),
indexes_to_difficulty_adjustment: computed_h!("difficulty_adjustment", Source::Compute, sum()),
indexes_to_blocks_before_next_difficulty_adjustment: computed_h!("blocks_before_next_difficulty_adjustment", Source::Compute, v2, last()),
indexes_to_days_before_next_difficulty_adjustment: computed_h!("days_before_next_difficulty_adjustment", Source::Compute, v2, last()),
indexes_to_blocks_before_next_halving: computed_h!("blocks_before_next_halving", Source::Compute, v2, last()),
indexes_to_days_before_next_halving: computed_h!("days_before_next_halving", Source::Compute, v2, last()),
indexes_to_hash_price_ths: computed_h!("hash_price_ths", Source::Compute, v4, last()),
indexes_to_hash_price_phs: computed_h!("hash_price_phs", Source::Compute, v4, last()),
indexes_to_hash_value_ths: computed_h!("hash_value_ths", Source::Compute, v4, last()),
indexes_to_hash_value_phs: computed_h!("hash_value_phs", Source::Compute, v4, last()),
indexes_to_hash_price_ths_min: computed_h!("hash_price_ths_min", Source::Compute, v4, last()),
indexes_to_hash_price_phs_min: computed_h!("hash_price_phs_min", Source::Compute, v4, last()),
indexes_to_hash_price_rebound: computed_h!("hash_price_rebound", Source::Compute, v4, last()),
indexes_to_hash_value_ths_min: computed_h!("hash_value_ths_min", Source::Compute, v4, last()),
indexes_to_hash_value_phs_min: computed_h!("hash_value_phs_min", Source::Compute, v4, last()),
indexes_to_hash_value_rebound: computed_h!("hash_value_rebound", Source::Compute, v4, last()),
indexes_to_inflation_rate: computed_di!("inflation_rate", last()),
indexes_to_annualized_volume: computed_di!("annualized_volume", last()),
indexes_to_annualized_volume_btc: computed_di!("annualized_volume_btc", last()),
indexes_to_annualized_volume_usd: computed_di!("annualized_volume_usd", last()),
indexes_to_tx_btc_velocity: computed_di!("tx_btc_velocity", last()),
indexes_to_tx_usd_velocity: computed_di!("tx_usd_velocity", last()),
indexes_to_tx_per_sec: computed_di!("tx_per_sec", v2, last()),
indexes_to_outputs_per_sec: computed_di!("outputs_per_sec", v2, last()),
indexes_to_inputs_per_sec: computed_di!("inputs_per_sec", v2, last()),
txindex_to_is_coinbase,
txinindex_to_value,
@@ -1441,7 +881,7 @@ impl Vecs {
.indexes_to_coinbase
.dollars
.as_ref()
.map(|c| c.height.as_ref().unwrap().into_iter())
.map(|c| c.height.u().into_iter())
{
self.height_to_24h_coinbase_usd_sum.compute_transform(
starting_indexes.height,
@@ -1462,7 +902,7 @@ impl Vecs {
.compute_all(indexes, price, starting_indexes, exit, |vec| {
vec.compute_transform2(
starting_indexes.height,
self.indexes_to_coinbase.sats.height.as_ref().unwrap(),
self.indexes_to_coinbase.sats.height.u(),
self.indexes_to_fee.sats.height.unwrap_sum(),
|(height, coinbase, fees, ..)| {
(
@@ -1486,7 +926,7 @@ impl Vecs {
|vec| {
vec.compute_transform(
starting_indexes.height,
self.indexes_to_subsidy.sats.height.as_ref().unwrap(),
self.indexes_to_subsidy.sats.height.u(),
|(height, subsidy, ..)| {
let halving = HalvingEpoch::from(height);
let expected = Sats::FIFTY_BTC / 2_usize.pow(halving.to_usize() as u32);
@@ -1738,7 +1178,7 @@ impl Vecs {
v.compute_transform2(
starting_indexes.height,
&self.height_to_24h_block_count,
self.indexes_to_difficulty_as_hash.height.as_ref().unwrap(),
self.indexes_to_difficulty_as_hash.height.u(),
|(i, block_count_sum, difficulty_as_hash, ..)| {
(
i,
@@ -1916,7 +1356,7 @@ impl Vecs {
v.compute_transform2(
starting_indexes.height,
&self.height_to_24h_coinbase_usd_sum,
self.indexes_to_hash_rate.height.as_ref().unwrap(),
self.indexes_to_hash_rate.height.u(),
|(i, coinbase_sum, hashrate, ..)| {
(i, (*coinbase_sum / (*hashrate / ONE_TERA_HASH)).into())
},
@@ -1929,7 +1369,7 @@ impl Vecs {
.compute_all(indexes, starting_indexes, exit, |v| {
v.compute_transform(
starting_indexes.height,
self.indexes_to_hash_price_ths.height.as_ref().unwrap(),
self.indexes_to_hash_price_ths.height.u(),
|(i, price, ..)| (i, (*price * 1000.0).into()),
exit,
)?;
@@ -1941,7 +1381,7 @@ impl Vecs {
v.compute_transform2(
starting_indexes.height,
&self.height_to_24h_coinbase_sum,
self.indexes_to_hash_rate.height.as_ref().unwrap(),
self.indexes_to_hash_rate.height.u(),
|(i, coinbase_sum, hashrate, ..)| {
(
i,
@@ -1957,7 +1397,7 @@ impl Vecs {
.compute_all(indexes, starting_indexes, exit, |v| {
v.compute_transform(
starting_indexes.height,
self.indexes_to_hash_value_ths.height.as_ref().unwrap(),
self.indexes_to_hash_value_ths.height.u(),
|(i, value, ..)| (i, (*value * 1000.0).into()),
exit,
)?;
@@ -1968,7 +1408,7 @@ impl Vecs {
.compute_all(indexes, starting_indexes, exit, |v| {
v.compute_all_time_low_(
starting_indexes.height,
self.indexes_to_hash_price_ths.height.as_ref().unwrap(),
self.indexes_to_hash_price_ths.height.u(),
exit,
true,
)?;
@@ -1979,7 +1419,7 @@ impl Vecs {
.compute_all(indexes, starting_indexes, exit, |v| {
v.compute_all_time_low_(
starting_indexes.height,
self.indexes_to_hash_price_phs.height.as_ref().unwrap(),
self.indexes_to_hash_price_phs.height.u(),
exit,
true,
)?;
@@ -1990,7 +1430,7 @@ impl Vecs {
.compute_all(indexes, starting_indexes, exit, |v| {
v.compute_all_time_low_(
starting_indexes.height,
self.indexes_to_hash_value_ths.height.as_ref().unwrap(),
self.indexes_to_hash_value_ths.height.u(),
exit,
true,
)?;
@@ -2001,7 +1441,7 @@ impl Vecs {
.compute_all(indexes, starting_indexes, exit, |v| {
v.compute_all_time_low_(
starting_indexes.height,
self.indexes_to_hash_value_phs.height.as_ref().unwrap(),
self.indexes_to_hash_value_phs.height.u(),
exit,
true,
)?;
@@ -2012,8 +1452,8 @@ impl Vecs {
.compute_all(indexes, starting_indexes, exit, |v| {
v.compute_percentage_difference(
starting_indexes.height,
self.indexes_to_hash_price_phs.height.as_ref().unwrap(),
self.indexes_to_hash_price_phs_min.height.as_ref().unwrap(),
self.indexes_to_hash_price_phs.height.u(),
self.indexes_to_hash_price_phs_min.height.u(),
exit,
)?;
Ok(())
@@ -2023,8 +1463,8 @@ impl Vecs {
.compute_all(indexes, starting_indexes, exit, |v| {
v.compute_percentage_difference(
starting_indexes.height,
self.indexes_to_hash_value_phs.height.as_ref().unwrap(),
self.indexes_to_hash_value_phs_min.height.as_ref().unwrap(),
self.indexes_to_hash_value_phs.height.u(),
self.indexes_to_hash_value_phs_min.height.u(),
exit,
)?;
Ok(())

View File

@@ -5,7 +5,7 @@ use brk_traversable::Traversable;
use brk_types::{Bitcoin, CheckedSub, Dollars, StoredF32, StoredF64, Version};
use vecdb::{Database, Exit, PAGE_SIZE, TypedVecIterator};
use crate::grouped::ComputedVecsFromDateIndex;
use crate::{grouped::ComputedVecsFromDateIndex, utils::OptionExt};
use super::{
Indexes, chain,
@@ -60,220 +60,63 @@ impl Vecs {
db.set_min_len(PAGE_SIZE * 1_000_000)?;
let compute_dollars = price.is_some();
let v0 = parent_version;
let v1 = parent_version + Version::ONE;
let version = parent_version + Version::ZERO;
let last = || VecBuilderOptions::default().add_last();
let sum_cum = || VecBuilderOptions::default().add_sum().add_cumulative();
macro_rules! computed_h {
($name:expr, $opts:expr) => {
ComputedVecsFromHeight::forced_import(&db, $name, Source::Compute, v0, indexes, $opts)?
};
($name:expr, $v:expr, $opts:expr) => {
ComputedVecsFromHeight::forced_import(&db, $name, Source::Compute, $v, indexes, $opts)?
};
}
macro_rules! computed_di {
($name:expr, $opts:expr) => {
ComputedVecsFromDateIndex::forced_import(&db, $name, Source::Compute, v0, indexes, $opts)?
};
}
macro_rules! ratio_di {
($name:expr) => {
ComputedRatioVecsFromDateIndex::forced_import(&db, $name, Source::None, v0, indexes, true)?
};
}
macro_rules! value_h {
($name:expr) => {
ComputedValueVecsFromHeight::forced_import(&db, $name, Source::Compute, v1, last(), compute_dollars, indexes)?
};
}
let this = Self {
indexes_to_coinblocks_created: ComputedVecsFromHeight::forced_import(
&db,
"coinblocks_created",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_sum().add_cumulative(),
)?,
indexes_to_coinblocks_stored: ComputedVecsFromHeight::forced_import(
&db,
"coinblocks_stored",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_sum().add_cumulative(),
)?,
indexes_to_liveliness: ComputedVecsFromHeight::forced_import(
&db,
"liveliness",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_vaultedness: ComputedVecsFromHeight::forced_import(
&db,
"vaultedness",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_activity_to_vaultedness_ratio: ComputedVecsFromHeight::forced_import(
&db,
"activity_to_vaultedness_ratio",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_vaulted_supply: ComputedValueVecsFromHeight::forced_import(
&db,
"vaulted_supply",
Source::Compute,
version + Version::ONE,
VecBuilderOptions::default().add_last(),
compute_dollars,
indexes,
)?,
indexes_to_active_supply: ComputedValueVecsFromHeight::forced_import(
&db,
"active_supply",
Source::Compute,
version + Version::ONE,
VecBuilderOptions::default().add_last(),
compute_dollars,
indexes,
)?,
indexes_to_thermo_cap: ComputedVecsFromHeight::forced_import(
&db,
"thermo_cap",
Source::Compute,
version + Version::ONE,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_investor_cap: ComputedVecsFromHeight::forced_import(
&db,
"investor_cap",
Source::Compute,
version + Version::ONE,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_vaulted_cap: ComputedVecsFromHeight::forced_import(
&db,
"vaulted_cap",
Source::Compute,
version + Version::ONE,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_active_cap: ComputedVecsFromHeight::forced_import(
&db,
"active_cap",
Source::Compute,
version + Version::ONE,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_vaulted_price: ComputedVecsFromHeight::forced_import(
&db,
"vaulted_price",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_vaulted_price_ratio: ComputedRatioVecsFromDateIndex::forced_import(
&db,
"vaulted_price",
Source::None,
version + Version::ZERO,
indexes,
true,
)?,
indexes_to_active_price: ComputedVecsFromHeight::forced_import(
&db,
"active_price",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_active_price_ratio: ComputedRatioVecsFromDateIndex::forced_import(
&db,
"active_price",
Source::None,
version + Version::ZERO,
indexes,
true,
)?,
indexes_to_true_market_mean: ComputedVecsFromHeight::forced_import(
&db,
"true_market_mean",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_true_market_mean_ratio: ComputedRatioVecsFromDateIndex::forced_import(
&db,
"true_market_mean",
Source::None,
version + Version::ZERO,
indexes,
true,
)?,
indexes_to_cointime_value_destroyed: ComputedVecsFromHeight::forced_import(
&db,
"cointime_value_destroyed",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_sum().add_cumulative(),
)?,
indexes_to_cointime_value_created: ComputedVecsFromHeight::forced_import(
&db,
"cointime_value_created",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_sum().add_cumulative(),
)?,
indexes_to_cointime_value_stored: ComputedVecsFromHeight::forced_import(
&db,
"cointime_value_stored",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_sum().add_cumulative(),
)?,
indexes_to_cointime_price: ComputedVecsFromHeight::forced_import(
&db,
"cointime_price",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_cointime_cap: ComputedVecsFromHeight::forced_import(
&db,
"cointime_cap",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_cointime_price_ratio: ComputedRatioVecsFromDateIndex::forced_import(
&db,
"cointime_price",
Source::None,
version + Version::ZERO,
indexes,
true,
)?,
indexes_to_cointime_adj_inflation_rate: ComputedVecsFromDateIndex::forced_import(
&db,
"cointime_adj_inflation_rate",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_cointime_adj_tx_btc_velocity: ComputedVecsFromDateIndex::forced_import(
&db,
"cointime_adj_tx_btc_velocity",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_cointime_adj_tx_usd_velocity: ComputedVecsFromDateIndex::forced_import(
&db,
"cointime_adj_tx_usd_velocity",
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_coinblocks_created: computed_h!("coinblocks_created", sum_cum()),
indexes_to_coinblocks_stored: computed_h!("coinblocks_stored", sum_cum()),
indexes_to_liveliness: computed_h!("liveliness", last()),
indexes_to_vaultedness: computed_h!("vaultedness", last()),
indexes_to_activity_to_vaultedness_ratio: computed_h!("activity_to_vaultedness_ratio", last()),
indexes_to_vaulted_supply: value_h!("vaulted_supply"),
indexes_to_active_supply: value_h!("active_supply"),
indexes_to_thermo_cap: computed_h!("thermo_cap", v1, last()),
indexes_to_investor_cap: computed_h!("investor_cap", v1, last()),
indexes_to_vaulted_cap: computed_h!("vaulted_cap", v1, last()),
indexes_to_active_cap: computed_h!("active_cap", v1, last()),
indexes_to_vaulted_price: computed_h!("vaulted_price", last()),
indexes_to_vaulted_price_ratio: ratio_di!("vaulted_price"),
indexes_to_active_price: computed_h!("active_price", last()),
indexes_to_active_price_ratio: ratio_di!("active_price"),
indexes_to_true_market_mean: computed_h!("true_market_mean", last()),
indexes_to_true_market_mean_ratio: ratio_di!("true_market_mean"),
indexes_to_cointime_value_destroyed: computed_h!("cointime_value_destroyed", sum_cum()),
indexes_to_cointime_value_created: computed_h!("cointime_value_created", sum_cum()),
indexes_to_cointime_value_stored: computed_h!("cointime_value_stored", sum_cum()),
indexes_to_cointime_price: computed_h!("cointime_price", last()),
indexes_to_cointime_cap: computed_h!("cointime_cap", last()),
indexes_to_cointime_price_ratio: ratio_di!("cointime_price"),
indexes_to_cointime_adj_inflation_rate: computed_di!("cointime_adj_inflation_rate", last()),
indexes_to_cointime_adj_tx_btc_velocity: computed_di!("cointime_adj_tx_btc_velocity", last()),
indexes_to_cointime_adj_tx_usd_velocity: computed_di!("cointime_adj_tx_usd_velocity", last()),
db,
};
@@ -339,7 +182,7 @@ impl Vecs {
.into_iter();
vec.compute_transform(
starting_indexes.height,
self.indexes_to_coinblocks_created.height.as_ref().unwrap(),
self.indexes_to_coinblocks_created.height.u(),
|(i, created, ..)| {
let destroyed = coinblocks_destroyed_iter.get_unwrap(i);
(i, created.checked_sub(destroyed).unwrap())
@@ -369,7 +212,7 @@ impl Vecs {
.compute_all(indexes, starting_indexes, exit, |vec| {
vec.compute_transform(
starting_indexes.height,
liveliness.height.as_ref().unwrap(),
liveliness.height.u(),
|(i, v, ..)| (i, StoredF64::from(1.0).checked_sub(v).unwrap()),
exit,
)?;
@@ -384,8 +227,8 @@ impl Vecs {
|vec| {
vec.compute_divide(
starting_indexes.height,
liveliness.height.as_ref().unwrap(),
vaultedness.height.as_ref().unwrap(),
liveliness.height.u(),
vaultedness.height.u(),
exit,
)?;
Ok(())
@@ -401,7 +244,7 @@ impl Vecs {
vec.compute_multiply(
starting_indexes.height,
circulating_supply,
vaultedness.height.as_ref().unwrap(),
vaultedness.height.u(),
exit,
)?;
Ok(())
@@ -417,7 +260,7 @@ impl Vecs {
vec.compute_multiply(
starting_indexes.height,
circulating_supply,
liveliness.height.as_ref().unwrap(),
liveliness.height.u(),
exit,
)?;
Ok(())
@@ -431,7 +274,7 @@ impl Vecs {
self.indexes_to_activity_to_vaultedness_ratio
.dateindex
.unwrap_last(),
chain.indexes_to_inflation_rate.dateindex.as_ref().unwrap(),
chain.indexes_to_inflation_rate.dateindex.u(),
exit,
)?;
Ok(())
@@ -444,7 +287,7 @@ impl Vecs {
self.indexes_to_activity_to_vaultedness_ratio
.dateindex
.unwrap_last(),
chain.indexes_to_tx_btc_velocity.dateindex.as_ref().unwrap(),
chain.indexes_to_tx_btc_velocity.dateindex.u(),
exit,
)?;
Ok(())
@@ -492,7 +335,7 @@ impl Vecs {
vec.compute_subtract(
starting_indexes.height,
realized_cap,
self.indexes_to_thermo_cap.height.as_ref().unwrap(),
self.indexes_to_thermo_cap.height.u(),
exit,
)?;
Ok(())
@@ -503,7 +346,7 @@ impl Vecs {
vec.compute_divide(
starting_indexes.height,
realized_cap,
self.indexes_to_vaultedness.height.as_ref().unwrap(),
self.indexes_to_vaultedness.height.u(),
exit,
)?;
Ok(())
@@ -514,7 +357,7 @@ impl Vecs {
vec.compute_multiply(
starting_indexes.height,
realized_cap,
self.indexes_to_liveliness.height.as_ref().unwrap(),
self.indexes_to_liveliness.height.u(),
exit,
)?;
Ok(())
@@ -525,7 +368,7 @@ impl Vecs {
vec.compute_divide(
starting_indexes.height,
realized_price,
self.indexes_to_vaultedness.height.as_ref().unwrap(),
self.indexes_to_vaultedness.height.u(),
exit,
)?;
Ok(())
@@ -543,7 +386,7 @@ impl Vecs {
vec.compute_multiply(
starting_indexes.height,
realized_price,
self.indexes_to_liveliness.height.as_ref().unwrap(),
self.indexes_to_liveliness.height.u(),
exit,
)?;
Ok(())
@@ -563,7 +406,7 @@ impl Vecs {
|vec| {
vec.compute_divide(
starting_indexes.height,
self.indexes_to_investor_cap.height.as_ref().unwrap(),
self.indexes_to_investor_cap.height.u(),
self.indexes_to_active_supply
.bitcoin
.height
@@ -592,7 +435,7 @@ impl Vecs {
vec.compute_multiply(
starting_indexes.height,
&price.chainindexes_to_price_close.height,
indexes_to_coinblocks_destroyed.height.as_ref().unwrap(),
indexes_to_coinblocks_destroyed.height.u(),
exit,
)?;
Ok(())
@@ -607,7 +450,7 @@ impl Vecs {
vec.compute_multiply(
starting_indexes.height,
&price.chainindexes_to_price_close.height,
self.indexes_to_coinblocks_created.height.as_ref().unwrap(),
self.indexes_to_coinblocks_created.height.u(),
exit,
)?;
Ok(())
@@ -622,7 +465,7 @@ impl Vecs {
vec.compute_multiply(
starting_indexes.height,
&price.chainindexes_to_price_close.height,
self.indexes_to_coinblocks_stored.height.as_ref().unwrap(),
self.indexes_to_coinblocks_stored.height.u(),
exit,
)?;
Ok(())
@@ -648,7 +491,7 @@ impl Vecs {
.compute_all(indexes, starting_indexes, exit, |vec| {
vec.compute_multiply(
starting_indexes.height,
self.indexes_to_cointime_price.height.as_ref().unwrap(),
self.indexes_to_cointime_price.height.u(),
circulating_supply,
exit,
)?;
@@ -671,7 +514,7 @@ impl Vecs {
self.indexes_to_activity_to_vaultedness_ratio
.dateindex
.unwrap_last(),
chain.indexes_to_tx_usd_velocity.dateindex.as_ref().unwrap(),
chain.indexes_to_tx_usd_velocity.dateindex.u(),
exit,
)?;
Ok(())

View File

@@ -10,7 +10,7 @@ use vecdb::{
PAGE_SIZE, TypedVecIterator, VecIndex,
};
use super::{Indexes, indexes};
use super::{Indexes, indexes, utils::OptionExt};
#[derive(Clone, Traversable)]
pub struct Vecs {
@@ -114,7 +114,7 @@ impl Vecs {
let ohlc = if i.to_usize() + 100 >= self.dateindex_to_price_ohlc_in_cents.len()
&& let Ok(mut ohlc) = self.fetcher.get_date(d)
{
let prev_open = *prev.as_ref().unwrap().close;
let prev_open = *prev.u().close;
*ohlc.open = prev_open;
*ohlc.high = (*ohlc.high).max(prev_open);
*ohlc.low = (*ohlc.low).min(prev_open);

View File

@@ -6,7 +6,7 @@ use vecdb::{
PcoVec, VecIndex, VecValue,
};
use crate::utils::get_percentile;
use crate::utils::{get_percentile, OptionExt};
use super::ComputedVecValue;
@@ -44,135 +44,34 @@ where
options: VecBuilderOptions,
) -> Result<Self> {
let only_one_active = options.is_only_one_active();
let suffix = |s: &str| format!("{name}_{s}");
let maybe_suffix = |s: &str| if only_one_active { name.to_string() } else { suffix(s) };
let v = version + VERSION;
let maybe_suffix = |s: &str| {
if only_one_active {
name.to_string()
} else {
suffix(s)
}
};
macro_rules! import {
($s:expr) => { Box::new(EagerVec::forced_import(db, &maybe_suffix($s), v).unwrap()) };
}
let s = Self {
first: options.first.then(|| {
Box::new(
EagerVec::forced_import(
db,
&maybe_suffix("first"),
version + VERSION + Version::ZERO,
)
.unwrap(),
)
}),
last: options.last.then(|| {
Box::new(EagerVec::forced_import(db, name, version + Version::ZERO).unwrap())
}),
min: options.min.then(|| {
Box::new(
EagerVec::forced_import(
db,
&maybe_suffix("min"),
version + VERSION + Version::ZERO,
)
.unwrap(),
)
}),
max: options.max.then(|| {
Box::new(
EagerVec::forced_import(
db,
&maybe_suffix("max"),
version + VERSION + Version::ZERO,
)
.unwrap(),
)
}),
median: options.median.then(|| {
Box::new(
EagerVec::forced_import(
db,
&maybe_suffix("median"),
version + VERSION + Version::ZERO,
)
.unwrap(),
)
}),
average: options.average.then(|| {
Box::new(
EagerVec::forced_import(
db,
&maybe_suffix("avg"),
version + VERSION + Version::ZERO,
)
.unwrap(),
)
}),
first: options.first.then(|| import!("first")),
last: options.last.then(|| Box::new(EagerVec::forced_import(db, name, v).unwrap())),
min: options.min.then(|| import!("min")),
max: options.max.then(|| import!("max")),
median: options.median.then(|| import!("median")),
average: options.average.then(|| import!("avg")),
sum: options.sum.then(|| {
Box::new(
EagerVec::forced_import(
db,
&(if !options.last && !options.average && !options.min && !options.max {
name.to_string()
} else {
maybe_suffix("sum")
}),
version + VERSION + Version::ZERO,
)
.unwrap(),
)
}),
cumulative: options.cumulative.then(|| {
Box::new(
EagerVec::forced_import(
db,
&suffix("cumulative"),
version + VERSION + Version::ZERO,
)
.unwrap(),
)
}),
pct90: options.pct90.then(|| {
Box::new(
EagerVec::forced_import(
db,
&maybe_suffix("pct90"),
version + VERSION + Version::ZERO,
)
.unwrap(),
)
}),
pct75: options.pct75.then(|| {
Box::new(
EagerVec::forced_import(
db,
&maybe_suffix("pct75"),
version + VERSION + Version::ZERO,
)
.unwrap(),
)
}),
pct25: options.pct25.then(|| {
Box::new(
EagerVec::forced_import(
db,
&maybe_suffix("pct25"),
version + VERSION + Version::ZERO,
)
.unwrap(),
)
}),
pct10: options.pct10.then(|| {
Box::new(
EagerVec::forced_import(
db,
&maybe_suffix("pct10"),
version + VERSION + Version::ZERO,
)
.unwrap(),
)
let sum_name = if !options.last && !options.average && !options.min && !options.max {
name.to_string()
} else {
maybe_suffix("sum")
};
Box::new(EagerVec::forced_import(db, &sum_name, v).unwrap())
}),
cumulative: options.cumulative.then(|| Box::new(EagerVec::forced_import(db, &suffix("cumulative"), v).unwrap())),
pct90: options.pct90.then(|| import!("pct90")),
pct75: options.pct75.then(|| import!("pct75")),
pct25: options.pct25.then(|| import!("pct25")),
pct10: options.pct10.then(|| import!("pct10")),
};
Ok(s)
@@ -192,7 +91,7 @@ where
let index = self.starting_index(max_from);
let cumulative_vec = self.cumulative.as_mut().unwrap();
let cumulative_vec = self.cumulative.um();
let mut cumulative = index.decremented().map_or(T::from(0_usize), |index| {
cumulative_vec.iter().get_unwrap(index)
@@ -421,7 +320,7 @@ where
let count_index = count_indexes_iter.next().unwrap();
if let Some(first) = self.first.as_mut() {
let v = source_first_iter.as_mut().unwrap().get_unwrap(first_index);
let v = source_first_iter.um().get_unwrap(first_index);
first.truncate_push_at(index, v)?;
}
@@ -431,7 +330,7 @@ where
panic!("should compute last if count can be 0")
}
let last_index = first_index + (count_index - 1);
let v = source_last_iter.as_mut().unwrap().get_unwrap(last_index);
let v = source_last_iter.um().get_unwrap(last_index);
last.truncate_push_at(index, v)?;
}
@@ -444,7 +343,7 @@ where
if needs_values {
if needs_sorted {
if let Some(max) = self.max.as_mut() {
let source_max_iter = source_max_iter.as_mut().unwrap();
let source_max_iter = source_max_iter.um();
source_max_iter.set_position(first_index);
let mut values = source_max_iter
.take(*count_index as usize)
@@ -454,7 +353,7 @@ where
}
if let Some(min) = self.min.as_mut() {
let source_min_iter = source_min_iter.as_mut().unwrap();
let source_min_iter = source_min_iter.um();
source_min_iter.set_position(first_index);
let mut values = source_min_iter
.take(*count_index as usize)
@@ -466,7 +365,7 @@ where
if needs_average_sum_or_cumulative {
if let Some(average) = self.average.as_mut() {
let source_average_iter = source_average_iter.as_mut().unwrap();
let source_average_iter = source_average_iter.um();
source_average_iter.set_position(first_index);
let values = source_average_iter
.take(*count_index as usize)
@@ -481,7 +380,7 @@ where
}
if needs_sum_or_cumulative {
let source_sum_iter = source_sum_iter.as_mut().unwrap();
let source_sum_iter = source_sum_iter.um();
source_sum_iter.set_position(first_index);
let values = source_sum_iter
.take(*count_index as usize)
@@ -517,47 +416,47 @@ where
}
pub fn unwrap_first(&self) -> &EagerVec<PcoVec<I, T>> {
self.first.as_ref().unwrap()
self.first.u()
}
#[allow(unused)]
pub fn unwrap_average(&self) -> &EagerVec<PcoVec<I, T>> {
self.average.as_ref().unwrap()
self.average.u()
}
pub fn unwrap_sum(&self) -> &EagerVec<PcoVec<I, T>> {
self.sum.as_ref().unwrap()
self.sum.u()
}
pub fn unwrap_max(&self) -> &EagerVec<PcoVec<I, T>> {
self.max.as_ref().unwrap()
self.max.u()
}
#[allow(unused)]
pub fn unwrap_pct90(&self) -> &EagerVec<PcoVec<I, T>> {
self.pct90.as_ref().unwrap()
self.pct90.u()
}
#[allow(unused)]
pub fn unwrap_pct75(&self) -> &EagerVec<PcoVec<I, T>> {
self.pct75.as_ref().unwrap()
self.pct75.u()
}
#[allow(unused)]
pub fn unwrap_median(&self) -> &EagerVec<PcoVec<I, T>> {
self.median.as_ref().unwrap()
self.median.u()
}
#[allow(unused)]
pub fn unwrap_pct25(&self) -> &EagerVec<PcoVec<I, T>> {
self.pct25.as_ref().unwrap()
self.pct25.u()
}
#[allow(unused)]
pub fn unwrap_pct10(&self) -> &EagerVec<PcoVec<I, T>> {
self.pct10.as_ref().unwrap()
self.pct10.u()
}
pub fn unwrap_min(&self) -> &EagerVec<PcoVec<I, T>> {
self.min.as_ref().unwrap()
self.min.u()
}
pub fn unwrap_last(&self) -> &EagerVec<PcoVec<I, T>> {
self.last.as_ref().unwrap()
self.last.u()
}
#[allow(unused)]
pub fn unwrap_cumulative(&self) -> &EagerVec<PcoVec<I, T>> {
self.cumulative.as_ref().unwrap()
self.cumulative.u()
}
pub fn safe_flush(&mut self, exit: &Exit) -> Result<()> {

View File

@@ -3,6 +3,7 @@ use brk_types::Version;
use vecdb::{FromCoarserIndex, IterableBoxedVec, IterableCloneableVec, LazyVecFrom2, VecIndex};
use crate::grouped::{EagerVecsBuilder, VecBuilderOptions};
use crate::utils::OptionExt;
use super::ComputedVecValue;
@@ -62,7 +63,7 @@ where
source_extra
.first
.as_ref()
.map_or_else(|| source.as_ref().unwrap().clone(), |v| v.clone()),
.map_or_else(|| source.u().clone(), |v| v.clone()),
len_source.clone(),
|i: I, source, len_source| {
if i.to_usize() >= len_source.vec_len() {
@@ -104,7 +105,7 @@ where
source_extra
.min
.as_ref()
.map_or_else(|| source.as_ref().unwrap().clone(), |v| v.clone()),
.map_or_else(|| source.u().clone(), |v| v.clone()),
len_source.clone(),
|i: I, source, len_source| {
if i.to_usize() >= len_source.vec_len() {
@@ -123,7 +124,7 @@ where
source_extra
.max
.as_ref()
.map_or_else(|| source.as_ref().unwrap().clone(), |v| v.clone()),
.map_or_else(|| source.u().clone(), |v| v.clone()),
len_source.clone(),
|i: I, source, len_source| {
if i.to_usize() >= len_source.vec_len() {
@@ -142,7 +143,7 @@ where
source_extra
.average
.as_ref()
.map_or_else(|| source.as_ref().unwrap().clone(), |v| v.clone()),
.map_or_else(|| source.u().clone(), |v| v.clone()),
len_source.clone(),
|i: I, source, len_source| {
if i.to_usize() >= len_source.vec_len() {
@@ -172,7 +173,7 @@ where
source_extra
.sum
.as_ref()
.map_or_else(|| source.as_ref().unwrap().clone(), |v| v.clone()),
.map_or_else(|| source.u().clone(), |v| v.clone()),
len_source.clone(),
|i: I, source, len_source| {
if i.to_usize() >= len_source.vec_len() {
@@ -194,7 +195,7 @@ where
Box::new(LazyVecFrom2::init(
&suffix("cumulative"),
version + VERSION + Version::ZERO,
source_extra.cumulative.as_ref().unwrap().boxed_clone(),
source_extra.cumulative.u().boxed_clone(),
len_source.clone(),
|i: I, source, len_source| {
if i.to_usize() >= len_source.vec_len() {
@@ -214,27 +215,27 @@ where
}
pub fn unwrap_first(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.first.as_ref().unwrap()
self.first.u()
}
#[allow(unused)]
pub fn unwrap_average(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.average.as_ref().unwrap()
self.average.u()
}
pub fn unwrap_sum(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.sum.as_ref().unwrap()
self.sum.u()
}
pub fn unwrap_max(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.max.as_ref().unwrap()
self.max.u()
}
pub fn unwrap_min(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.min.as_ref().unwrap()
self.min.u()
}
pub fn unwrap_last(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.last.as_ref().unwrap()
self.last.u()
}
#[allow(unused)]
pub fn unwrap_cumulative(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.cumulative.as_ref().unwrap()
self.cumulative.u()
}
}

View File

@@ -9,7 +9,7 @@ use vecdb::{
PcoVec,
};
use crate::{Indexes, grouped::LazyVecsBuilder, indexes};
use crate::{Indexes, grouped::LazyVecsBuilder, indexes, utils::OptionExt};
use super::{ComputedVecValue, EagerVecsBuilder, Source, VecBuilderOptions};
@@ -121,7 +121,7 @@ where
where
F: FnMut(&mut EagerVec<PcoVec<DateIndex, T>>) -> Result<()>,
{
compute(self.dateindex.as_mut().unwrap())?;
compute(self.dateindex.um())?;
let dateindex: Option<&EagerVec<PcoVec<DateIndex, T>>> = None;
self.compute_rest(starting_indexes, exit, dateindex)
@@ -137,7 +137,7 @@ where
self.dateindex_extra
.extend(starting_indexes.dateindex, dateindex, exit)?;
} else {
let dateindex = self.dateindex.as_ref().unwrap();
let dateindex = self.dateindex.u();
self.dateindex_extra
.extend(starting_indexes.dateindex, dateindex, exit)?;

View File

@@ -14,6 +14,7 @@ use crate::{
Indexes,
grouped::{LazyVecsBuilder, Source},
indexes,
utils::OptionExt,
};
use super::{ComputedVecValue, EagerVecsBuilder, VecBuilderOptions};
@@ -140,7 +141,7 @@ where
where
F: FnMut(&mut EagerVec<PcoVec<Height, T>>) -> Result<()>,
{
compute(self.height.as_mut().unwrap())?;
compute(self.height.um())?;
let height: Option<&EagerVec<PcoVec<Height, T>>> = None;
self.compute_rest(indexes, starting_indexes, exit, height)
@@ -173,7 +174,7 @@ where
exit,
)?;
} else {
let height = self.height.as_ref().unwrap();
let height = self.height.u();
self.height_extra
.extend(starting_indexes.height, height, exit)?;

View File

@@ -14,6 +14,7 @@ use crate::{
Indexes,
grouped::{LazyVecsBuilder, Source},
indexes, price,
utils::OptionExt,
};
use super::{ComputedVecValue, EagerVecsBuilder, VecBuilderOptions};
@@ -146,7 +147,7 @@ where
// ) -> Result<()>,
// {
// compute(
// self.txindex.as_mut().unwrap(),
// self.txindex.um(),
// indexer,
// indexes,
// starting_indexes,
@@ -176,7 +177,7 @@ where
exit,
)?;
} else {
let txindex = self.txindex.as_ref().unwrap().as_ref();
let txindex = self.txindex.u().as_ref();
self.height.compute(
starting_indexes.height,
@@ -229,7 +230,7 @@ impl ComputedVecsFromTxindex<Bitcoin> {
let txindex_version = if let Some(txindex) = txindex {
txindex.version()
} else {
self.txindex.as_ref().unwrap().as_ref().version()
self.txindex.u().as_ref().version()
};
self.height
@@ -237,84 +238,93 @@ impl ComputedVecsFromTxindex<Bitcoin> {
let starting_index = self.height.starting_index(starting_indexes.height);
// Create iterators once before the loop to avoid repeated iterator creation
let mut first_iter = sats.height.first.as_ref().map(|v| v.into_iter());
let mut average_iter = sats.height.average.as_ref().map(|v| v.into_iter());
let mut sum_iter = sats.height.sum.as_ref().map(|v| v.into_iter());
let mut max_iter = sats.height.max.as_ref().map(|v| v.into_iter());
let mut pct90_iter = sats.height.pct90.as_ref().map(|v| v.into_iter());
let mut pct75_iter = sats.height.pct75.as_ref().map(|v| v.into_iter());
let mut median_iter = sats.height.median.as_ref().map(|v| v.into_iter());
let mut pct25_iter = sats.height.pct25.as_ref().map(|v| v.into_iter());
let mut pct10_iter = sats.height.pct10.as_ref().map(|v| v.into_iter());
let mut min_iter = sats.height.min.as_ref().map(|v| v.into_iter());
let mut last_iter = sats.height.last.as_ref().map(|v| v.into_iter());
let mut cumulative_iter = sats.height.cumulative.as_ref().map(|v| v.into_iter());
(starting_index.to_usize()..indexer.vecs.height_to_weight.len())
.map(Height::from)
.try_for_each(|height| -> Result<()> {
if let Some(first) = self.height.first.as_mut() {
first.truncate_push(
height,
Bitcoin::from(sats.height.unwrap_first().into_iter().get_unwrap(height)),
Bitcoin::from(first_iter.um().get_unwrap(height)),
)?;
}
if let Some(average) = self.height.average.as_mut() {
average.truncate_push(
height,
Bitcoin::from(sats.height.unwrap_average().into_iter().get_unwrap(height)),
Bitcoin::from(average_iter.um().get_unwrap(height)),
)?;
}
if let Some(sum) = self.height.sum.as_mut() {
sum.truncate_push(
height,
Bitcoin::from(sats.height.unwrap_sum().into_iter().get_unwrap(height)),
Bitcoin::from(sum_iter.um().get_unwrap(height)),
)?;
}
if let Some(max) = self.height.max.as_mut() {
max.truncate_push(
height,
Bitcoin::from(sats.height.unwrap_max().into_iter().get_unwrap(height)),
Bitcoin::from(max_iter.um().get_unwrap(height)),
)?;
}
if let Some(pct90) = self.height.pct90.as_mut() {
pct90.truncate_push(
height,
Bitcoin::from(sats.height.unwrap_pct90().into_iter().get_unwrap(height)),
Bitcoin::from(pct90_iter.um().get_unwrap(height)),
)?;
}
if let Some(pct75) = self.height.pct75.as_mut() {
pct75.truncate_push(
height,
Bitcoin::from(sats.height.unwrap_pct75().into_iter().get_unwrap(height)),
Bitcoin::from(pct75_iter.um().get_unwrap(height)),
)?;
}
if let Some(median) = self.height.median.as_mut() {
median.truncate_push(
height,
Bitcoin::from(sats.height.unwrap_median().into_iter().get_unwrap(height)),
Bitcoin::from(median_iter.um().get_unwrap(height)),
)?;
}
if let Some(pct25) = self.height.pct25.as_mut() {
pct25.truncate_push(
height,
Bitcoin::from(sats.height.unwrap_pct25().into_iter().get_unwrap(height)),
Bitcoin::from(pct25_iter.um().get_unwrap(height)),
)?;
}
if let Some(pct10) = self.height.pct10.as_mut() {
pct10.truncate_push(
height,
Bitcoin::from(sats.height.unwrap_pct10().into_iter().get_unwrap(height)),
Bitcoin::from(pct10_iter.um().get_unwrap(height)),
)?;
}
if let Some(min) = self.height.min.as_mut() {
min.truncate_push(
height,
Bitcoin::from(sats.height.unwrap_min().into_iter().get_unwrap(height)),
Bitcoin::from(min_iter.um().get_unwrap(height)),
)?;
}
if let Some(last) = self.height.last.as_mut() {
last.truncate_push(
height,
Bitcoin::from(sats.height.unwrap_last().into_iter().get_unwrap(height)),
Bitcoin::from(last_iter.um().get_unwrap(height)),
)?;
}
if let Some(cumulative) = self.height.cumulative.as_mut() {
cumulative.truncate_push(
height,
Bitcoin::from(
sats.height
.unwrap_cumulative()
.into_iter()
.get_unwrap(height),
),
Bitcoin::from(cumulative_iter.um().get_unwrap(height)),
)?;
}
Ok(())
@@ -341,7 +351,7 @@ impl ComputedVecsFromTxindex<Dollars> {
let txindex_version = if let Some(txindex) = txindex {
txindex.version()
} else {
self.txindex.as_ref().unwrap().as_ref().version()
self.txindex.u().as_ref().version()
};
self.height
@@ -351,6 +361,20 @@ impl ComputedVecsFromTxindex<Dollars> {
let mut close_iter = price.chainindexes_to_price_close.height.into_iter();
// Create iterators once before the loop to avoid repeated iterator creation
let mut first_iter = bitcoin.height.first.as_ref().map(|v| v.into_iter());
let mut average_iter = bitcoin.height.average.as_ref().map(|v| v.into_iter());
let mut sum_iter = bitcoin.height.sum.as_ref().map(|v| v.into_iter());
let mut max_iter = bitcoin.height.max.as_ref().map(|v| v.into_iter());
let mut pct90_iter = bitcoin.height.pct90.as_ref().map(|v| v.into_iter());
let mut pct75_iter = bitcoin.height.pct75.as_ref().map(|v| v.into_iter());
let mut median_iter = bitcoin.height.median.as_ref().map(|v| v.into_iter());
let mut pct25_iter = bitcoin.height.pct25.as_ref().map(|v| v.into_iter());
let mut pct10_iter = bitcoin.height.pct10.as_ref().map(|v| v.into_iter());
let mut min_iter = bitcoin.height.min.as_ref().map(|v| v.into_iter());
let mut last_iter = bitcoin.height.last.as_ref().map(|v| v.into_iter());
let mut cumulative_iter = bitcoin.height.cumulative.as_ref().map(|v| v.into_iter());
(starting_index.to_usize()..indexer.vecs.height_to_weight.len())
.map(Height::from)
.try_for_each(|height| -> Result<()> {
@@ -359,88 +383,73 @@ impl ComputedVecsFromTxindex<Dollars> {
if let Some(first) = self.height.first.as_mut() {
first.truncate_push(
height,
price * bitcoin.height.unwrap_first().into_iter().get_unwrap(height),
price * first_iter.um().get_unwrap(height),
)?;
}
if let Some(average) = self.height.average.as_mut() {
average.truncate_push(
height,
price
* bitcoin
.height
.unwrap_average()
.into_iter()
.get_unwrap(height),
price * average_iter.um().get_unwrap(height),
)?;
}
if let Some(sum) = self.height.sum.as_mut() {
sum.truncate_push(
height,
price * bitcoin.height.unwrap_sum().into_iter().get_unwrap(height),
price * sum_iter.um().get_unwrap(height),
)?;
}
if let Some(max) = self.height.max.as_mut() {
max.truncate_push(
height,
price * bitcoin.height.unwrap_max().into_iter().get_unwrap(height),
price * max_iter.um().get_unwrap(height),
)?;
}
if let Some(pct90) = self.height.pct90.as_mut() {
pct90.truncate_push(
height,
price * bitcoin.height.unwrap_pct90().into_iter().get_unwrap(height),
price * pct90_iter.um().get_unwrap(height),
)?;
}
if let Some(pct75) = self.height.pct75.as_mut() {
pct75.truncate_push(
height,
price * bitcoin.height.unwrap_pct75().into_iter().get_unwrap(height),
price * pct75_iter.um().get_unwrap(height),
)?;
}
if let Some(median) = self.height.median.as_mut() {
median.truncate_push(
height,
price
* bitcoin
.height
.unwrap_median()
.into_iter()
.get_unwrap(height),
price * median_iter.um().get_unwrap(height),
)?;
}
if let Some(pct25) = self.height.pct25.as_mut() {
pct25.truncate_push(
height,
price * bitcoin.height.unwrap_pct25().into_iter().get_unwrap(height),
price * pct25_iter.um().get_unwrap(height),
)?;
}
if let Some(pct10) = self.height.pct10.as_mut() {
pct10.truncate_push(
height,
price * bitcoin.height.unwrap_pct10().into_iter().get_unwrap(height),
price * pct10_iter.um().get_unwrap(height),
)?;
}
if let Some(min) = self.height.min.as_mut() {
min.truncate_push(
height,
price * bitcoin.height.unwrap_min().into_iter().get_unwrap(height),
price * min_iter.um().get_unwrap(height),
)?;
}
if let Some(last) = self.height.last.as_mut() {
last.truncate_push(
height,
price * bitcoin.height.unwrap_last().into_iter().get_unwrap(height),
price * last_iter.um().get_unwrap(height),
)?;
}
if let Some(cumulative) = self.height.cumulative.as_mut() {
cumulative.truncate_push(
height,
price
* bitcoin
.height
.unwrap_cumulative()
.into_iter()
.get_unwrap(height),
price * cumulative_iter.um().get_unwrap(height),
)?;
}
Ok(())

View File

@@ -12,7 +12,7 @@ use crate::{
ComputedStandardDeviationVecsFromDateIndex, StandardDeviationVecsOptions, source::Source,
},
indexes, price,
utils::get_percentile,
utils::{get_percentile, OptionExt},
};
use super::{ComputedVecsFromDateIndex, VecBuilderOptions};
@@ -55,230 +55,48 @@ impl ComputedRatioVecsFromDateIndex {
indexes: &indexes::Vecs,
extended: bool,
) -> Result<Self> {
let options = VecBuilderOptions::default().add_last();
let opts = VecBuilderOptions::default().add_last();
let v = version + VERSION;
macro_rules! import {
($suffix:expr) => {
ComputedVecsFromDateIndex::forced_import(
db, &format!("{name}_{}", $suffix), Source::Compute, v, indexes, opts,
).unwrap()
};
}
macro_rules! import_sd {
($suffix:expr, $days:expr) => {
ComputedStandardDeviationVecsFromDateIndex::forced_import(
db, &format!("{name}_{}", $suffix), $days, Source::Compute, v, indexes,
StandardDeviationVecsOptions::default().add_all(),
).unwrap()
};
}
Ok(Self {
price: source.is_compute().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
name,
Source::Compute,
version + VERSION,
indexes,
options,
)
.unwrap()
}),
ratio: ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)?,
ratio_1w_sma: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_1w_sma"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_1m_sma: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_1m_sma"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_sd: extended.then(|| {
ComputedStandardDeviationVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio"),
usize::MAX,
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
StandardDeviationVecsOptions::default().add_all(),
)
.unwrap()
}),
ratio_1y_sd: extended.then(|| {
ComputedStandardDeviationVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_1y"),
365,
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
StandardDeviationVecsOptions::default().add_all(),
)
.unwrap()
}),
ratio_2y_sd: extended.then(|| {
ComputedStandardDeviationVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_2y"),
2 * 365,
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
StandardDeviationVecsOptions::default().add_all(),
)
.unwrap()
}),
ratio_4y_sd: extended.then(|| {
ComputedStandardDeviationVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_4y"),
4 * 365,
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
StandardDeviationVecsOptions::default().add_all(),
)
.unwrap()
}),
ratio_pct99: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct99"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_pct98: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct98"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_pct95: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct95"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_pct5: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct5"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_pct2: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct2"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_pct1: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct1"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_pct99_usd: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct99_usd"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_pct98_usd: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct98_usd"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_pct95_usd: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct95_usd"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_pct5_usd: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct5_usd"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_pct2_usd: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct2_usd"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
}),
ratio_pct1_usd: extended.then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_ratio_pct1_usd"),
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
options,
)
.unwrap()
ComputedVecsFromDateIndex::forced_import(db, name, Source::Compute, v, indexes, opts).unwrap()
}),
ratio: import!("ratio"),
ratio_1w_sma: extended.then(|| import!("ratio_1w_sma")),
ratio_1m_sma: extended.then(|| import!("ratio_1m_sma")),
ratio_sd: extended.then(|| import_sd!("ratio", usize::MAX)),
ratio_1y_sd: extended.then(|| import_sd!("ratio_1y", 365)),
ratio_2y_sd: extended.then(|| import_sd!("ratio_2y", 2 * 365)),
ratio_4y_sd: extended.then(|| import_sd!("ratio_4y", 4 * 365)),
ratio_pct99: extended.then(|| import!("ratio_pct99")),
ratio_pct98: extended.then(|| import!("ratio_pct98")),
ratio_pct95: extended.then(|| import!("ratio_pct95")),
ratio_pct5: extended.then(|| import!("ratio_pct5")),
ratio_pct2: extended.then(|| import!("ratio_pct2")),
ratio_pct1: extended.then(|| import!("ratio_pct1")),
ratio_pct99_usd: extended.then(|| import!("ratio_pct99_usd")),
ratio_pct98_usd: extended.then(|| import!("ratio_pct98_usd")),
ratio_pct95_usd: extended.then(|| import!("ratio_pct95_usd")),
ratio_pct5_usd: extended.then(|| import!("ratio_pct5_usd")),
ratio_pct2_usd: extended.then(|| import!("ratio_pct2_usd")),
ratio_pct1_usd: extended.then(|| import!("ratio_pct1_usd")),
})
}
@@ -308,10 +126,10 @@ impl ComputedRatioVecsFromDateIndex {
exit: &Exit,
price_opt: Option<&impl IterableVec<DateIndex, Dollars>>,
) -> Result<()> {
let closes = price.timeindexes_to_price_close.dateindex.as_ref().unwrap();
let closes = price.timeindexes_to_price_close.dateindex.u();
let price = price_opt.unwrap_or_else(|| unsafe {
std::mem::transmute(&self.price.as_ref().unwrap().dateindex)
std::mem::transmute(&self.price.u().dateindex)
});
self.ratio.compute_all(starting_indexes, exit, |v| {
@@ -343,7 +161,7 @@ impl ComputedRatioVecsFromDateIndex {
.compute_all(starting_indexes, exit, |v| {
v.compute_sma_(
starting_indexes.dateindex,
self.ratio.dateindex.as_ref().unwrap(),
self.ratio.dateindex.u(),
7,
exit,
Some(min_ratio_date),
@@ -357,7 +175,7 @@ impl ComputedRatioVecsFromDateIndex {
.compute_all(starting_indexes, exit, |v| {
v.compute_sma_(
starting_indexes.dateindex,
self.ratio.dateindex.as_ref().unwrap(),
self.ratio.dateindex.u(),
30,
exit,
Some(min_ratio_date),
@@ -365,7 +183,7 @@ impl ComputedRatioVecsFromDateIndex {
Ok(())
})?;
let ratio_version = self.ratio.dateindex.as_ref().unwrap().version();
let ratio_version = self.ratio.dateindex.u().version();
self.mut_ratio_vecs()
.iter_mut()
.try_for_each(|v| -> Result<()> {
@@ -385,7 +203,7 @@ impl ComputedRatioVecsFromDateIndex {
let min_ratio_date_usize = min_ratio_date.to_usize();
let mut sorted = self.ratio.dateindex.as_ref().unwrap().collect_range(
let mut sorted = self.ratio.dateindex.u().collect_range(
Some(min_ratio_date_usize),
Some(starting_dateindex.to_usize()),
);
@@ -498,39 +316,39 @@ impl ComputedRatioVecsFromDateIndex {
.into_iter()
.try_for_each(|v| v.safe_flush(exit))?;
self.ratio_pct1.as_mut().unwrap().compute_rest(
self.ratio_pct1.um().compute_rest(
starting_indexes,
exit,
None as Option<&EagerVec<PcoVec<_, _>>>,
)?;
self.ratio_pct2.as_mut().unwrap().compute_rest(
self.ratio_pct2.um().compute_rest(
starting_indexes,
exit,
None as Option<&EagerVec<PcoVec<_, _>>>,
)?;
self.ratio_pct5.as_mut().unwrap().compute_rest(
self.ratio_pct5.um().compute_rest(
starting_indexes,
exit,
None as Option<&EagerVec<PcoVec<_, _>>>,
)?;
self.ratio_pct95.as_mut().unwrap().compute_rest(
self.ratio_pct95.um().compute_rest(
starting_indexes,
exit,
None as Option<&EagerVec<PcoVec<_, _>>>,
)?;
self.ratio_pct98.as_mut().unwrap().compute_rest(
self.ratio_pct98.um().compute_rest(
starting_indexes,
exit,
None as Option<&EagerVec<PcoVec<_, _>>>,
)?;
self.ratio_pct99.as_mut().unwrap().compute_rest(
self.ratio_pct99.um().compute_rest(
starting_indexes,
exit,
None as Option<&EagerVec<PcoVec<_, _>>>,
)?;
let date_to_price = price_opt.unwrap_or_else(|| unsafe {
std::mem::transmute(&self.price.as_ref().unwrap().dateindex)
std::mem::transmute(&self.price.u().dateindex)
});
self.ratio_pct99_usd
@@ -561,7 +379,7 @@ impl ComputedRatioVecsFromDateIndex {
|usd: Option<&mut ComputedVecsFromDateIndex<Dollars>>,
source: Option<&ComputedVecsFromDateIndex<StoredF32>>| {
usd.unwrap().compute_all(starting_indexes, exit, |vec| {
let mut iter = source.unwrap().dateindex.as_ref().unwrap().into_iter();
let mut iter = source.unwrap().dateindex.u().into_iter();
vec.compute_transform(
starting_indexes.dateindex,
date_to_price,
@@ -582,28 +400,28 @@ impl ComputedRatioVecsFromDateIndex {
compute_usd(self.ratio_pct98_usd.as_mut(), self.ratio_pct98.as_ref())?;
compute_usd(self.ratio_pct99_usd.as_mut(), self.ratio_pct99.as_ref())?;
self.ratio_sd.as_mut().unwrap().compute_all(
self.ratio_sd.um().compute_all(
starting_indexes,
exit,
self.ratio.dateindex.as_ref().unwrap(),
self.ratio.dateindex.u(),
Some(date_to_price),
)?;
self.ratio_4y_sd.as_mut().unwrap().compute_all(
self.ratio_4y_sd.um().compute_all(
starting_indexes,
exit,
self.ratio.dateindex.as_ref().unwrap(),
self.ratio.dateindex.u(),
Some(date_to_price),
)?;
self.ratio_2y_sd.as_mut().unwrap().compute_all(
self.ratio_2y_sd.um().compute_all(
starting_indexes,
exit,
self.ratio.dateindex.as_ref().unwrap(),
self.ratio.dateindex.u(),
Some(date_to_price),
)?;
self.ratio_1y_sd.as_mut().unwrap().compute_all(
self.ratio_1y_sd.um().compute_all(
starting_indexes,
exit,
self.ratio.dateindex.as_ref().unwrap(),
self.ratio.dateindex.u(),
Some(date_to_price),
)?;
@@ -613,22 +431,22 @@ impl ComputedRatioVecsFromDateIndex {
fn mut_ratio_vecs(&mut self) -> Vec<&mut EagerVec<PcoVec<DateIndex, StoredF32>>> {
let mut vecs = Vec::with_capacity(6);
if let Some(v) = self.ratio_pct1.as_mut() {
vecs.push(v.dateindex.as_mut().unwrap());
vecs.push(v.dateindex.um());
}
if let Some(v) = self.ratio_pct2.as_mut() {
vecs.push(v.dateindex.as_mut().unwrap());
vecs.push(v.dateindex.um());
}
if let Some(v) = self.ratio_pct5.as_mut() {
vecs.push(v.dateindex.as_mut().unwrap());
vecs.push(v.dateindex.um());
}
if let Some(v) = self.ratio_pct95.as_mut() {
vecs.push(v.dateindex.as_mut().unwrap());
vecs.push(v.dateindex.um());
}
if let Some(v) = self.ratio_pct98.as_mut() {
vecs.push(v.dateindex.as_mut().unwrap());
vecs.push(v.dateindex.um());
}
if let Some(v) = self.ratio_pct99.as_mut() {
vecs.push(v.dateindex.as_mut().unwrap());
vecs.push(v.dateindex.um());
}
vecs
}

View File

@@ -6,7 +6,7 @@ use vecdb::{PcoVec,
GenericStoredVec, IterableVec, VecIndex,
};
use crate::{Indexes, grouped::source::Source, indexes};
use crate::{Indexes, grouped::source::Source, indexes, utils::OptionExt};
use super::{ComputedVecsFromDateIndex, VecBuilderOptions};
@@ -103,317 +103,47 @@ impl ComputedStandardDeviationVecsFromDateIndex {
indexes: &indexes::Vecs,
options: StandardDeviationVecsOptions,
) -> Result<Self> {
let builder_options = VecBuilderOptions::default().add_last();
let opts = VecBuilderOptions::default().add_last();
let version = parent_version + Version::ONE;
macro_rules! import {
($suffix:expr) => {
ComputedVecsFromDateIndex::forced_import(
db, &format!("{name}_{}", $suffix), Source::Compute, version, indexes, opts,
).unwrap()
};
}
Ok(Self {
days,
sma: sma.is_compute().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_sma"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
sd: ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)?,
p0_5sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p0_5sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
p1sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p1sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
p1_5sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p1_5sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
p2sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p2sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
p2_5sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p2_5sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
p3sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p3sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m0_5sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m0_5sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m1sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m1sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m1_5sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m1_5sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m2sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m2sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m2_5sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m2_5sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m3sd: options.bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m3sd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
_0sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_0sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
p0_5sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p0_5sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
p1sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p1sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
p1_5sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p1_5sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
p2sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p2sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
p2_5sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p2_5sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
p3sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_p3sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m0_5sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m0_5sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m1sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m1sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m1_5sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m1_5sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m2sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m2sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m2_5sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m2_5sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
m3sd_usd: options.price_bands().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_m3sd_usd"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
zscore: options.zscore().then(|| {
ComputedVecsFromDateIndex::forced_import(
db,
&format!("{name}_zscore"),
Source::Compute,
version + Version::ZERO,
indexes,
builder_options,
)
.unwrap()
}),
sma: sma.is_compute().then(|| import!("sma")),
sd: import!("sd"),
p0_5sd: options.bands().then(|| import!("p0_5sd")),
p1sd: options.bands().then(|| import!("p1sd")),
p1_5sd: options.bands().then(|| import!("p1_5sd")),
p2sd: options.bands().then(|| import!("p2sd")),
p2_5sd: options.bands().then(|| import!("p2_5sd")),
p3sd: options.bands().then(|| import!("p3sd")),
m0_5sd: options.bands().then(|| import!("m0_5sd")),
m1sd: options.bands().then(|| import!("m1sd")),
m1_5sd: options.bands().then(|| import!("m1_5sd")),
m2sd: options.bands().then(|| import!("m2sd")),
m2_5sd: options.bands().then(|| import!("m2_5sd")),
m3sd: options.bands().then(|| import!("m3sd")),
_0sd_usd: options.price_bands().then(|| import!("0sd_usd")),
p0_5sd_usd: options.price_bands().then(|| import!("p0_5sd_usd")),
p1sd_usd: options.price_bands().then(|| import!("p1sd_usd")),
p1_5sd_usd: options.price_bands().then(|| import!("p1_5sd_usd")),
p2sd_usd: options.price_bands().then(|| import!("p2sd_usd")),
p2_5sd_usd: options.price_bands().then(|| import!("p2_5sd_usd")),
p3sd_usd: options.price_bands().then(|| import!("p3sd_usd")),
m0_5sd_usd: options.price_bands().then(|| import!("m0_5sd_usd")),
m1sd_usd: options.price_bands().then(|| import!("m1sd_usd")),
m1_5sd_usd: options.price_bands().then(|| import!("m1_5sd_usd")),
m2sd_usd: options.price_bands().then(|| import!("m2sd_usd")),
m2_5sd_usd: options.price_bands().then(|| import!("m2_5sd_usd")),
m3sd_usd: options.price_bands().then(|| import!("m3sd_usd")),
zscore: options.zscore().then(|| import!("zscore")),
})
}
@@ -454,7 +184,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
price_opt: Option<&impl IterableVec<DateIndex, Dollars>>,
) -> Result<()> {
let sma = sma_opt.unwrap_or_else(|| unsafe {
std::mem::transmute(&self.sma.as_ref().unwrap().dateindex)
std::mem::transmute(&self.sma.u().dateindex)
});
let min_date = DateIndex::try_from(Date::MIN_RATIO).unwrap();
@@ -483,18 +213,18 @@ impl ComputedStandardDeviationVecsFromDateIndex {
sorted.sort_unstable();
let mut p0_5sd = self.p0_5sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut p1sd = self.p1sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut p1_5sd = self.p1_5sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut p2sd = self.p2sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut p2_5sd = self.p2_5sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut p3sd = self.p3sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut m0_5sd = self.m0_5sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut m1sd = self.m1sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut m1_5sd = self.m1_5sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut m2sd = self.m2sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut m2_5sd = self.m2_5sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut m3sd = self.m3sd.as_mut().map(|c| c.dateindex.as_mut().unwrap());
let mut p0_5sd = self.p0_5sd.as_mut().map(|c| c.dateindex.um());
let mut p1sd = self.p1sd.as_mut().map(|c| c.dateindex.um());
let mut p1_5sd = self.p1_5sd.as_mut().map(|c| c.dateindex.um());
let mut p2sd = self.p2sd.as_mut().map(|c| c.dateindex.um());
let mut p2_5sd = self.p2_5sd.as_mut().map(|c| c.dateindex.um());
let mut p3sd = self.p3sd.as_mut().map(|c| c.dateindex.um());
let mut m0_5sd = self.m0_5sd.as_mut().map(|c| c.dateindex.um());
let mut m1sd = self.m1sd.as_mut().map(|c| c.dateindex.um());
let mut m1_5sd = self.m1_5sd.as_mut().map(|c| c.dateindex.um());
let mut m2sd = self.m2sd.as_mut().map(|c| c.dateindex.um());
let mut m2_5sd = self.m2_5sd.as_mut().map(|c| c.dateindex.um());
let mut m3sd = self.m3sd.as_mut().map(|c| c.dateindex.um());
let min_date_usize = min_date.to_usize();
let mut sma_iter = sma.iter().skip(starting_dateindex.to_usize());
@@ -624,7 +354,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
starting_indexes.dateindex,
source,
sma,
self.sd.dateindex.as_ref().unwrap(),
self.sd.dateindex.u(),
exit,
)?;
Ok(())
@@ -656,9 +386,9 @@ impl ComputedStandardDeviationVecsFromDateIndex {
return Ok(());
}
compute_usd(self._0sd_usd.as_mut().unwrap(), sma.iter())?;
compute_usd(self._0sd_usd.um(), sma.iter())?;
compute_usd(
self.p0_5sd_usd.as_mut().unwrap(),
self.p0_5sd_usd.um(),
self.p0_5sd
.as_ref()
.unwrap()
@@ -668,7 +398,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
.iter(),
)?;
compute_usd(
self.p1sd_usd.as_mut().unwrap(),
self.p1sd_usd.um(),
self.p1sd
.as_ref()
.unwrap()
@@ -678,7 +408,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
.iter(),
)?;
compute_usd(
self.p1_5sd_usd.as_mut().unwrap(),
self.p1_5sd_usd.um(),
self.p1_5sd
.as_ref()
.unwrap()
@@ -688,7 +418,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
.iter(),
)?;
compute_usd(
self.p2sd_usd.as_mut().unwrap(),
self.p2sd_usd.um(),
self.p2sd
.as_ref()
.unwrap()
@@ -698,7 +428,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
.iter(),
)?;
compute_usd(
self.p2_5sd_usd.as_mut().unwrap(),
self.p2_5sd_usd.um(),
self.p2_5sd
.as_ref()
.unwrap()
@@ -708,7 +438,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
.iter(),
)?;
compute_usd(
self.p3sd_usd.as_mut().unwrap(),
self.p3sd_usd.um(),
self.p3sd
.as_ref()
.unwrap()
@@ -718,7 +448,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
.iter(),
)?;
compute_usd(
self.m0_5sd_usd.as_mut().unwrap(),
self.m0_5sd_usd.um(),
self.m0_5sd
.as_ref()
.unwrap()
@@ -728,7 +458,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
.iter(),
)?;
compute_usd(
self.m1sd_usd.as_mut().unwrap(),
self.m1sd_usd.um(),
self.m1sd
.as_ref()
.unwrap()
@@ -738,7 +468,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
.iter(),
)?;
compute_usd(
self.m1_5sd_usd.as_mut().unwrap(),
self.m1_5sd_usd.um(),
self.m1_5sd
.as_ref()
.unwrap()
@@ -748,7 +478,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
.iter(),
)?;
compute_usd(
self.m2sd_usd.as_mut().unwrap(),
self.m2sd_usd.um(),
self.m2sd
.as_ref()
.unwrap()
@@ -758,7 +488,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
.iter(),
)?;
compute_usd(
self.m2_5sd_usd.as_mut().unwrap(),
self.m2_5sd_usd.um(),
self.m2_5sd
.as_ref()
.unwrap()
@@ -768,7 +498,7 @@ impl ComputedStandardDeviationVecsFromDateIndex {
.iter(),
)?;
compute_usd(
self.m3sd_usd.as_mut().unwrap(),
self.m3sd_usd.um(),
self.m3sd
.as_ref()
.unwrap()
@@ -807,6 +537,6 @@ impl ComputedStandardDeviationVecsFromDateIndex {
&mut self,
) -> impl Iterator<Item = &mut EagerVec<PcoVec<DateIndex, StoredF32>>> {
self.mut_stateful_computed()
.map(|c| c.dateindex.as_mut().unwrap())
.map(|c| c.dateindex.um())
}
}

View File

@@ -8,6 +8,7 @@ use crate::{
grouped::ComputedVecsFromDateIndex,
indexes, price,
traits::{ComputeFromBitcoin, ComputeFromSats},
utils::OptionExt,
};
use super::{Source, VecBuilderOptions};
@@ -73,7 +74,7 @@ impl ComputedValueVecsFromDateIndex {
where
F: FnMut(&mut EagerVec<PcoVec<DateIndex, Sats>>) -> Result<()>,
{
compute(self.sats.dateindex.as_mut().unwrap())?;
compute(self.sats.dateindex.um())?;
let dateindex: Option<&PcoVec<DateIndex, Sats>> = None;
self.compute_rest(price, starting_indexes, exit, dateindex)?;
@@ -103,13 +104,13 @@ impl ComputedValueVecsFromDateIndex {
self.bitcoin.compute_all(starting_indexes, exit, |v| {
v.compute_from_sats(
starting_indexes.dateindex,
self.sats.dateindex.as_ref().unwrap(),
self.sats.dateindex.u(),
exit,
)
})?;
}
let dateindex_to_bitcoin = self.bitcoin.dateindex.as_ref().unwrap();
let dateindex_to_bitcoin = self.bitcoin.dateindex.u();
let dateindex_to_price_close = price
.as_ref()
.unwrap()

View File

@@ -8,6 +8,7 @@ use crate::{
grouped::Source,
indexes, price,
traits::{ComputeFromBitcoin, ComputeFromSats},
utils::OptionExt,
};
use super::{ComputedVecsFromHeight, VecBuilderOptions};
@@ -74,7 +75,7 @@ impl ComputedValueVecsFromHeight {
where
F: FnMut(&mut EagerVec<PcoVec<Height, Sats>>) -> Result<()>,
{
compute(self.sats.height.as_mut().unwrap())?;
compute(self.sats.height.um())?;
let height: Option<&PcoVec<Height, Sats>> = None;
self.compute_rest(indexes, price, starting_indexes, exit, height)?;
@@ -108,14 +109,14 @@ impl ComputedValueVecsFromHeight {
.compute_all(indexes, starting_indexes, exit, |v| {
v.compute_from_sats(
starting_indexes.height,
self.sats.height.as_ref().unwrap(),
self.sats.height.u(),
exit,
)
})?;
}
let height_to_bitcoin = self.bitcoin.height.as_ref().unwrap();
let height_to_price_close = &price.as_ref().unwrap().chainindexes_to_price_close.height;
let height_to_bitcoin = self.bitcoin.height.u();
let height_to_price_close = &price.u().chainindexes_to_price_close.height;
if let Some(dollars) = self.dollars.as_mut() {
dollars.compute_all(indexes, starting_indexes, exit, |v| {

View File

@@ -7,7 +7,7 @@ use vecdb::{
VecIndex,
};
use crate::{Indexes, grouped::Source, indexes, price};
use crate::{Indexes, grouped::Source, indexes, price, utils::OptionExt};
use super::{ComputedVecsFromTxindex, VecBuilderOptions};
@@ -56,7 +56,7 @@ impl ComputedValueVecsFromTxindex {
let bitcoin_txindex = LazyVecFrom1::init(
&name_btc,
version + VERSION,
source_vec.map_or_else(|| sats.txindex.as_ref().unwrap().boxed_clone(), |s| s),
source_vec.map_or_else(|| sats.txindex.u().boxed_clone(), |s| s),
|txindex: TxIndex, iter| iter.get_at(txindex.to_usize()).map(Bitcoin::from),
);
@@ -130,7 +130,7 @@ impl ComputedValueVecsFromTxindex {
// ) -> Result<()>,
// {
// compute(
// self.sats.txindex.as_mut().unwrap(),
// self.sats.txindex.um(),
// indexer,
// indexes,
// starting_indexes,
@@ -178,7 +178,7 @@ impl ComputedValueVecsFromTxindex {
)?;
if let Some(dollars) = self.dollars.as_mut() {
let dollars_txindex = self.dollars_txindex.as_mut().unwrap();
let dollars_txindex = self.dollars_txindex.um();
dollars.compute_rest_from_bitcoin(
indexer,
@@ -187,7 +187,7 @@ impl ComputedValueVecsFromTxindex {
exit,
&self.bitcoin,
Some(dollars_txindex),
price.as_ref().unwrap(),
price.u(),
)?;
}

View File

@@ -8,6 +8,7 @@ use crate::{
grouped::Source,
price,
traits::{ComputeFromBitcoin, ComputeFromSats},
utils::OptionExt,
};
#[derive(Clone, Traversable)]
@@ -57,7 +58,7 @@ impl ComputedHeightValueVecs {
where
F: FnMut(&mut EagerVec<PcoVec<Height, Sats>>) -> Result<()>,
{
compute(self.sats.as_mut().unwrap())?;
compute(self.sats.um())?;
let height: Option<&PcoVec<Height, Sats>> = None;
self.compute_rest(price, starting_indexes, exit, height)?;
@@ -78,13 +79,13 @@ impl ComputedHeightValueVecs {
} else {
self.bitcoin.compute_from_sats(
starting_indexes.height,
self.sats.as_ref().unwrap(),
self.sats.u(),
exit,
)?;
}
let height_to_bitcoin = &self.bitcoin;
let height_to_price_close = &price.as_ref().unwrap().chainindexes_to_price_close.height;
let height_to_price_close = &price.u().chainindexes_to_price_close.height;
if let Some(dollars) = self.dollars.as_mut() {
dollars.compute_from_bitcoin(

View File

@@ -107,301 +107,75 @@ impl Vecs {
let version = parent_version + VERSION;
macro_rules! eager {
($name:expr) => { EagerVec::forced_import(&db, $name, version)? };
}
macro_rules! lazy {
($name:expr, $source:expr) => {
LazyVecFrom1::init($name, version, $source.boxed_clone(), |index, _| Some(index))
};
}
let this = Self {
txinindex_to_txoutindex: EagerVec::forced_import(&db, "txoutindex", version)?,
txoutindex_to_txoutindex: LazyVecFrom1::init(
"txoutindex",
version + Version::ZERO,
indexer.vecs.txoutindex_to_value.boxed_clone(),
|index, _| Some(index),
),
txinindex_to_txinindex: LazyVecFrom1::init(
"txinindex",
version + Version::ZERO,
indexer.vecs.txinindex_to_outpoint.boxed_clone(),
|index, _| Some(index),
),
p2pk33addressindex_to_p2pk33addressindex: LazyVecFrom1::init(
"p2pk33addressindex",
version + Version::ZERO,
indexer.vecs.p2pk33addressindex_to_p2pk33bytes.boxed_clone(),
|index, _| Some(index),
),
p2pk65addressindex_to_p2pk65addressindex: LazyVecFrom1::init(
"p2pk65addressindex",
version + Version::ZERO,
indexer.vecs.p2pk65addressindex_to_p2pk65bytes.boxed_clone(),
|index, _| Some(index),
),
p2pkhaddressindex_to_p2pkhaddressindex: LazyVecFrom1::init(
"p2pkhaddressindex",
version + Version::ZERO,
indexer.vecs.p2pkhaddressindex_to_p2pkhbytes.boxed_clone(),
|index, _| Some(index),
),
p2shaddressindex_to_p2shaddressindex: LazyVecFrom1::init(
"p2shaddressindex",
version + Version::ZERO,
indexer.vecs.p2shaddressindex_to_p2shbytes.boxed_clone(),
|index, _| Some(index),
),
p2traddressindex_to_p2traddressindex: LazyVecFrom1::init(
"p2traddressindex",
version + Version::ZERO,
indexer.vecs.p2traddressindex_to_p2trbytes.boxed_clone(),
|index, _| Some(index),
),
p2wpkhaddressindex_to_p2wpkhaddressindex: LazyVecFrom1::init(
"p2wpkhaddressindex",
version + Version::ZERO,
indexer.vecs.p2wpkhaddressindex_to_p2wpkhbytes.boxed_clone(),
|index, _| Some(index),
),
p2wshaddressindex_to_p2wshaddressindex: LazyVecFrom1::init(
"p2wshaddressindex",
version + Version::ZERO,
indexer.vecs.p2wshaddressindex_to_p2wshbytes.boxed_clone(),
|index, _| Some(index),
),
p2aaddressindex_to_p2aaddressindex: LazyVecFrom1::init(
"p2aaddressindex",
version + Version::ZERO,
indexer.vecs.p2aaddressindex_to_p2abytes.boxed_clone(),
|index, _| Some(index),
),
p2msoutputindex_to_p2msoutputindex: LazyVecFrom1::init(
"p2msoutputindex",
version + Version::ZERO,
indexer.vecs.p2msoutputindex_to_txindex.boxed_clone(),
|index, _| Some(index),
),
emptyoutputindex_to_emptyoutputindex: LazyVecFrom1::init(
"emptyoutputindex",
version + Version::ZERO,
indexer.vecs.emptyoutputindex_to_txindex.boxed_clone(),
|index, _| Some(index),
),
unknownoutputindex_to_unknownoutputindex: LazyVecFrom1::init(
"unknownoutputindex",
version + Version::ZERO,
indexer.vecs.unknownoutputindex_to_txindex.boxed_clone(),
|index, _| Some(index),
),
opreturnindex_to_opreturnindex: LazyVecFrom1::init(
"opreturnindex",
version + Version::ZERO,
indexer.vecs.opreturnindex_to_txindex.boxed_clone(),
|index, _| Some(index),
),
txindex_to_txindex: LazyVecFrom1::init(
"txindex",
version + Version::ZERO,
indexer.vecs.txindex_to_txid.boxed_clone(),
|index, _| Some(index),
),
txindex_to_input_count: EagerVec::forced_import(
&db,
"input_count",
version + Version::ZERO,
)?,
txindex_to_output_count: EagerVec::forced_import(
&db,
"output_count",
version + Version::ZERO,
)?,
dateindex_to_date: EagerVec::forced_import(&db, "date", version + Version::ZERO)?,
dateindex_to_dateindex: EagerVec::forced_import(
&db,
"dateindex",
version + Version::ZERO,
)?,
dateindex_to_first_height: EagerVec::forced_import(
&db,
"first_height",
version + Version::ZERO,
)?,
dateindex_to_monthindex: EagerVec::forced_import(
&db,
"monthindex",
version + Version::ZERO,
)?,
dateindex_to_weekindex: EagerVec::forced_import(
&db,
"weekindex",
version + Version::ZERO,
)?,
decadeindex_to_decadeindex: EagerVec::forced_import(
&db,
"decadeindex",
version + Version::ZERO,
)?,
decadeindex_to_first_yearindex: EagerVec::forced_import(
&db,
"first_yearindex",
version + Version::ZERO,
)?,
difficultyepoch_to_difficultyepoch: EagerVec::forced_import(
&db,
"difficultyepoch",
version + Version::ZERO,
)?,
difficultyepoch_to_first_height: EagerVec::forced_import(
&db,
"first_height",
version + Version::ZERO,
)?,
halvingepoch_to_first_height: EagerVec::forced_import(
&db,
"first_height",
version + Version::ZERO,
)?,
halvingepoch_to_halvingepoch: EagerVec::forced_import(
&db,
"halvingepoch",
version + Version::ZERO,
)?,
height_to_date: EagerVec::forced_import(&db, "date", version + Version::ZERO)?,
height_to_difficultyepoch: EagerVec::forced_import(
&db,
"difficultyepoch",
version + Version::ZERO,
)?,
height_to_halvingepoch: EagerVec::forced_import(
&db,
"halvingepoch",
version + Version::ZERO,
)?,
height_to_height: EagerVec::forced_import(&db, "height", version + Version::ZERO)?,
monthindex_to_first_dateindex: EagerVec::forced_import(
&db,
"first_dateindex",
version + Version::ZERO,
)?,
monthindex_to_monthindex: EagerVec::forced_import(
&db,
"monthindex",
version + Version::ZERO,
)?,
monthindex_to_quarterindex: EagerVec::forced_import(
&db,
"quarterindex",
version + Version::ZERO,
)?,
monthindex_to_semesterindex: EagerVec::forced_import(
&db,
"semesterindex",
version + Version::ZERO,
)?,
monthindex_to_yearindex: EagerVec::forced_import(
&db,
"yearindex",
version + Version::ZERO,
)?,
quarterindex_to_first_monthindex: EagerVec::forced_import(
&db,
"first_monthindex",
version + Version::ZERO,
)?,
semesterindex_to_first_monthindex: EagerVec::forced_import(
&db,
"first_monthindex",
version + Version::ZERO,
)?,
weekindex_to_first_dateindex: EagerVec::forced_import(
&db,
"first_dateindex",
version + Version::ZERO,
)?,
yearindex_to_first_monthindex: EagerVec::forced_import(
&db,
"first_monthindex",
version + Version::ZERO,
)?,
quarterindex_to_quarterindex: EagerVec::forced_import(
&db,
"quarterindex",
version + Version::ZERO,
)?,
semesterindex_to_semesterindex: EagerVec::forced_import(
&db,
"semesterindex",
version + Version::ZERO,
)?,
weekindex_to_weekindex: EagerVec::forced_import(
&db,
"weekindex",
version + Version::ZERO,
)?,
yearindex_to_decadeindex: EagerVec::forced_import(
&db,
"decadeindex",
version + Version::ZERO,
)?,
yearindex_to_yearindex: EagerVec::forced_import(
&db,
"yearindex",
version + Version::ZERO,
)?,
height_to_date_fixed: EagerVec::forced_import(
&db,
"date_fixed",
version + Version::ZERO,
)?,
height_to_dateindex: EagerVec::forced_import(
&db,
"dateindex",
version + Version::ZERO,
)?,
height_to_timestamp_fixed: EagerVec::forced_import(
&db,
"timestamp_fixed",
version + Version::ZERO,
)?,
height_to_txindex_count: EagerVec::forced_import(
&db,
"txindex_count",
version + Version::ZERO,
)?,
dateindex_to_height_count: EagerVec::forced_import(
&db,
"height_count",
version + Version::ZERO,
)?,
weekindex_to_dateindex_count: EagerVec::forced_import(
&db,
"dateindex_count",
version + Version::ZERO,
)?,
difficultyepoch_to_height_count: EagerVec::forced_import(
&db,
"height_count",
version + Version::ZERO,
)?,
monthindex_to_dateindex_count: EagerVec::forced_import(
&db,
"dateindex_count",
version + Version::ZERO,
)?,
quarterindex_to_monthindex_count: EagerVec::forced_import(
&db,
"monthindex_count",
version + Version::ZERO,
)?,
semesterindex_to_monthindex_count: EagerVec::forced_import(
&db,
"monthindex_count",
version + Version::ZERO,
)?,
yearindex_to_monthindex_count: EagerVec::forced_import(
&db,
"monthindex_count",
version + Version::ZERO,
)?,
decadeindex_to_yearindex_count: EagerVec::forced_import(
&db,
"yearindex_count",
version + Version::ZERO,
)?,
txinindex_to_txoutindex: eager!("txoutindex"),
txoutindex_to_txoutindex: lazy!("txoutindex", indexer.vecs.txoutindex_to_value),
txinindex_to_txinindex: lazy!("txinindex", indexer.vecs.txinindex_to_outpoint),
p2pk33addressindex_to_p2pk33addressindex: lazy!("p2pk33addressindex", indexer.vecs.p2pk33addressindex_to_p2pk33bytes),
p2pk65addressindex_to_p2pk65addressindex: lazy!("p2pk65addressindex", indexer.vecs.p2pk65addressindex_to_p2pk65bytes),
p2pkhaddressindex_to_p2pkhaddressindex: lazy!("p2pkhaddressindex", indexer.vecs.p2pkhaddressindex_to_p2pkhbytes),
p2shaddressindex_to_p2shaddressindex: lazy!("p2shaddressindex", indexer.vecs.p2shaddressindex_to_p2shbytes),
p2traddressindex_to_p2traddressindex: lazy!("p2traddressindex", indexer.vecs.p2traddressindex_to_p2trbytes),
p2wpkhaddressindex_to_p2wpkhaddressindex: lazy!("p2wpkhaddressindex", indexer.vecs.p2wpkhaddressindex_to_p2wpkhbytes),
p2wshaddressindex_to_p2wshaddressindex: lazy!("p2wshaddressindex", indexer.vecs.p2wshaddressindex_to_p2wshbytes),
p2aaddressindex_to_p2aaddressindex: lazy!("p2aaddressindex", indexer.vecs.p2aaddressindex_to_p2abytes),
p2msoutputindex_to_p2msoutputindex: lazy!("p2msoutputindex", indexer.vecs.p2msoutputindex_to_txindex),
emptyoutputindex_to_emptyoutputindex: lazy!("emptyoutputindex", indexer.vecs.emptyoutputindex_to_txindex),
unknownoutputindex_to_unknownoutputindex: lazy!("unknownoutputindex", indexer.vecs.unknownoutputindex_to_txindex),
opreturnindex_to_opreturnindex: lazy!("opreturnindex", indexer.vecs.opreturnindex_to_txindex),
txindex_to_txindex: lazy!("txindex", indexer.vecs.txindex_to_txid),
txindex_to_input_count: eager!("input_count"),
txindex_to_output_count: eager!("output_count"),
dateindex_to_date: eager!("date"),
dateindex_to_dateindex: eager!("dateindex"),
dateindex_to_first_height: eager!("first_height"),
dateindex_to_monthindex: eager!("monthindex"),
dateindex_to_weekindex: eager!("weekindex"),
decadeindex_to_decadeindex: eager!("decadeindex"),
decadeindex_to_first_yearindex: eager!("first_yearindex"),
difficultyepoch_to_difficultyepoch: eager!("difficultyepoch"),
difficultyepoch_to_first_height: eager!("first_height"),
halvingepoch_to_first_height: eager!("first_height"),
halvingepoch_to_halvingepoch: eager!("halvingepoch"),
height_to_date: eager!("date"),
height_to_difficultyepoch: eager!("difficultyepoch"),
height_to_halvingepoch: eager!("halvingepoch"),
height_to_height: eager!("height"),
monthindex_to_first_dateindex: eager!("first_dateindex"),
monthindex_to_monthindex: eager!("monthindex"),
monthindex_to_quarterindex: eager!("quarterindex"),
monthindex_to_semesterindex: eager!("semesterindex"),
monthindex_to_yearindex: eager!("yearindex"),
quarterindex_to_first_monthindex: eager!("first_monthindex"),
semesterindex_to_first_monthindex: eager!("first_monthindex"),
weekindex_to_first_dateindex: eager!("first_dateindex"),
yearindex_to_first_monthindex: eager!("first_monthindex"),
quarterindex_to_quarterindex: eager!("quarterindex"),
semesterindex_to_semesterindex: eager!("semesterindex"),
weekindex_to_weekindex: eager!("weekindex"),
yearindex_to_decadeindex: eager!("decadeindex"),
yearindex_to_yearindex: eager!("yearindex"),
height_to_date_fixed: eager!("date_fixed"),
height_to_dateindex: eager!("dateindex"),
height_to_timestamp_fixed: eager!("timestamp_fixed"),
height_to_txindex_count: eager!("txindex_count"),
dateindex_to_height_count: eager!("height_count"),
weekindex_to_dateindex_count: eager!("dateindex_count"),
difficultyepoch_to_height_count: eager!("height_count"),
monthindex_to_dateindex_count: eager!("dateindex_count"),
quarterindex_to_monthindex_count: eager!("monthindex_count"),
semesterindex_to_monthindex_count: eager!("monthindex_count"),
yearindex_to_monthindex_count: eager!("monthindex_count"),
decadeindex_to_yearindex_count: eager!("yearindex_count"),
db,
};

View File

@@ -27,6 +27,7 @@ mod traits;
mod utils;
use indexes::Indexes;
use utils::OptionExt;
// pub use pools::*;
pub use states::PriceToAmount;
@@ -162,7 +163,7 @@ impl Computer {
info!("Computing prices...");
let i = Instant::now();
self.price.as_mut().unwrap().compute(
self.price.um().compute(
&self.indexes,
&starting_indexes,
fetched,

File diff suppressed because it is too large Load Diff

View File

@@ -11,6 +11,7 @@ use crate::{
},
indexes::{self, Indexes},
price,
utils::OptionExt,
};
#[derive(Clone, Traversable)]
@@ -47,115 +48,40 @@ impl Vecs {
let compute_dollars = price.is_some();
let version = parent_version + Version::ZERO;
let last = VecBuilderOptions::default().add_last();
let sum_cum = VecBuilderOptions::default().add_sum().add_cumulative();
macro_rules! import_di {
($name:expr) => {
ComputedVecsFromDateIndex::forced_import(
db, &suffix($name), Source::Compute, version, indexes, last.clone(),
)?
};
}
Ok(Self {
id,
indexes_to_blocks_mined: ComputedVecsFromHeight::forced_import(
db,
&suffix("blocks_mined"),
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_sum().add_cumulative(),
)?,
indexes_to_1w_blocks_mined: ComputedVecsFromDateIndex::forced_import(
db,
&suffix("1w_blocks_mined"),
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_1m_blocks_mined: ComputedVecsFromDateIndex::forced_import(
db,
&suffix("1m_blocks_mined"),
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_1y_blocks_mined: ComputedVecsFromDateIndex::forced_import(
db,
&suffix("1y_blocks_mined"),
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
db, &suffix("blocks_mined"), Source::Compute, version, indexes, sum_cum.clone(),
)?,
indexes_to_1w_blocks_mined: import_di!("1w_blocks_mined"),
indexes_to_1m_blocks_mined: import_di!("1m_blocks_mined"),
indexes_to_1y_blocks_mined: import_di!("1y_blocks_mined"),
indexes_to_subsidy: ComputedValueVecsFromHeight::forced_import(
db,
&suffix("subsidy"),
Source::Compute,
version + Version::ZERO,
VecBuilderOptions::default().add_sum().add_cumulative(),
compute_dollars,
indexes,
db, &suffix("subsidy"), Source::Compute, version, sum_cum.clone(), compute_dollars, indexes,
)?,
indexes_to_fee: ComputedValueVecsFromHeight::forced_import(
db,
&suffix("fee"),
Source::Compute,
version + Version::ZERO,
VecBuilderOptions::default().add_sum().add_cumulative(),
compute_dollars,
indexes,
db, &suffix("fee"), Source::Compute, version, sum_cum.clone(), compute_dollars, indexes,
)?,
indexes_to_coinbase: ComputedValueVecsFromHeight::forced_import(
db,
&suffix("coinbase"),
Source::Compute,
version + Version::ZERO,
VecBuilderOptions::default().add_sum().add_cumulative(),
compute_dollars,
indexes,
)?,
indexes_to_dominance: ComputedVecsFromDateIndex::forced_import(
db,
&suffix("dominance"),
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_1d_dominance: ComputedVecsFromDateIndex::forced_import(
db,
&suffix("1d_dominance"),
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_1w_dominance: ComputedVecsFromDateIndex::forced_import(
db,
&suffix("1w_dominance"),
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_1m_dominance: ComputedVecsFromDateIndex::forced_import(
db,
&suffix("1m_dominance"),
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_1y_dominance: ComputedVecsFromDateIndex::forced_import(
db,
&suffix("1y_dominance"),
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
indexes_to_days_since_block: ComputedVecsFromDateIndex::forced_import(
db,
&suffix("days_since_block"),
Source::Compute,
version + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
db, &suffix("coinbase"), Source::Compute, version, sum_cum, compute_dollars, indexes,
)?,
indexes_to_dominance: import_di!("dominance"),
indexes_to_1d_dominance: import_di!("1d_dominance"),
indexes_to_1w_dominance: import_di!("1w_dominance"),
indexes_to_1m_dominance: import_di!("1m_dominance"),
indexes_to_1y_dominance: import_di!("1y_dominance"),
indexes_to_days_since_block: import_di!("days_since_block"),
})
}
@@ -222,14 +148,14 @@ impl Vecs {
Ok(())
})?;
let height_to_blocks_mined = self.indexes_to_blocks_mined.height.as_ref().unwrap();
let height_to_blocks_mined = self.indexes_to_blocks_mined.height.u();
self.indexes_to_subsidy
.compute_all(indexes, price, starting_indexes, exit, |vec| {
vec.compute_transform2(
starting_indexes.height,
height_to_blocks_mined,
chain.indexes_to_subsidy.sats.height.as_ref().unwrap(),
chain.indexes_to_subsidy.sats.height.u(),
|(h, mined, sats, ..)| {
(
h,
@@ -271,7 +197,7 @@ impl Vecs {
vec.compute_transform2(
starting_indexes.height,
height_to_blocks_mined,
chain.indexes_to_coinbase.sats.height.as_ref().unwrap(),
chain.indexes_to_coinbase.sats.height.u(),
|(h, mined, sats, ..)| {
(
h,
@@ -313,8 +239,8 @@ impl Vecs {
.compute_all(starting_indexes, exit, |vec| {
vec.compute_percentage(
starting_indexes.dateindex,
self.indexes_to_1w_blocks_mined.dateindex.as_ref().unwrap(),
chain.indexes_to_1w_block_count.dateindex.as_ref().unwrap(),
self.indexes_to_1w_blocks_mined.dateindex.u(),
chain.indexes_to_1w_block_count.dateindex.u(),
exit,
)?;
Ok(())
@@ -324,8 +250,8 @@ impl Vecs {
.compute_all(starting_indexes, exit, |vec| {
vec.compute_percentage(
starting_indexes.dateindex,
self.indexes_to_1m_blocks_mined.dateindex.as_ref().unwrap(),
chain.indexes_to_1m_block_count.dateindex.as_ref().unwrap(),
self.indexes_to_1m_blocks_mined.dateindex.u(),
chain.indexes_to_1m_block_count.dateindex.u(),
exit,
)?;
Ok(())
@@ -335,8 +261,8 @@ impl Vecs {
.compute_all(starting_indexes, exit, |vec| {
vec.compute_percentage(
starting_indexes.dateindex,
self.indexes_to_1y_blocks_mined.dateindex.as_ref().unwrap(),
chain.indexes_to_1y_block_count.dateindex.as_ref().unwrap(),
self.indexes_to_1y_blocks_mined.dateindex.u(),
chain.indexes_to_1y_block_count.dateindex.u(),
exit,
)?;
Ok(())

View File

@@ -8,7 +8,7 @@ use brk_types::{
};
use vecdb::{BytesVec, Database, EagerVec, Exit, ImportableVec, PAGE_SIZE, PcoVec};
use crate::{fetched, grouped::Source};
use crate::{fetched, grouped::Source, utils::OptionExt};
use super::{
Indexes,
@@ -74,251 +74,85 @@ impl Vecs {
let db = Database::open(&parent.join("price"))?;
db.set_min_len(PAGE_SIZE * 1_000_000)?;
let v = version + VERSION;
let v_sats = version + VERSION + VERSION_IN_SATS;
macro_rules! eager {
($name:expr) => { EagerVec::forced_import(&db, $name, v)? };
}
macro_rules! eager_sats {
($name:expr) => { EagerVec::forced_import(&db, $name, v_sats)? };
}
macro_rules! computed_di {
($name:expr, $opts:expr) => {
ComputedVecsFromDateIndex::forced_import(&db, $name, Source::Compute, v, indexes, $opts)?
};
}
macro_rules! computed_di_sats {
($name:expr, $opts:expr) => {
ComputedVecsFromDateIndex::forced_import(&db, $name, Source::Compute, v_sats, indexes, $opts)?
};
}
macro_rules! computed_h {
($name:expr, $opts:expr) => {
ComputedVecsFromHeightStrict::forced_import(&db, $name, v, $opts)?
};
}
macro_rules! computed_h_sats {
($name:expr, $opts:expr) => {
ComputedVecsFromHeightStrict::forced_import(&db, $name, v_sats, $opts)?
};
}
let first = || VecBuilderOptions::default().add_first();
let last = || VecBuilderOptions::default().add_last();
let min = || VecBuilderOptions::default().add_min();
let max = || VecBuilderOptions::default().add_max();
let this = Self {
dateindex_to_price_ohlc: EagerVec::forced_import(
&db,
"price_ohlc",
version + VERSION + Version::ZERO,
)?,
dateindex_to_price_ohlc_in_sats: EagerVec::forced_import(
&db,
"price_ohlc_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
)?,
dateindex_to_price_close_in_cents: EagerVec::forced_import(
&db,
"price_close_in_cents",
version + VERSION + Version::ZERO,
)?,
dateindex_to_price_high_in_cents: EagerVec::forced_import(
&db,
"price_high_in_cents",
version + VERSION + Version::ZERO,
)?,
dateindex_to_price_low_in_cents: EagerVec::forced_import(
&db,
"price_low_in_cents",
version + VERSION + Version::ZERO,
)?,
dateindex_to_price_open_in_cents: EagerVec::forced_import(
&db,
"price_open_in_cents",
version + VERSION + Version::ZERO,
)?,
height_to_price_ohlc: EagerVec::forced_import(
&db,
"price_ohlc",
version + VERSION + Version::ZERO,
)?,
height_to_price_ohlc_in_sats: EagerVec::forced_import(
&db,
"price_ohlc_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
)?,
height_to_price_close_in_cents: EagerVec::forced_import(
&db,
"price_close_in_cents",
version + VERSION + Version::ZERO,
)?,
height_to_price_high_in_cents: EagerVec::forced_import(
&db,
"price_high_in_cents",
version + VERSION + Version::ZERO,
)?,
height_to_price_low_in_cents: EagerVec::forced_import(
&db,
"price_low_in_cents",
version + VERSION + Version::ZERO,
)?,
height_to_price_open_in_cents: EagerVec::forced_import(
&db,
"price_open_in_cents",
version + VERSION + Version::ZERO,
)?,
timeindexes_to_price_open: ComputedVecsFromDateIndex::forced_import(
&db,
"price_open",
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
VecBuilderOptions::default().add_first(),
)?,
timeindexes_to_price_high: ComputedVecsFromDateIndex::forced_import(
&db,
"price_high",
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
VecBuilderOptions::default().add_max(),
)?,
timeindexes_to_price_low: ComputedVecsFromDateIndex::forced_import(
&db,
"price_low",
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
VecBuilderOptions::default().add_min(),
)?,
timeindexes_to_price_close: ComputedVecsFromDateIndex::forced_import(
&db,
"price_close",
Source::Compute,
version + VERSION + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
timeindexes_to_price_open_in_sats: ComputedVecsFromDateIndex::forced_import(
&db,
"price_open_in_sats",
Source::Compute,
version + VERSION + VERSION_IN_SATS + Version::ZERO,
indexes,
VecBuilderOptions::default().add_first(),
)?,
timeindexes_to_price_high_in_sats: ComputedVecsFromDateIndex::forced_import(
&db,
"price_high_in_sats",
Source::Compute,
version + VERSION + VERSION_IN_SATS + Version::ZERO,
indexes,
VecBuilderOptions::default().add_max(),
)?,
timeindexes_to_price_low_in_sats: ComputedVecsFromDateIndex::forced_import(
&db,
"price_low_in_sats",
Source::Compute,
version + VERSION + VERSION_IN_SATS + Version::ZERO,
indexes,
VecBuilderOptions::default().add_min(),
)?,
timeindexes_to_price_close_in_sats: ComputedVecsFromDateIndex::forced_import(
&db,
"price_close_in_sats",
Source::Compute,
version + VERSION + VERSION_IN_SATS + Version::ZERO,
indexes,
VecBuilderOptions::default().add_last(),
)?,
chainindexes_to_price_open: ComputedVecsFromHeightStrict::forced_import(
&db,
"price_open",
version + VERSION + Version::ZERO,
VecBuilderOptions::default().add_first(),
)?,
chainindexes_to_price_high: ComputedVecsFromHeightStrict::forced_import(
&db,
"price_high",
version + VERSION + Version::ZERO,
VecBuilderOptions::default().add_max(),
)?,
chainindexes_to_price_low: ComputedVecsFromHeightStrict::forced_import(
&db,
"price_low",
version + VERSION + Version::ZERO,
VecBuilderOptions::default().add_min(),
)?,
chainindexes_to_price_close: ComputedVecsFromHeightStrict::forced_import(
&db,
"price_close",
version + VERSION + Version::ZERO,
VecBuilderOptions::default().add_last(),
)?,
chainindexes_to_price_open_in_sats: ComputedVecsFromHeightStrict::forced_import(
&db,
"price_open_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
VecBuilderOptions::default().add_first(),
)?,
chainindexes_to_price_high_in_sats: ComputedVecsFromHeightStrict::forced_import(
&db,
"price_high_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
VecBuilderOptions::default().add_max(),
)?,
chainindexes_to_price_low_in_sats: ComputedVecsFromHeightStrict::forced_import(
&db,
"price_low_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
VecBuilderOptions::default().add_min(),
)?,
chainindexes_to_price_close_in_sats: ComputedVecsFromHeightStrict::forced_import(
&db,
"price_close_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
VecBuilderOptions::default().add_last(),
)?,
weekindex_to_price_ohlc: EagerVec::forced_import(
&db,
"price_ohlc",
version + VERSION + Version::ZERO,
)?,
weekindex_to_price_ohlc_in_sats: EagerVec::forced_import(
&db,
"price_ohlc_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
)?,
difficultyepoch_to_price_ohlc: EagerVec::forced_import(
&db,
"price_ohlc",
version + VERSION + Version::ZERO,
)?,
difficultyepoch_to_price_ohlc_in_sats: EagerVec::forced_import(
&db,
"price_ohlc_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
)?,
monthindex_to_price_ohlc: EagerVec::forced_import(
&db,
"price_ohlc",
version + VERSION + Version::ZERO,
)?,
monthindex_to_price_ohlc_in_sats: EagerVec::forced_import(
&db,
"price_ohlc_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
)?,
quarterindex_to_price_ohlc: EagerVec::forced_import(
&db,
"price_ohlc",
version + VERSION + Version::ZERO,
)?,
quarterindex_to_price_ohlc_in_sats: EagerVec::forced_import(
&db,
"price_ohlc_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
)?,
semesterindex_to_price_ohlc: EagerVec::forced_import(
&db,
"price_ohlc",
version + VERSION + Version::ZERO,
)?,
semesterindex_to_price_ohlc_in_sats: EagerVec::forced_import(
&db,
"price_ohlc_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
)?,
yearindex_to_price_ohlc: EagerVec::forced_import(
&db,
"price_ohlc",
version + VERSION + Version::ZERO,
)?,
yearindex_to_price_ohlc_in_sats: EagerVec::forced_import(
&db,
"price_ohlc_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
)?,
dateindex_to_price_ohlc: eager!("price_ohlc"),
dateindex_to_price_ohlc_in_sats: eager_sats!("price_ohlc_in_sats"),
dateindex_to_price_close_in_cents: eager!("price_close_in_cents"),
dateindex_to_price_high_in_cents: eager!("price_high_in_cents"),
dateindex_to_price_low_in_cents: eager!("price_low_in_cents"),
dateindex_to_price_open_in_cents: eager!("price_open_in_cents"),
height_to_price_ohlc: eager!("price_ohlc"),
height_to_price_ohlc_in_sats: eager_sats!("price_ohlc_in_sats"),
height_to_price_close_in_cents: eager!("price_close_in_cents"),
height_to_price_high_in_cents: eager!("price_high_in_cents"),
height_to_price_low_in_cents: eager!("price_low_in_cents"),
height_to_price_open_in_cents: eager!("price_open_in_cents"),
timeindexes_to_price_open: computed_di!("price_open", first()),
timeindexes_to_price_high: computed_di!("price_high", max()),
timeindexes_to_price_low: computed_di!("price_low", min()),
timeindexes_to_price_close: computed_di!("price_close", last()),
timeindexes_to_price_open_in_sats: computed_di_sats!("price_open_in_sats", first()),
timeindexes_to_price_high_in_sats: computed_di_sats!("price_high_in_sats", max()),
timeindexes_to_price_low_in_sats: computed_di_sats!("price_low_in_sats", min()),
timeindexes_to_price_close_in_sats: computed_di_sats!("price_close_in_sats", last()),
chainindexes_to_price_open: computed_h!("price_open", first()),
chainindexes_to_price_high: computed_h!("price_high", max()),
chainindexes_to_price_low: computed_h!("price_low", min()),
chainindexes_to_price_close: computed_h!("price_close", last()),
chainindexes_to_price_open_in_sats: computed_h_sats!("price_open_in_sats", first()),
chainindexes_to_price_high_in_sats: computed_h_sats!("price_high_in_sats", max()),
chainindexes_to_price_low_in_sats: computed_h_sats!("price_low_in_sats", min()),
chainindexes_to_price_close_in_sats: computed_h_sats!("price_close_in_sats", last()),
weekindex_to_price_ohlc: eager!("price_ohlc"),
weekindex_to_price_ohlc_in_sats: eager_sats!("price_ohlc_in_sats"),
difficultyepoch_to_price_ohlc: eager!("price_ohlc"),
difficultyepoch_to_price_ohlc_in_sats: eager_sats!("price_ohlc_in_sats"),
monthindex_to_price_ohlc: eager!("price_ohlc"),
monthindex_to_price_ohlc_in_sats: eager_sats!("price_ohlc_in_sats"),
quarterindex_to_price_ohlc: eager!("price_ohlc"),
quarterindex_to_price_ohlc_in_sats: eager_sats!("price_ohlc_in_sats"),
semesterindex_to_price_ohlc: eager!("price_ohlc"),
semesterindex_to_price_ohlc_in_sats: eager_sats!("price_ohlc_in_sats"),
yearindex_to_price_ohlc: eager!("price_ohlc"),
yearindex_to_price_ohlc_in_sats: eager_sats!("price_ohlc_in_sats"),
// halvingepoch_to_price_ohlc: StorableVec::forced_import(db,
// "halvingepoch_to_price_ohlc"), version + VERSION + Version::ZERO, format)?,
decadeindex_to_price_ohlc: EagerVec::forced_import(
&db,
"price_ohlc",
version + VERSION + Version::ZERO,
)?,
decadeindex_to_price_ohlc_in_sats: EagerVec::forced_import(
&db,
"price_ohlc_in_sats",
version + VERSION + VERSION_IN_SATS + Version::ZERO,
)?,
decadeindex_to_price_ohlc: eager!("price_ohlc"),
decadeindex_to_price_ohlc_in_sats: eager_sats!("price_ohlc_in_sats"),
db,
};
@@ -706,7 +540,7 @@ impl Vecs {
.compute_all(starting_indexes, exit, |v| {
v.compute_transform(
starting_indexes.dateindex,
self.timeindexes_to_price_open.dateindex.as_ref().unwrap(),
self.timeindexes_to_price_open.dateindex.u(),
|(i, open, ..)| (i, Open::new(Sats::ONE_BTC / *open)),
exit,
)?;
@@ -717,7 +551,7 @@ impl Vecs {
.compute_all(starting_indexes, exit, |v| {
v.compute_transform(
starting_indexes.dateindex,
self.timeindexes_to_price_low.dateindex.as_ref().unwrap(),
self.timeindexes_to_price_low.dateindex.u(),
|(i, low, ..)| (i, High::new(Sats::ONE_BTC / *low)),
exit,
)?;
@@ -728,7 +562,7 @@ impl Vecs {
.compute_all(starting_indexes, exit, |v| {
v.compute_transform(
starting_indexes.dateindex,
self.timeindexes_to_price_high.dateindex.as_ref().unwrap(),
self.timeindexes_to_price_high.dateindex.u(),
|(i, high, ..)| (i, Low::new(Sats::ONE_BTC / *high)),
exit,
)?;
@@ -739,7 +573,7 @@ impl Vecs {
.compute_all(starting_indexes, exit, |v| {
v.compute_transform(
starting_indexes.dateindex,
self.timeindexes_to_price_close.dateindex.as_ref().unwrap(),
self.timeindexes_to_price_close.dateindex.u(),
|(i, close, ..)| (i, Close::new(Sats::ONE_BTC / *close)),
exit,
)?;

View File

@@ -18,6 +18,7 @@ use crate::{
r#trait::{CohortVecs, DynCohortVecs},
},
states::AddressCohortState,
utils::OptionExt,
};
const VERSION: Version = Version::ZERO;
@@ -37,7 +38,6 @@ pub struct Vecs {
}
impl Vecs {
#[allow(clippy::too_many_arguments)]
pub fn forced_import(
db: &Database,
filter: Filter,
@@ -45,7 +45,6 @@ impl Vecs {
indexes: &indexes::Vecs,
price: Option<&price::Vecs>,
states_path: Option<&Path>,
compute_rel_to_all: bool,
) -> Result<Self> {
let compute_dollars = price.is_some();
@@ -83,9 +82,6 @@ impl Vecs {
version,
indexes,
price,
false,
compute_rel_to_all,
false,
)?,
})
}
@@ -106,12 +102,12 @@ impl DynCohortVecs for Vecs {
fn import_state(&mut self, starting_height: Height) -> Result<Height> {
let starting_height = self
.inner
.import_state(starting_height, &mut self.state.as_mut().unwrap().inner)?;
.import_state(starting_height, &mut self.state.um().inner)?;
self.starting_height = Some(starting_height);
if let Some(prev_height) = starting_height.decremented() {
self.state.as_mut().unwrap().addr_count = *self
self.state.um().addr_count = *self
.height_to_addr_count
.into_iter()
.get_unwrap(prev_height);
@@ -135,10 +131,10 @@ impl DynCohortVecs for Vecs {
}
self.height_to_addr_count
.truncate_push(height, self.state.as_ref().unwrap().addr_count.into())?;
.truncate_push(height, self.state.u().addr_count.into())?;
self.inner
.truncate_push(height, &self.state.as_ref().unwrap().inner)
.truncate_push(height, &self.state.u().inner)
}
fn compute_then_truncate_push_unrealized_states(
@@ -153,7 +149,7 @@ impl DynCohortVecs for Vecs {
height_price,
dateindex,
date_price,
&self.state.as_ref().unwrap().inner,
&self.state.u().inner,
)
}
@@ -161,7 +157,7 @@ impl DynCohortVecs for Vecs {
self.height_to_addr_count.safe_flush(exit)?;
self.inner
.safe_flush_stateful_vecs(height, exit, &mut self.state.as_mut().unwrap().inner)
.safe_flush_stateful_vecs(height, exit, &mut self.state.um().inner)
}
#[allow(clippy::too_many_arguments)]

View File

@@ -41,7 +41,6 @@ impl Vecs {
indexes,
price,
states_path,
true,
)
.unwrap()
})))

View File

@@ -0,0 +1,184 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{
AnyAddressIndex, EmptyAddressData, EmptyAddressIndex, Height, LoadedAddressData,
LoadedAddressIndex, OutputType, P2AAddressIndex, P2PK33AddressIndex, P2PK65AddressIndex,
P2PKHAddressIndex, P2SHAddressIndex, P2TRAddressIndex, P2WPKHAddressIndex, P2WSHAddressIndex,
TypeIndex,
};
use vecdb::{AnyStoredVec, BytesVec, GenericStoredVec, Reader, Stamp};
#[derive(Clone, Traversable)]
pub struct AnyAddressIndexesVecs {
pub p2pk33: BytesVec<P2PK33AddressIndex, AnyAddressIndex>,
pub p2pk65: BytesVec<P2PK65AddressIndex, AnyAddressIndex>,
pub p2pkh: BytesVec<P2PKHAddressIndex, AnyAddressIndex>,
pub p2sh: BytesVec<P2SHAddressIndex, AnyAddressIndex>,
pub p2tr: BytesVec<P2TRAddressIndex, AnyAddressIndex>,
pub p2wpkh: BytesVec<P2WPKHAddressIndex, AnyAddressIndex>,
pub p2wsh: BytesVec<P2WSHAddressIndex, AnyAddressIndex>,
pub p2a: BytesVec<P2AAddressIndex, AnyAddressIndex>,
}
impl AnyAddressIndexesVecs {
pub fn min_stamped_height(&self) -> Height {
Height::from(self.p2pk33.stamp())
.incremented()
.min(Height::from(self.p2pk65.stamp()).incremented())
.min(Height::from(self.p2pkh.stamp()).incremented())
.min(Height::from(self.p2sh.stamp()).incremented())
.min(Height::from(self.p2tr.stamp()).incremented())
.min(Height::from(self.p2wpkh.stamp()).incremented())
.min(Height::from(self.p2wsh.stamp()).incremented())
.min(Height::from(self.p2a.stamp()).incremented())
}
pub fn rollback_before(&mut self, stamp: Stamp) -> Result<[Stamp; 8]> {
Ok([
self.p2pk33.rollback_before(stamp)?,
self.p2pk65.rollback_before(stamp)?,
self.p2pkh.rollback_before(stamp)?,
self.p2sh.rollback_before(stamp)?,
self.p2tr.rollback_before(stamp)?,
self.p2wpkh.rollback_before(stamp)?,
self.p2wsh.rollback_before(stamp)?,
self.p2a.rollback_before(stamp)?,
])
}
pub fn reset(&mut self) -> Result<()> {
self.p2pk33.reset()?;
self.p2pk65.reset()?;
self.p2pkh.reset()?;
self.p2sh.reset()?;
self.p2tr.reset()?;
self.p2wpkh.reset()?;
self.p2wsh.reset()?;
self.p2a.reset()?;
Ok(())
}
pub fn get_anyaddressindex(
&self,
address_type: OutputType,
typeindex: TypeIndex,
reader: &Reader,
) -> AnyAddressIndex {
match address_type {
OutputType::P2PK33 => self
.p2pk33
.get_pushed_or_read_at_unwrap(typeindex.into(), reader),
OutputType::P2PK65 => self
.p2pk65
.get_pushed_or_read_at_unwrap(typeindex.into(), reader),
OutputType::P2PKH => self
.p2pkh
.get_pushed_or_read_at_unwrap(typeindex.into(), reader),
OutputType::P2SH => self
.p2sh
.get_pushed_or_read_at_unwrap(typeindex.into(), reader),
OutputType::P2TR => self
.p2tr
.get_pushed_or_read_at_unwrap(typeindex.into(), reader),
OutputType::P2WPKH => self
.p2wpkh
.get_pushed_or_read_at_unwrap(typeindex.into(), reader),
OutputType::P2WSH => self
.p2wsh
.get_pushed_or_read_at_unwrap(typeindex.into(), reader),
OutputType::P2A => self
.p2a
.get_pushed_or_read_at_unwrap(typeindex.into(), reader),
_ => unreachable!(),
}
}
pub fn update_or_push(
&mut self,
address_type: OutputType,
typeindex: TypeIndex,
anyaddressindex: AnyAddressIndex,
) -> Result<()> {
(match address_type {
OutputType::P2PK33 => self
.p2pk33
.update_or_push(typeindex.into(), anyaddressindex),
OutputType::P2PK65 => self
.p2pk65
.update_or_push(typeindex.into(), anyaddressindex),
OutputType::P2PKH => self.p2pkh.update_or_push(typeindex.into(), anyaddressindex),
OutputType::P2SH => self.p2sh.update_or_push(typeindex.into(), anyaddressindex),
OutputType::P2TR => self.p2tr.update_or_push(typeindex.into(), anyaddressindex),
OutputType::P2WPKH => self
.p2wpkh
.update_or_push(typeindex.into(), anyaddressindex),
OutputType::P2WSH => self.p2wsh.update_or_push(typeindex.into(), anyaddressindex),
OutputType::P2A => self.p2a.update_or_push(typeindex.into(), anyaddressindex),
_ => unreachable!(),
})?;
Ok(())
}
pub fn stamped_flush_maybe_with_changes(
&mut self,
stamp: Stamp,
with_changes: bool,
) -> Result<()> {
self.p2pk33
.stamped_flush_maybe_with_changes(stamp, with_changes)?;
self.p2pk65
.stamped_flush_maybe_with_changes(stamp, with_changes)?;
self.p2pkh
.stamped_flush_maybe_with_changes(stamp, with_changes)?;
self.p2sh
.stamped_flush_maybe_with_changes(stamp, with_changes)?;
self.p2tr
.stamped_flush_maybe_with_changes(stamp, with_changes)?;
self.p2wpkh
.stamped_flush_maybe_with_changes(stamp, with_changes)?;
self.p2wsh
.stamped_flush_maybe_with_changes(stamp, with_changes)?;
self.p2a
.stamped_flush_maybe_with_changes(stamp, with_changes)?;
Ok(())
}
}
#[derive(Clone, Traversable)]
pub struct AddressesDataVecs {
pub loaded: BytesVec<LoadedAddressIndex, LoadedAddressData>,
pub empty: BytesVec<EmptyAddressIndex, EmptyAddressData>,
}
impl AddressesDataVecs {
pub fn min_stamped_height(&self) -> Height {
Height::from(self.loaded.stamp())
.incremented()
.min(Height::from(self.empty.stamp()).incremented())
}
pub fn rollback_before(&mut self, stamp: Stamp) -> Result<[Stamp; 2]> {
Ok([
self.loaded.rollback_before(stamp)?,
self.empty.rollback_before(stamp)?,
])
}
pub fn reset(&mut self) -> Result<()> {
self.loaded.reset()?;
self.empty.reset()?;
Ok(())
}
pub fn stamped_flush_maybe_with_changes(
&mut self,
stamp: Stamp,
with_changes: bool,
) -> Result<()> {
self.loaded
.stamped_flush_maybe_with_changes(stamp, with_changes)?;
self.empty
.stamped_flush_maybe_with_changes(stamp, with_changes)?;
Ok(())
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,97 @@
use brk_grouper::{ByAddressType, ByAnyAddress};
use brk_indexer::Indexer;
use brk_types::{OutputType, StoredU64, TxIndex};
use vecdb::{BoxedVecIterator, GenericStoredVec, Reader, VecIndex};
use super::Vecs;
pub struct IndexerReaders {
pub txinindex_to_outpoint: Reader,
pub txindex_to_first_txoutindex: Reader,
pub txoutindex_to_value: Reader,
pub txoutindex_to_outputtype: Reader,
pub txoutindex_to_typeindex: Reader,
}
impl IndexerReaders {
pub fn new(indexer: &Indexer) -> Self {
Self {
txinindex_to_outpoint: indexer.vecs.txinindex_to_outpoint.create_reader(),
txindex_to_first_txoutindex: indexer.vecs.txindex_to_first_txoutindex.create_reader(),
txoutindex_to_value: indexer.vecs.txoutindex_to_value.create_reader(),
txoutindex_to_outputtype: indexer.vecs.txoutindex_to_outputtype.create_reader(),
txoutindex_to_typeindex: indexer.vecs.txoutindex_to_typeindex.create_reader(),
}
}
}
pub struct VecsReaders {
pub addresstypeindex_to_anyaddressindex: ByAddressType<Reader>,
pub anyaddressindex_to_anyaddressdata: ByAnyAddress<Reader>,
}
impl VecsReaders {
pub fn new(vecs: &Vecs) -> Self {
Self {
addresstypeindex_to_anyaddressindex: ByAddressType {
p2pk33: vecs.any_address_indexes.p2pk33.create_reader(),
p2pk65: vecs.any_address_indexes.p2pk65.create_reader(),
p2pkh: vecs.any_address_indexes.p2pkh.create_reader(),
p2sh: vecs.any_address_indexes.p2sh.create_reader(),
p2tr: vecs.any_address_indexes.p2tr.create_reader(),
p2wpkh: vecs.any_address_indexes.p2wpkh.create_reader(),
p2wsh: vecs.any_address_indexes.p2wsh.create_reader(),
p2a: vecs.any_address_indexes.p2a.create_reader(),
},
anyaddressindex_to_anyaddressdata: ByAnyAddress {
loaded: vecs.addresses_data.loaded.create_reader(),
empty: vecs.addresses_data.empty.create_reader(),
},
}
}
pub fn get_anyaddressindex_reader(&self, address_type: OutputType) -> &Reader {
self.addresstypeindex_to_anyaddressindex
.get_unwrap(address_type)
}
}
pub fn build_txoutindex_to_txindex<'a>(
block_first_txindex: TxIndex,
block_tx_count: u64,
txindex_to_output_count: &mut BoxedVecIterator<'a, TxIndex, StoredU64>,
) -> Vec<TxIndex> {
let mut vec = Vec::new();
let block_first_txindex = block_first_txindex.to_usize();
for tx_offset in 0..block_tx_count as usize {
let txindex = TxIndex::from(block_first_txindex + tx_offset);
let output_count = u64::from(txindex_to_output_count.get_unwrap(txindex));
for _ in 0..output_count {
vec.push(txindex);
}
}
vec
}
pub fn build_txinindex_to_txindex<'a>(
block_first_txindex: TxIndex,
block_tx_count: u64,
txindex_to_input_count: &mut BoxedVecIterator<'a, TxIndex, StoredU64>,
) -> Vec<TxIndex> {
let mut vec = Vec::new();
let block_first_txindex = block_first_txindex.to_usize();
for tx_offset in 0..block_tx_count as usize {
let txindex = TxIndex::from(block_first_txindex + tx_offset);
let input_count = u64::from(txindex_to_input_count.get_unwrap(txindex));
for _ in 0..input_count {
vec.push(txindex);
}
}
vec
}

View File

@@ -0,0 +1,217 @@
use brk_error::Result;
use brk_grouper::{ByAddressType, Filtered};
use brk_types::{CheckedSub, Dollars, EmptyAddressData, Height, LoadedAddressData, Sats, Timestamp, TypeIndex};
use vecdb::VecIndex;
use crate::utils::OptionExt;
use super::{
address_cohorts,
addresstype::{
AddressTypeToTypeIndexMap, AddressTypeToVec,
HeightToAddressTypeToVec,
},
withaddressdatasource::WithAddressDataSource,
};
impl AddressTypeToVec<(TypeIndex, Sats)> {
#[allow(clippy::too_many_arguments)]
pub fn process_received(
self,
vecs: &mut address_cohorts::Vecs,
addresstype_to_typeindex_to_loadedaddressdata: &mut AddressTypeToTypeIndexMap<
WithAddressDataSource<LoadedAddressData>,
>,
addresstype_to_typeindex_to_emptyaddressdata: &mut AddressTypeToTypeIndexMap<
WithAddressDataSource<EmptyAddressData>,
>,
price: Option<Dollars>,
addresstype_to_addr_count: &mut ByAddressType<u64>,
addresstype_to_empty_addr_count: &mut ByAddressType<u64>,
stored_or_new_addresstype_to_typeindex_to_addressdatawithsource: &mut AddressTypeToTypeIndexMap<
WithAddressDataSource<LoadedAddressData>,
>,
) {
self.unwrap().into_iter().for_each(|(_type, vec)| {
vec.into_iter().for_each(|(type_index, value)| {
let mut is_new = false;
let mut from_any_empty = false;
let addressdata_withsource = addresstype_to_typeindex_to_loadedaddressdata
.get_mut(_type)
.unwrap()
.entry(type_index)
.or_insert_with(|| {
addresstype_to_typeindex_to_emptyaddressdata
.get_mut(_type)
.unwrap()
.remove(&type_index)
.map(|ad| {
from_any_empty = true;
ad.into()
})
.unwrap_or_else(|| {
let addressdata =
stored_or_new_addresstype_to_typeindex_to_addressdatawithsource
.remove_for_type(_type, &type_index);
is_new = addressdata.is_new();
from_any_empty = addressdata.is_from_emptyaddressdata();
addressdata
})
});
if is_new || from_any_empty {
(*addresstype_to_addr_count.get_mut(_type).unwrap()) += 1;
if from_any_empty {
(*addresstype_to_empty_addr_count.get_mut(_type).unwrap()) -= 1;
}
}
let addressdata = addressdata_withsource.deref_mut();
let prev_amount = addressdata.balance();
let amount = prev_amount + value;
if is_new
|| from_any_empty
|| vecs.amount_range.get_mut(amount).filter().clone()
!= vecs.amount_range.get_mut(prev_amount).filter().clone()
{
if !is_new && !from_any_empty {
vecs.amount_range
.get_mut(prev_amount)
.state.um()
.subtract(addressdata);
}
addressdata.receive(value, price);
vecs.amount_range
.get_mut(amount)
.state.um()
.add(addressdata);
} else {
vecs.amount_range
.get_mut(amount)
.state.um()
.receive(addressdata, value, price);
}
});
});
}
}
impl HeightToAddressTypeToVec<(TypeIndex, Sats)> {
#[allow(clippy::too_many_arguments)]
pub fn process_sent(
self,
vecs: &mut address_cohorts::Vecs,
addresstype_to_typeindex_to_loadedaddressdata: &mut AddressTypeToTypeIndexMap<
WithAddressDataSource<LoadedAddressData>,
>,
addresstype_to_typeindex_to_emptyaddressdata: &mut AddressTypeToTypeIndexMap<
WithAddressDataSource<EmptyAddressData>,
>,
price: Option<Dollars>,
addresstype_to_addr_count: &mut ByAddressType<u64>,
addresstype_to_empty_addr_count: &mut ByAddressType<u64>,
height_to_price_close_vec: Option<&Vec<brk_types::Close<Dollars>>>,
height_to_timestamp_fixed_vec: &[Timestamp],
height: Height,
timestamp: Timestamp,
stored_or_new_addresstype_to_typeindex_to_addressdatawithsource: &mut AddressTypeToTypeIndexMap<
WithAddressDataSource<LoadedAddressData>,
>,
) -> Result<()> {
self.0.into_iter().try_for_each(|(prev_height, v)| {
let prev_price = height_to_price_close_vec
.as_ref()
.map(|v| **v.get(prev_height.to_usize()).unwrap());
let prev_timestamp = *height_to_timestamp_fixed_vec
.get(prev_height.to_usize())
.unwrap();
let blocks_old = height.to_usize() - prev_height.to_usize();
let days_old = timestamp.difference_in_days_between_float(prev_timestamp);
let older_than_hour = timestamp
.checked_sub(prev_timestamp)
.unwrap()
.is_more_than_hour();
v.unwrap().into_iter().try_for_each(|(_type, vec)| {
vec.into_iter().try_for_each(|(type_index, value)| {
let typeindex_to_loadedaddressdata =
addresstype_to_typeindex_to_loadedaddressdata.get_mut_unwrap(_type);
let addressdata_withsource = typeindex_to_loadedaddressdata
.entry(type_index)
.or_insert_with(|| {
stored_or_new_addresstype_to_typeindex_to_addressdatawithsource
.remove_for_type(_type, &type_index)
});
let addressdata = addressdata_withsource.deref_mut();
let prev_amount = addressdata.balance();
let amount = prev_amount.checked_sub(value).unwrap();
let will_be_empty = addressdata.has_1_utxos();
if will_be_empty
|| vecs.amount_range.get_mut(amount).filter().clone()
!= vecs.amount_range.get_mut(prev_amount).filter().clone()
{
vecs.amount_range
.get_mut(prev_amount)
.state.um()
.subtract(addressdata);
addressdata.send(value, prev_price)?;
if will_be_empty {
if amount.is_not_zero() {
unreachable!()
}
(*addresstype_to_addr_count.get_mut(_type).unwrap()) -= 1;
(*addresstype_to_empty_addr_count.get_mut(_type).unwrap()) += 1;
let addressdata =
typeindex_to_loadedaddressdata.remove(&type_index).unwrap();
addresstype_to_typeindex_to_emptyaddressdata
.get_mut(_type)
.unwrap()
.insert(type_index, addressdata.into());
} else {
vecs.amount_range
.get_mut(amount)
.state.um()
.add(addressdata);
}
} else {
vecs.amount_range
.get_mut(amount)
.state.um()
.send(
addressdata,
value,
price,
prev_price,
blocks_old,
days_old,
older_than_hour,
)?;
}
Ok(())
})
})
})
}
}

View File

@@ -14,6 +14,7 @@ use crate::{
common,
r#trait::{CohortVecs, DynCohortVecs},
},
utils::OptionExt,
};
#[derive(Clone, Traversable)]
@@ -32,7 +33,6 @@ pub struct Vecs {
}
impl Vecs {
#[allow(clippy::too_many_arguments)]
pub fn forced_import(
db: &Database,
filter: Filter,
@@ -41,9 +41,6 @@ impl Vecs {
price: Option<&price::Vecs>,
states_path: &Path,
state_level: StateLevel,
extended: bool,
compute_rel_to_all: bool,
compute_adjusted: bool,
) -> Result<Self> {
let compute_dollars = price.is_some();
@@ -75,9 +72,6 @@ impl Vecs {
version,
indexes,
price,
extended,
compute_rel_to_all,
compute_adjusted,
)?,
})
}
@@ -95,7 +89,7 @@ impl DynCohortVecs for Vecs {
fn import_state(&mut self, starting_height: Height) -> Result<Height> {
let starting_height = self
.inner
.import_state(starting_height, self.state.as_mut().unwrap())?;
.import_state(starting_height, self.state.um())?;
self.state_starting_height = Some(starting_height);
@@ -112,7 +106,7 @@ impl DynCohortVecs for Vecs {
}
self.inner
.truncate_push(height, self.state.as_ref().unwrap())
.truncate_push(height, self.state.u())
}
fn compute_then_truncate_push_unrealized_states(
@@ -127,13 +121,13 @@ impl DynCohortVecs for Vecs {
height_price,
dateindex,
date_price,
self.state.as_mut().unwrap(),
self.state.um(),
)
}
fn safe_flush_stateful_vecs(&mut self, height: Height, exit: &Exit) -> Result<()> {
self.inner
.safe_flush_stateful_vecs(height, exit, self.state.as_mut().unwrap())
.safe_flush_stateful_vecs(height, exit, self.state.um())
}
#[allow(clippy::too_many_arguments)]

File diff suppressed because it is too large Load Diff

View File

@@ -5,6 +5,7 @@ use brk_types::{CheckedSub, Dollars, Height, Sats};
use crate::{
grouped::{PERCENTILES, PERCENTILES_LEN},
utils::OptionExt,
PriceToAmount, RealizedState, SupplyState, UnrealizedState,
};
@@ -49,11 +50,11 @@ impl CohortState {
}
pub fn price_to_amount_first_key_value(&self) -> Option<(&Dollars, &Sats)> {
self.price_to_amount.as_ref().unwrap().first_key_value()
self.price_to_amount.u().first_key_value()
}
pub fn price_to_amount_last_key_value(&self) -> Option<(&Dollars, &Sats)> {
self.price_to_amount.as_ref().unwrap().last_key_value()
self.price_to_amount.u().last_key_value()
}
pub fn reset_single_iteration_values(&mut self) {
@@ -287,7 +288,7 @@ impl CohortState {
height_price: Dollars,
date_price: Option<Dollars>,
) -> (UnrealizedState, Option<UnrealizedState>) {
if self.price_to_amount.as_ref().unwrap().is_empty() {
if self.price_to_amount.u().is_empty() {
return (
UnrealizedState::NAN,
date_price.map(|_| UnrealizedState::NAN),
@@ -340,7 +341,7 @@ impl CohortState {
price,
date_price,
sats,
date_unrealized_state.as_mut().unwrap(),
date_unrealized_state.um(),
)
}
});

View File

@@ -11,7 +11,7 @@ use pco::standalone::{simple_decompress, simpler_compress};
use serde::{Deserialize, Serialize};
use vecdb::Bytes;
use crate::states::SupplyState;
use crate::{states::SupplyState, utils::OptionExt};
#[derive(Clone, Debug)]
pub struct PriceToAmount {
@@ -41,30 +41,30 @@ impl PriceToAmount {
}
pub fn iter(&self) -> impl Iterator<Item = (&Dollars, &Sats)> {
self.state.as_ref().unwrap().iter()
self.state.u().iter()
}
pub fn is_empty(&self) -> bool {
self.state.as_ref().unwrap().is_empty()
self.state.u().is_empty()
}
pub fn first_key_value(&self) -> Option<(&Dollars, &Sats)> {
self.state.as_ref().unwrap().first_key_value()
self.state.u().first_key_value()
}
pub fn last_key_value(&self) -> Option<(&Dollars, &Sats)> {
self.state.as_ref().unwrap().last_key_value()
self.state.u().last_key_value()
}
pub fn increment(&mut self, price: Dollars, supply_state: &SupplyState) {
*self.state.as_mut().unwrap().entry(price).or_default() += supply_state.value;
*self.state.um().entry(price).or_default() += supply_state.value;
}
pub fn decrement(&mut self, price: Dollars, supply_state: &SupplyState) {
if let Some(amount) = self.state.as_mut().unwrap().get_mut(&price) {
if let Some(amount) = self.state.um().get_mut(&price) {
*amount -= supply_state.value;
if *amount == Sats::ZERO {
self.state.as_mut().unwrap().remove(&price);
self.state.um().remove(&price);
}
} else {
dbg!(price, &self.pathbuf);
@@ -114,7 +114,7 @@ impl PriceToAmount {
fs::write(
self.path_state(height),
self.state.as_ref().unwrap().serialize()?,
self.state.u().serialize()?,
)?;
Ok(())

View File

@@ -1,5 +1,25 @@
use std::ops::{Add, Div};
/// Extension trait for Option to provide shorter unwrap methods
pub trait OptionExt<T> {
/// Shorthand for `.as_ref().unwrap()`
fn u(&self) -> &T;
/// Shorthand for `.as_mut().unwrap()`
fn um(&mut self) -> &mut T;
}
impl<T> OptionExt<T> for Option<T> {
#[inline]
fn u(&self) -> &T {
self.as_ref().unwrap()
}
#[inline]
fn um(&mut self) -> &mut T {
self.as_mut().unwrap()
}
}
pub fn get_percentile<T>(sorted: &[T], percentile: f64) -> T
where
T: Clone + Div<usize, Output = T> + Add<T, Output = T>,