mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-17 05:58:11 -07:00
global: snapshot
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
//! PerBlock with rolling average (no distribution stats).
|
||||
//!
|
||||
//! Stored height data + f64 cumulative + lazy 4-window rolling averages.
|
||||
//! Stored height data + cumulative + lazy 4-window rolling averages.
|
||||
//! Rolling averages are computed on-the-fly from the cumulative via DeltaAvg.
|
||||
//!
|
||||
//! Type parameters:
|
||||
//! - `T`: per-block value type
|
||||
//! - `C`: cumulative type, defaults to `T`. Use a wider type (e.g., `StoredU64`)
|
||||
//! when the prefix sum of `T` values could overflow `T`.
|
||||
|
||||
use brk_error::Result;
|
||||
|
||||
@@ -15,20 +20,22 @@ use crate::indexes;
|
||||
use crate::internal::{LazyRollingAvgsFromHeight, NumericValue, WindowStartVec, Windows};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct PerBlockRollingAverage<T, M: StorageMode = Rw>
|
||||
pub struct PerBlockRollingAverage<T, C = T, M: StorageMode = Rw>
|
||||
where
|
||||
T: NumericValue + JsonSchema,
|
||||
C: NumericValue + JsonSchema,
|
||||
{
|
||||
pub block: M::Stored<EagerVec<PcoVec<Height, T>>>,
|
||||
#[traversable(hidden)]
|
||||
pub cumulative: M::Stored<EagerVec<PcoVec<Height, T>>>,
|
||||
pub cumulative: M::Stored<EagerVec<PcoVec<Height, C>>>,
|
||||
#[traversable(flatten)]
|
||||
pub average: LazyRollingAvgsFromHeight<T>,
|
||||
pub average: LazyRollingAvgsFromHeight<C>,
|
||||
}
|
||||
|
||||
impl<T> PerBlockRollingAverage<T>
|
||||
impl<T, C> PerBlockRollingAverage<T, C>
|
||||
where
|
||||
T: NumericValue + JsonSchema,
|
||||
T: NumericValue + JsonSchema + Into<C>,
|
||||
C: NumericValue + JsonSchema,
|
||||
{
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
@@ -38,11 +45,11 @@ where
|
||||
cached_starts: &Windows<&WindowStartVec>,
|
||||
) -> Result<Self> {
|
||||
let block: EagerVec<PcoVec<Height, T>> = EagerVec::forced_import(db, name, version)?;
|
||||
let cumulative: EagerVec<PcoVec<Height, T>> =
|
||||
EagerVec::forced_import(db, &format!("{name}_cumulative"), version + Version::ONE)?;
|
||||
let cumulative: EagerVec<PcoVec<Height, C>> =
|
||||
EagerVec::forced_import(db, &format!("{name}_cumulative"), version + Version::TWO)?;
|
||||
let average = LazyRollingAvgsFromHeight::new(
|
||||
&format!("{name}_average"),
|
||||
version + Version::ONE,
|
||||
version + Version::TWO,
|
||||
&cumulative,
|
||||
cached_starts,
|
||||
indexes,
|
||||
|
||||
Reference in New Issue
Block a user