global: snapshot part 4

This commit is contained in:
nym21
2026-03-20 14:27:10 +01:00
parent 1d671ea41f
commit 8f93ff9f68
47 changed files with 683 additions and 637 deletions
@@ -1,28 +1,31 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Cents, Height, Sats, Version};
use brk_types::{Height, Sats, Version};
use derive_more::{Deref, DerefMut};
use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
use crate::{
indexes,
internal::{
AmountPerBlock, CachedWindowStarts, LazyRollingAvgsAmountFromHeight,
LazyRollingSumsAmountFromHeight, SatsToCents,
AmountPerBlockCumulative, CachedWindowStarts, LazyRollingAvgsAmountFromHeight,
LazyRollingSumsAmountFromHeight,
},
prices,
};
#[derive(Traversable)]
pub struct AmountPerBlockCumulativeWithSums<M: StorageMode = Rw> {
pub base: AmountPerBlock<M>,
pub cumulative: AmountPerBlock<M>,
#[derive(Deref, DerefMut, Traversable)]
pub struct AmountPerBlockCumulativeRolling<M: StorageMode = Rw> {
#[deref]
#[deref_mut]
#[traversable(flatten)]
pub inner: AmountPerBlockCumulative<M>,
pub sum: LazyRollingSumsAmountFromHeight,
pub average: LazyRollingAvgsAmountFromHeight,
}
const VERSION: Version = Version::TWO;
impl AmountPerBlockCumulativeWithSums {
impl AmountPerBlockCumulativeRolling {
pub(crate) fn forced_import(
db: &Database,
name: &str,
@@ -32,29 +35,26 @@ impl AmountPerBlockCumulativeWithSums {
) -> Result<Self> {
let v = version + VERSION;
let base = AmountPerBlock::forced_import(db, name, v, indexes)?;
let cumulative =
AmountPerBlock::forced_import(db, &format!("{name}_cumulative"), v, indexes)?;
let inner = AmountPerBlockCumulative::forced_import(db, name, v, indexes)?;
let sum = LazyRollingSumsAmountFromHeight::new(
&format!("{name}_sum"),
v,
&cumulative.sats.height,
&cumulative.cents.height,
&inner.cumulative.sats.height,
&inner.cumulative.cents.height,
cached_starts,
indexes,
);
let average = LazyRollingAvgsAmountFromHeight::new(
&format!("{name}_average"),
v,
&cumulative.sats.height,
&cumulative.cents.height,
&inner.cumulative.sats.height,
&inner.cumulative.cents.height,
cached_starts,
indexes,
);
Ok(Self {
base,
cumulative,
inner,
sum,
average,
})
@@ -77,26 +77,6 @@ impl AmountPerBlockCumulativeWithSums {
prices: &prices::Vecs,
exit: &Exit,
) -> Result<()> {
self.cumulative
.sats
.height
.compute_cumulative(max_from, &self.base.sats.height, exit)?;
self.base
.cents
.height
.compute_binary::<Sats, Cents, SatsToCents>(
max_from,
&self.base.sats.height,
&prices.spot.cents.height,
exit,
)?;
self.cumulative
.cents
.height
.compute_cumulative(max_from, &self.base.cents.height, exit)?;
Ok(())
self.inner.compute(prices, max_from, exit)
}
}
@@ -1,24 +1,24 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Cents, Height, Sats, Version};
use brk_types::{Height, Sats, Version};
use derive_more::{Deref, DerefMut};
use vecdb::{Database, EagerVec, Exit, PcoVec, Rw, StorageMode};
use crate::{
indexes,
internal::{
AmountPerBlock, CachedWindowStarts, LazyRollingAvgsAmountFromHeight,
LazyRollingSumsAmountFromHeight, RollingDistributionAmountPerBlock, SatsToCents,
AmountPerBlockCumulativeRolling, CachedWindowStarts, RollingDistributionAmountPerBlock,
WindowStarts,
},
prices,
};
#[derive(Traversable)]
#[derive(Deref, DerefMut, Traversable)]
pub struct AmountPerBlockFull<M: StorageMode = Rw> {
pub base: AmountPerBlock<M>,
pub cumulative: AmountPerBlock<M>,
pub sum: LazyRollingSumsAmountFromHeight,
pub average: LazyRollingAvgsAmountFromHeight,
#[deref]
#[deref_mut]
#[traversable(flatten)]
pub inner: AmountPerBlockCumulativeRolling<M>,
#[traversable(flatten)]
pub distribution: RollingDistributionAmountPerBlock<M>,
}
@@ -35,33 +35,14 @@ impl AmountPerBlockFull {
) -> Result<Self> {
let v = version + VERSION;
let base = AmountPerBlock::forced_import(db, name, v, indexes)?;
let cumulative =
AmountPerBlock::forced_import(db, &format!("{name}_cumulative"), v, indexes)?;
let sum = LazyRollingSumsAmountFromHeight::new(
&format!("{name}_sum"),
v,
&cumulative.sats.height,
&cumulative.cents.height,
cached_starts,
indexes,
);
let average = LazyRollingAvgsAmountFromHeight::new(
&format!("{name}_average"),
v,
&cumulative.sats.height,
&cumulative.cents.height,
cached_starts,
indexes,
);
let rolling = RollingDistributionAmountPerBlock::forced_import(db, name, v, indexes)?;
let inner =
AmountPerBlockCumulativeRolling::forced_import(db, name, v, indexes, cached_starts)?;
let distribution =
RollingDistributionAmountPerBlock::forced_import(db, name, v, indexes)?;
Ok(Self {
base,
cumulative,
sum,
average,
distribution: rolling,
inner,
distribution,
})
}
@@ -73,33 +54,15 @@ impl AmountPerBlockFull {
exit: &Exit,
compute_sats: impl FnOnce(&mut EagerVec<PcoVec<Height, Sats>>) -> Result<()>,
) -> Result<()> {
compute_sats(&mut self.base.sats.height)?;
compute_sats(&mut self.inner.base.sats.height)?;
self.cumulative
.sats
.height
.compute_cumulative(max_from, &self.base.sats.height, exit)?;
self.base
.cents
.height
.compute_binary::<Sats, Cents, SatsToCents>(
max_from,
&self.base.sats.height,
&prices.spot.cents.height,
exit,
)?;
self.cumulative
.cents
.height
.compute_cumulative(max_from, &self.base.cents.height, exit)?;
self.inner.compute_rest(max_from, prices, exit)?;
self.distribution.compute(
max_from,
windows,
&self.base.sats.height,
&self.base.cents.height,
&self.inner.base.sats.height,
&self.inner.base.cents.height,
exit,
)?;
@@ -1,6 +1,6 @@
mod base;
mod cumulative;
mod cumulative_sum;
mod cumulative_rolling;
mod full;
mod lazy;
mod lazy_derived_resolutions;
@@ -11,7 +11,7 @@ mod with_deltas;
pub use base::*;
pub use cumulative::*;
pub use cumulative_sum::*;
pub use cumulative_rolling::*;
pub use full::*;
pub use lazy::*;
pub use lazy_derived_resolutions::*;
@@ -1,4 +1,4 @@
//! PerBlockCumulativeWithSums - base PerBlock + cumulative PerBlock + lazy rolling sums.
//! PerBlockCumulativeRolling - base PerBlock + cumulative PerBlock + lazy rolling sums.
//!
//! Rolling sums are derived lazily from the cumulative vec via LazyDeltaVec.
//! No rolling sum vecs are stored on disk.
@@ -24,7 +24,7 @@ use crate::{
};
#[derive(Traversable)]
pub struct PerBlockCumulativeWithSums<T, C, M: StorageMode = Rw>
pub struct PerBlockCumulativeRolling<T, C, M: StorageMode = Rw>
where
T: NumericValue + JsonSchema,
C: NumericValue + JsonSchema,
@@ -35,7 +35,7 @@ where
pub average: LazyRollingAvgsFromHeight<C>,
}
impl<T, C> PerBlockCumulativeWithSums<T, C>
impl<T, C> PerBlockCumulativeRolling<T, C>
where
T: NumericValue + JsonSchema + Into<C>,
C: NumericValue + JsonSchema,
@@ -1,6 +1,6 @@
mod aggregated;
mod base;
mod cumulative_sum;
mod cumulative_rolling;
mod distribution;
mod full;
mod lazy_distribution;
@@ -12,7 +12,7 @@ mod with_deltas;
pub use aggregated::*;
pub use base::*;
pub use cumulative_sum::*;
pub use cumulative_rolling::*;
pub use distribution::*;
pub use full::*;
pub use lazy_distribution::*;