global: snapshot

This commit is contained in:
nym21
2026-01-27 00:30:58 +01:00
parent 3d01822d27
commit ec1f2de5cf
41 changed files with 2712 additions and 5334 deletions
@@ -0,0 +1,82 @@
//! ComputedFromHeight using Distribution aggregation (no sum/cumulative).
//!
//! Use for block-based metrics where sum/cumulative would be misleading
//! (e.g., activity counts that can't be deduplicated across blocks).
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Height, Version};
use derive_more::{Deref, DerefMut};
use schemars::JsonSchema;
use vecdb::{Database, EagerVec, Exit, ImportableVec, IterableCloneableVec, PcoVec};
use crate::{ComputeIndexes, indexes};
use crate::internal::{ComputedHeightDerivedDistribution, ComputedVecValue, NumericValue};
#[derive(Clone, Deref, DerefMut, Traversable)]
#[traversable(merge)]
pub struct ComputedFromHeightDistribution<T>
where
T: ComputedVecValue + PartialOrd + JsonSchema,
{
#[traversable(rename = "base")]
pub height: EagerVec<PcoVec<Height, T>>,
#[deref]
#[deref_mut]
pub rest: ComputedHeightDerivedDistribution<T>,
}
const VERSION: Version = Version::ZERO;
impl<T> ComputedFromHeightDistribution<T>
where
T: NumericValue + JsonSchema,
{
pub fn forced_import(
db: &Database,
name: &str,
version: Version,
indexes: &indexes::Vecs,
) -> Result<Self> {
let v = version + VERSION;
let height: EagerVec<PcoVec<Height, T>> = EagerVec::forced_import(db, name, v)?;
let rest = ComputedHeightDerivedDistribution::forced_import(
db,
name,
height.boxed_clone(),
v,
indexes,
)?;
Ok(Self { height, rest })
}
pub fn compute_all<F>(
&mut self,
indexes: &indexes::Vecs,
starting_indexes: &ComputeIndexes,
exit: &Exit,
mut compute: F,
) -> Result<()>
where
F: FnMut(&mut EagerVec<PcoVec<Height, T>>) -> Result<()>,
{
compute(&mut self.height)?;
self.compute_rest(indexes, starting_indexes, exit)
}
/// Compute rest from self.height (for stateful computation patterns).
pub fn compute_rest(
&mut self,
indexes: &indexes::Vecs,
starting_indexes: &ComputeIndexes,
exit: &Exit,
) -> Result<()> {
self.rest
.derive_from(indexes, starting_indexes, &self.height, exit)
}
}
@@ -0,0 +1,74 @@
//! LazyBinaryComputedFromHeightDistribution - lazy binary transform with distribution stats.
//!
//! Height-level values are lazy: `transform(source1[h], source2[h])`.
//! Uses Distribution aggregation (no sum/cumulative) - appropriate for ratios.
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Height, Version};
use derive_more::{Deref, DerefMut};
use schemars::JsonSchema;
use vecdb::{BinaryTransform, Database, Exit, IterableBoxedVec, IterableCloneableVec, LazyVecFrom2};
use crate::{
ComputeIndexes, indexes,
internal::{ComputedHeightDerivedDistribution, ComputedVecValue, NumericValue},
};
const VERSION: Version = Version::ZERO;
/// Lazy binary transform at height with distribution stats (no sum/cumulative).
#[derive(Clone, Deref, DerefMut, Traversable)]
#[traversable(merge)]
pub struct LazyBinaryComputedFromHeightDistribution<T, S1T = T, S2T = T>
where
T: ComputedVecValue + PartialOrd + JsonSchema,
S1T: ComputedVecValue,
S2T: ComputedVecValue,
{
#[traversable(rename = "base")]
pub height: LazyVecFrom2<Height, T, Height, S1T, Height, S2T>,
#[deref]
#[deref_mut]
pub rest: ComputedHeightDerivedDistribution<T>,
}
impl<T, S1T, S2T> LazyBinaryComputedFromHeightDistribution<T, S1T, S2T>
where
T: NumericValue + JsonSchema,
S1T: ComputedVecValue + JsonSchema,
S2T: ComputedVecValue + JsonSchema,
{
pub fn forced_import<F: BinaryTransform<S1T, S2T, T>>(
db: &Database,
name: &str,
version: Version,
source1: IterableBoxedVec<Height, S1T>,
source2: IterableBoxedVec<Height, S2T>,
indexes: &indexes::Vecs,
) -> Result<Self> {
let v = version + VERSION;
let height = LazyVecFrom2::transformed::<F>(name, v, source1, source2);
let rest = ComputedHeightDerivedDistribution::forced_import(
db,
name,
height.boxed_clone(),
v,
indexes,
)?;
Ok(Self { height, rest })
}
pub fn derive_from(
&mut self,
indexes: &indexes::Vecs,
starting_indexes: &ComputeIndexes,
exit: &Exit,
) -> Result<()> {
self.rest
.derive_from(indexes, starting_indexes, &self.height, exit)
}
}
@@ -2,11 +2,13 @@ mod binary_full;
mod binary_last;
mod binary_sum;
mod binary_sum_cum;
mod distribution;
mod full;
mod last;
mod lazy_distribution;
mod lazy_full;
mod lazy_transform_distribution;
mod lazy_binary_computed_distribution;
mod lazy_binary_computed_full;
mod lazy_binary_computed_last;
mod lazy_binary_computed_sum;
@@ -35,11 +37,13 @@ pub use binary_full::*;
pub use binary_last::*;
pub use binary_sum::*;
pub use binary_sum_cum::*;
pub use distribution::*;
pub use full::*;
pub use last::*;
pub use lazy_distribution::*;
pub use lazy_full::*;
pub use lazy_transform_distribution::*;
pub use lazy_binary_computed_distribution::*;
pub use lazy_binary_computed_full::*;
pub use lazy_binary_computed_last::*;
pub use lazy_binary_computed_sum::*;
@@ -1,11 +0,0 @@
use brk_types::{Cents, Dollars};
use vecdb::UnaryTransform;
pub struct CentsToDollars;
impl UnaryTransform<Cents, Dollars> for CentsToDollars {
#[inline(always)]
fn apply(cents: Cents) -> Dollars {
Dollars::from(cents)
}
}
@@ -1,4 +1,3 @@
mod cents_to_dollars;
mod close_price_times_ratio;
mod close_price_times_sats;
mod difference_f32;
@@ -21,6 +20,7 @@ mod price_times_ratio;
mod ratio32;
mod ratio32_neg;
mod ratio_f32;
mod ratio_u64_f32;
mod return_f32_tenths;
mod return_i8;
mod return_u16;
@@ -34,12 +34,12 @@ mod sat_plus_to_bitcoin;
mod sat_to_bitcoin;
mod sats_times_close_price;
mod u16_to_years;
mod u64_plus;
mod volatility_sqrt30;
mod volatility_sqrt365;
mod volatility_sqrt7;
mod weight_to_fullness;
pub use cents_to_dollars::*;
pub use close_price_times_ratio::*;
pub use close_price_times_sats::*;
pub use difference_f32::*;
@@ -62,6 +62,7 @@ pub use price_times_ratio::*;
pub use ratio32::*;
pub use ratio32_neg::*;
pub use ratio_f32::*;
pub use ratio_u64_f32::*;
pub use return_f32_tenths::*;
pub use return_i8::*;
pub use return_u16::*;
@@ -75,6 +76,7 @@ pub use sat_plus_to_bitcoin::*;
pub use sat_to_bitcoin::*;
pub use sats_times_close_price::*;
pub use u16_to_years::*;
pub use u64_plus::*;
pub use volatility_sqrt30::*;
pub use volatility_sqrt365::*;
pub use volatility_sqrt7::*;
@@ -0,0 +1,20 @@
//! (StoredU64, StoredU64) -> StoredF32 ratio
use brk_types::{StoredF32, StoredU64};
use vecdb::BinaryTransform;
/// (StoredU64, StoredU64) -> StoredF32 ratio (a/b)
pub struct RatioU64F32;
impl BinaryTransform<StoredU64, StoredU64, StoredF32> for RatioU64F32 {
#[inline(always)]
fn apply(numerator: StoredU64, denominator: StoredU64) -> StoredF32 {
let num: f64 = (*numerator) as f64;
let den: f64 = (*denominator) as f64;
if den == 0.0 {
StoredF32::from(0.0)
} else {
StoredF32::from(num / den)
}
}
}
@@ -0,0 +1,13 @@
use brk_types::StoredU64;
use vecdb::BinaryTransform;
/// (StoredU64, StoredU64) -> StoredU64 addition
/// Used for computing total_addr_count = addr_count + empty_addr_count
pub struct U64Plus;
impl BinaryTransform<StoredU64, StoredU64, StoredU64> for U64Plus {
#[inline(always)]
fn apply(lhs: StoredU64, rhs: StoredU64) -> StoredU64 {
StoredU64::from(u64::from(lhs) + u64::from(rhs))
}
}