computer: part 4

This commit is contained in:
nym21
2025-04-05 12:12:55 +02:00
parent 6b1863d3b4
commit 0d4f4aec4e
22 changed files with 822 additions and 750 deletions

View File

@@ -1,21 +1,21 @@
use std::{fs, path::Path};
use brk_core::{CheckedSub, Dateindex, Height, Timestamp};
use brk_core::{CheckedSub, Dateindex, Timestamp};
use brk_exit::Exit;
use brk_indexer::Indexer;
use brk_vec::{AnyStorableVec, Compressed, Version};
use super::{
Indexes, StorableVec, indexes,
stats::{StorableVecGeneatorOptions, StorableVecsStatsFromHeight},
ComputedVec, Indexes,
grouped::{ComputedVecsFromHeight, StorableVecGeneatorOptions},
indexes,
};
#[derive(Clone)]
pub struct Vecs {
pub height_to_block_interval: StorableVec<Height, Timestamp>,
pub indexes_to_block_interval_stats: StorableVecsStatsFromHeight<Timestamp>,
pub dateindex_to_block_count: StorableVec<Dateindex, u16>,
pub dateindex_to_total_block_count: StorableVec<Dateindex, u32>,
pub indexes_to_block_interval: ComputedVecsFromHeight<Timestamp>,
pub dateindex_to_block_count: ComputedVec<Dateindex, u16>,
pub dateindex_to_total_block_count: ComputedVec<Dateindex, u32>,
}
impl Vecs {
@@ -23,25 +23,22 @@ impl Vecs {
fs::create_dir_all(path)?;
Ok(Self {
height_to_block_interval: StorableVec::forced_import(
&path.join("height_to_block_interval"),
indexes_to_block_interval: ComputedVecsFromHeight::forced_import(
path,
"block_interval",
Version::ONE,
compressed,
)?,
indexes_to_block_interval_stats: StorableVecsStatsFromHeight::forced_import(
&path.join("block_interval"),
compressed,
StorableVecGeneatorOptions::default()
.add_percentiles()
.add_minmax()
.add_average(),
)?,
dateindex_to_block_count: StorableVec::forced_import(
dateindex_to_block_count: ComputedVec::forced_import(
&path.join("dateindex_to_block_count"),
Version::ONE,
compressed,
)?,
dateindex_to_total_block_count: StorableVec::forced_import(
dateindex_to_total_block_count: ComputedVec::forced_import(
&path.join("dateindex_to_total_block_count"),
Version::ONE,
compressed,
@@ -58,23 +55,23 @@ impl Vecs {
) -> color_eyre::Result<()> {
let indexer_vecs = indexer.mut_vecs();
self.height_to_block_interval.compute_transform(
starting_indexes.height,
indexer_vecs.height_to_timestamp.mut_vec(),
|(height, timestamp, _, height_to_timestamp)| {
let interval = height.decremented().map_or(Timestamp::ZERO, |prev_h| {
let prev_timestamp = *height_to_timestamp.get(prev_h).unwrap().unwrap();
timestamp
.checked_sub(prev_timestamp)
.unwrap_or(Timestamp::ZERO)
});
(height, interval)
self.indexes_to_block_interval.compute(
|v| {
v.compute_transform(
starting_indexes.height,
indexer_vecs.height_to_timestamp.mut_vec(),
|(height, timestamp, _, height_to_timestamp)| {
let interval = height.decremented().map_or(Timestamp::ZERO, |prev_h| {
let prev_timestamp = *height_to_timestamp.get(prev_h).unwrap().unwrap();
timestamp
.checked_sub(prev_timestamp)
.unwrap_or(Timestamp::ZERO)
});
(height, interval)
},
exit,
)
},
exit,
)?;
self.indexes_to_block_interval_stats.compute(
&mut self.height_to_block_interval,
indexes,
starting_indexes,
exit,
@@ -86,11 +83,10 @@ impl Vecs {
pub fn as_any_vecs(&self) -> Vec<&dyn AnyStorableVec> {
[
vec![
self.height_to_block_interval.any_vec(),
self.dateindex_to_block_count.any_vec(),
self.dateindex_to_total_block_count.any_vec(),
],
self.indexes_to_block_interval_stats.as_any_vecs(),
self.indexes_to_block_interval.any_vecs(),
]
.concat()
}