mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-17 05:58:11 -07:00
computer: stateful snapshot
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::{Traversable, TreeNode};
|
||||
use brk_types::{Dollars, Height, Version};
|
||||
use brk_types::{DateIndex, Dollars, Version};
|
||||
use vecdb::{AnyExportableVec, AnyStoredVec, Database, EagerVec, Exit, GenericStoredVec, PcoVec};
|
||||
|
||||
use crate::{Indexes, indexes, stateful::Flushable};
|
||||
|
||||
use super::{ComputedVecsFromHeight, Source, VecBuilderOptions};
|
||||
use super::{ComputedVecsFromDateIndex, Source, VecBuilderOptions};
|
||||
|
||||
pub const PERCENTILES: [u8; 19] = [
|
||||
5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95,
|
||||
@@ -14,7 +14,7 @@ pub const PERCENTILES_LEN: usize = PERCENTILES.len();
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PricePercentiles {
|
||||
pub vecs: [Option<ComputedVecsFromHeight<Dollars>>; PERCENTILES_LEN],
|
||||
pub vecs: [Option<ComputedVecsFromDateIndex<Dollars>>; PERCENTILES_LEN],
|
||||
}
|
||||
|
||||
const VERSION: Version = Version::ZERO;
|
||||
@@ -29,7 +29,7 @@ impl PricePercentiles {
|
||||
) -> Result<Self> {
|
||||
let vecs = PERCENTILES.map(|p| {
|
||||
compute.then(|| {
|
||||
ComputedVecsFromHeight::forced_import(
|
||||
ComputedVecsFromDateIndex::forced_import(
|
||||
db,
|
||||
&format!("{name}_price_pct{p:02}"),
|
||||
Source::Compute,
|
||||
@@ -44,17 +44,19 @@ impl PricePercentiles {
|
||||
Ok(Self { vecs })
|
||||
}
|
||||
|
||||
/// Push percentile prices at date boundary.
|
||||
/// Only called when dateindex is Some (last height of the day).
|
||||
pub fn truncate_push(
|
||||
&mut self,
|
||||
height: Height,
|
||||
dateindex: DateIndex,
|
||||
percentile_prices: &[Dollars; PERCENTILES_LEN],
|
||||
) -> Result<()> {
|
||||
for (i, vec) in self.vecs.iter_mut().enumerate() {
|
||||
if let Some(v) = vec {
|
||||
v.height
|
||||
v.dateindex
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.truncate_push(height, percentile_prices[i])?;
|
||||
.truncate_push(dateindex, percentile_prices[i])?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -62,22 +64,20 @@ impl PricePercentiles {
|
||||
|
||||
pub fn compute_rest(
|
||||
&mut self,
|
||||
indexes: &indexes::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
for vec in self.vecs.iter_mut().flatten() {
|
||||
vec.compute_rest(
|
||||
indexes,
|
||||
starting_indexes,
|
||||
exit,
|
||||
None::<&EagerVec<PcoVec<Height, Dollars>>>,
|
||||
None::<&EagerVec<PcoVec<DateIndex, Dollars>>>,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get(&self, percentile: u8) -> Option<&ComputedVecsFromHeight<Dollars>> {
|
||||
pub fn get(&self, percentile: u8) -> Option<&ComputedVecsFromDateIndex<Dollars>> {
|
||||
PERCENTILES
|
||||
.iter()
|
||||
.position(|&p| p == percentile)
|
||||
@@ -88,8 +88,8 @@ impl PricePercentiles {
|
||||
impl Flushable for PricePercentiles {
|
||||
fn safe_flush(&mut self, exit: &Exit) -> Result<()> {
|
||||
for vec in self.vecs.iter_mut().flatten() {
|
||||
if let Some(height_vec) = vec.height.as_mut() {
|
||||
height_vec.safe_flush(exit)?;
|
||||
if let Some(dateindex_vec) = vec.dateindex.as_mut() {
|
||||
dateindex_vec.safe_flush(exit)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -99,8 +99,8 @@ impl Flushable for PricePercentiles {
|
||||
impl PricePercentiles {
|
||||
pub fn safe_write(&mut self, exit: &Exit) -> Result<()> {
|
||||
for vec in self.vecs.iter_mut().flatten() {
|
||||
if let Some(height_vec) = vec.height.as_mut() {
|
||||
height_vec.safe_write(exit)?;
|
||||
if let Some(dateindex_vec) = vec.dateindex.as_mut() {
|
||||
dateindex_vec.safe_write(exit)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -109,8 +109,8 @@ impl PricePercentiles {
|
||||
/// Validate computed versions or reset if mismatched.
|
||||
pub fn validate_computed_version_or_reset(&mut self, version: Version) -> Result<()> {
|
||||
for vec in self.vecs.iter_mut().flatten() {
|
||||
if let Some(height_vec) = vec.height.as_mut() {
|
||||
height_vec.validate_computed_version_or_reset(version)?;
|
||||
if let Some(dateindex_vec) = vec.dateindex.as_mut() {
|
||||
dateindex_vec.validate_computed_version_or_reset(version)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user