From 286256ebf0e25151b33b687434d7a60ef8621686 Mon Sep 17 00:00:00 2001 From: nym21 Date: Fri, 10 Apr 2026 11:30:29 +0200 Subject: [PATCH] global: veccached change --- .../brk_computer/src/blocks/count/import.rs | 4 +- .../src/blocks/difficulty/import.rs | 6 +- crates/brk_computer/src/blocks/import.rs | 10 +- .../src/blocks/interval/import.rs | 4 +- crates/brk_computer/src/blocks/lookback.rs | 57 ++++---- crates/brk_computer/src/blocks/size/import.rs | 4 +- .../brk_computer/src/blocks/weight/import.rs | 4 +- .../src/cointime/activity/import.rs | 4 +- crates/brk_computer/src/cointime/import.rs | 4 +- .../src/cointime/reserve_risk/compute.rs | 4 +- .../src/cointime/value/compute.rs | 8 +- .../brk_computer/src/cointime/value/import.rs | 4 +- .../src/distribution/addr/activity.rs | 8 +- .../src/distribution/addr/delta.rs | 4 +- .../src/distribution/addr/new_addr_count.rs | 4 +- .../src/distribution/cohorts/addr/groups.rs | 9 +- .../src/distribution/cohorts/addr/vecs.rs | 4 +- .../src/distribution/cohorts/utxo/groups.rs | 4 +- .../src/distribution/metrics/activity/full.rs | 11 +- .../src/distribution/metrics/cohort/all.rs | 6 +- .../src/distribution/metrics/cohort/basic.rs | 2 +- .../src/distribution/metrics/cohort/core.rs | 2 +- .../distribution/metrics/cohort/extended.rs | 6 +- .../distribution/metrics/cohort/minimal.rs | 2 +- .../src/distribution/metrics/cohort/type.rs | 2 +- .../src/distribution/metrics/config.rs | 10 +- .../src/distribution/metrics/profitability.rs | 12 +- .../src/distribution/metrics/realized/core.rs | 4 +- .../distribution/metrics/unrealized/full.rs | 4 +- crates/brk_computer/src/distribution/vecs.rs | 4 +- .../src/indexes/cached_mappings.rs | 48 ------- crates/brk_computer/src/indexes/day1.rs | 10 +- crates/brk_computer/src/indexes/day3.rs | 20 --- crates/brk_computer/src/indexes/epoch.rs | 22 --- crates/brk_computer/src/indexes/halving.rs | 20 --- crates/brk_computer/src/indexes/hour1.rs | 20 --- crates/brk_computer/src/indexes/hour12.rs | 20 --- crates/brk_computer/src/indexes/hour4.rs | 20 --- crates/brk_computer/src/indexes/minute10.rs | 20 --- crates/brk_computer/src/indexes/minute30.rs | 20 --- crates/brk_computer/src/indexes/mod.rs | 133 ++++++------------ crates/brk_computer/src/indexes/month1.rs | 22 --- crates/brk_computer/src/indexes/month3.rs | 22 --- crates/brk_computer/src/indexes/month6.rs | 22 --- crates/brk_computer/src/indexes/resolution.rs | 68 +++++++++ crates/brk_computer/src/indexes/timestamp.rs | 4 +- crates/brk_computer/src/indexes/week1.rs | 22 --- crates/brk_computer/src/indexes/year1.rs | 22 --- crates/brk_computer/src/indexes/year10.rs | 22 --- .../src/indicators/realized_envelope.rs | 2 +- .../brk_computer/src/inputs/count/import.rs | 4 +- crates/brk_computer/src/inputs/import.rs | 4 +- .../brk_computer/src/internal/cache_budget.rs | 11 +- .../src/internal/containers/windows.rs | 2 +- .../src/internal/per_block/amount/base.rs | 2 +- .../src/internal/per_block/amount/block.rs | 2 +- .../per_block/amount/cumulative_rolling.rs | 6 +- .../src/internal/per_block/amount/full.rs | 6 +- .../per_block/amount/lazy_rolling_avg.rs | 22 +-- .../per_block/amount/lazy_rolling_sum.rs | 22 +-- .../internal/per_block/amount/with_deltas.rs | 4 +- .../internal/per_block/computed/aggregated.rs | 4 +- .../src/internal/per_block/computed/base.rs | 6 +- .../src/internal/per_block/computed/cached.rs | 48 +++++++ .../per_block/computed/cumulative_rolling.rs | 6 +- .../src/internal/per_block/computed/full.rs | 4 +- .../per_block/computed/lazy_rolling.rs | 6 +- .../src/internal/per_block/computed/mod.rs | 2 + .../per_block/computed/resolutions.rs | 51 +++---- .../internal/per_block/computed/rolling.rs | 4 +- .../per_block/computed/rolling_average.rs | 4 +- .../per_block/computed/with_deltas.rs | 6 +- .../internal/per_block/fiat/cumulative_sum.rs | 4 +- .../fiat/cumulative_sum_with_deltas.rs | 4 +- .../per_block/fiat/lazy_rolling_sum.rs | 18 +-- .../internal/per_block/fiat/with_deltas.rs | 4 +- .../src/internal/per_block/lazy/base.rs | 58 ++++++-- .../per_block/lazy/derived_resolutions.rs | 20 +-- .../per_block/ratio/price_extended.rs | 4 +- .../src/internal/per_block/ratio/sma.rs | 6 +- .../src/internal/per_block/rolling/avgs.rs | 14 +- .../internal/per_block/rolling/complete.rs | 6 +- .../src/internal/per_block/rolling/delta.rs | 59 ++++---- .../per_block/rolling/lazy_complete.rs | 6 +- .../src/internal/per_block/rolling/sums.rs | 19 +-- .../src/internal/per_block/rolling/windows.rs | 5 +- crates/brk_computer/src/investing/compute.rs | 6 +- crates/brk_computer/src/lib.rs | 20 +-- crates/brk_computer/src/market/ath/compute.rs | 6 +- .../src/market/lookback/compute.rs | 2 +- .../src/market/moving_average/compute.rs | 2 +- .../brk_computer/src/market/range/compute.rs | 8 +- .../src/market/returns/compute.rs | 2 +- .../brk_computer/src/market/technical/macd.rs | 2 +- .../src/mining/hashrate/compute.rs | 6 +- crates/brk_computer/src/mining/import.rs | 4 +- .../brk_computer/src/mining/rewards/import.rs | 6 +- .../brk_computer/src/outputs/count/import.rs | 4 +- crates/brk_computer/src/outputs/import.rs | 4 +- crates/brk_computer/src/pools/major.rs | 6 +- crates/brk_computer/src/pools/minor.rs | 4 +- crates/brk_computer/src/pools/mod.rs | 4 +- crates/brk_computer/src/prices/by_unit.rs | 4 +- crates/brk_computer/src/prices/compute.rs | 8 +- crates/brk_computer/src/prices/mod.rs | 39 ++--- .../brk_computer/src/scripts/count/import.rs | 4 +- crates/brk_computer/src/scripts/import.rs | 4 +- crates/brk_computer/src/supply/import.rs | 6 +- .../src/transactions/count/import.rs | 4 +- .../brk_computer/src/transactions/import.rs | 4 +- .../src/transactions/versions/import.rs | 4 +- .../src/transactions/volume/import.rs | 4 +- crates/brk_query/src/impl/addr.rs | 15 +- crates/brk_query/src/impl/block/info.rs | 4 +- crates/brk_query/src/impl/block/status.rs | 5 +- .../brk_query/src/impl/mining/block_window.rs | 11 +- crates/brk_query/src/impl/mining/pools.rs | 52 ++----- crates/brk_query/src/lib.rs | 2 +- 118 files changed, 579 insertions(+), 900 deletions(-) delete mode 100644 crates/brk_computer/src/indexes/cached_mappings.rs delete mode 100644 crates/brk_computer/src/indexes/day3.rs delete mode 100644 crates/brk_computer/src/indexes/epoch.rs delete mode 100644 crates/brk_computer/src/indexes/halving.rs delete mode 100644 crates/brk_computer/src/indexes/hour1.rs delete mode 100644 crates/brk_computer/src/indexes/hour12.rs delete mode 100644 crates/brk_computer/src/indexes/hour4.rs delete mode 100644 crates/brk_computer/src/indexes/minute10.rs delete mode 100644 crates/brk_computer/src/indexes/minute30.rs delete mode 100644 crates/brk_computer/src/indexes/month1.rs delete mode 100644 crates/brk_computer/src/indexes/month3.rs delete mode 100644 crates/brk_computer/src/indexes/month6.rs create mode 100644 crates/brk_computer/src/indexes/resolution.rs delete mode 100644 crates/brk_computer/src/indexes/week1.rs delete mode 100644 crates/brk_computer/src/indexes/year1.rs delete mode 100644 crates/brk_computer/src/indexes/year10.rs create mode 100644 crates/brk_computer/src/internal/per_block/computed/cached.rs diff --git a/crates/brk_computer/src/blocks/count/import.rs b/crates/brk_computer/src/blocks/count/import.rs index e8aab9905..31a772ecc 100644 --- a/crates/brk_computer/src/blocks/count/import.rs +++ b/crates/brk_computer/src/blocks/count/import.rs @@ -7,7 +7,7 @@ use crate::{ indexes, internal::{ BlockCountTarget1m, BlockCountTarget1w, BlockCountTarget1y, BlockCountTarget24h, - CachedWindowStarts, ConstantVecs, PerBlockCumulativeRolling, Windows, + ConstantVecs, PerBlockCumulativeRolling, WindowStartVec, Windows, }, }; @@ -16,7 +16,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { Ok(Self { target: Windows { diff --git a/crates/brk_computer/src/blocks/difficulty/import.rs b/crates/brk_computer/src/blocks/difficulty/import.rs index 57396fd52..1c78cc9dd 100644 --- a/crates/brk_computer/src/blocks/difficulty/import.rs +++ b/crates/brk_computer/src/blocks/difficulty/import.rs @@ -20,10 +20,10 @@ impl Vecs { ) -> Result { let v2 = Version::TWO; - let hashrate = LazyPerBlock::from_height_source::( + let hashrate = LazyPerBlock::from_height_source::( "difficulty_hashrate", version, - indexer.vecs.blocks.difficulty.read_only_boxed_clone(), + indexer.vecs.blocks.difficulty.read_only_clone(), indexes, ); @@ -40,7 +40,7 @@ impl Vecs { Ok(Self { value: Resolutions::forced_import( "difficulty", - indexer.vecs.blocks.difficulty.read_only_boxed_clone(), + indexer.vecs.blocks.difficulty.read_only_clone(), version, indexes, ), diff --git a/crates/brk_computer/src/blocks/import.rs b/crates/brk_computer/src/blocks/import.rs index 21540a32e..d541a0b05 100644 --- a/crates/brk_computer/src/blocks/import.rs +++ b/crates/brk_computer/src/blocks/import.rs @@ -24,11 +24,11 @@ impl Vecs { let version = parent_version; let lookback = LookbackVecs::forced_import(&db, version)?; - let cached_starts = &lookback.cached_window_starts; - let count = CountVecs::forced_import(&db, version, indexes, cached_starts)?; - let interval = IntervalVecs::forced_import(&db, version, indexes, cached_starts)?; - let size = SizeVecs::forced_import(&db, version, indexes, cached_starts)?; - let weight = WeightVecs::forced_import(&db, version, indexes, cached_starts, &size)?; + let cached_starts = lookback.cached_window_starts(); + let count = CountVecs::forced_import(&db, version, indexes, &cached_starts)?; + let interval = IntervalVecs::forced_import(&db, version, indexes, &cached_starts)?; + let size = SizeVecs::forced_import(&db, version, indexes, &cached_starts)?; + let weight = WeightVecs::forced_import(&db, version, indexes, &cached_starts, &size)?; let difficulty = DifficultyVecs::forced_import(&db, version, indexer, indexes)?; let halving = HalvingVecs::forced_import(&db, version, indexes)?; diff --git a/crates/brk_computer/src/blocks/interval/import.rs b/crates/brk_computer/src/blocks/interval/import.rs index 4871d88ad..3da683394 100644 --- a/crates/brk_computer/src/blocks/interval/import.rs +++ b/crates/brk_computer/src/blocks/interval/import.rs @@ -5,7 +5,7 @@ use vecdb::Database; use super::Vecs; use crate::{ indexes, - internal::{CachedWindowStarts, PerBlockRollingAverage}, + internal::{PerBlockRollingAverage, WindowStartVec, Windows}, }; impl Vecs { @@ -13,7 +13,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let interval = PerBlockRollingAverage::forced_import( db, diff --git a/crates/brk_computer/src/blocks/lookback.rs b/crates/brk_computer/src/blocks/lookback.rs index 917ce553e..e0095c557 100644 --- a/crates/brk_computer/src/blocks/lookback.rs +++ b/crates/brk_computer/src/blocks/lookback.rs @@ -8,17 +8,15 @@ use vecdb::{ use crate::{ indexes, - internal::{CachedWindowStarts, WindowStarts, Windows}, + internal::{WindowStartVec, WindowStarts, Windows}, }; #[derive(Traversable)] pub struct Vecs { - #[traversable(skip)] - pub cached_window_starts: CachedWindowStarts, pub _1h: M::Stored>>, - pub _24h: M::Stored>>, // 1d + pub _24h: CachedVec>>>, // 1d pub _3d: M::Stored>>, - pub _1w: M::Stored>>, // 7d + pub _1w: CachedVec>>>, // 7d pub _8d: M::Stored>>, pub _9d: M::Stored>>, pub _12d: M::Stored>>, @@ -26,7 +24,7 @@ pub struct Vecs { pub _2w: M::Stored>>, // 14d pub _21d: M::Stored>>, pub _26d: M::Stored>>, - pub _1m: M::Stored>>, // 30d + pub _1m: CachedVec>>>, // 30d pub _34d: M::Stored>>, pub _55d: M::Stored>>, pub _2m: M::Stored>>, // 60d @@ -43,7 +41,7 @@ pub struct Vecs { pub _9m: M::Stored>>, // 270d pub _350d: M::Stored>>, pub _12m: M::Stored>>, // 360d - pub _1y: M::Stored>>, // 365d + pub _1y: CachedVec>>>, // 365d pub _14m: M::Stored>>, // 420d pub _2y: M::Stored>>, // 730d pub _26m: M::Stored>>, // 780d @@ -106,19 +104,11 @@ impl Vecs { let _14y = ImportableVec::forced_import(db, "height_14y_ago", version)?; let _26y = ImportableVec::forced_import(db, "height_26y_ago", version)?; - let cached_window_starts = CachedWindowStarts(Windows { - _24h: CachedVec::new(&_24h), - _1w: CachedVec::new(&_1w), - _1m: CachedVec::new(&_1m), - _1y: CachedVec::new(&_1y), - }); - Ok(Self { - cached_window_starts, _1h, - _24h, + _24h: CachedVec::wrap(_24h), _3d, - _1w, + _1w: CachedVec::wrap(_1w), _8d, _9d, _12d, @@ -126,7 +116,7 @@ impl Vecs { _2w, _21d, _26d, - _1m, + _1m: CachedVec::wrap(_1m), _34d, _55d, _2m, @@ -143,7 +133,7 @@ impl Vecs { _9m, _350d, _12m, - _1y, + _1y: CachedVec::wrap(_1y), _14m, _2y, _26m, @@ -161,8 +151,8 @@ impl Vecs { }) } - pub fn window_starts(&self) -> WindowStarts<'_> { - WindowStarts { + pub fn cached_window_starts(&self) -> Windows<&WindowStartVec> { + Windows { _24h: &self._24h, _1w: &self._1w, _1m: &self._1m, @@ -170,11 +160,20 @@ impl Vecs { } } + pub fn window_starts(&self) -> WindowStarts<'_> { + WindowStarts { + _24h: &self._24h.inner, + _1w: &self._1w.inner, + _1m: &self._1m.inner, + _1y: &self._1y.inner, + } + } + pub fn start_vec(&self, days: usize) -> &EagerVec> { match days { - 1 => &self._24h, + 1 => &self._24h.inner, 3 => &self._3d, - 7 => &self._1w, + 7 => &self._1w.inner, 8 => &self._8d, 9 => &self._9d, 12 => &self._12d, @@ -182,7 +181,7 @@ impl Vecs { 14 => &self._2w, 21 => &self._21d, 26 => &self._26d, - 30 => &self._1m, + 30 => &self._1m.inner, 34 => &self._34d, 55 => &self._55d, 60 => &self._2m, @@ -199,7 +198,7 @@ impl Vecs { 270 => &self._9m, 350 => &self._350d, 360 => &self._12m, - 365 => &self._1y, + 365 => &self._1y.inner, 420 => &self._14m, 730 => &self._2y, 780 => &self._26m, @@ -225,9 +224,9 @@ impl Vecs { exit: &Exit, ) -> Result<()> { self.compute_rolling_start_hours(indexes, starting_indexes, exit, 1, |s| &mut s._1h)?; - self.compute_rolling_start(indexes, starting_indexes, exit, 1, |s| &mut s._24h)?; + self.compute_rolling_start(indexes, starting_indexes, exit, 1, |s| &mut s._24h.inner)?; self.compute_rolling_start(indexes, starting_indexes, exit, 3, |s| &mut s._3d)?; - self.compute_rolling_start(indexes, starting_indexes, exit, 7, |s| &mut s._1w)?; + self.compute_rolling_start(indexes, starting_indexes, exit, 7, |s| &mut s._1w.inner)?; self.compute_rolling_start(indexes, starting_indexes, exit, 8, |s| &mut s._8d)?; self.compute_rolling_start(indexes, starting_indexes, exit, 9, |s| &mut s._9d)?; self.compute_rolling_start(indexes, starting_indexes, exit, 12, |s| &mut s._12d)?; @@ -235,7 +234,7 @@ impl Vecs { self.compute_rolling_start(indexes, starting_indexes, exit, 14, |s| &mut s._2w)?; self.compute_rolling_start(indexes, starting_indexes, exit, 21, |s| &mut s._21d)?; self.compute_rolling_start(indexes, starting_indexes, exit, 26, |s| &mut s._26d)?; - self.compute_rolling_start(indexes, starting_indexes, exit, 30, |s| &mut s._1m)?; + self.compute_rolling_start(indexes, starting_indexes, exit, 30, |s| &mut s._1m.inner)?; self.compute_rolling_start(indexes, starting_indexes, exit, 34, |s| &mut s._34d)?; self.compute_rolling_start(indexes, starting_indexes, exit, 55, |s| &mut s._55d)?; self.compute_rolling_start(indexes, starting_indexes, exit, 60, |s| &mut s._2m)?; @@ -252,7 +251,7 @@ impl Vecs { self.compute_rolling_start(indexes, starting_indexes, exit, 270, |s| &mut s._9m)?; self.compute_rolling_start(indexes, starting_indexes, exit, 350, |s| &mut s._350d)?; self.compute_rolling_start(indexes, starting_indexes, exit, 360, |s| &mut s._12m)?; - self.compute_rolling_start(indexes, starting_indexes, exit, 365, |s| &mut s._1y)?; + self.compute_rolling_start(indexes, starting_indexes, exit, 365, |s| &mut s._1y.inner)?; self.compute_rolling_start(indexes, starting_indexes, exit, 420, |s| &mut s._14m)?; self.compute_rolling_start(indexes, starting_indexes, exit, 730, |s| &mut s._2y)?; self.compute_rolling_start(indexes, starting_indexes, exit, 780, |s| &mut s._26m)?; diff --git a/crates/brk_computer/src/blocks/size/import.rs b/crates/brk_computer/src/blocks/size/import.rs index b4dfbb671..49de02015 100644 --- a/crates/brk_computer/src/blocks/size/import.rs +++ b/crates/brk_computer/src/blocks/size/import.rs @@ -5,7 +5,7 @@ use vecdb::Database; use super::Vecs; use crate::{ indexes, - internal::{CachedWindowStarts, PerBlockFull, PerBlockRolling}, + internal::{PerBlockFull, PerBlockRolling, WindowStartVec, Windows}, }; impl Vecs { @@ -13,7 +13,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { Ok(Self { vbytes: PerBlockFull::forced_import( diff --git a/crates/brk_computer/src/blocks/weight/import.rs b/crates/brk_computer/src/blocks/weight/import.rs index aa20fb203..4f3701ec7 100644 --- a/crates/brk_computer/src/blocks/weight/import.rs +++ b/crates/brk_computer/src/blocks/weight/import.rs @@ -6,7 +6,7 @@ use super::Vecs; use crate::{ blocks::SizeVecs, indexes, - internal::{CachedWindowStarts, LazyPerBlockRolling, PercentVec, VBytesToWeight}, + internal::{LazyPerBlockRolling, PercentVec, VBytesToWeight, WindowStartVec, Windows}, }; impl Vecs { @@ -14,7 +14,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, size: &SizeVecs, ) -> Result { let weight = LazyPerBlockRolling::from_per_block_full::( diff --git a/crates/brk_computer/src/cointime/activity/import.rs b/crates/brk_computer/src/cointime/activity/import.rs index 48a9b0e27..89dc42f7a 100644 --- a/crates/brk_computer/src/cointime/activity/import.rs +++ b/crates/brk_computer/src/cointime/activity/import.rs @@ -6,7 +6,7 @@ use super::Vecs; use crate::{ indexes, internal::{ - CachedWindowStarts, LazyPerBlock, OneMinusF64, PerBlock, PerBlockCumulativeRolling, + LazyPerBlock, OneMinusF64, PerBlock, PerBlockCumulativeRolling, WindowStartVec, Windows, }, }; @@ -15,7 +15,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let liveliness = PerBlock::forced_import(db, "liveliness", version, indexes)?; diff --git a/crates/brk_computer/src/cointime/import.rs b/crates/brk_computer/src/cointime/import.rs index c7808d5bd..8b16b7eac 100644 --- a/crates/brk_computer/src/cointime/import.rs +++ b/crates/brk_computer/src/cointime/import.rs @@ -13,14 +13,14 @@ use super::{ ValueVecs, Vecs, }; -use crate::internal::CachedWindowStarts; +use crate::internal::{WindowStartVec, Windows}; impl Vecs { pub(crate) fn forced_import( parent_path: &Path, parent_version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let db = open_db(parent_path, DB_NAME, 250_000)?; let version = parent_version; diff --git a/crates/brk_computer/src/cointime/reserve_risk/compute.rs b/crates/brk_computer/src/cointime/reserve_risk/compute.rs index 140d89038..71281dc2f 100644 --- a/crates/brk_computer/src/cointime/reserve_risk/compute.rs +++ b/crates/brk_computer/src/cointime/reserve_risk/compute.rs @@ -23,7 +23,7 @@ impl Vecs { self.hodl_bank.compute_cumulative_transformed_binary( starting_indexes.height, - &prices.cached_spot_usd, + &prices.spot.usd.height, &self.vocdd_median_1y, |price, median| StoredF64::from(f64::from(price) - f64::from(median)), exit, @@ -31,7 +31,7 @@ impl Vecs { self.value.height.compute_divide( starting_indexes.height, - &prices.cached_spot_usd, + &prices.spot.usd.height, &self.hodl_bank, exit, )?; diff --git a/crates/brk_computer/src/cointime/value/compute.rs b/crates/brk_computer/src/cointime/value/compute.rs index e3cc21318..14110e9e9 100644 --- a/crates/brk_computer/src/cointime/value/compute.rs +++ b/crates/brk_computer/src/cointime/value/compute.rs @@ -24,7 +24,7 @@ impl Vecs { .compute(starting_indexes.height, exit, |vec| { vec.compute_multiply( starting_indexes.height, - &prices.cached_spot_usd, + &prices.spot.usd.height, &coinblocks_destroyed.block, exit, )?; @@ -34,7 +34,7 @@ impl Vecs { self.created.compute(starting_indexes.height, exit, |vec| { vec.compute_multiply( starting_indexes.height, - &prices.cached_spot_usd, + &prices.spot.usd.height, &activity.coinblocks_created.block, exit, )?; @@ -44,7 +44,7 @@ impl Vecs { self.stored.compute(starting_indexes.height, exit, |vec| { vec.compute_multiply( starting_indexes.height, - &prices.cached_spot_usd, + &prices.spot.usd.height, &activity.coinblocks_stored.block, exit, )?; @@ -57,7 +57,7 @@ impl Vecs { self.vocdd.compute(starting_indexes.height, exit, |vec| { vec.compute_transform3( starting_indexes.height, - &prices.cached_spot_usd, + &prices.spot.usd.height, &coindays_destroyed.block, circulating_supply, |(i, price, cdd, supply, _): (_, Dollars, StoredF64, Bitcoin, _)| { diff --git a/crates/brk_computer/src/cointime/value/import.rs b/crates/brk_computer/src/cointime/value/import.rs index b5fcdbc1f..19461c202 100644 --- a/crates/brk_computer/src/cointime/value/import.rs +++ b/crates/brk_computer/src/cointime/value/import.rs @@ -5,7 +5,7 @@ use vecdb::Database; use super::Vecs; use crate::{ indexes, - internal::{CachedWindowStarts, PerBlockCumulativeRolling}, + internal::{PerBlockCumulativeRolling, WindowStartVec, Windows}, }; impl Vecs { @@ -13,7 +13,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { Ok(Self { destroyed: PerBlockCumulativeRolling::forced_import( diff --git a/crates/brk_computer/src/distribution/addr/activity.rs b/crates/brk_computer/src/distribution/addr/activity.rs index ee8d89b2f..d2fe7f7f8 100644 --- a/crates/brk_computer/src/distribution/addr/activity.rs +++ b/crates/brk_computer/src/distribution/addr/activity.rs @@ -19,7 +19,7 @@ use vecdb::{AnyStoredVec, AnyVec, Database, Exit, Rw, StorageMode, WritableVec}; use crate::{ indexes, - internal::{CachedWindowStarts, PerBlockRollingAverage}, + internal::{PerBlockRollingAverage, WindowStartVec, Windows}, }; /// Per-block activity counts - reset each block. @@ -77,7 +77,7 @@ impl ActivityCountVecs { name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { Ok(Self { reactivated: PerBlockRollingAverage::forced_import( @@ -174,7 +174,7 @@ impl AddrTypeToActivityCountVecs { name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { Ok(Self::from(ByAddrType::::new_with_name( |type_name| { @@ -246,7 +246,7 @@ impl AddrActivityVecs { name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { Ok(Self { all: ActivityCountVecs::forced_import(db, name, version, indexes, cached_starts)?, diff --git a/crates/brk_computer/src/distribution/addr/delta.rs b/crates/brk_computer/src/distribution/addr/delta.rs index 4f8a21570..836b79d1a 100644 --- a/crates/brk_computer/src/distribution/addr/delta.rs +++ b/crates/brk_computer/src/distribution/addr/delta.rs @@ -4,7 +4,7 @@ use brk_types::{BasisPointsSigned32, StoredI64, StoredU64, Version}; use crate::{ indexes, - internal::{CachedWindowStarts, LazyRollingDeltasFromHeight}, + internal::{LazyRollingDeltasFromHeight, WindowStartVec, Windows}, }; use super::AddrCountsVecs; @@ -22,7 +22,7 @@ impl DeltaVecs { pub(crate) fn new( version: Version, addr_count: &AddrCountsVecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, indexes: &indexes::Vecs, ) -> Self { let version = version + Version::TWO; diff --git a/crates/brk_computer/src/distribution/addr/new_addr_count.rs b/crates/brk_computer/src/distribution/addr/new_addr_count.rs index 8ffad8fc0..3d5fb43a4 100644 --- a/crates/brk_computer/src/distribution/addr/new_addr_count.rs +++ b/crates/brk_computer/src/distribution/addr/new_addr_count.rs @@ -6,7 +6,7 @@ use vecdb::{Database, Exit, Rw, StorageMode}; use crate::{ indexes, - internal::{CachedWindowStarts, PerBlockCumulativeRolling}, + internal::{PerBlockCumulativeRolling, WindowStartVec, Windows}, }; use super::TotalAddrCountVecs; @@ -24,7 +24,7 @@ impl NewAddrCountVecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let all = PerBlockCumulativeRolling::forced_import( db, diff --git a/crates/brk_computer/src/distribution/cohorts/addr/groups.rs b/crates/brk_computer/src/distribution/cohorts/addr/groups.rs index 13b104442..1b6bf0c14 100644 --- a/crates/brk_computer/src/distribution/cohorts/addr/groups.rs +++ b/crates/brk_computer/src/distribution/cohorts/addr/groups.rs @@ -8,7 +8,12 @@ use derive_more::{Deref, DerefMut}; use rayon::prelude::*; use vecdb::{AnyStoredVec, Database, Exit, ReadableVec, Rw, StorageMode}; -use crate::{distribution::DynCohortVecs, indexes, internal::CachedWindowStarts, prices}; +use crate::{ + distribution::DynCohortVecs, + indexes, + internal::{WindowStartVec, Windows}, + prices, +}; use super::{super::traits::CohortVecs, vecs::AddrCohortVecs}; @@ -25,7 +30,7 @@ impl AddrCohorts { version: Version, indexes: &indexes::Vecs, states_path: &Path, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let v = version + VERSION; diff --git a/crates/brk_computer/src/distribution/cohorts/addr/vecs.rs b/crates/brk_computer/src/distribution/cohorts/addr/vecs.rs index 379555909..2aaa798c0 100644 --- a/crates/brk_computer/src/distribution/cohorts/addr/vecs.rs +++ b/crates/brk_computer/src/distribution/cohorts/addr/vecs.rs @@ -10,7 +10,7 @@ use vecdb::{AnyStoredVec, AnyVec, Database, Exit, ReadableVec, Rw, StorageMode, use crate::{ distribution::state::{AddrCohortState, MinimalRealizedState}, indexes, - internal::{CachedWindowStarts, PerBlockWithDeltas}, + internal::{PerBlockWithDeltas, WindowStartVec, Windows}, prices, }; @@ -38,7 +38,7 @@ impl AddrCohortVecs { version: Version, indexes: &indexes::Vecs, states_path: Option<&Path>, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let full_name = CohortContext::Addr.full_name(&filter, name); diff --git a/crates/brk_computer/src/distribution/cohorts/utxo/groups.rs b/crates/brk_computer/src/distribution/cohorts/utxo/groups.rs index a7633e344..45e74ba9a 100644 --- a/crates/brk_computer/src/distribution/cohorts/utxo/groups.rs +++ b/crates/brk_computer/src/distribution/cohorts/utxo/groups.rs @@ -25,7 +25,7 @@ use crate::{ state::UTXOCohortState, }, indexes, - internal::{AmountPerBlockCumulativeRolling, CachedWindowStarts}, + internal::{AmountPerBlockCumulativeRolling, WindowStartVec, Windows}, prices, }; @@ -75,7 +75,7 @@ impl UTXOCohorts { version: Version, indexes: &indexes::Vecs, states_path: &Path, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let v = version + VERSION; diff --git a/crates/brk_computer/src/distribution/metrics/activity/full.rs b/crates/brk_computer/src/distribution/metrics/activity/full.rs index 2521d92d0..b10334746 100644 --- a/crates/brk_computer/src/distribution/metrics/activity/full.rs +++ b/crates/brk_computer/src/distribution/metrics/activity/full.rs @@ -2,7 +2,7 @@ use brk_error::Result; use brk_traversable::Traversable; use brk_types::{Indexes, StoredF32, StoredF64, Version}; use derive_more::{Deref, DerefMut}; -use vecdb::{AnyStoredVec, Exit, ReadableCloneableVec, Rw, StorageMode}; +use vecdb::{AnyStoredVec, Exit, Rw, StorageMode}; use crate::internal::{Identity, LazyPerBlock, PerBlock, Windows}; @@ -33,15 +33,10 @@ impl ActivityFull { let v1 = Version::ONE; let inner = ActivityCore::forced_import(cfg)?; - let coinyears_destroyed = LazyPerBlock::from_height_source::>( + let coinyears_destroyed = LazyPerBlock::from_height_source::, _>( &cfg.name("coinyears_destroyed"), cfg.version + v1, - inner - .coindays_destroyed - .sum - ._1y - .height - .read_only_boxed_clone(), + inner.coindays_destroyed.sum._1y.height.clone(), cfg.indexes, ); diff --git a/crates/brk_computer/src/distribution/metrics/cohort/all.rs b/crates/brk_computer/src/distribution/metrics/cohort/all.rs index c21e55ded..663a56dfd 100644 --- a/crates/brk_computer/src/distribution/metrics/cohort/all.rs +++ b/crates/brk_computer/src/distribution/metrics/cohort/all.rs @@ -119,7 +119,7 @@ impl AllCohortMetrics { self.unrealized.compute( starting_indexes.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.realized.price.cents.height, exit, )?; @@ -139,7 +139,7 @@ impl AllCohortMetrics { self.cost_basis.compute_prices( starting_indexes, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.unrealized.invested_capital.in_profit.cents.height, &self.unrealized.invested_capital.in_loss.cents.height, &self.supply.in_profit.sats.height, @@ -150,7 +150,7 @@ impl AllCohortMetrics { )?; self.unrealized - .compute_sentiment(starting_indexes, &prices.cached_spot_cents, exit)?; + .compute_sentiment(starting_indexes, &prices.spot.cents.height, exit)?; self.relative.compute( starting_indexes.height, diff --git a/crates/brk_computer/src/distribution/metrics/cohort/basic.rs b/crates/brk_computer/src/distribution/metrics/cohort/basic.rs index fcc7c4e5b..c16245ba1 100644 --- a/crates/brk_computer/src/distribution/metrics/cohort/basic.rs +++ b/crates/brk_computer/src/distribution/metrics/cohort/basic.rs @@ -82,7 +82,7 @@ impl BasicCohortMetrics { self.unrealized.compute( starting_indexes.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.realized.price.cents.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/cohort/core.rs b/crates/brk_computer/src/distribution/metrics/cohort/core.rs index 233e39a58..de14a707a 100644 --- a/crates/brk_computer/src/distribution/metrics/cohort/core.rs +++ b/crates/brk_computer/src/distribution/metrics/cohort/core.rs @@ -140,7 +140,7 @@ impl CoreCohortMetrics { self.unrealized.compute( starting_indexes.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.realized.price.cents.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/cohort/extended.rs b/crates/brk_computer/src/distribution/metrics/cohort/extended.rs index 2a20cf907..f4c857f56 100644 --- a/crates/brk_computer/src/distribution/metrics/cohort/extended.rs +++ b/crates/brk_computer/src/distribution/metrics/cohort/extended.rs @@ -108,14 +108,14 @@ impl ExtendedCohortMetrics { self.unrealized.compute( starting_indexes.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.realized.price.cents.height, exit, )?; self.cost_basis.compute_prices( starting_indexes, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.unrealized.invested_capital.in_profit.cents.height, &self.unrealized.invested_capital.in_loss.cents.height, &self.supply.in_profit.sats.height, @@ -126,7 +126,7 @@ impl ExtendedCohortMetrics { )?; self.unrealized - .compute_sentiment(starting_indexes, &prices.cached_spot_cents, exit)?; + .compute_sentiment(starting_indexes, &prices.spot.cents.height, exit)?; self.relative.compute( starting_indexes.height, diff --git a/crates/brk_computer/src/distribution/metrics/cohort/minimal.rs b/crates/brk_computer/src/distribution/metrics/cohort/minimal.rs index ad01be6e3..834aed0db 100644 --- a/crates/brk_computer/src/distribution/metrics/cohort/minimal.rs +++ b/crates/brk_computer/src/distribution/metrics/cohort/minimal.rs @@ -124,7 +124,7 @@ impl MinimalCohortMetrics { self.unrealized.compute( starting_indexes.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.realized.price.cents.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/cohort/type.rs b/crates/brk_computer/src/distribution/metrics/cohort/type.rs index eeb15e2f3..b0870b1bb 100644 --- a/crates/brk_computer/src/distribution/metrics/cohort/type.rs +++ b/crates/brk_computer/src/distribution/metrics/cohort/type.rs @@ -86,7 +86,7 @@ impl TypeCohortMetrics { self.unrealized.compute( starting_indexes.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.realized.price.cents.height, exit, )?; diff --git a/crates/brk_computer/src/distribution/metrics/config.rs b/crates/brk_computer/src/distribution/metrics/config.rs index ef11c8b3d..367cebc97 100644 --- a/crates/brk_computer/src/distribution/metrics/config.rs +++ b/crates/brk_computer/src/distribution/metrics/config.rs @@ -7,11 +7,11 @@ use vecdb::{BytesVec, BytesVecValue, Database, ImportableVec}; use crate::{ indexes, internal::{ - AmountPerBlock, AmountPerBlockCumulative, AmountPerBlockCumulativeRolling, - CachedWindowStarts, CentsType, FiatPerBlock, FiatPerBlockCumulativeWithSums, NumericValue, - PerBlock, PerBlockCumulativeRolling, PercentPerBlock, PercentRollingWindows, Price, + AmountPerBlock, AmountPerBlockCumulative, AmountPerBlockCumulativeRolling, CentsType, + FiatPerBlock, FiatPerBlockCumulativeWithSums, NumericValue, PerBlock, + PerBlockCumulativeRolling, PercentPerBlock, PercentRollingWindows, Price, PriceWithRatioExtendedPerBlock, PriceWithRatioPerBlock, RatioPerBlock, - RollingWindow24hPerBlock, RollingWindows, RollingWindowsFrom1w, + RollingWindow24hPerBlock, RollingWindows, RollingWindowsFrom1w, WindowStartVec, Windows, }, }; @@ -128,7 +128,7 @@ pub struct ImportConfig<'a> { pub full_name: &'a str, pub version: Version, pub indexes: &'a indexes::Vecs, - pub cached_starts: &'a CachedWindowStarts, + pub cached_starts: &'a Windows<&'a WindowStartVec>, } impl<'a> ImportConfig<'a> { diff --git a/crates/brk_computer/src/distribution/metrics/profitability.rs b/crates/brk_computer/src/distribution/metrics/profitability.rs index f289fa8f9..f5608881d 100644 --- a/crates/brk_computer/src/distribution/metrics/profitability.rs +++ b/crates/brk_computer/src/distribution/metrics/profitability.rs @@ -7,7 +7,7 @@ use vecdb::{AnyStoredVec, AnyVec, Database, Exit, Rw, StorageMode, WritableVec}; use crate::{ indexes, internal::{ - AmountPerBlock, AmountPerBlockWithDeltas, CachedWindowStarts, PerBlock, RatioPerBlock, + AmountPerBlock, AmountPerBlockWithDeltas, PerBlock, RatioPerBlock, WindowStartVec, Windows, }, prices, }; @@ -43,7 +43,7 @@ impl ProfitabilityBucket { name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { Ok(Self { supply: WithSth { @@ -126,7 +126,7 @@ impl ProfitabilityBucket { self.unrealized_pnl.all.height.compute_transform3( max_from, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.realized_cap.all.height, &self.supply.all.sats.height, |(i, spot, cap, supply, ..)| { @@ -139,7 +139,7 @@ impl ProfitabilityBucket { )?; self.unrealized_pnl.sth.height.compute_transform3( max_from, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.realized_cap.sth.height, &self.supply.sth.sats.height, |(i, spot, cap, supply, ..)| { @@ -153,7 +153,7 @@ impl ProfitabilityBucket { self.nupl.bps.height.compute_transform3( max_from, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.realized_cap.all.height, &self.supply.all.sats.height, |(i, spot, cap_dollars, supply_sats, ..)| { @@ -267,7 +267,7 @@ impl ProfitabilityMetrics { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let range = ProfitabilityRange::try_new(|name| { ProfitabilityBucket::forced_import(db, name, version, indexes, cached_starts) diff --git a/crates/brk_computer/src/distribution/metrics/realized/core.rs b/crates/brk_computer/src/distribution/metrics/realized/core.rs index 28353d4ae..d531cec77 100644 --- a/crates/brk_computer/src/distribution/metrics/realized/core.rs +++ b/crates/brk_computer/src/distribution/metrics/realized/core.rs @@ -62,10 +62,10 @@ impl RealizedCore { ); let neg_loss_sum = minimal.loss.sum.0.map_with_suffix(|suffix, slot| { - LazyPerBlock::from_height_source::( + LazyPerBlock::from_height_source::( &cfg.name(&format!("realized_loss_neg_sum_{suffix}")), cfg.version + Version::ONE, - slot.cents.height.read_only_boxed_clone(), + slot.cents.height.clone(), cfg.indexes, ) }); diff --git a/crates/brk_computer/src/distribution/metrics/unrealized/full.rs b/crates/brk_computer/src/distribution/metrics/unrealized/full.rs index 32a61af6e..e5c276821 100644 --- a/crates/brk_computer/src/distribution/metrics/unrealized/full.rs +++ b/crates/brk_computer/src/distribution/metrics/unrealized/full.rs @@ -122,7 +122,7 @@ impl UnrealizedFull { .compute_transform3( starting_indexes.height, supply_in_profit_sats, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.inner.basic.profit.cents.height, |(h, supply_sats, spot, profit, ..): (_, Sats, Cents, Cents, _)| { let market_value = supply_sats.as_u128() * spot.as_u128() / Sats::ONE_BTC_U128; @@ -142,7 +142,7 @@ impl UnrealizedFull { .compute_transform3( starting_indexes.height, supply_in_loss_sats, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.inner.basic.loss.cents.height, |(h, supply_sats, spot, loss, ..): (_, Sats, Cents, Cents, _)| { let market_value = supply_sats.as_u128() * spot.as_u128() / Sats::ONE_BTC_U128; diff --git a/crates/brk_computer/src/distribution/vecs.rs b/crates/brk_computer/src/distribution/vecs.rs index a2a32205c..c3c95246a 100644 --- a/crates/brk_computer/src/distribution/vecs.rs +++ b/crates/brk_computer/src/distribution/vecs.rs @@ -24,7 +24,7 @@ use crate::{ }, indexes, inputs, internal::{ - CachedWindowStarts, PerBlockCumulativeRolling, + PerBlockCumulativeRolling, WindowStartVec, Windows, db_utils::{finalize_db, open_db}, }, outputs, prices, transactions, @@ -102,7 +102,7 @@ impl Vecs { parent: &Path, parent_version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let db_path = parent.join(super::DB_NAME); let states_path = db_path.join("states"); diff --git a/crates/brk_computer/src/indexes/cached_mappings.rs b/crates/brk_computer/src/indexes/cached_mappings.rs deleted file mode 100644 index edca6f8d5..000000000 --- a/crates/brk_computer/src/indexes/cached_mappings.rs +++ /dev/null @@ -1,48 +0,0 @@ -use brk_types::{ - Day1, Day3, Epoch, Halving, Height, Hour1, Hour4, Hour12, Minute10, Minute30, Month1, Month3, - Month6, Week1, Year1, Year10, -}; -use vecdb::CachedVec; - -use super::Vecs; - -#[derive(Clone)] -pub struct CachedMappings { - pub minute10_first_height: CachedVec, - pub minute30_first_height: CachedVec, - pub hour1_first_height: CachedVec, - pub hour4_first_height: CachedVec, - pub hour12_first_height: CachedVec, - pub day1_first_height: CachedVec, - pub day3_first_height: CachedVec, - pub week1_first_height: CachedVec, - pub month1_first_height: CachedVec, - pub month3_first_height: CachedVec, - pub month6_first_height: CachedVec, - pub year1_first_height: CachedVec, - pub year10_first_height: CachedVec, - pub halving_identity: CachedVec, - pub epoch_identity: CachedVec, -} - -impl CachedMappings { - pub fn new(vecs: &Vecs) -> Self { - Self { - minute10_first_height: CachedVec::new(&vecs.minute10.first_height), - minute30_first_height: CachedVec::new(&vecs.minute30.first_height), - hour1_first_height: CachedVec::new(&vecs.hour1.first_height), - hour4_first_height: CachedVec::new(&vecs.hour4.first_height), - hour12_first_height: CachedVec::new(&vecs.hour12.first_height), - day1_first_height: CachedVec::new(&vecs.day1.first_height), - day3_first_height: CachedVec::new(&vecs.day3.first_height), - week1_first_height: CachedVec::new(&vecs.week1.first_height), - month1_first_height: CachedVec::new(&vecs.month1.first_height), - month3_first_height: CachedVec::new(&vecs.month3.first_height), - month6_first_height: CachedVec::new(&vecs.month6.first_height), - year1_first_height: CachedVec::new(&vecs.year1.first_height), - year10_first_height: CachedVec::new(&vecs.year10.first_height), - halving_identity: CachedVec::new(&vecs.halving.identity), - epoch_identity: CachedVec::new(&vecs.epoch.identity), - } - } -} diff --git a/crates/brk_computer/src/indexes/day1.rs b/crates/brk_computer/src/indexes/day1.rs index 7a7b79e3c..c31ba3861 100644 --- a/crates/brk_computer/src/indexes/day1.rs +++ b/crates/brk_computer/src/indexes/day1.rs @@ -1,6 +1,6 @@ use brk_traversable::Traversable; -use brk_types::{Date, Day1, Height, StoredU64, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; +use brk_types::{Date, Day1, Height, Version}; +use vecdb::{CachedVec, Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; use brk_error::Result; @@ -8,8 +8,7 @@ use brk_error::Result; pub struct Vecs { pub identity: M::Stored>>, pub date: M::Stored>>, - pub first_height: M::Stored>>, - pub height_count: M::Stored>>, + pub first_height: CachedVec>>>, } impl Vecs { @@ -17,8 +16,7 @@ impl Vecs { Ok(Self { identity: EagerVec::forced_import(db, "day1_index", version)?, date: EagerVec::forced_import(db, "date", version + Version::ONE)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - height_count: EagerVec::forced_import(db, "height_count", version)?, + first_height: CachedVec::wrap(EagerVec::forced_import(db, "first_height", version)?), }) } } diff --git a/crates/brk_computer/src/indexes/day3.rs b/crates/brk_computer/src/indexes/day3.rs deleted file mode 100644 index 58a4632bf..000000000 --- a/crates/brk_computer/src/indexes/day3.rs +++ /dev/null @@ -1,20 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Day3, Height, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "day3_index", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/epoch.rs b/crates/brk_computer/src/indexes/epoch.rs deleted file mode 100644 index ffa3fc330..000000000 --- a/crates/brk_computer/src/indexes/epoch.rs +++ /dev/null @@ -1,22 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Epoch, Height, StoredU64, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub first_height: M::Stored>>, - pub height_count: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "epoch", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - height_count: EagerVec::forced_import(db, "height_count", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/halving.rs b/crates/brk_computer/src/indexes/halving.rs deleted file mode 100644 index 12e2d515d..000000000 --- a/crates/brk_computer/src/indexes/halving.rs +++ /dev/null @@ -1,20 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Halving, Height, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "halving", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/hour1.rs b/crates/brk_computer/src/indexes/hour1.rs deleted file mode 100644 index a67c6b951..000000000 --- a/crates/brk_computer/src/indexes/hour1.rs +++ /dev/null @@ -1,20 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Height, Hour1, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "hour1_index", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/hour12.rs b/crates/brk_computer/src/indexes/hour12.rs deleted file mode 100644 index e83b807ad..000000000 --- a/crates/brk_computer/src/indexes/hour12.rs +++ /dev/null @@ -1,20 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Height, Hour12, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "hour12_index", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/hour4.rs b/crates/brk_computer/src/indexes/hour4.rs deleted file mode 100644 index 174054943..000000000 --- a/crates/brk_computer/src/indexes/hour4.rs +++ /dev/null @@ -1,20 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Height, Hour4, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "hour4_index", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/minute10.rs b/crates/brk_computer/src/indexes/minute10.rs deleted file mode 100644 index 7ed7b01b9..000000000 --- a/crates/brk_computer/src/indexes/minute10.rs +++ /dev/null @@ -1,20 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Height, Minute10, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "minute10_index", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/minute30.rs b/crates/brk_computer/src/indexes/minute30.rs deleted file mode 100644 index 80ef85d4a..000000000 --- a/crates/brk_computer/src/indexes/minute30.rs +++ /dev/null @@ -1,20 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Height, Minute30, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "minute30_index", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/mod.rs b/crates/brk_computer/src/indexes/mod.rs index 98790abf0..b8408ab0c 100644 --- a/crates/brk_computer/src/indexes/mod.rs +++ b/crates/brk_computer/src/indexes/mod.rs @@ -1,26 +1,12 @@ mod addr; -mod cached_mappings; mod day1; -mod day3; -mod epoch; -mod halving; mod height; -mod hour1; -mod hour12; -mod hour4; -mod minute10; -mod minute30; -mod month1; -mod month3; -mod month6; +mod resolution; pub mod timestamp; mod tx_heights; mod tx_index; mod txin_index; mod txout_index; -mod week1; -mod year1; -mod year10; use std::path::Path; @@ -28,36 +14,37 @@ use brk_error::Result; use brk_indexer::Indexer; use brk_traversable::Traversable; use brk_types::{ - Date, Day1, Day3, Height, Hour1, Hour4, Hour12, Indexes, Minute10, Minute30, Month1, Month3, - Month6, Version, Week1, Year1, Year10, + Date, Day1, Day3, Epoch, Halving, Height, Hour1, Hour4, Hour12, Indexes, Minute10, Minute30, + Month1, Month3, Month6, Version, Week1, Year1, Year10, }; -use vecdb::{CachedVec, Database, Exit, ReadableVec, Rw, StorageMode}; +use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode}; use crate::internal::db_utils::{finalize_db, open_db}; pub use addr::Vecs as AddrVecs; -pub use cached_mappings::CachedMappings; pub use day1::Vecs as Day1Vecs; -pub use day3::Vecs as Day3Vecs; -pub use epoch::Vecs as EpochVecs; -pub use halving::Vecs as HalvingVecs; pub use height::Vecs as HeightVecs; -pub use hour1::Vecs as Hour1Vecs; -pub use hour4::Vecs as Hour4Vecs; -pub use hour12::Vecs as Hour12Vecs; -pub use minute10::Vecs as Minute10Vecs; -pub use minute30::Vecs as Minute30Vecs; -pub use month1::Vecs as Month1Vecs; -pub use month3::Vecs as Month3Vecs; -pub use month6::Vecs as Month6Vecs; +pub use resolution::{CachedResolutionVecs, DatedResolutionVecs, ResolutionVecs}; pub use timestamp::Timestamps; pub use tx_heights::TxHeights; pub use tx_index::Vecs as TxIndexVecs; pub use txin_index::Vecs as TxInIndexVecs; pub use txout_index::Vecs as TxOutIndexVecs; -pub use week1::Vecs as Week1Vecs; -pub use year1::Vecs as Year1Vecs; -pub use year10::Vecs as Year10Vecs; + +pub type Minute10Vecs = ResolutionVecs; +pub type Minute30Vecs = ResolutionVecs; +pub type Hour1Vecs = ResolutionVecs; +pub type Hour4Vecs = ResolutionVecs; +pub type Hour12Vecs = ResolutionVecs; +pub type Day3Vecs = ResolutionVecs; +pub type EpochVecs = CachedResolutionVecs; +pub type HalvingVecs = CachedResolutionVecs; +pub type Week1Vecs = DatedResolutionVecs; +pub type Month1Vecs = DatedResolutionVecs; +pub type Month3Vecs = DatedResolutionVecs; +pub type Month6Vecs = DatedResolutionVecs; +pub type Year1Vecs = DatedResolutionVecs; +pub type Year10Vecs = DatedResolutionVecs; pub const DB_NAME: &str = "indexes"; @@ -65,8 +52,6 @@ pub const DB_NAME: &str = "indexes"; pub struct Vecs { db: Database, #[traversable(skip)] - pub cached_mappings: CachedMappings, - #[traversable(skip)] pub tx_heights: TxHeights, pub addr: AddrVecs, pub height: HeightVecs, @@ -103,50 +88,31 @@ impl Vecs { let addr = AddrVecs::forced_import(version, indexer); let height = HeightVecs::forced_import(&db, version)?; - let epoch = EpochVecs::forced_import(&db, version)?; - let halving = HalvingVecs::forced_import(&db, version)?; - let minute10 = Minute10Vecs::forced_import(&db, version)?; - let minute30 = Minute30Vecs::forced_import(&db, version)?; - let hour1 = Hour1Vecs::forced_import(&db, version)?; - let hour4 = Hour4Vecs::forced_import(&db, version)?; - let hour12 = Hour12Vecs::forced_import(&db, version)?; + let epoch = CachedResolutionVecs::forced_import(&db, "epoch", version)?; + let halving = CachedResolutionVecs::forced_import(&db, "halving", version)?; + let minute10 = ResolutionVecs::forced_import(&db, "minute10_index", version)?; + let minute30 = ResolutionVecs::forced_import(&db, "minute30_index", version)?; + let hour1 = ResolutionVecs::forced_import(&db, "hour1_index", version)?; + let hour4 = ResolutionVecs::forced_import(&db, "hour4_index", version)?; + let hour12 = ResolutionVecs::forced_import(&db, "hour12_index", version)?; let day1 = Day1Vecs::forced_import(&db, version)?; - let day3 = Day3Vecs::forced_import(&db, version)?; - let week1 = Week1Vecs::forced_import(&db, version)?; - let month1 = Month1Vecs::forced_import(&db, version)?; - let month3 = Month3Vecs::forced_import(&db, version)?; - let month6 = Month6Vecs::forced_import(&db, version)?; - let year1 = Year1Vecs::forced_import(&db, version)?; - let year10 = Year10Vecs::forced_import(&db, version)?; + let day3 = ResolutionVecs::forced_import(&db, "day3_index", version)?; + let week1 = DatedResolutionVecs::forced_import(&db, "week1_index", version)?; + let month1 = DatedResolutionVecs::forced_import(&db, "month1_index", version)?; + let month3 = DatedResolutionVecs::forced_import(&db, "month3_index", version)?; + let month6 = DatedResolutionVecs::forced_import(&db, "month6_index", version)?; + let year1 = DatedResolutionVecs::forced_import(&db, "year1_index", version)?; + let year10 = DatedResolutionVecs::forced_import(&db, "year10_index", version)?; let tx_index = TxIndexVecs::forced_import(&db, version, indexer)?; let txin_index = TxInIndexVecs::forced_import(version, indexer); let txout_index = TxOutIndexVecs::forced_import(version, indexer); - let cached_mappings = CachedMappings { - minute10_first_height: CachedVec::new(&minute10.first_height), - minute30_first_height: CachedVec::new(&minute30.first_height), - hour1_first_height: CachedVec::new(&hour1.first_height), - hour4_first_height: CachedVec::new(&hour4.first_height), - hour12_first_height: CachedVec::new(&hour12.first_height), - day1_first_height: CachedVec::new(&day1.first_height), - day3_first_height: CachedVec::new(&day3.first_height), - week1_first_height: CachedVec::new(&week1.first_height), - month1_first_height: CachedVec::new(&month1.first_height), - month3_first_height: CachedVec::new(&month3.first_height), - month6_first_height: CachedVec::new(&month6.first_height), - year1_first_height: CachedVec::new(&year1.first_height), - year10_first_height: CachedVec::new(&year10.first_height), - halving_identity: CachedVec::new(&halving.identity), - epoch_identity: CachedVec::new(&epoch.identity), - }; - let timestamp = Timestamps::forced_import_from_locals( &db, version, &minute10, &minute30, &hour1, &hour4, &hour12, &day1, &day3, &week1, &month1, &month3, &month6, &year1, &year10, )?; let this = Self { - cached_mappings, tx_heights: TxHeights::init(indexer), addr, height, @@ -200,7 +166,7 @@ impl Vecs { let starting_day1 = self.compute_calendar_mappings(indexer, &starting_indexes, prev_height, exit)?; - self.compute_period_vecs(indexer, &starting_indexes, prev_height, starting_day1, exit)?; + self.compute_period_vecs(&starting_indexes, prev_height, starting_day1, exit)?; self.timestamp.compute_per_resolution( indexer, @@ -380,23 +346,16 @@ impl Vecs { &indexer.vecs.blocks.weight, exit, )?; - self.epoch.first_height.compute_first_per_index( + self.epoch.first_height.inner.compute_first_per_index( starting_indexes.height, &self.height.epoch, exit, )?; - self.epoch.identity.compute_from_index( + self.epoch.identity.inner.compute_from_index( starting_difficulty, &self.epoch.first_height, exit, )?; - self.epoch.height_count.compute_count_from_indexes( - starting_difficulty, - &self.epoch.first_height, - &self.timestamp.monotonic, - exit, - )?; - let starting_halving = self .height .halving @@ -408,12 +367,12 @@ impl Vecs { &indexer.vecs.blocks.weight, exit, )?; - self.halving.first_height.compute_first_per_index( + self.halving.first_height.inner.compute_first_per_index( starting_indexes.height, &self.height.halving, exit, )?; - self.halving.identity.compute_from_index( + self.halving.identity.inner.compute_from_index( starting_halving, &self.halving.first_height, exit, @@ -424,7 +383,6 @@ impl Vecs { fn compute_period_vecs( &mut self, - indexer: &Indexer, starting_indexes: &Indexes, prev_height: Height, starting_day1: Day1, @@ -432,7 +390,7 @@ impl Vecs { ) -> Result<()> { macro_rules! basic_period { ($period:ident) => { - self.$period.first_height.compute_first_per_index( + self.$period.first_height.inner.compute_first_per_index( starting_indexes.height, &self.height.$period, exit, @@ -455,7 +413,7 @@ impl Vecs { basic_period!(hour12); basic_period!(day3); - self.day1.first_height.compute_first_per_index( + self.day1.first_height.inner.compute_first_per_index( starting_indexes.height, &self.height.day1, exit, @@ -469,18 +427,11 @@ impl Vecs { |(di, ..)| (di, Date::from(di)), exit, )?; - self.day1.height_count.compute_count_from_indexes( - starting_day1, - &self.day1.first_height, - &indexer.vecs.blocks.weight, - exit, - )?; - let ts = &self.timestamp.monotonic; macro_rules! dated_period { ($period:ident) => {{ - self.$period.first_height.compute_first_per_index( + self.$period.first_height.inner.compute_first_per_index( starting_indexes.height, &self.height.$period, exit, diff --git a/crates/brk_computer/src/indexes/month1.rs b/crates/brk_computer/src/indexes/month1.rs deleted file mode 100644 index cef66df10..000000000 --- a/crates/brk_computer/src/indexes/month1.rs +++ /dev/null @@ -1,22 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Date, Height, Month1, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub date: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "month1_index", version)?, - date: EagerVec::forced_import(db, "date", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/month3.rs b/crates/brk_computer/src/indexes/month3.rs deleted file mode 100644 index c9cac6ced..000000000 --- a/crates/brk_computer/src/indexes/month3.rs +++ /dev/null @@ -1,22 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Date, Height, Month3, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub date: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "month3_index", version)?, - date: EagerVec::forced_import(db, "date", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/month6.rs b/crates/brk_computer/src/indexes/month6.rs deleted file mode 100644 index 433c22e8a..000000000 --- a/crates/brk_computer/src/indexes/month6.rs +++ /dev/null @@ -1,22 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Date, Height, Month6, Version}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub date: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "month6_index", version)?, - date: EagerVec::forced_import(db, "date", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/resolution.rs b/crates/brk_computer/src/indexes/resolution.rs new file mode 100644 index 000000000..5e5bae8e3 --- /dev/null +++ b/crates/brk_computer/src/indexes/resolution.rs @@ -0,0 +1,68 @@ +use brk_error::Result; +use brk_traversable::Traversable; +use brk_types::{Date, Height, Version}; +use vecdb::{ + CachedVec, Database, EagerVec, ImportableVec, PcoVec, PcoVecValue, Rw, StorageMode, VecIndex, +}; + +/// Resolution with identity mapping and cached first-height lookup. +#[derive(Traversable)] +pub struct ResolutionVecs { + pub identity: M::Stored>>, + pub first_height: CachedVec>>>, +} + +/// Resolution with both identity and first-height cached (halving, epoch). +#[derive(Traversable)] +pub struct CachedResolutionVecs { + pub identity: CachedVec>>>, + pub first_height: CachedVec>>>, +} + +impl CachedResolutionVecs { + pub(crate) fn forced_import( + db: &Database, + identity_name: &str, + version: Version, + ) -> Result { + Ok(Self { + identity: CachedVec::wrap(EagerVec::forced_import(db, identity_name, version)?), + first_height: CachedVec::wrap(EagerVec::forced_import(db, "first_height", version)?), + }) + } +} + +impl ResolutionVecs { + pub(crate) fn forced_import( + db: &Database, + identity_name: &str, + version: Version, + ) -> Result { + Ok(Self { + identity: EagerVec::forced_import(db, identity_name, version)?, + first_height: CachedVec::wrap(EagerVec::forced_import(db, "first_height", version)?), + }) + } +} + +/// Resolution with identity, date, and cached first-height lookup. +#[derive(Traversable)] +pub struct DatedResolutionVecs { + pub identity: M::Stored>>, + pub date: M::Stored>>, + pub first_height: CachedVec>>>, +} + +impl DatedResolutionVecs { + pub(crate) fn forced_import( + db: &Database, + identity_name: &str, + version: Version, + ) -> Result { + Ok(Self { + identity: EagerVec::forced_import(db, identity_name, version)?, + date: EagerVec::forced_import(db, "date", version)?, + first_height: CachedVec::wrap(EagerVec::forced_import(db, "first_height", version)?), + }) + } +} diff --git a/crates/brk_computer/src/indexes/timestamp.rs b/crates/brk_computer/src/indexes/timestamp.rs index 1ad227abb..e1c434a53 100644 --- a/crates/brk_computer/src/indexes/timestamp.rs +++ b/crates/brk_computer/src/indexes/timestamp.rs @@ -6,8 +6,8 @@ use brk_types::{ }; use derive_more::{Deref, DerefMut}; use vecdb::{ - Database, EagerVec, Exit, ImportableVec, LazyVecFrom1, PcoVec, ReadableCloneableVec, - ReadableVec, Rw, StorageMode, Version, + Database, EagerVec, Exit, ImportableVec, LazyVecFrom1, PcoVec, ReadableVec, Rw, StorageMode, + Version, }; use crate::internal::PerResolution; diff --git a/crates/brk_computer/src/indexes/week1.rs b/crates/brk_computer/src/indexes/week1.rs deleted file mode 100644 index 2904b4c05..000000000 --- a/crates/brk_computer/src/indexes/week1.rs +++ /dev/null @@ -1,22 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Date, Height, Version, Week1}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub date: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "week1_index", version)?, - date: EagerVec::forced_import(db, "date", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/year1.rs b/crates/brk_computer/src/indexes/year1.rs deleted file mode 100644 index 65fcc54df..000000000 --- a/crates/brk_computer/src/indexes/year1.rs +++ /dev/null @@ -1,22 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Date, Height, Version, Year1}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub date: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "year1_index", version)?, - date: EagerVec::forced_import(db, "date", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indexes/year10.rs b/crates/brk_computer/src/indexes/year10.rs deleted file mode 100644 index 6b3ec9668..000000000 --- a/crates/brk_computer/src/indexes/year10.rs +++ /dev/null @@ -1,22 +0,0 @@ -use brk_traversable::Traversable; -use brk_types::{Date, Height, Version, Year10}; -use vecdb::{Database, EagerVec, ImportableVec, PcoVec, Rw, StorageMode}; - -use brk_error::Result; - -#[derive(Traversable)] -pub struct Vecs { - pub identity: M::Stored>>, - pub date: M::Stored>>, - pub first_height: M::Stored>>, -} - -impl Vecs { - pub(crate) fn forced_import(db: &Database, version: Version) -> Result { - Ok(Self { - identity: EagerVec::forced_import(db, "year10_index", version)?, - date: EagerVec::forced_import(db, "date", version)?, - first_height: EagerVec::forced_import(db, "first_height", version)?, - }) - } -} diff --git a/crates/brk_computer/src/indicators/realized_envelope.rs b/crates/brk_computer/src/indicators/realized_envelope.rs index 759529f36..41adb3d1d 100644 --- a/crates/brk_computer/src/indicators/realized_envelope.rs +++ b/crates/brk_computer/src/indicators/realized_envelope.rs @@ -123,7 +123,7 @@ impl RealizedEnvelope { exit, )?; - let spot = &prices.cached_spot_cents; + let spot = &prices.spot.cents.height; // Zone: spot vs own envelope bands (-4 to +4) self.compute_index(spot, starting_indexes, exit)?; diff --git a/crates/brk_computer/src/inputs/count/import.rs b/crates/brk_computer/src/inputs/count/import.rs index b1e93868e..ce22ed0ab 100644 --- a/crates/brk_computer/src/inputs/count/import.rs +++ b/crates/brk_computer/src/inputs/count/import.rs @@ -5,7 +5,7 @@ use vecdb::Database; use super::Vecs; use crate::{ indexes, - internal::{CachedWindowStarts, PerBlockAggregated}, + internal::{PerBlockAggregated, WindowStartVec, Windows}, }; impl Vecs { @@ -13,7 +13,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { Ok(Self(PerBlockAggregated::forced_import( db, diff --git a/crates/brk_computer/src/inputs/import.rs b/crates/brk_computer/src/inputs/import.rs index 9b42342aa..e048792db 100644 --- a/crates/brk_computer/src/inputs/import.rs +++ b/crates/brk_computer/src/inputs/import.rs @@ -6,7 +6,7 @@ use brk_types::Version; use crate::{ indexes, internal::{ - CachedWindowStarts, + WindowStartVec, Windows, db_utils::{finalize_db, open_db}, }, }; @@ -18,7 +18,7 @@ impl Vecs { parent_path: &Path, parent_version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let db = open_db(parent_path, super::DB_NAME, 20_000_000)?; let version = parent_version; diff --git a/crates/brk_computer/src/internal/cache_budget.rs b/crates/brk_computer/src/internal/cache_budget.rs index 5c4f62464..37d6a8059 100644 --- a/crates/brk_computer/src/internal/cache_budget.rs +++ b/crates/brk_computer/src/internal/cache_budget.rs @@ -4,7 +4,7 @@ use std::sync::{ }; use parking_lot::Mutex; -use vecdb::{CachedVec, CachedVecBudget, ReadableBoxedVec, VecIndex, VecValue}; +use vecdb::{CachedVec, CachedVecBudget, ReadableVec, TypedVec}; const MAX_CACHED: usize = 256; const MIN_ACCESSES: u64 = 2; @@ -67,10 +67,13 @@ fn evict_less_popular_than(threshold: u64) -> bool { } } -/// Wraps a boxed source in a budgeted [`CachedVec`] and registers it for eviction. -pub fn cache_wrap(source: ReadableBoxedVec) -> CachedVec { +/// Wraps a source vec in a budgeted [`CachedVec`] and registers it for eviction. +pub fn cache_wrap(source: V) -> CachedVec +where + V: TypedVec + ReadableVec + Clone + 'static, +{ let access_count = Arc::new(AtomicU64::new(0)); - let cached = CachedVec::new_budgeted(source, &BUDGET, access_count.clone()); + let cached = CachedVec::wrap_budgeted(source, &BUDGET, access_count.clone()); let clone = cached.clone(); CACHES.lock().push(CacheEntry { access_count, diff --git a/crates/brk_computer/src/internal/containers/windows.rs b/crates/brk_computer/src/internal/containers/windows.rs index 55e5ef868..2b3d96a2d 100644 --- a/crates/brk_computer/src/internal/containers/windows.rs +++ b/crates/brk_computer/src/internal/containers/windows.rs @@ -1,6 +1,6 @@ use brk_traversable::Traversable; -#[derive(Clone, Traversable)] +#[derive(Clone, Copy, Traversable)] pub struct Windows { pub _24h: A, pub _1w: A, diff --git a/crates/brk_computer/src/internal/per_block/amount/base.rs b/crates/brk_computer/src/internal/per_block/amount/base.rs index dd2ea32a6..3b2067882 100644 --- a/crates/brk_computer/src/internal/per_block/amount/base.rs +++ b/crates/brk_computer/src/internal/per_block/amount/base.rs @@ -59,7 +59,7 @@ impl AmountPerBlock { self.cents.compute_binary::( max_from, &self.sats.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, exit, )?; Ok(()) diff --git a/crates/brk_computer/src/internal/per_block/amount/block.rs b/crates/brk_computer/src/internal/per_block/amount/block.rs index cf011f042..e7897bb1c 100644 --- a/crates/brk_computer/src/internal/per_block/amount/block.rs +++ b/crates/brk_computer/src/internal/per_block/amount/block.rs @@ -50,7 +50,7 @@ impl AmountBlock { self.cents.compute_binary::( max_from, &self.sats, - &prices.cached_spot_cents, + &prices.spot.cents.height, exit, )?; Ok(()) diff --git a/crates/brk_computer/src/internal/per_block/amount/cumulative_rolling.rs b/crates/brk_computer/src/internal/per_block/amount/cumulative_rolling.rs index a49eeee99..5e517cf1b 100644 --- a/crates/brk_computer/src/internal/per_block/amount/cumulative_rolling.rs +++ b/crates/brk_computer/src/internal/per_block/amount/cumulative_rolling.rs @@ -7,8 +7,8 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode}; use crate::{ indexes, internal::{ - AmountPerBlockCumulative, CachedWindowStarts, LazyRollingAvgsAmountFromHeight, - LazyRollingSumsAmountFromHeight, + AmountPerBlockCumulative, LazyRollingAvgsAmountFromHeight, LazyRollingSumsAmountFromHeight, + WindowStartVec, Windows, }, prices, }; @@ -31,7 +31,7 @@ impl AmountPerBlockCumulativeRolling { name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let v = version + VERSION; diff --git a/crates/brk_computer/src/internal/per_block/amount/full.rs b/crates/brk_computer/src/internal/per_block/amount/full.rs index 7d40c5538..969a880dc 100644 --- a/crates/brk_computer/src/internal/per_block/amount/full.rs +++ b/crates/brk_computer/src/internal/per_block/amount/full.rs @@ -7,8 +7,8 @@ use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode}; use crate::{ indexes, internal::{ - AmountPerBlockCumulativeRolling, CachedWindowStarts, RollingDistributionAmountPerBlock, - WindowStarts, + AmountPerBlockCumulativeRolling, RollingDistributionAmountPerBlock, WindowStartVec, + WindowStarts, Windows, }, prices, }; @@ -31,7 +31,7 @@ impl AmountPerBlockFull { name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let v = version + VERSION; diff --git a/crates/brk_computer/src/internal/per_block/amount/lazy_rolling_avg.rs b/crates/brk_computer/src/internal/per_block/amount/lazy_rolling_avg.rs index 0c90ba63a..a1dfe34e4 100644 --- a/crates/brk_computer/src/internal/per_block/amount/lazy_rolling_avg.rs +++ b/crates/brk_computer/src/internal/per_block/amount/lazy_rolling_avg.rs @@ -1,13 +1,13 @@ use brk_traversable::Traversable; use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, StoredF32, Version}; use derive_more::{Deref, DerefMut}; -use vecdb::{DeltaAvg, LazyDeltaVec, LazyVecFrom1, ReadableCloneableVec}; +use vecdb::{DeltaAvg, LazyDeltaVec, LazyVecFrom1, ReadOnlyClone, ReadableCloneableVec}; use crate::{ indexes, internal::{ - AvgCentsToUsd, AvgSatsToBtc, CachedWindowStarts, DerivedResolutions, LazyPerBlock, - LazyRollingAvgFromHeight, Resolutions, Windows, + AvgCentsToUsd, AvgSatsToBtc, DerivedResolutions, LazyPerBlock, LazyRollingAvgFromHeight, + Resolutions, WindowStartVec, Windows, }, }; @@ -31,15 +31,15 @@ impl LazyRollingAvgsAmountFromHeight { version: Version, cumulative_sats: &(impl ReadableCloneableVec + 'static), cumulative_cents: &(impl ReadableCloneableVec + 'static), - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, indexes: &indexes::Vecs, ) -> Self { let cum_sats = cumulative_sats.read_only_boxed_clone(); let cum_cents = cumulative_cents.read_only_boxed_clone(); - let make_slot = |suffix: &str, cached_start: &vecdb::CachedVec| { + let make_slot = |suffix: &str, cached_start: &&WindowStartVec| { let full_name = format!("{name}_{suffix}"); - let cached = cached_start.clone(); + let cached = cached_start.read_only_clone(); let starts_version = cached.version(); // Sats lazy rolling avg → f64 @@ -50,12 +50,12 @@ impl LazyRollingAvgsAmountFromHeight { starts_version, { let cached = cached.clone(); - move || cached.get() + move || cached.cached() }, ); let sats_resolutions = Resolutions::forced_import( &format!("{full_name}_sats"), - sats_avg.read_only_boxed_clone(), + sats_avg.clone(), version, indexes, ); @@ -84,11 +84,11 @@ impl LazyRollingAvgsAmountFromHeight { version, cum_cents.clone(), starts_version, - move || cached.get(), + move || cached.cached(), ); let cents_resolutions = Resolutions::forced_import( &format!("{full_name}_cents"), - cents_avg.read_only_boxed_clone(), + cents_avg.clone(), version, indexes, ); @@ -119,6 +119,6 @@ impl LazyRollingAvgsAmountFromHeight { } }; - Self(cached_starts.0.map_with_suffix(make_slot)) + Self(cached_starts.map_with_suffix(make_slot)) } } diff --git a/crates/brk_computer/src/internal/per_block/amount/lazy_rolling_sum.rs b/crates/brk_computer/src/internal/per_block/amount/lazy_rolling_sum.rs index 163037a22..9764ff956 100644 --- a/crates/brk_computer/src/internal/per_block/amount/lazy_rolling_sum.rs +++ b/crates/brk_computer/src/internal/per_block/amount/lazy_rolling_sum.rs @@ -1,13 +1,13 @@ use brk_traversable::Traversable; use brk_types::{Bitcoin, Cents, Dollars, Height, Sats, Version}; use derive_more::{Deref, DerefMut}; -use vecdb::{DeltaSub, LazyDeltaVec, LazyVecFrom1, ReadableCloneableVec}; +use vecdb::{DeltaSub, LazyDeltaVec, LazyVecFrom1, ReadOnlyClone, ReadableCloneableVec}; use crate::{ indexes, internal::{ - CachedWindowStarts, CentsUnsignedToDollars, DerivedResolutions, LazyPerBlock, - LazyRollingSumFromHeight, Resolutions, SatsToBitcoin, Windows, + CentsUnsignedToDollars, DerivedResolutions, LazyPerBlock, LazyRollingSumFromHeight, + Resolutions, SatsToBitcoin, WindowStartVec, Windows, }, }; @@ -31,15 +31,15 @@ impl LazyRollingSumsAmountFromHeight { version: Version, cumulative_sats: &(impl ReadableCloneableVec + 'static), cumulative_cents: &(impl ReadableCloneableVec + 'static), - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, indexes: &indexes::Vecs, ) -> Self { let cum_sats = cumulative_sats.read_only_boxed_clone(); let cum_cents = cumulative_cents.read_only_boxed_clone(); - let make_slot = |suffix: &str, cached_start: &vecdb::CachedVec| { + let make_slot = |suffix: &str, cached_start: &&WindowStartVec| { let full_name = format!("{name}_{suffix}"); - let cached = cached_start.clone(); + let cached = cached_start.read_only_clone(); let starts_version = cached.version(); // Sats lazy rolling sum @@ -50,12 +50,12 @@ impl LazyRollingSumsAmountFromHeight { starts_version, { let cached = cached.clone(); - move || cached.get() + move || cached.cached() }, ); let sats_resolutions = Resolutions::forced_import( &format!("{full_name}_sats"), - sats_sum.read_only_boxed_clone(), + sats_sum.clone(), version, indexes, ); @@ -84,11 +84,11 @@ impl LazyRollingSumsAmountFromHeight { version, cum_cents.clone(), starts_version, - move || cached.get(), + move || cached.cached(), ); let cents_resolutions = Resolutions::forced_import( &format!("{full_name}_cents"), - cents_sum.read_only_boxed_clone(), + cents_sum.clone(), version, indexes, ); @@ -119,6 +119,6 @@ impl LazyRollingSumsAmountFromHeight { } }; - Self(cached_starts.0.map_with_suffix(make_slot)) + Self(cached_starts.map_with_suffix(make_slot)) } } diff --git a/crates/brk_computer/src/internal/per_block/amount/with_deltas.rs b/crates/brk_computer/src/internal/per_block/amount/with_deltas.rs index 2b22a63e1..05043d570 100644 --- a/crates/brk_computer/src/internal/per_block/amount/with_deltas.rs +++ b/crates/brk_computer/src/internal/per_block/amount/with_deltas.rs @@ -6,7 +6,7 @@ use vecdb::{Database, Rw, StorageMode}; use crate::{ indexes, - internal::{AmountPerBlock, CachedWindowStarts, LazyRollingDeltasFromHeight}, + internal::{AmountPerBlock, LazyRollingDeltasFromHeight, WindowStartVec, Windows}, }; #[derive(Deref, DerefMut, Traversable)] @@ -24,7 +24,7 @@ impl AmountPerBlockWithDeltas { name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let inner = AmountPerBlock::forced_import(db, name, version, indexes)?; diff --git a/crates/brk_computer/src/internal/per_block/computed/aggregated.rs b/crates/brk_computer/src/internal/per_block/computed/aggregated.rs index 4b9f7b3f1..d001911fb 100644 --- a/crates/brk_computer/src/internal/per_block/computed/aggregated.rs +++ b/crates/brk_computer/src/internal/per_block/computed/aggregated.rs @@ -9,7 +9,7 @@ use vecdb::{ use crate::{ indexes, - internal::{CachedWindowStarts, NumericValue, PerBlock, RollingComplete, WindowStarts}, + internal::{NumericValue, PerBlock, RollingComplete, WindowStartVec, WindowStarts, Windows}, }; #[derive(Traversable)] @@ -31,7 +31,7 @@ where name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let sum = EagerVec::forced_import(db, &format!("{name}_sum"), version)?; let cumulative = diff --git a/crates/brk_computer/src/internal/per_block/computed/base.rs b/crates/brk_computer/src/internal/per_block/computed/base.rs index a0b8419a0..1bb086808 100644 --- a/crates/brk_computer/src/internal/per_block/computed/base.rs +++ b/crates/brk_computer/src/internal/per_block/computed/base.rs @@ -5,8 +5,8 @@ use brk_types::{Height, Version}; use derive_more::{Deref, DerefMut}; use schemars::JsonSchema; use vecdb::{ - BinaryTransform, Database, EagerVec, Exit, ImportableVec, PcoVec, ReadableCloneableVec, - ReadableVec, Rw, StorageMode, VecValue, + BinaryTransform, Database, EagerVec, Exit, ImportableVec, PcoVec, ReadOnlyClone, ReadableVec, + Rw, StorageMode, VecValue, }; use crate::indexes; @@ -39,7 +39,7 @@ where let height: EagerVec> = EagerVec::forced_import(db, name, version)?; let resolutions = - Resolutions::forced_import(name, height.read_only_boxed_clone(), version, indexes); + Resolutions::forced_import(name, height.read_only_clone(), version, indexes); Ok(Self { height, diff --git a/crates/brk_computer/src/internal/per_block/computed/cached.rs b/crates/brk_computer/src/internal/per_block/computed/cached.rs new file mode 100644 index 000000000..fe5d2806b --- /dev/null +++ b/crates/brk_computer/src/internal/per_block/computed/cached.rs @@ -0,0 +1,48 @@ +use brk_error::Result; +use brk_traversable::Traversable; +use brk_types::{Height, Version}; +use derive_more::{Deref, DerefMut}; +use schemars::JsonSchema; +use vecdb::{CachedVec, Database, EagerVec, ImportableVec, PcoVec, ReadOnlyClone, Rw, StorageMode}; + +use crate::{ + indexes, + internal::{NumericValue, Resolutions}, +}; + +/// Like [`PerBlock`](super::PerBlock) but with height wrapped in [`CachedVec`] +/// for fast repeated reads. +#[derive(Deref, DerefMut, Traversable)] +#[traversable(merge)] +pub struct CachedPerBlock +where + T: NumericValue + JsonSchema, +{ + pub height: CachedVec>>>, + #[deref] + #[deref_mut] + #[traversable(flatten)] + pub resolutions: Box>, +} + +impl CachedPerBlock +where + T: NumericValue + JsonSchema, +{ + pub(crate) fn forced_import( + db: &Database, + name: &str, + version: Version, + indexes: &indexes::Vecs, + ) -> Result { + let height: EagerVec> = EagerVec::forced_import(db, name, version)?; + + let resolutions = + Resolutions::forced_import(name, height.read_only_clone(), version, indexes); + + Ok(Self { + height: CachedVec::wrap(height), + resolutions: Box::new(resolutions), + }) + } +} diff --git a/crates/brk_computer/src/internal/per_block/computed/cumulative_rolling.rs b/crates/brk_computer/src/internal/per_block/computed/cumulative_rolling.rs index dd5e1aa26..7a45a6bf1 100644 --- a/crates/brk_computer/src/internal/per_block/computed/cumulative_rolling.rs +++ b/crates/brk_computer/src/internal/per_block/computed/cumulative_rolling.rs @@ -18,8 +18,8 @@ use vecdb::{Database, EagerVec, Exit, ImportableVec, PcoVec, Rw, StorageMode}; use crate::{ indexes, internal::{ - CachedWindowStarts, LazyRollingAvgsFromHeight, LazyRollingSumsFromHeight, NumericValue, - PerBlock, + LazyRollingAvgsFromHeight, LazyRollingSumsFromHeight, NumericValue, PerBlock, + WindowStartVec, Windows, }, }; @@ -45,7 +45,7 @@ where name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let block = EagerVec::forced_import(db, name, version)?; let cumulative = diff --git a/crates/brk_computer/src/internal/per_block/computed/full.rs b/crates/brk_computer/src/internal/per_block/computed/full.rs index 761c1166f..b1364522e 100644 --- a/crates/brk_computer/src/internal/per_block/computed/full.rs +++ b/crates/brk_computer/src/internal/per_block/computed/full.rs @@ -10,7 +10,7 @@ use vecdb::{Database, EagerVec, Exit, ImportableVec, PcoVec, Rw, StorageMode}; use crate::{ indexes, - internal::{CachedWindowStarts, NumericValue, PerBlock, RollingComplete, WindowStarts}, + internal::{NumericValue, PerBlock, RollingComplete, WindowStartVec, WindowStarts, Windows}, }; #[derive(Traversable)] @@ -33,7 +33,7 @@ where name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let block = EagerVec::forced_import(db, name, version)?; let cumulative = diff --git a/crates/brk_computer/src/internal/per_block/computed/lazy_rolling.rs b/crates/brk_computer/src/internal/per_block/computed/lazy_rolling.rs index 0864a94d5..5060924e2 100644 --- a/crates/brk_computer/src/internal/per_block/computed/lazy_rolling.rs +++ b/crates/brk_computer/src/internal/per_block/computed/lazy_rolling.rs @@ -6,8 +6,8 @@ use vecdb::{ReadableCloneableVec, UnaryTransform}; use crate::{ indexes, internal::{ - CachedWindowStarts, ComputedVecValue, LazyPerBlock, LazyRollingComplete, NumericValue, - PerBlockFull, + ComputedVecValue, LazyPerBlock, LazyRollingComplete, NumericValue, PerBlockFull, + WindowStartVec, Windows, }, }; @@ -33,7 +33,7 @@ where name: &str, version: Version, source: &PerBlockFull, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, indexes: &indexes::Vecs, ) -> Self { let cumulative = LazyPerBlock::from_computed::( diff --git a/crates/brk_computer/src/internal/per_block/computed/mod.rs b/crates/brk_computer/src/internal/per_block/computed/mod.rs index 136fa071b..0945878af 100644 --- a/crates/brk_computer/src/internal/per_block/computed/mod.rs +++ b/crates/brk_computer/src/internal/per_block/computed/mod.rs @@ -1,5 +1,6 @@ mod aggregated; mod base; +mod cached; mod cumulative_rolling; mod distribution; mod full; @@ -12,6 +13,7 @@ mod with_deltas; pub use aggregated::*; pub use base::*; +pub use cached::*; pub use cumulative_rolling::*; pub use distribution::*; pub use full::*; diff --git a/crates/brk_computer/src/internal/per_block/computed/resolutions.rs b/crates/brk_computer/src/internal/per_block/computed/resolutions.rs index bc733f435..c679e0c24 100644 --- a/crates/brk_computer/src/internal/per_block/computed/resolutions.rs +++ b/crates/brk_computer/src/internal/per_block/computed/resolutions.rs @@ -8,8 +8,8 @@ use brk_types::{ use derive_more::{Deref, DerefMut}; use schemars::JsonSchema; use vecdb::{ - AggFold, LazyAggVec, ReadOnlyClone, ReadableBoxedVec, ReadableCloneableVec, ReadableVec, - VecIndex, VecValue, + AggFold, LazyAggVec, ReadOnlyClone, ReadableCloneableVec, ReadableVec, TypedVec, VecIndex, + VecValue, }; use crate::{ @@ -102,47 +102,48 @@ impl Resolutions where T: NumericValue + JsonSchema, { - pub(crate) fn forced_import( + pub(crate) fn forced_import( name: &str, - height_source: ReadableBoxedVec, + height_source: V, version: Version, indexes: &indexes::Vecs, - ) -> Self { + ) -> Self + where + V: TypedVec + ReadableVec + Clone + 'static, + { let cached = cache_wrap(height_source); let height_source = cached.read_only_boxed_clone(); - let cm = &indexes.cached_mappings; - macro_rules! res { - ($cached:expr) => {{ - let cached = $cached.clone(); + ($field:expr) => {{ + let cached = $field.read_only_clone(); let mapping_version = cached.version(); LazyAggVec::new( name, version, mapping_version, height_source.clone(), - move || cached.get(), + move || cached.cached(), ) }}; } Self(PerResolution { - minute10: res!(cm.minute10_first_height), - minute30: res!(cm.minute30_first_height), - hour1: res!(cm.hour1_first_height), - hour4: res!(cm.hour4_first_height), - hour12: res!(cm.hour12_first_height), - day1: res!(cm.day1_first_height), - day3: res!(cm.day3_first_height), - week1: res!(cm.week1_first_height), - month1: res!(cm.month1_first_height), - month3: res!(cm.month3_first_height), - month6: res!(cm.month6_first_height), - year1: res!(cm.year1_first_height), - year10: res!(cm.year10_first_height), - halving: res!(cm.halving_identity), - epoch: res!(cm.epoch_identity), + minute10: res!(indexes.minute10.first_height), + minute30: res!(indexes.minute30.first_height), + hour1: res!(indexes.hour1.first_height), + hour4: res!(indexes.hour4.first_height), + hour12: res!(indexes.hour12.first_height), + day1: res!(indexes.day1.first_height), + day3: res!(indexes.day3.first_height), + week1: res!(indexes.week1.first_height), + month1: res!(indexes.month1.first_height), + month3: res!(indexes.month3.first_height), + month6: res!(indexes.month6.first_height), + year1: res!(indexes.year1.first_height), + year10: res!(indexes.year10.first_height), + halving: res!(indexes.halving.identity), + epoch: res!(indexes.epoch.identity), }) } } diff --git a/crates/brk_computer/src/internal/per_block/computed/rolling.rs b/crates/brk_computer/src/internal/per_block/computed/rolling.rs index 2a1d0d894..8ce1c6e29 100644 --- a/crates/brk_computer/src/internal/per_block/computed/rolling.rs +++ b/crates/brk_computer/src/internal/per_block/computed/rolling.rs @@ -11,7 +11,7 @@ use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode}; use crate::{ indexes, - internal::{CachedWindowStarts, NumericValue, PerBlock, RollingComplete, WindowStarts}, + internal::{NumericValue, PerBlock, RollingComplete, WindowStartVec, WindowStarts, Windows}, }; #[derive(Traversable)] @@ -33,7 +33,7 @@ where name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let cumulative = PerBlock::forced_import(db, &format!("{name}_cumulative"), version, indexes)?; diff --git a/crates/brk_computer/src/internal/per_block/computed/rolling_average.rs b/crates/brk_computer/src/internal/per_block/computed/rolling_average.rs index 6f0bb03c0..b43fbf8af 100644 --- a/crates/brk_computer/src/internal/per_block/computed/rolling_average.rs +++ b/crates/brk_computer/src/internal/per_block/computed/rolling_average.rs @@ -12,7 +12,7 @@ use vecdb::{Database, EagerVec, Exit, ImportableVec, PcoVec, Rw, StorageMode}; use crate::indexes; -use crate::internal::{CachedWindowStarts, LazyRollingAvgsFromHeight, NumericValue}; +use crate::internal::{LazyRollingAvgsFromHeight, NumericValue, WindowStartVec, Windows}; #[derive(Traversable)] pub struct PerBlockRollingAverage @@ -35,7 +35,7 @@ where name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let block: EagerVec> = EagerVec::forced_import(db, name, version)?; let cumulative: EagerVec> = diff --git a/crates/brk_computer/src/internal/per_block/computed/with_deltas.rs b/crates/brk_computer/src/internal/per_block/computed/with_deltas.rs index 50a874cc0..087089ecd 100644 --- a/crates/brk_computer/src/internal/per_block/computed/with_deltas.rs +++ b/crates/brk_computer/src/internal/per_block/computed/with_deltas.rs @@ -7,7 +7,9 @@ use vecdb::{Rw, StorageMode}; use crate::{ indexes, - internal::{BpsType, CachedWindowStarts, LazyRollingDeltasFromHeight, NumericValue, PerBlock}, + internal::{ + BpsType, LazyRollingDeltasFromHeight, NumericValue, PerBlock, WindowStartVec, Windows, + }, }; #[derive(Deref, DerefMut, Traversable)] @@ -35,7 +37,7 @@ where version: Version, delta_version_offset: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let base = PerBlock::forced_import(db, name, version, indexes)?; diff --git a/crates/brk_computer/src/internal/per_block/fiat/cumulative_sum.rs b/crates/brk_computer/src/internal/per_block/fiat/cumulative_sum.rs index c2e3c416c..3b48e6892 100644 --- a/crates/brk_computer/src/internal/per_block/fiat/cumulative_sum.rs +++ b/crates/brk_computer/src/internal/per_block/fiat/cumulative_sum.rs @@ -6,7 +6,7 @@ use vecdb::{Database, Exit, Rw, StorageMode}; use crate::{ indexes, internal::{ - CachedWindowStarts, CentsType, FiatBlock, FiatPerBlock, LazyRollingSumsFiatFromHeight, + CentsType, FiatBlock, FiatPerBlock, LazyRollingSumsFiatFromHeight, WindowStartVec, Windows, }, }; @@ -23,7 +23,7 @@ impl FiatPerBlockCumulativeWithSums { name: &str, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let block = FiatBlock::forced_import(db, name, version)?; let cumulative = diff --git a/crates/brk_computer/src/internal/per_block/fiat/cumulative_sum_with_deltas.rs b/crates/brk_computer/src/internal/per_block/fiat/cumulative_sum_with_deltas.rs index fff0aac64..985993514 100644 --- a/crates/brk_computer/src/internal/per_block/fiat/cumulative_sum_with_deltas.rs +++ b/crates/brk_computer/src/internal/per_block/fiat/cumulative_sum_with_deltas.rs @@ -6,7 +6,7 @@ use vecdb::{Database, Rw, StorageMode}; use crate::{ indexes, - internal::{BpsType, CachedWindowStarts, LazyRollingDeltasFiatFromHeight}, + internal::{BpsType, LazyRollingDeltasFiatFromHeight, WindowStartVec, Windows}, }; use super::{CentsType, FiatPerBlockCumulativeWithSums}; @@ -37,7 +37,7 @@ where version: Version, delta_version_offset: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let inner = FiatPerBlockCumulativeWithSums::forced_import( db, diff --git a/crates/brk_computer/src/internal/per_block/fiat/lazy_rolling_sum.rs b/crates/brk_computer/src/internal/per_block/fiat/lazy_rolling_sum.rs index aabcb4a15..47c4dc874 100644 --- a/crates/brk_computer/src/internal/per_block/fiat/lazy_rolling_sum.rs +++ b/crates/brk_computer/src/internal/per_block/fiat/lazy_rolling_sum.rs @@ -1,13 +1,13 @@ use brk_traversable::Traversable; use brk_types::{Dollars, Height, Version}; use derive_more::{Deref, DerefMut}; -use vecdb::{DeltaSub, LazyDeltaVec, LazyVecFrom1, ReadableCloneableVec}; +use vecdb::{DeltaSub, LazyDeltaVec, LazyVecFrom1, ReadOnlyClone, ReadableCloneableVec}; use crate::{ indexes, internal::{ - CachedWindowStarts, CentsType, DerivedResolutions, LazyPerBlock, LazyRollingSumFromHeight, - Resolutions, Windows, + CentsType, DerivedResolutions, LazyPerBlock, LazyRollingSumFromHeight, Resolutions, + WindowStartVec, Windows, }, }; @@ -28,14 +28,14 @@ impl LazyRollingSumsFiatFromHeight { name: &str, version: Version, cumulative_cents: &(impl ReadableCloneableVec + 'static), - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, indexes: &indexes::Vecs, ) -> Self { let cum_cents = cumulative_cents.read_only_boxed_clone(); - let make_slot = |suffix: &str, cached_start: &vecdb::CachedVec| { + let make_slot = |suffix: &str, cached_start: &&WindowStartVec| { let full_name = format!("{name}_{suffix}"); - let cached = cached_start.clone(); + let cached = cached_start.read_only_clone(); let starts_version = cached.version(); let cents_sum = LazyDeltaVec::::new( @@ -43,11 +43,11 @@ impl LazyRollingSumsFiatFromHeight { version, cum_cents.clone(), starts_version, - move || cached.get(), + move || cached.cached(), ); let cents_resolutions = Resolutions::forced_import( &format!("{full_name}_cents"), - cents_sum.read_only_boxed_clone(), + cents_sum.clone(), version, indexes, ); @@ -72,6 +72,6 @@ impl LazyRollingSumsFiatFromHeight { LazyRollingSumFiatFromHeight { usd, cents } }; - Self(cached_starts.0.map_with_suffix(make_slot)) + Self(cached_starts.map_with_suffix(make_slot)) } } diff --git a/crates/brk_computer/src/internal/per_block/fiat/with_deltas.rs b/crates/brk_computer/src/internal/per_block/fiat/with_deltas.rs index f85338e08..196e428fc 100644 --- a/crates/brk_computer/src/internal/per_block/fiat/with_deltas.rs +++ b/crates/brk_computer/src/internal/per_block/fiat/with_deltas.rs @@ -7,7 +7,7 @@ use vecdb::{Database, Rw, StorageMode}; use crate::{ indexes, - internal::{BpsType, CachedWindowStarts, LazyRollingDeltasFiatFromHeight}, + internal::{BpsType, LazyRollingDeltasFiatFromHeight, WindowStartVec, Windows}, }; use super::{CentsType, FiatPerBlock}; @@ -38,7 +38,7 @@ where version: Version, delta_version_offset: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let inner = FiatPerBlock::forced_import(db, name, version, indexes)?; diff --git a/crates/brk_computer/src/internal/per_block/lazy/base.rs b/crates/brk_computer/src/internal/per_block/lazy/base.rs index 72a8618c9..d8aeeeff9 100644 --- a/crates/brk_computer/src/internal/per_block/lazy/base.rs +++ b/crates/brk_computer/src/internal/per_block/lazy/base.rs @@ -2,11 +2,16 @@ use brk_traversable::Traversable; use brk_types::{Height, Version}; use derive_more::{Deref, DerefMut}; use schemars::JsonSchema; -use vecdb::{LazyVecFrom1, ReadOnlyClone, ReadableBoxedVec, ReadableCloneableVec, UnaryTransform}; +use vecdb::{ + LazyVecFrom1, ReadOnlyClone, ReadableBoxedVec, ReadableCloneableVec, ReadableVec, TypedVec, + UnaryTransform, +}; use crate::{ indexes, - internal::{ComputedVecValue, DerivedResolutions, NumericValue, PerBlock}, + internal::{ + CachedPerBlock, ComputedVecValue, DerivedResolutions, NumericValue, PerBlock, Resolutions, + }, }; #[derive(Clone, Deref, DerefMut, Traversable)] #[traversable(merge)] @@ -27,6 +32,23 @@ where T: ComputedVecValue + JsonSchema + 'static, S1T: ComputedVecValue + JsonSchema, { + pub(crate) fn from_resolutions>( + name: &str, + version: Version, + height_source: ReadableBoxedVec, + resolutions: &Resolutions, + ) -> Self + where + S1T: NumericValue, + { + Self { + height: LazyVecFrom1::transformed::(name, version, height_source), + resolutions: Box::new(DerivedResolutions::from_derived_computed::( + name, version, resolutions, + )), + } + } + pub(crate) fn from_computed>( name: &str, version: Version, @@ -36,26 +58,38 @@ where where S1T: NumericValue, { - Self { - height: LazyVecFrom1::transformed::(name, version, height_source), - resolutions: Box::new(DerivedResolutions::from_computed::( - name, version, source, - )), - } + Self::from_resolutions::(name, version, height_source, &source.resolutions) } - pub(crate) fn from_height_source>( + pub(crate) fn from_cached_computed>( name: &str, version: Version, height_source: ReadableBoxedVec, - indexes: &indexes::Vecs, + source: &CachedPerBlock, ) -> Self where S1T: NumericValue, + { + Self::from_resolutions::(name, version, height_source, &source.resolutions) + } + + pub(crate) fn from_height_source, V>( + name: &str, + version: Version, + height_source: V, + indexes: &indexes::Vecs, + ) -> Self + where + S1T: NumericValue, + V: TypedVec + ReadableVec + Clone + 'static, { Self { - height: LazyVecFrom1::transformed::(name, version, height_source.clone()), - resolutions: Box::new(DerivedResolutions::from_height_source::( + height: LazyVecFrom1::transformed::( + name, + version, + height_source.read_only_boxed_clone(), + ), + resolutions: Box::new(DerivedResolutions::from_height_source::( name, version, height_source, diff --git a/crates/brk_computer/src/internal/per_block/lazy/derived_resolutions.rs b/crates/brk_computer/src/internal/per_block/lazy/derived_resolutions.rs index fedf327a8..7e0922e42 100644 --- a/crates/brk_computer/src/internal/per_block/lazy/derived_resolutions.rs +++ b/crates/brk_computer/src/internal/per_block/lazy/derived_resolutions.rs @@ -5,11 +5,11 @@ use brk_types::{ }; use derive_more::{Deref, DerefMut}; use schemars::JsonSchema; -use vecdb::{ReadableBoxedVec, ReadableCloneableVec, UnaryTransform, VecValue}; +use vecdb::{ReadableCloneableVec, ReadableVec, TypedVec, UnaryTransform, VecValue}; use crate::{ indexes, - internal::{ComputedVecValue, NumericValue, PerBlock, PerResolution, Resolutions}, + internal::{ComputedVecValue, NumericValue, PerResolution, Resolutions}, }; use super::{LazyTransformLast, MapOption}; @@ -45,25 +45,15 @@ where T: VecValue + PartialOrd + JsonSchema + 'static, S1T: VecValue + PartialOrd + JsonSchema, { - pub(crate) fn from_computed>( + pub(crate) fn from_height_source, V>( name: &str, version: Version, - source: &PerBlock, - ) -> Self - where - S1T: NumericValue, - { - Self::from_derived_computed::(name, version, &source.resolutions) - } - - pub(crate) fn from_height_source>( - name: &str, - version: Version, - height_source: ReadableBoxedVec, + height_source: V, indexes: &indexes::Vecs, ) -> Self where S1T: NumericValue, + V: TypedVec + ReadableVec + Clone + 'static, { let derived = Resolutions::forced_import(name, height_source, version, indexes); Self::from_derived_computed::(name, version, &derived) diff --git a/crates/brk_computer/src/internal/per_block/ratio/price_extended.rs b/crates/brk_computer/src/internal/per_block/ratio/price_extended.rs index c7c913c3d..13fe4a799 100644 --- a/crates/brk_computer/src/internal/per_block/ratio/price_extended.rs +++ b/crates/brk_computer/src/internal/per_block/ratio/price_extended.rs @@ -71,7 +71,7 @@ impl PriceWithRatioPerBlock { F: FnMut(&mut EagerVec>) -> Result<()>, { compute_price(&mut self.cents.height)?; - self.compute_ratio(starting_indexes, &prices.cached_spot_cents, exit) + self.compute_ratio(starting_indexes, &prices.spot.cents.height, exit) } } @@ -104,7 +104,7 @@ impl PriceWithRatioExtendedPerBlock { starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let close_price = &prices.cached_spot_cents; + let close_price = &prices.spot.cents.height; self.base .compute_ratio(starting_indexes, close_price, exit)?; self.percentiles.compute( diff --git a/crates/brk_computer/src/internal/per_block/ratio/sma.rs b/crates/brk_computer/src/internal/per_block/ratio/sma.rs index 326042849..f9097acfe 100644 --- a/crates/brk_computer/src/internal/per_block/ratio/sma.rs +++ b/crates/brk_computer/src/internal/per_block/ratio/sma.rs @@ -67,9 +67,9 @@ impl RatioSma { // Rolling SMAs for (sma, lookback) in [ - (&mut self._1w, &blocks.lookback._1w), - (&mut self._1m, &blocks.lookback._1m), - (&mut self._1y, &blocks.lookback._1y), + (&mut self._1w, &blocks.lookback._1w.inner), + (&mut self._1m, &blocks.lookback._1m.inner), + (&mut self._1y, &blocks.lookback._1y.inner), (&mut self._2y, &blocks.lookback._2y), (&mut self._4y, &blocks.lookback._4y), ] { diff --git a/crates/brk_computer/src/internal/per_block/rolling/avgs.rs b/crates/brk_computer/src/internal/per_block/rolling/avgs.rs index a687c497c..74b13d596 100644 --- a/crates/brk_computer/src/internal/per_block/rolling/avgs.rs +++ b/crates/brk_computer/src/internal/per_block/rolling/avgs.rs @@ -2,11 +2,11 @@ use brk_traversable::Traversable; use brk_types::{Height, StoredF32, Version}; use derive_more::{Deref, DerefMut}; use schemars::JsonSchema; -use vecdb::{DeltaAvg, LazyDeltaVec, ReadableCloneableVec}; +use vecdb::{DeltaAvg, LazyDeltaVec, ReadOnlyClone, ReadableCloneableVec}; use crate::{ indexes, - internal::{CachedWindowStarts, NumericValue, Resolutions, Windows}, + internal::{NumericValue, Resolutions, WindowStartVec, Windows}, }; use super::LazyRollingAvgFromHeight; @@ -30,25 +30,25 @@ where name: &str, version: Version, cumulative: &(impl ReadableCloneableVec + 'static), - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, indexes: &indexes::Vecs, ) -> Self { let cum_source = cumulative.read_only_boxed_clone(); - Self(cached_starts.0.map_with_suffix(|suffix, cached_start| { + Self(cached_starts.map_with_suffix(|suffix, cached_start| { let full_name = format!("{name}_{suffix}"); - let cached = cached_start.clone(); + let cached = cached_start.read_only_clone(); let starts_version = cached.version(); let avg = LazyDeltaVec::::new( &full_name, version, cum_source.clone(), starts_version, - move || cached.get(), + move || cached.cached(), ); let resolutions = Resolutions::forced_import( &full_name, - avg.read_only_boxed_clone(), + avg.clone(), version, indexes, ); diff --git a/crates/brk_computer/src/internal/per_block/rolling/complete.rs b/crates/brk_computer/src/internal/per_block/rolling/complete.rs index 2d4d7f783..ab73b9672 100644 --- a/crates/brk_computer/src/internal/per_block/rolling/complete.rs +++ b/crates/brk_computer/src/internal/per_block/rolling/complete.rs @@ -10,8 +10,8 @@ use vecdb::{Database, Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode}; use crate::{ indexes, internal::{ - CachedWindowStarts, LazyRollingAvgsFromHeight, LazyRollingSumsFromHeight, NumericValue, - RollingDistribution, WindowStarts, + LazyRollingAvgsFromHeight, LazyRollingSumsFromHeight, NumericValue, RollingDistribution, + WindowStartVec, WindowStarts, Windows, }, }; @@ -37,7 +37,7 @@ where version: Version, indexes: &indexes::Vecs, cumulative: &(impl ReadableCloneableVec + 'static), - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let sum = LazyRollingSumsFromHeight::new( &format!("{name}_sum"), diff --git a/crates/brk_computer/src/internal/per_block/rolling/delta.rs b/crates/brk_computer/src/internal/per_block/rolling/delta.rs index 371f9b695..270ce072e 100644 --- a/crates/brk_computer/src/internal/per_block/rolling/delta.rs +++ b/crates/brk_computer/src/internal/per_block/rolling/delta.rs @@ -2,13 +2,16 @@ use brk_traversable::Traversable; use brk_types::{Dollars, Height, StoredF32, Version}; use derive_more::{Deref, DerefMut}; use schemars::JsonSchema; -use vecdb::{DeltaChange, DeltaRate, LazyDeltaVec, LazyVecFrom1, ReadableCloneableVec, VecValue}; +use vecdb::{ + DeltaChange, DeltaRate, LazyDeltaVec, LazyVecFrom1, ReadOnlyClone, ReadableCloneableVec, + VecValue, +}; use crate::{ indexes, internal::{ - BpsType, CachedWindowStarts, CentsType, DerivedResolutions, LazyPerBlock, NumericValue, - Percent, Resolutions, Windows, + BpsType, CentsType, DerivedResolutions, LazyPerBlock, NumericValue, Percent, Resolutions, + WindowStartVec, Windows, }, }; @@ -68,14 +71,14 @@ where name: &str, version: Version, source: &(impl ReadableCloneableVec + 'static), - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, indexes: &indexes::Vecs, ) -> Self { let src = source.read_only_boxed_clone(); - let make_slot = |suffix: &str, cached_start: &vecdb::CachedVec| { + let make_slot = |suffix: &str, cached_start: &&WindowStartVec| { let full_name = format!("{name}_{suffix}"); - let cached = cached_start.clone(); + let cached = cached_start.read_only_clone(); let starts_version = cached.version(); // Absolute change: source[h] - source[ago] as C (via f64) @@ -86,15 +89,11 @@ where starts_version, { let cached = cached.clone(); - move || cached.get() + move || cached.cached() }, ); - let change_resolutions = Resolutions::forced_import( - &full_name, - change_vec.read_only_boxed_clone(), - version, - indexes, - ); + let change_resolutions = + Resolutions::forced_import(&full_name, change_vec.clone(), version, indexes); let absolute = LazyDeltaFromHeight { height: change_vec, resolutions: Box::new(change_resolutions), @@ -107,14 +106,10 @@ where version, src.clone(), starts_version, - move || cached.get(), - ); - let rate_resolutions = Resolutions::forced_import( - &rate_bps_name, - rate_vec.read_only_boxed_clone(), - version, - indexes, + move || cached.cached(), ); + let rate_resolutions = + Resolutions::forced_import(&rate_bps_name, rate_vec.clone(), version, indexes); let bps = LazyDeltaFromHeight { height: rate_vec, resolutions: Box::new(rate_resolutions), @@ -159,7 +154,7 @@ where (absolute, rate) }; - let (absolute, rate) = cached_starts.0.map_with_suffix(make_slot).unzip(); + let (absolute, rate) = cached_starts.map_with_suffix(make_slot).unzip(); Self { absolute, rate } } @@ -206,14 +201,14 @@ where name: &str, version: Version, source: &(impl ReadableCloneableVec + 'static), - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, indexes: &indexes::Vecs, ) -> Self { let src = source.read_only_boxed_clone(); - let make_slot = |suffix: &str, cached_start: &vecdb::CachedVec| { + let make_slot = |suffix: &str, cached_start: &&WindowStartVec| { let full_name = format!("{name}_{suffix}"); - let cached = cached_start.clone(); + let cached = cached_start.read_only_clone(); let starts_version = cached.version(); // Absolute change (cents): source[h] - source[ago] as C (via f64) @@ -225,12 +220,12 @@ where starts_version, { let cached = cached.clone(); - move || cached.get() + move || cached.cached() }, ); let change_resolutions = Resolutions::forced_import( ¢s_name, - change_vec.read_only_boxed_clone(), + change_vec.clone(), version, indexes, ); @@ -262,14 +257,10 @@ where version, src.clone(), starts_version, - move || cached.get(), - ); - let rate_resolutions = Resolutions::forced_import( - &rate_bps_name, - rate_vec.read_only_boxed_clone(), - version, - indexes, + move || cached.cached(), ); + let rate_resolutions = + Resolutions::forced_import(&rate_bps_name, rate_vec.clone(), version, indexes); let bps = LazyDeltaFromHeight { height: rate_vec, resolutions: Box::new(rate_resolutions), @@ -312,7 +303,7 @@ where (absolute, rate) }; - let (absolute, rate) = cached_starts.0.map_with_suffix(make_slot).unzip(); + let (absolute, rate) = cached_starts.map_with_suffix(make_slot).unzip(); Self { absolute, rate } } diff --git a/crates/brk_computer/src/internal/per_block/rolling/lazy_complete.rs b/crates/brk_computer/src/internal/per_block/rolling/lazy_complete.rs index 52a3b0919..5d8df24e1 100644 --- a/crates/brk_computer/src/internal/per_block/rolling/lazy_complete.rs +++ b/crates/brk_computer/src/internal/per_block/rolling/lazy_complete.rs @@ -6,8 +6,8 @@ use vecdb::{ReadableCloneableVec, UnaryTransform}; use crate::{ indexes, internal::{ - CachedWindowStarts, ComputedVecValue, LazyRollingAvgsFromHeight, LazyRollingDistribution, - LazyRollingSumsFromHeight, NumericValue, RollingComplete, + ComputedVecValue, LazyRollingAvgsFromHeight, LazyRollingDistribution, + LazyRollingSumsFromHeight, NumericValue, RollingComplete, WindowStartVec, Windows, }, }; @@ -35,7 +35,7 @@ where version: Version, cumulative: &(impl ReadableCloneableVec + 'static), source: &RollingComplete, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, indexes: &indexes::Vecs, ) -> Self { let sum = LazyRollingSumsFromHeight::new( diff --git a/crates/brk_computer/src/internal/per_block/rolling/sums.rs b/crates/brk_computer/src/internal/per_block/rolling/sums.rs index 376f4f4e9..5008944ea 100644 --- a/crates/brk_computer/src/internal/per_block/rolling/sums.rs +++ b/crates/brk_computer/src/internal/per_block/rolling/sums.rs @@ -2,11 +2,11 @@ use brk_traversable::Traversable; use brk_types::{Height, Version}; use derive_more::{Deref, DerefMut}; use schemars::JsonSchema; -use vecdb::{DeltaSub, LazyDeltaVec, ReadableCloneableVec}; +use vecdb::{DeltaSub, LazyDeltaVec, ReadOnlyClone, ReadableCloneableVec}; use crate::{ indexes, - internal::{CachedWindowStarts, NumericValue, Resolutions, Windows}, + internal::{NumericValue, Resolutions, WindowStartVec, Windows}, }; use super::LazyRollingSumFromHeight; @@ -33,28 +33,23 @@ where name: &str, version: Version, cumulative: &(impl ReadableCloneableVec + 'static), - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, indexes: &indexes::Vecs, ) -> Self { let cum_source = cumulative.read_only_boxed_clone(); - Self(cached_starts.0.map_with_suffix(|suffix, cached_start| { + Self(cached_starts.map_with_suffix(|suffix, cached_start| { let full_name = format!("{name}_{suffix}"); - let cached = cached_start.clone(); + let cached = cached_start.read_only_clone(); let starts_version = cached.version(); let sum = LazyDeltaVec::::new( &full_name, version, cum_source.clone(), starts_version, - move || cached.get(), - ); - let resolutions = Resolutions::forced_import( - &full_name, - sum.read_only_boxed_clone(), - version, - indexes, + move || cached.cached(), ); + let resolutions = Resolutions::forced_import(&full_name, sum.clone(), version, indexes); LazyRollingSumFromHeight { height: sum, resolutions: Box::new(resolutions), diff --git a/crates/brk_computer/src/internal/per_block/rolling/windows.rs b/crates/brk_computer/src/internal/per_block/rolling/windows.rs index df527f0ee..232d18709 100644 --- a/crates/brk_computer/src/internal/per_block/rolling/windows.rs +++ b/crates/brk_computer/src/internal/per_block/rolling/windows.rs @@ -18,10 +18,7 @@ use crate::{ }, }; -/// Cached window starts for lazy rolling computations. -/// Clone-cheap (all fields are Arc-backed). Shared across all metrics. -#[derive(Clone)] -pub struct CachedWindowStarts(pub Windows>); +pub type WindowStartVec = CachedVec>>; /// Rolling window start heights — the 4 height-ago vecs (24h, 1w, 1m, 1y). pub type WindowStarts<'a> = Windows<&'a EagerVec>>; diff --git a/crates/brk_computer/src/investing/compute.rs b/crates/brk_computer/src/investing/compute.rs index ec129c0a9..c89456bd5 100644 --- a/crates/brk_computer/src/investing/compute.rs +++ b/crates/brk_computer/src/investing/compute.rs @@ -102,7 +102,7 @@ impl Vecs { { returns.compute_binary::( starting_indexes.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, &average_price.cents.height, exit, )?; @@ -163,7 +163,7 @@ impl Vecs { { returns.compute_binary::( starting_indexes.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, &lookback_price.cents.height, exit, )?; @@ -266,7 +266,7 @@ impl Vecs { { returns.compute_binary::( starting_indexes.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, &average_price.cents.height, exit, )?; diff --git a/crates/brk_computer/src/lib.rs b/crates/brk_computer/src/lib.rs index 916f0debe..4da11370d 100644 --- a/crates/brk_computer/src/lib.rs +++ b/crates/brk_computer/src/lib.rs @@ -87,7 +87,7 @@ impl Computer { )?)) })?; - let cached_starts = &blocks.lookback.cached_window_starts; + let cached_starts = blocks.lookback.cached_window_starts(); let (inputs, outputs, mining, transactions, scripts, pools, cointime) = timed( "Imported inputs/outputs/mining/tx/scripts/pools/cointime", @@ -98,7 +98,7 @@ impl Computer { &computed_path, VERSION, &indexes, - cached_starts, + &cached_starts, )?)) })?; @@ -107,7 +107,7 @@ impl Computer { &computed_path, VERSION, &indexes, - cached_starts, + &cached_starts, )?)) })?; @@ -116,7 +116,7 @@ impl Computer { &computed_path, VERSION, &indexes, - cached_starts, + &cached_starts, )?)) })?; @@ -126,7 +126,7 @@ impl Computer { VERSION, indexer, &indexes, - cached_starts, + &cached_starts, )?)) })?; @@ -135,7 +135,7 @@ impl Computer { &computed_path, VERSION, &indexes, - cached_starts, + &cached_starts, )?)) })?; @@ -144,7 +144,7 @@ impl Computer { &computed_path, VERSION, &indexes, - cached_starts, + &cached_starts, )?)) })?; @@ -152,7 +152,7 @@ impl Computer { &computed_path, VERSION, &indexes, - cached_starts, + &cached_starts, )?); let inputs = inputs_handle.join().unwrap()?; @@ -208,7 +208,7 @@ impl Computer { &computed_path, VERSION, &indexes, - cached_starts, + &cached_starts, )?); let market = market_handle.join().unwrap()?; @@ -225,7 +225,7 @@ impl Computer { &indexes, &distribution, &cointime, - cached_starts, + &cached_starts, )?)) })?; diff --git a/crates/brk_computer/src/market/ath/compute.rs b/crates/brk_computer/src/market/ath/compute.rs index 32a3f12fd..6b03d2152 100644 --- a/crates/brk_computer/src/market/ath/compute.rs +++ b/crates/brk_computer/src/market/ath/compute.rs @@ -15,7 +15,7 @@ impl Vecs { ) -> Result<()> { self.high.cents.height.compute_all_time_high( starting_indexes.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, exit, )?; @@ -23,7 +23,7 @@ impl Vecs { self.days_since.height.compute_transform3( starting_indexes.height, &self.high.cents.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, &indexes.timestamp.monotonic, |(i, ath, price, ts, slf)| { if ath_ts.is_none() { @@ -68,7 +68,7 @@ impl Vecs { self.drawdown.compute_drawdown( starting_indexes.height, - &prices.cached_spot_cents, + &prices.spot.cents.height, &self.high.cents.height, exit, )?; diff --git a/crates/brk_computer/src/market/lookback/compute.rs b/crates/brk_computer/src/market/lookback/compute.rs index c98e81dba..394aff667 100644 --- a/crates/brk_computer/src/market/lookback/compute.rs +++ b/crates/brk_computer/src/market/lookback/compute.rs @@ -13,7 +13,7 @@ impl Vecs { starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let price = &prices.cached_spot_cents; + let price = &prices.spot.cents.height; for (price_past, days) in self.price_past.iter_mut_with_days() { let window_starts = blocks.lookback.start_vec(days as usize); diff --git a/crates/brk_computer/src/market/moving_average/compute.rs b/crates/brk_computer/src/market/moving_average/compute.rs index b05c429ac..69eeaa10d 100644 --- a/crates/brk_computer/src/market/moving_average/compute.rs +++ b/crates/brk_computer/src/market/moving_average/compute.rs @@ -13,7 +13,7 @@ impl Vecs { starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let close = &prices.cached_spot_cents; + let close = &prices.spot.cents.height; for (sma, period) in [ (&mut self.sma._1w, 7), diff --git a/crates/brk_computer/src/market/range/compute.rs b/crates/brk_computer/src/market/range/compute.rs index 54784c5ea..2d333dbb0 100644 --- a/crates/brk_computer/src/market/range/compute.rs +++ b/crates/brk_computer/src/market/range/compute.rs @@ -13,13 +13,13 @@ impl Vecs { starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let price = &prices.cached_spot_cents; + let price = &prices.spot.cents.height; for (min_vec, max_vec, starts) in [ ( &mut self.min._1w.cents.height, &mut self.max._1w.cents.height, - &blocks.lookback._1w, + &blocks.lookback._1w.inner, ), ( &mut self.min._2w.cents.height, @@ -29,12 +29,12 @@ impl Vecs { ( &mut self.min._1m.cents.height, &mut self.max._1m.cents.height, - &blocks.lookback._1m, + &blocks.lookback._1m.inner, ), ( &mut self.min._1y.cents.height, &mut self.max._1y.cents.height, - &blocks.lookback._1y, + &blocks.lookback._1y.inner, ), ] { min_vec.compute_rolling_min_from_starts( diff --git a/crates/brk_computer/src/market/returns/compute.rs b/crates/brk_computer/src/market/returns/compute.rs index 0cec1682d..4f46c022d 100644 --- a/crates/brk_computer/src/market/returns/compute.rs +++ b/crates/brk_computer/src/market/returns/compute.rs @@ -24,7 +24,7 @@ impl Vecs { { returns.compute_binary::( starting_indexes.height, - &prices.cached_spot_usd, + &prices.spot.usd.height, &lookback_price.usd.height, exit, )?; diff --git a/crates/brk_computer/src/market/technical/macd.rs b/crates/brk_computer/src/market/technical/macd.rs index d008b3595..8af7b3fd5 100644 --- a/crates/brk_computer/src/market/technical/macd.rs +++ b/crates/brk_computer/src/market/technical/macd.rs @@ -16,7 +16,7 @@ pub(super) fn compute( starting_indexes: &Indexes, exit: &Exit, ) -> Result<()> { - let close = &prices.cached_spot_usd; + let close = &prices.spot.usd.height; let ws_fast = blocks.lookback.start_vec(fast_days); let ws_slow = blocks.lookback.start_vec(slow_days); let ws_signal = blocks.lookback.start_vec(signal_days); diff --git a/crates/brk_computer/src/mining/hashrate/compute.rs b/crates/brk_computer/src/mining/hashrate/compute.rs index d959fa98f..d49f5bf03 100644 --- a/crates/brk_computer/src/mining/hashrate/compute.rs +++ b/crates/brk_computer/src/mining/hashrate/compute.rs @@ -38,10 +38,10 @@ impl Vecs { let hash_rate = &self.rate.base.height; for (sma, window) in [ - (&mut self.rate.sma._1w.height, &lookback._1w), - (&mut self.rate.sma._1m.height, &lookback._1m), + (&mut self.rate.sma._1w.height, &lookback._1w.inner), + (&mut self.rate.sma._1m.height, &lookback._1m.inner), (&mut self.rate.sma._2m.height, &lookback._2m), - (&mut self.rate.sma._1y.height, &lookback._1y), + (&mut self.rate.sma._1y.height, &lookback._1y.inner), ] { sma.compute_rolling_average(starting_indexes.height, window, hash_rate, exit)?; } diff --git a/crates/brk_computer/src/mining/import.rs b/crates/brk_computer/src/mining/import.rs index 8a8ae6b3b..b1adf3d76 100644 --- a/crates/brk_computer/src/mining/import.rs +++ b/crates/brk_computer/src/mining/import.rs @@ -6,7 +6,7 @@ use brk_types::Version; use crate::{ indexes, internal::{ - CachedWindowStarts, + WindowStartVec, Windows, db_utils::{finalize_db, open_db}, }, }; @@ -18,7 +18,7 @@ impl Vecs { parent_path: &Path, parent_version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let db = open_db(parent_path, super::DB_NAME, 1_000_000)?; let version = parent_version; diff --git a/crates/brk_computer/src/mining/rewards/import.rs b/crates/brk_computer/src/mining/rewards/import.rs index 6b54f35c5..137e96593 100644 --- a/crates/brk_computer/src/mining/rewards/import.rs +++ b/crates/brk_computer/src/mining/rewards/import.rs @@ -7,8 +7,8 @@ use crate::{ indexes, internal::{ AmountPerBlockCumulative, AmountPerBlockCumulativeRolling, AmountPerBlockFull, - CachedWindowStarts, LazyPercentRollingWindows, OneMinusBp16, PercentPerBlock, - PercentRollingWindows, RatioRollingWindows, + LazyPercentRollingWindows, OneMinusBp16, PercentPerBlock, PercentRollingWindows, + RatioRollingWindows, WindowStartVec, Windows, }, }; @@ -17,7 +17,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let fee_dominance_rolling = PercentRollingWindows::forced_import(db, "fee_dominance", version, indexes)?; diff --git a/crates/brk_computer/src/outputs/count/import.rs b/crates/brk_computer/src/outputs/count/import.rs index d14ac80e8..61bdaf7cd 100644 --- a/crates/brk_computer/src/outputs/count/import.rs +++ b/crates/brk_computer/src/outputs/count/import.rs @@ -5,7 +5,7 @@ use vecdb::Database; use super::Vecs; use crate::{ indexes, - internal::{CachedWindowStarts, PerBlock, PerBlockAggregated}, + internal::{PerBlock, PerBlockAggregated, WindowStartVec, Windows}, }; impl Vecs { @@ -13,7 +13,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { Ok(Self { total: PerBlockAggregated::forced_import( diff --git a/crates/brk_computer/src/outputs/import.rs b/crates/brk_computer/src/outputs/import.rs index 9b42342aa..e048792db 100644 --- a/crates/brk_computer/src/outputs/import.rs +++ b/crates/brk_computer/src/outputs/import.rs @@ -6,7 +6,7 @@ use brk_types::Version; use crate::{ indexes, internal::{ - CachedWindowStarts, + WindowStartVec, Windows, db_utils::{finalize_db, open_db}, }, }; @@ -18,7 +18,7 @@ impl Vecs { parent_path: &Path, parent_version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let db = open_db(parent_path, super::DB_NAME, 20_000_000)?; let version = parent_version; diff --git a/crates/brk_computer/src/pools/major.rs b/crates/brk_computer/src/pools/major.rs index d8e85ac29..d076ee356 100644 --- a/crates/brk_computer/src/pools/major.rs +++ b/crates/brk_computer/src/pools/major.rs @@ -7,8 +7,8 @@ use vecdb::{BinaryTransform, Database, Exit, ReadableVec, Rw, StorageMode, Versi use crate::{ blocks, indexes, internal::{ - AmountPerBlockCumulativeRolling, CachedWindowStarts, MaskSats, PercentRollingWindows, - RatioU64Bp16, + AmountPerBlockCumulativeRolling, MaskSats, PercentRollingWindows, RatioU64Bp16, + WindowStartVec, Windows, }, mining, prices, }; @@ -33,7 +33,7 @@ impl Vecs { slug: PoolSlug, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let suffix = |s: &str| format!("{}_{s}", slug); diff --git a/crates/brk_computer/src/pools/minor.rs b/crates/brk_computer/src/pools/minor.rs index 83169b6cc..5028c05b2 100644 --- a/crates/brk_computer/src/pools/minor.rs +++ b/crates/brk_computer/src/pools/minor.rs @@ -5,7 +5,7 @@ use vecdb::{Database, Exit, ReadableVec, Rw, StorageMode, Version}; use crate::{ blocks, indexes, - internal::{CachedWindowStarts, PerBlockCumulativeRolling, PercentPerBlock, RatioU64Bp16}, + internal::{PerBlockCumulativeRolling, PercentPerBlock, RatioU64Bp16, WindowStartVec, Windows}, }; #[derive(Traversable)] @@ -23,7 +23,7 @@ impl Vecs { slug: PoolSlug, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let suffix = |s: &str| format!("{}_{s}", slug); diff --git a/crates/brk_computer/src/pools/mod.rs b/crates/brk_computer/src/pools/mod.rs index 8d8585d0d..87546e2e0 100644 --- a/crates/brk_computer/src/pools/mod.rs +++ b/crates/brk_computer/src/pools/mod.rs @@ -19,7 +19,7 @@ pub use pool_heights::PoolHeights; use crate::{ blocks, indexes, internal::{ - CachedWindowStarts, + WindowStartVec, Windows, db_utils::{finalize_db, open_db}, }, mining, prices, @@ -44,7 +44,7 @@ impl Vecs { parent_path: &Path, parent_version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let db = open_db(parent_path, DB_NAME, 100_000)?; let pools = pools(); diff --git a/crates/brk_computer/src/prices/by_unit.rs b/crates/brk_computer/src/prices/by_unit.rs index 6b9d9cdef..5e1316889 100644 --- a/crates/brk_computer/src/prices/by_unit.rs +++ b/crates/brk_computer/src/prices/by_unit.rs @@ -2,7 +2,7 @@ use brk_traversable::Traversable; use brk_types::{Cents, Dollars, OHLCCents, OHLCDollars, OHLCSats, Sats}; use vecdb::{Rw, StorageMode}; -use crate::internal::{EagerIndexes, LazyEagerIndexes, LazyPerBlock, PerBlock, Resolutions}; +use crate::internal::{CachedPerBlock, EagerIndexes, LazyEagerIndexes, LazyPerBlock, Resolutions}; use super::ohlcs::{LazyOhlcVecs, OhlcVecs}; @@ -38,6 +38,6 @@ pub struct OhlcByUnit { #[derive(Traversable)] pub struct PriceByUnit { pub usd: LazyPerBlock, - pub cents: PerBlock, + pub cents: CachedPerBlock, pub sats: LazyPerBlock, } diff --git a/crates/brk_computer/src/prices/compute.rs b/crates/brk_computer/src/prices/compute.rs index e983317a9..c5fd4e4e6 100644 --- a/crates/brk_computer/src/prices/compute.rs +++ b/crates/brk_computer/src/prices/compute.rs @@ -68,6 +68,7 @@ impl Vecs { self.spot .cents .height + .inner .validate_computed_version_or_reset(source_version)?; let total_heights = indexer.vecs.blocks.timestamp.len(); @@ -83,7 +84,7 @@ impl Vecs { .height .len() .min(starting_indexes.height.to_usize()); - self.spot.cents.height.truncate_if_needed_at(truncate_to)?; + self.spot.cents.height.inner.truncate_if_needed_at(truncate_to)?; if self.spot.cents.height.len() < START_HEIGHT { for line in brk_oracle::PRICES @@ -95,7 +96,7 @@ impl Vecs { } let dollars: f64 = line.parse().unwrap_or(0.0); let cents = (dollars * 100.0).round() as u64; - self.spot.cents.height.push(Cents::new(cents)); + self.spot.cents.height.inner.push(Cents::new(cents)); } } @@ -129,6 +130,7 @@ impl Vecs { self.spot .cents .height + .inner .push(Cents::new(bin_to_cents(ref_bin))); let progress = ((i + 1) * 100 / num_new) as u8; @@ -139,7 +141,7 @@ impl Vecs { { let _lock = exit.lock(); - self.spot.cents.height.write()?; + self.spot.cents.height.inner.write()?; } info!( diff --git a/crates/brk_computer/src/prices/mod.rs b/crates/brk_computer/src/prices/mod.rs index ac5d9f36a..186135430 100644 --- a/crates/brk_computer/src/prices/mod.rs +++ b/crates/brk_computer/src/prices/mod.rs @@ -6,14 +6,13 @@ use std::path::Path; use brk_traversable::Traversable; use brk_types::Version; -use brk_types::{Cents, Dollars, Height}; -use vecdb::{CachedVec, Database, LazyVecFrom1, ReadableCloneableVec, Rw, StorageMode}; +use vecdb::{Database, ReadOnlyClone, Rw, StorageMode}; use crate::{ indexes, internal::{ - CentsUnsignedToDollars, CentsUnsignedToSats, EagerIndexes, LazyEagerIndexes, LazyPerBlock, - OhlcCentsToDollars, OhlcCentsToSats, PerBlock, Resolutions, + CachedPerBlock, CentsUnsignedToDollars, CentsUnsignedToSats, EagerIndexes, + LazyEagerIndexes, LazyPerBlock, OhlcCentsToDollars, OhlcCentsToSats, Resolutions, db_utils::{finalize_db, open_db}, }, }; @@ -28,11 +27,6 @@ pub struct Vecs { #[traversable(skip)] pub db: Database, - #[traversable(skip)] - pub cached_spot_cents: CachedVec, - #[traversable(skip)] - pub cached_spot_usd: LazyVecFrom1, - pub split: SplitByUnit, pub ohlc: OhlcByUnit, pub spot: PriceByUnit, @@ -57,7 +51,7 @@ impl Vecs { ) -> brk_error::Result { let version = version + Version::new(11); - let price_cents = PerBlock::forced_import(db, "price_cents", version, indexes)?; + let price_cents = CachedPerBlock::forced_import(db, "price_cents", version, indexes)?; let open_cents = EagerIndexes::forced_import(db, "price_open_cents", version)?; let high_cents = EagerIndexes::forced_import(db, "price_high_cents", version)?; @@ -65,14 +59,14 @@ impl Vecs { let close_cents = Resolutions::forced_import( "price_close_cents", - price_cents.height.read_only_boxed_clone(), + price_cents.height.read_only_clone(), version, indexes, ); let ohlc_cents = OhlcVecs::forced_import(db, "price_ohlc_cents", version)?; - let price_usd = LazyPerBlock::from_computed::( + let price_usd = LazyPerBlock::from_cached_computed::( "price", version, price_cents.height.read_only_boxed_clone(), @@ -95,12 +89,8 @@ impl Vecs { &low_cents, ); - let close_usd = Resolutions::forced_import( - "price_close", - price_usd.height.read_only_boxed_clone(), - version, - indexes, - ); + let close_usd = + Resolutions::forced_import("price_close", price_usd.height.clone(), version, indexes); let ohlc_usd = LazyOhlcVecs::from_eager_ohlc_indexes::( "price_ohlc", @@ -108,7 +98,7 @@ impl Vecs { &ohlc_cents, ); - let price_sats = LazyPerBlock::from_computed::( + let price_sats = LazyPerBlock::from_cached_computed::( "price_sats", version, price_cents.height.read_only_boxed_clone(), @@ -134,7 +124,7 @@ impl Vecs { let close_sats = Resolutions::forced_import( "price_close_sats", - price_sats.height.read_only_boxed_clone(), + price_sats.height.clone(), version, indexes, ); @@ -175,13 +165,6 @@ impl Vecs { sats: ohlc_sats, }; - let cached_spot_cents = CachedVec::new(&price_cents.height); - let cached_spot_usd = LazyVecFrom1::transformed::( - "price", - version, - cached_spot_cents.read_only_boxed_clone(), - ); - let spot = PriceByUnit { usd: price_usd, cents: price_cents, @@ -190,8 +173,6 @@ impl Vecs { Ok(Self { db: db.clone(), - cached_spot_cents, - cached_spot_usd, split, ohlc, spot, diff --git a/crates/brk_computer/src/scripts/count/import.rs b/crates/brk_computer/src/scripts/count/import.rs index caaf15bb0..8c9788322 100644 --- a/crates/brk_computer/src/scripts/count/import.rs +++ b/crates/brk_computer/src/scripts/count/import.rs @@ -5,7 +5,7 @@ use vecdb::Database; use super::Vecs; use crate::{ indexes, - internal::{CachedWindowStarts, PerBlockCumulativeRolling}, + internal::{PerBlockCumulativeRolling, WindowStartVec, Windows}, }; impl Vecs { @@ -13,7 +13,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let p2a = PerBlockCumulativeRolling::forced_import( db, diff --git a/crates/brk_computer/src/scripts/import.rs b/crates/brk_computer/src/scripts/import.rs index 8243ce8f6..48cda9bc9 100644 --- a/crates/brk_computer/src/scripts/import.rs +++ b/crates/brk_computer/src/scripts/import.rs @@ -9,14 +9,14 @@ use crate::{ }; use super::{CountVecs, ValueVecs, Vecs}; -use crate::internal::CachedWindowStarts; +use crate::internal::{WindowStartVec, Windows}; impl Vecs { pub(crate) fn forced_import( parent_path: &Path, parent_version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let db = open_db(parent_path, super::DB_NAME, 1_000_000)?; let version = parent_version; diff --git a/crates/brk_computer/src/supply/import.rs b/crates/brk_computer/src/supply/import.rs index 4ef37fb2e..3577d0196 100644 --- a/crates/brk_computer/src/supply/import.rs +++ b/crates/brk_computer/src/supply/import.rs @@ -6,8 +6,8 @@ use brk_types::Version; use crate::{ cointime, distribution, indexes, internal::{ - CachedWindowStarts, LazyAmountPerBlock, LazyFiatPerBlock, LazyRollingDeltasFiatFromHeight, - PercentPerBlock, RollingWindows, + LazyAmountPerBlock, LazyFiatPerBlock, LazyRollingDeltasFiatFromHeight, PercentPerBlock, + RollingWindows, WindowStartVec, Windows, db_utils::{finalize_db, open_db}, }, supply::burned, @@ -24,7 +24,7 @@ impl Vecs { indexes: &indexes::Vecs, distribution: &distribution::Vecs, cointime: &cointime::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let db = open_db(parent, super::DB_NAME, 1_000_000)?; diff --git a/crates/brk_computer/src/transactions/count/import.rs b/crates/brk_computer/src/transactions/count/import.rs index 8648dbb85..bcd8a7151 100644 --- a/crates/brk_computer/src/transactions/count/import.rs +++ b/crates/brk_computer/src/transactions/count/import.rs @@ -5,7 +5,7 @@ use vecdb::Database; use super::Vecs; use crate::{ indexes, - internal::{CachedWindowStarts, PerBlockFull}, + internal::{PerBlockFull, WindowStartVec, Windows}, }; impl Vecs { @@ -13,7 +13,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { Ok(Self { total: PerBlockFull::forced_import(db, "tx_count", version, indexes, cached_starts)?, diff --git a/crates/brk_computer/src/transactions/import.rs b/crates/brk_computer/src/transactions/import.rs index 81c9c275a..2e6643a21 100644 --- a/crates/brk_computer/src/transactions/import.rs +++ b/crates/brk_computer/src/transactions/import.rs @@ -7,7 +7,7 @@ use brk_types::Version; use crate::{ indexes, internal::{ - CachedWindowStarts, + WindowStartVec, Windows, db_utils::{finalize_db, open_db}, }, }; @@ -20,7 +20,7 @@ impl Vecs { parent_version: Version, indexer: &Indexer, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let db = open_db(parent_path, super::DB_NAME, 10_000_000)?; let version = parent_version; diff --git a/crates/brk_computer/src/transactions/versions/import.rs b/crates/brk_computer/src/transactions/versions/import.rs index 15187bb93..ec66cb51a 100644 --- a/crates/brk_computer/src/transactions/versions/import.rs +++ b/crates/brk_computer/src/transactions/versions/import.rs @@ -5,7 +5,7 @@ use vecdb::Database; use super::Vecs; use crate::{ indexes, - internal::{CachedWindowStarts, PerBlockCumulativeRolling}, + internal::{PerBlockCumulativeRolling, WindowStartVec, Windows}, }; impl Vecs { @@ -13,7 +13,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { Ok(Self { v1: PerBlockCumulativeRolling::forced_import( diff --git a/crates/brk_computer/src/transactions/volume/import.rs b/crates/brk_computer/src/transactions/volume/import.rs index 43e79cd66..bf96a3af3 100644 --- a/crates/brk_computer/src/transactions/volume/import.rs +++ b/crates/brk_computer/src/transactions/volume/import.rs @@ -5,7 +5,7 @@ use vecdb::Database; use super::Vecs; use crate::{ indexes, - internal::{AmountPerBlockCumulativeRolling, CachedWindowStarts, PerBlock, Windows}, + internal::{AmountPerBlockCumulativeRolling, PerBlock, WindowStartVec, Windows}, }; impl Vecs { @@ -13,7 +13,7 @@ impl Vecs { db: &Database, version: Version, indexes: &indexes::Vecs, - cached_starts: &CachedWindowStarts, + cached_starts: &Windows<&WindowStartVec>, ) -> Result { let v = version + Version::TWO; Ok(Self { diff --git a/crates/brk_query/src/impl/addr.rs b/crates/brk_query/src/impl/addr.rs index e3085c094..f089d3dea 100644 --- a/crates/brk_query/src/impl/addr.rs +++ b/crates/brk_query/src/impl/addr.rs @@ -4,10 +4,10 @@ use bitcoin::{Network, PublicKey, ScriptBuf}; use brk_error::{Error, OptionData, Result}; use brk_types::{ Addr, AddrBytes, AddrChainStats, AddrHash, AddrIndexOutPoint, AddrIndexTxIndex, AddrStats, - AnyAddrDataIndexEnum, Dollars, Height, OutputType, Transaction, TxIndex, - TxStatus, Txid, TypeIndex, Unit, Utxo, Vout, + AnyAddrDataIndexEnum, Dollars, Height, OutputType, Transaction, TxIndex, TxStatus, Txid, + TypeIndex, Unit, Utxo, Vout, }; -use vecdb::{ReadableVec, VecIndex}; +use vecdb::VecIndex; use crate::Query; @@ -39,15 +39,10 @@ impl Query { let addr_type = output_type; let hash = AddrHash::from(&bytes); - let Some(store) = stores - .addr_type_to_addr_hash_to_addr_index - .get(addr_type) - else { + let Some(store) = stores.addr_type_to_addr_hash_to_addr_index.get(addr_type) else { return Err(Error::InvalidAddr); }; - let Ok(Some(type_index)) = store - .get(&hash) - .map(|opt| opt.map(|cow| cow.into_owned())) + let Ok(Some(type_index)) = store.get(&hash).map(|opt| opt.map(|cow| cow.into_owned())) else { return Err(Error::UnknownAddr); }; diff --git a/crates/brk_query/src/impl/block/info.rs b/crates/brk_query/src/impl/block/info.rs index 3f4841294..7e89b0d17 100644 --- a/crates/brk_query/src/impl/block/info.rs +++ b/crates/brk_query/src/impl/block/info.rs @@ -52,7 +52,7 @@ impl Query { if height > max_height { return Err(Error::OutOfRange("Block height out of range".into())); } - Ok(self.indexer().vecs.blocks.blockhash.read_once(height)?) + self.indexer().vecs.blocks.blockhash.get(height).data() } pub fn blocks(&self, start_height: Option) -> Result> { @@ -220,7 +220,7 @@ impl Query { .block .sats .collect_range_at(begin, end); - let prices = computer.prices.cached_spot_usd.collect_range_at(begin, end); + let prices = computer.prices.spot.usd.height.collect_range_at(begin, end); let output_volumes = computer .mining .rewards diff --git a/crates/brk_query/src/impl/block/status.rs b/crates/brk_query/src/impl/block/status.rs index 61eecc6c1..d474b1d10 100644 --- a/crates/brk_query/src/impl/block/status.rs +++ b/crates/brk_query/src/impl/block/status.rs @@ -1,4 +1,4 @@ -use brk_error::Result; +use brk_error::{OptionData, Result}; use brk_types::{BlockHash, BlockStatus, Height}; use vecdb::AnyVec; @@ -25,7 +25,8 @@ impl Query { .vecs .blocks .blockhash - .read_once(height.incremented())?, + .get(height.incremented()) + .data()?, ) } else { None diff --git a/crates/brk_query/src/impl/mining/block_window.rs b/crates/brk_query/src/impl/mining/block_window.rs index d13b7a495..848936901 100644 --- a/crates/brk_query/src/impl/mining/block_window.rs +++ b/crates/brk_query/src/impl/mining/block_window.rs @@ -39,15 +39,14 @@ impl BlockWindow { // Use pre-computed timestamp-based lookback for accurate time boundaries. // 24h, 1w, 1m, 1y use in-memory CachedVec; others fall back to PcoVec. - let cached = &lookback.cached_window_starts.0; let start_height = match time_period { - TimePeriod::Day => cached._24h.collect_one(current_height), + TimePeriod::Day => lookback._24h.collect_one(current_height), TimePeriod::ThreeDays => lookback._3d.collect_one(current_height), - TimePeriod::Week => cached._1w.collect_one(current_height), - TimePeriod::Month => cached._1m.collect_one(current_height), + TimePeriod::Week => lookback._1w.collect_one(current_height), + TimePeriod::Month => lookback._1m.collect_one(current_height), TimePeriod::ThreeMonths => lookback._3m.collect_one(current_height), TimePeriod::SixMonths => lookback._6m.collect_one(current_height), - TimePeriod::Year => cached._1y.collect_one(current_height), + TimePeriod::Year => lookback._1y.collect_one(current_height), TimePeriod::TwoYears => lookback._2y.collect_one(current_height), TimePeriod::ThreeYears => lookback._3y.collect_one(current_height), TimePeriod::All => None, @@ -79,7 +78,7 @@ impl BlockWindow { .collect_range_at(self.start, self.end); let all_prices: Vec = computer .prices - .cached_spot_cents + .spot.cents.height .collect_range_at(self.start, self.end); let read_start = self.start.saturating_sub(1).max(0); let all_cum = cumulative.collect_range_at(read_start, self.end); diff --git a/crates/brk_query/src/impl/mining/pools.rs b/crates/brk_query/src/impl/mining/pools.rs index 218dbc26c..574666f1e 100644 --- a/crates/brk_query/src/impl/mining/pools.rs +++ b/crates/brk_query/src/impl/mining/pools.rs @@ -40,29 +40,13 @@ impl Query { // Use timestamp-based lookback for accurate time boundaries let lookback = &computer.blocks.lookback; let start = match time_period { - TimePeriod::Day => lookback - .cached_window_starts - .0 - ._24h - .collect_one(current_height), + TimePeriod::Day => lookback._24h.collect_one(current_height), TimePeriod::ThreeDays => lookback._3d.collect_one(current_height), - TimePeriod::Week => lookback - .cached_window_starts - .0 - ._1w - .collect_one(current_height), - TimePeriod::Month => lookback - .cached_window_starts - .0 - ._1m - .collect_one(current_height), + TimePeriod::Week => lookback._1w.collect_one(current_height), + TimePeriod::Month => lookback._1m.collect_one(current_height), TimePeriod::ThreeMonths => lookback._3m.collect_one(current_height), TimePeriod::SixMonths => lookback._6m.collect_one(current_height), - TimePeriod::Year => lookback - .cached_window_starts - .0 - ._1y - .collect_one(current_height), + TimePeriod::Year => lookback._1y.collect_one(current_height), TimePeriod::TwoYears => lookback._2y.collect_one(current_height), TimePeriod::ThreeYears => lookback._3y.collect_one(current_height), TimePeriod::All => None, @@ -191,8 +175,6 @@ impl Query { // Use timestamp-based lookback for accurate time boundaries let lookback = &computer.blocks.lookback; let start_24h = lookback - .cached_window_starts - .0 ._24h .collect_one(current_height) .unwrap_or_default() @@ -207,8 +189,6 @@ impl Query { let total_24h = total_all.saturating_sub(count_before_24h); let start_1w = lookback - .cached_window_starts - .0 ._1w .collect_one(current_height) .unwrap_or_default() @@ -349,29 +329,13 @@ impl Query { let lookback = &self.computer().blocks.lookback; let current_height = self.height(); match tp { - TimePeriod::Day => lookback - .cached_window_starts - .0 - ._24h - .collect_one(current_height), + TimePeriod::Day => lookback._24h.collect_one(current_height), TimePeriod::ThreeDays => lookback._3d.collect_one(current_height), - TimePeriod::Week => lookback - .cached_window_starts - .0 - ._1w - .collect_one(current_height), - TimePeriod::Month => lookback - .cached_window_starts - .0 - ._1m - .collect_one(current_height), + TimePeriod::Week => lookback._1w.collect_one(current_height), + TimePeriod::Month => lookback._1m.collect_one(current_height), TimePeriod::ThreeMonths => lookback._3m.collect_one(current_height), TimePeriod::SixMonths => lookback._6m.collect_one(current_height), - TimePeriod::Year => lookback - .cached_window_starts - .0 - ._1y - .collect_one(current_height), + TimePeriod::Year => lookback._1y.collect_one(current_height), TimePeriod::TwoYears => lookback._2y.collect_one(current_height), TimePeriod::ThreeYears => lookback._3y.collect_one(current_height), TimePeriod::All => None, diff --git a/crates/brk_query/src/lib.rs b/crates/brk_query/src/lib.rs index 5739d9f3a..d862b0d64 100644 --- a/crates/brk_query/src/lib.rs +++ b/crates/brk_query/src/lib.rs @@ -58,7 +58,7 @@ impl Query { /// Current indexed height pub fn indexed_height(&self) -> Height { - Height::from(self.indexer().vecs.blocks.blockhash.stamp()) + Height::from(self.indexer().vecs.blocks.blockhash.inner.stamp()) } /// Current computed height (series)