mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-26 01:34:45 -07:00
computer: stateful snapshot
This commit is contained in:
159
crates/brk_grouper/src/by_year.rs
Normal file
159
crates/brk_grouper/src/by_year.rs
Normal file
@@ -0,0 +1,159 @@
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Timestamp, Year};
|
||||
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||
|
||||
use super::Filter;
|
||||
|
||||
#[derive(Default, Clone, Traversable)]
|
||||
pub struct ByYear<T> {
|
||||
pub _2009: T,
|
||||
pub _2010: T,
|
||||
pub _2011: T,
|
||||
pub _2012: T,
|
||||
pub _2013: T,
|
||||
pub _2014: T,
|
||||
pub _2015: T,
|
||||
pub _2016: T,
|
||||
pub _2017: T,
|
||||
pub _2018: T,
|
||||
pub _2019: T,
|
||||
pub _2020: T,
|
||||
pub _2021: T,
|
||||
pub _2022: T,
|
||||
pub _2023: T,
|
||||
pub _2024: T,
|
||||
pub _2025: T,
|
||||
pub _2026: T,
|
||||
}
|
||||
|
||||
impl<T> ByYear<T> {
|
||||
pub fn new<F>(mut create: F) -> Self
|
||||
where
|
||||
F: FnMut(Filter) -> T,
|
||||
{
|
||||
Self {
|
||||
_2009: create(Filter::Year(Year::new(2009))),
|
||||
_2010: create(Filter::Year(Year::new(2010))),
|
||||
_2011: create(Filter::Year(Year::new(2011))),
|
||||
_2012: create(Filter::Year(Year::new(2012))),
|
||||
_2013: create(Filter::Year(Year::new(2013))),
|
||||
_2014: create(Filter::Year(Year::new(2014))),
|
||||
_2015: create(Filter::Year(Year::new(2015))),
|
||||
_2016: create(Filter::Year(Year::new(2016))),
|
||||
_2017: create(Filter::Year(Year::new(2017))),
|
||||
_2018: create(Filter::Year(Year::new(2018))),
|
||||
_2019: create(Filter::Year(Year::new(2019))),
|
||||
_2020: create(Filter::Year(Year::new(2020))),
|
||||
_2021: create(Filter::Year(Year::new(2021))),
|
||||
_2022: create(Filter::Year(Year::new(2022))),
|
||||
_2023: create(Filter::Year(Year::new(2023))),
|
||||
_2024: create(Filter::Year(Year::new(2024))),
|
||||
_2025: create(Filter::Year(Year::new(2025))),
|
||||
_2026: create(Filter::Year(Year::new(2026))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
||||
[
|
||||
&self._2009,
|
||||
&self._2010,
|
||||
&self._2011,
|
||||
&self._2012,
|
||||
&self._2013,
|
||||
&self._2014,
|
||||
&self._2015,
|
||||
&self._2016,
|
||||
&self._2017,
|
||||
&self._2018,
|
||||
&self._2019,
|
||||
&self._2020,
|
||||
&self._2021,
|
||||
&self._2022,
|
||||
&self._2023,
|
||||
&self._2024,
|
||||
&self._2025,
|
||||
&self._2026,
|
||||
]
|
||||
.into_iter()
|
||||
}
|
||||
|
||||
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
|
||||
[
|
||||
&mut self._2009,
|
||||
&mut self._2010,
|
||||
&mut self._2011,
|
||||
&mut self._2012,
|
||||
&mut self._2013,
|
||||
&mut self._2014,
|
||||
&mut self._2015,
|
||||
&mut self._2016,
|
||||
&mut self._2017,
|
||||
&mut self._2018,
|
||||
&mut self._2019,
|
||||
&mut self._2020,
|
||||
&mut self._2021,
|
||||
&mut self._2022,
|
||||
&mut self._2023,
|
||||
&mut self._2024,
|
||||
&mut self._2025,
|
||||
&mut self._2026,
|
||||
]
|
||||
.into_iter()
|
||||
}
|
||||
|
||||
pub fn par_iter_mut(&mut self) -> impl ParallelIterator<Item = &mut T>
|
||||
where
|
||||
T: Send + Sync,
|
||||
{
|
||||
[
|
||||
&mut self._2009,
|
||||
&mut self._2010,
|
||||
&mut self._2011,
|
||||
&mut self._2012,
|
||||
&mut self._2013,
|
||||
&mut self._2014,
|
||||
&mut self._2015,
|
||||
&mut self._2016,
|
||||
&mut self._2017,
|
||||
&mut self._2018,
|
||||
&mut self._2019,
|
||||
&mut self._2020,
|
||||
&mut self._2021,
|
||||
&mut self._2022,
|
||||
&mut self._2023,
|
||||
&mut self._2024,
|
||||
&mut self._2025,
|
||||
&mut self._2026,
|
||||
]
|
||||
.into_par_iter()
|
||||
}
|
||||
|
||||
pub fn mut_vec_from_timestamp(&mut self, timestamp: Timestamp) -> &mut T {
|
||||
let year = Year::from(timestamp);
|
||||
self.get_mut(year)
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, year: Year) -> &mut T {
|
||||
match u16::from(year) {
|
||||
2009 => &mut self._2009,
|
||||
2010 => &mut self._2010,
|
||||
2011 => &mut self._2011,
|
||||
2012 => &mut self._2012,
|
||||
2013 => &mut self._2013,
|
||||
2014 => &mut self._2014,
|
||||
2015 => &mut self._2015,
|
||||
2016 => &mut self._2016,
|
||||
2017 => &mut self._2017,
|
||||
2018 => &mut self._2018,
|
||||
2019 => &mut self._2019,
|
||||
2020 => &mut self._2020,
|
||||
2021 => &mut self._2021,
|
||||
2022 => &mut self._2022,
|
||||
2023 => &mut self._2023,
|
||||
2024 => &mut self._2024,
|
||||
2025 => &mut self._2025,
|
||||
2026 => &mut self._2026,
|
||||
_ => todo!("Year {} not yet supported", u16::from(year)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use brk_types::{HalvingEpoch, OutputType, Sats};
|
||||
use brk_types::{HalvingEpoch, OutputType, Sats, Year};
|
||||
|
||||
use super::{AmountFilter, CohortContext, Term, TimeFilter};
|
||||
|
||||
@@ -9,6 +9,7 @@ pub enum Filter {
|
||||
Time(TimeFilter),
|
||||
Amount(AmountFilter),
|
||||
Epoch(HalvingEpoch),
|
||||
Year(Year),
|
||||
Type(OutputType),
|
||||
}
|
||||
|
||||
@@ -35,6 +36,7 @@ impl Filter {
|
||||
Filter::Time(t) => t.to_name_suffix(),
|
||||
Filter::Amount(a) => a.to_name_suffix(),
|
||||
Filter::Epoch(e) => format!("epoch_{}", usize::from(*e)),
|
||||
Filter::Year(y) => format!("year_{}", u16::from(*y)),
|
||||
Filter::Type(t) => match t {
|
||||
OutputType::P2MS => "p2ms_outputs".to_string(),
|
||||
OutputType::Empty => "empty_outputs".to_string(),
|
||||
@@ -57,7 +59,7 @@ impl Filter {
|
||||
}
|
||||
|
||||
let needs_prefix = match self {
|
||||
Filter::All | Filter::Term(_) | Filter::Epoch(_) | Filter::Type(_) => false,
|
||||
Filter::All | Filter::Term(_) | Filter::Epoch(_) | Filter::Year(_) | Filter::Type(_) => false,
|
||||
Filter::Time(_) | Filter::Amount(_) => true,
|
||||
};
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ mod by_amount_range;
|
||||
mod by_any_address;
|
||||
mod by_epoch;
|
||||
mod by_ge_amount;
|
||||
mod by_year;
|
||||
mod by_lt_amount;
|
||||
mod by_max_age;
|
||||
mod by_min_age;
|
||||
@@ -31,6 +32,7 @@ pub use by_amount_range::*;
|
||||
pub use by_any_address::*;
|
||||
pub use by_epoch::*;
|
||||
pub use by_ge_amount::*;
|
||||
pub use by_year::*;
|
||||
pub use by_lt_amount::*;
|
||||
pub use by_max_age::*;
|
||||
pub use by_min_age::*;
|
||||
|
||||
@@ -3,7 +3,7 @@ use rayon::prelude::*;
|
||||
|
||||
use crate::{
|
||||
ByAgeRange, ByAmountRange, ByEpoch, ByGreatEqualAmount, ByLowerThanAmount, ByMaxAge, ByMinAge,
|
||||
BySpendableType, ByTerm, Filter,
|
||||
BySpendableType, ByTerm, ByYear, Filter,
|
||||
};
|
||||
|
||||
#[derive(Default, Clone, Traversable)]
|
||||
@@ -11,6 +11,7 @@ pub struct UTXOGroups<T> {
|
||||
pub all: T,
|
||||
pub age_range: ByAgeRange<T>,
|
||||
pub epoch: ByEpoch<T>,
|
||||
pub year: ByYear<T>,
|
||||
pub min_age: ByMinAge<T>,
|
||||
pub ge_amount: ByGreatEqualAmount<T>,
|
||||
pub amount_range: ByAmountRange<T>,
|
||||
@@ -29,6 +30,7 @@ impl<T> UTXOGroups<T> {
|
||||
all: create(Filter::All),
|
||||
age_range: ByAgeRange::new(&mut create),
|
||||
epoch: ByEpoch::new(&mut create),
|
||||
year: ByYear::new(&mut create),
|
||||
min_age: ByMinAge::new(&mut create),
|
||||
ge_amount: ByGreatEqualAmount::new(&mut create),
|
||||
amount_range: ByAmountRange::new(&mut create),
|
||||
@@ -48,6 +50,7 @@ impl<T> UTXOGroups<T> {
|
||||
.chain(self.ge_amount.iter())
|
||||
.chain(self.age_range.iter())
|
||||
.chain(self.epoch.iter())
|
||||
.chain(self.year.iter())
|
||||
.chain(self.amount_range.iter())
|
||||
.chain(self.lt_amount.iter())
|
||||
.chain(self.type_.iter())
|
||||
@@ -62,6 +65,7 @@ impl<T> UTXOGroups<T> {
|
||||
.chain(self.ge_amount.iter_mut())
|
||||
.chain(self.age_range.iter_mut())
|
||||
.chain(self.epoch.iter_mut())
|
||||
.chain(self.year.iter_mut())
|
||||
.chain(self.amount_range.iter_mut())
|
||||
.chain(self.lt_amount.iter_mut())
|
||||
.chain(self.type_.iter_mut())
|
||||
@@ -79,6 +83,7 @@ impl<T> UTXOGroups<T> {
|
||||
.chain(self.ge_amount.par_iter_mut())
|
||||
.chain(self.age_range.par_iter_mut())
|
||||
.chain(self.epoch.par_iter_mut())
|
||||
.chain(self.year.par_iter_mut())
|
||||
.chain(self.amount_range.par_iter_mut())
|
||||
.chain(self.lt_amount.par_iter_mut())
|
||||
.chain(self.type_.par_iter_mut())
|
||||
@@ -88,6 +93,7 @@ impl<T> UTXOGroups<T> {
|
||||
self.age_range
|
||||
.iter()
|
||||
.chain(self.epoch.iter())
|
||||
.chain(self.year.iter())
|
||||
.chain(self.amount_range.iter())
|
||||
.chain(self.type_.iter())
|
||||
}
|
||||
@@ -96,6 +102,7 @@ impl<T> UTXOGroups<T> {
|
||||
self.age_range
|
||||
.iter_mut()
|
||||
.chain(self.epoch.iter_mut())
|
||||
.chain(self.year.iter_mut())
|
||||
.chain(self.amount_range.iter_mut())
|
||||
.chain(self.type_.iter_mut())
|
||||
}
|
||||
@@ -107,6 +114,7 @@ impl<T> UTXOGroups<T> {
|
||||
self.age_range
|
||||
.par_iter_mut()
|
||||
.chain(self.epoch.par_iter_mut())
|
||||
.chain(self.year.par_iter_mut())
|
||||
.chain(self.amount_range.par_iter_mut())
|
||||
.chain(self.type_.par_iter_mut())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user