mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 06:39:58 -07:00
computer: part 2
This commit is contained in:
@@ -82,7 +82,7 @@ where
|
||||
self.vec.version()
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
pub fn len(&self) -> usize {
|
||||
self.vec.len()
|
||||
}
|
||||
|
||||
@@ -98,6 +98,10 @@ where
|
||||
&self.vec
|
||||
}
|
||||
|
||||
pub fn mut_any_vec(&mut self) -> &mut dyn AnyStorableVec {
|
||||
&mut self.vec
|
||||
}
|
||||
|
||||
pub fn get(&mut self, index: I) -> Result<Option<&T>> {
|
||||
self.vec.get(index)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use brk_core::{CheckedSub, Height, Timestamp};
|
||||
use brk_core::{CheckedSub, Dateindex, Height, Timestamp};
|
||||
use brk_exit::Exit;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_vec::{AnyStorableVec, Compressed, Version};
|
||||
@@ -14,8 +14,8 @@ use super::{
|
||||
pub struct Vecs {
|
||||
pub height_to_block_interval: StorableVec<Height, Timestamp>,
|
||||
pub indexes_to_block_interval_stats: StorableVecGeneatorByIndex<Timestamp>,
|
||||
pub dateindex_to_block_count: StorableVec<Height, u16>,
|
||||
pub dateindex_to_total_block_count: StorableVec<Height, u32>,
|
||||
pub dateindex_to_block_count: StorableVec<Dateindex, u16>,
|
||||
pub dateindex_to_total_block_count: StorableVec<Dateindex, u32>,
|
||||
}
|
||||
|
||||
impl Vecs {
|
||||
@@ -61,15 +61,15 @@ impl Vecs {
|
||||
self.height_to_block_interval.compute_transform(
|
||||
starting_indexes.height,
|
||||
indexer_vecs.height_to_timestamp.mut_vec(),
|
||||
|(height, timestamp, height_to_timestamp, ..)| {
|
||||
|(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();
|
||||
if prev_timestamp >= timestamp {
|
||||
Timestamp::ZERO
|
||||
} else {
|
||||
timestamp.checked_sub(prev_timestamp).unwrap()
|
||||
}
|
||||
dbg!((timestamp, prev_timestamp));
|
||||
timestamp
|
||||
.checked_sub(prev_timestamp)
|
||||
.unwrap_or(Timestamp::ZERO)
|
||||
});
|
||||
dbg!((height, interval));
|
||||
(height, interval)
|
||||
},
|
||||
exit,
|
||||
@@ -77,10 +77,10 @@ impl Vecs {
|
||||
|
||||
self.indexes_to_block_interval_stats.compute(
|
||||
&mut self.height_to_block_interval,
|
||||
indexer,
|
||||
indexes,
|
||||
starting_indexes,
|
||||
);
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use std::{fs, ops::Deref, path::Path};
|
||||
|
||||
use brk_core::{Date, Dateindex, Height, Txindex, Txinindex, Txoutindex};
|
||||
use brk_core::{
|
||||
Date, Dateindex, Decadeindex, Difficultyepoch, Halvingepoch, Height, Monthindex, Txindex,
|
||||
Txinindex, Txoutindex, Weekindex, Yearindex,
|
||||
};
|
||||
use brk_exit::Exit;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_vec::{AnyStorableVec, Compressed, Version};
|
||||
@@ -15,13 +18,37 @@ pub struct Vecs {
|
||||
pub dateindex_to_dateindex: StorableVec<Dateindex, Dateindex>,
|
||||
pub dateindex_to_first_height: StorableVec<Dateindex, Height>,
|
||||
pub dateindex_to_last_height: StorableVec<Dateindex, Height>,
|
||||
pub dateindex_to_monthindex: StorableVec<Dateindex, Monthindex>,
|
||||
pub dateindex_to_weekindex: StorableVec<Dateindex, Weekindex>,
|
||||
pub decadeindex_to_decadeindex: StorableVec<Decadeindex, Decadeindex>,
|
||||
pub decadeindex_to_first_yearindex: StorableVec<Decadeindex, Yearindex>,
|
||||
pub decadeindex_to_last_yearindex: StorableVec<Decadeindex, Yearindex>,
|
||||
pub difficultyepoch_to_difficultyepoch: StorableVec<Difficultyepoch, Difficultyepoch>,
|
||||
pub difficultyepoch_to_first_height: StorableVec<Difficultyepoch, Height>,
|
||||
pub difficultyepoch_to_last_height: StorableVec<Difficultyepoch, Height>,
|
||||
pub halvingepoch_to_first_height: StorableVec<Halvingepoch, Height>,
|
||||
pub halvingepoch_to_halvingepoch: StorableVec<Halvingepoch, Halvingepoch>,
|
||||
pub halvingepoch_to_last_height: StorableVec<Halvingepoch, Height>,
|
||||
pub height_to_dateindex: StorableVec<Height, Dateindex>,
|
||||
pub height_to_difficultyepoch: StorableVec<Height, Difficultyepoch>,
|
||||
pub height_to_fixed_date: StorableVec<Height, Date>,
|
||||
pub height_to_halvingepoch: StorableVec<Height, Halvingepoch>,
|
||||
pub height_to_height: StorableVec<Height, Height>,
|
||||
pub height_to_last_txindex: StorableVec<Height, Txindex>,
|
||||
pub height_to_real_date: StorableVec<Height, Date>,
|
||||
pub monthindex_to_first_dateindex: StorableVec<Monthindex, Dateindex>,
|
||||
pub monthindex_to_last_dateindex: StorableVec<Monthindex, Dateindex>,
|
||||
pub monthindex_to_monthindex: StorableVec<Monthindex, Monthindex>,
|
||||
pub monthindex_to_yearindex: StorableVec<Monthindex, Yearindex>,
|
||||
pub txindex_to_last_txinindex: StorableVec<Txindex, Txinindex>,
|
||||
pub txindex_to_last_txoutindex: StorableVec<Txindex, Txoutindex>,
|
||||
pub weekindex_to_first_dateindex: StorableVec<Weekindex, Dateindex>,
|
||||
pub weekindex_to_last_dateindex: StorableVec<Weekindex, Dateindex>,
|
||||
pub weekindex_to_weekindex: StorableVec<Weekindex, Weekindex>,
|
||||
pub yearindex_to_decadeindex: StorableVec<Yearindex, Decadeindex>,
|
||||
pub yearindex_to_first_monthindex: StorableVec<Yearindex, Monthindex>,
|
||||
pub yearindex_to_last_monthindex: StorableVec<Yearindex, Monthindex>,
|
||||
pub yearindex_to_yearindex: StorableVec<Yearindex, Yearindex>,
|
||||
}
|
||||
|
||||
impl Vecs {
|
||||
@@ -74,7 +101,6 @@ impl Vecs {
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
|
||||
txindex_to_last_txinindex: StorableVec::forced_import(
|
||||
&path.join("txindex_to_last_txinindex"),
|
||||
Version::from(1),
|
||||
@@ -85,6 +111,126 @@ impl Vecs {
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
difficultyepoch_to_first_height: StorableVec::forced_import(
|
||||
&path.join("difficultyepoch_to_first_height"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
difficultyepoch_to_last_height: StorableVec::forced_import(
|
||||
&path.join("difficultyepoch_to_last_height"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
halvingepoch_to_first_height: StorableVec::forced_import(
|
||||
&path.join("halvingepoch_to_first_height"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
halvingepoch_to_last_height: StorableVec::forced_import(
|
||||
&path.join("halvingepoch_to_last_height"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
weekindex_to_first_dateindex: StorableVec::forced_import(
|
||||
&path.join("weekindex_to_first_dateindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
weekindex_to_last_dateindex: StorableVec::forced_import(
|
||||
&path.join("weekindex_to_last_dateindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
monthindex_to_first_dateindex: StorableVec::forced_import(
|
||||
&path.join("monthindex_to_first_dateindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
monthindex_to_last_dateindex: StorableVec::forced_import(
|
||||
&path.join("monthindex_to_last_dateindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
yearindex_to_first_monthindex: StorableVec::forced_import(
|
||||
&path.join("yearindex_to_first_monthindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
yearindex_to_last_monthindex: StorableVec::forced_import(
|
||||
&path.join("yearindex_to_last_monthindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
decadeindex_to_first_yearindex: StorableVec::forced_import(
|
||||
&path.join("decadeindex_to_first_yearindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
decadeindex_to_last_yearindex: StorableVec::forced_import(
|
||||
&path.join("decadeindex_to_last_yearindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
dateindex_to_weekindex: StorableVec::forced_import(
|
||||
&path.join("dateindex_to_weekindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
dateindex_to_monthindex: StorableVec::forced_import(
|
||||
&path.join("dateindex_to_monthindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
monthindex_to_yearindex: StorableVec::forced_import(
|
||||
&path.join("monthindex_to_yearindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
yearindex_to_decadeindex: StorableVec::forced_import(
|
||||
&path.join("yearindex_to_decadeindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
height_to_difficultyepoch: StorableVec::forced_import(
|
||||
&path.join("height_to_difficultyepoch"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
height_to_halvingepoch: StorableVec::forced_import(
|
||||
&path.join("height_to_halvingepoch"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
weekindex_to_weekindex: StorableVec::forced_import(
|
||||
&path.join("weekindex_to_weekindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
monthindex_to_monthindex: StorableVec::forced_import(
|
||||
&path.join("monthindex_to_monthindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
yearindex_to_yearindex: StorableVec::forced_import(
|
||||
&path.join("yearindex_to_yearindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
decadeindex_to_decadeindex: StorableVec::forced_import(
|
||||
&path.join("decadeindex_to_decadeindex"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
difficultyepoch_to_difficultyepoch: StorableVec::forced_import(
|
||||
&path.join("difficultyepoch_to_difficultyepoch"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
halvingepoch_to_halvingepoch: StorableVec::forced_import(
|
||||
&path.join("halvingepoch_to_halvingepoch"),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -132,6 +278,12 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let starting_dateindex = self
|
||||
.height_to_dateindex
|
||||
.get(starting_indexes.height.decremented().unwrap_or_default())?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
|
||||
self.height_to_dateindex.compute_transform(
|
||||
starting_indexes.height,
|
||||
self.height_to_fixed_date.mut_vec(),
|
||||
@@ -139,11 +291,15 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let starting_dateindex = self
|
||||
let starting_dateindex = if let Some(dateindex) = self
|
||||
.height_to_dateindex
|
||||
.get(starting_indexes.height.decremented().unwrap_or_default())?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
{
|
||||
starting_dateindex.min(dateindex)
|
||||
} else {
|
||||
starting_dateindex
|
||||
};
|
||||
|
||||
self.dateindex_to_first_height
|
||||
.compute_inverse_more_to_less(
|
||||
@@ -152,6 +308,8 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let date_count = self.dateindex_to_first_height.len();
|
||||
|
||||
self.dateindex_to_last_height
|
||||
.compute_last_index_from_first(
|
||||
starting_dateindex,
|
||||
@@ -197,7 +355,242 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(Indexes::from((starting_indexes, starting_dateindex)))
|
||||
// ---
|
||||
|
||||
let starting_weekindex = self
|
||||
.dateindex_to_weekindex
|
||||
.get(starting_dateindex)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
|
||||
self.dateindex_to_weekindex.compute_transform(
|
||||
starting_dateindex,
|
||||
self.dateindex_to_dateindex.mut_vec(),
|
||||
|(di, ..)| (di, Weekindex::from(di)),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.weekindex_to_first_dateindex
|
||||
.compute_inverse_more_to_less(
|
||||
starting_dateindex,
|
||||
self.dateindex_to_weekindex.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.weekindex_to_last_dateindex
|
||||
.compute_last_index_from_first(
|
||||
starting_weekindex,
|
||||
self.weekindex_to_first_dateindex.mut_vec(),
|
||||
date_count,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.weekindex_to_weekindex.compute_transform(
|
||||
starting_weekindex,
|
||||
self.weekindex_to_first_dateindex.mut_vec(),
|
||||
|(wi, ..)| (wi, wi),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// ---
|
||||
|
||||
let starting_monthindex = self
|
||||
.dateindex_to_monthindex
|
||||
.get(starting_dateindex)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
|
||||
self.dateindex_to_monthindex.compute_transform(
|
||||
starting_dateindex,
|
||||
self.dateindex_to_dateindex.mut_vec(),
|
||||
|(di, ..)| (di, Monthindex::from(di)),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.monthindex_to_first_dateindex
|
||||
.compute_inverse_more_to_less(
|
||||
starting_dateindex,
|
||||
self.dateindex_to_monthindex.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let month_count = self.monthindex_to_first_dateindex.len();
|
||||
|
||||
self.monthindex_to_last_dateindex
|
||||
.compute_last_index_from_first(
|
||||
starting_monthindex,
|
||||
self.monthindex_to_first_dateindex.mut_vec(),
|
||||
date_count,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.monthindex_to_monthindex.compute_transform(
|
||||
starting_monthindex,
|
||||
self.monthindex_to_first_dateindex.mut_vec(),
|
||||
|(mi, ..)| (mi, mi),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// ---
|
||||
|
||||
let starting_yearindex = self
|
||||
.monthindex_to_yearindex
|
||||
.get(starting_monthindex)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
|
||||
self.monthindex_to_yearindex.compute_transform(
|
||||
starting_monthindex,
|
||||
self.monthindex_to_monthindex.mut_vec(),
|
||||
|(mi, ..)| (mi, Yearindex::from(mi)),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.yearindex_to_first_monthindex
|
||||
.compute_inverse_more_to_less(
|
||||
starting_monthindex,
|
||||
self.monthindex_to_yearindex.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let year_count = self.yearindex_to_first_monthindex.len();
|
||||
|
||||
self.yearindex_to_last_monthindex
|
||||
.compute_last_index_from_first(
|
||||
starting_yearindex,
|
||||
self.yearindex_to_first_monthindex.mut_vec(),
|
||||
month_count,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.yearindex_to_yearindex.compute_transform(
|
||||
starting_yearindex,
|
||||
self.yearindex_to_first_monthindex.mut_vec(),
|
||||
|(yi, ..)| (yi, yi),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// ---
|
||||
|
||||
let starting_decadeindex = self
|
||||
.yearindex_to_decadeindex
|
||||
.get(starting_yearindex)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
|
||||
self.yearindex_to_decadeindex.compute_transform(
|
||||
starting_yearindex,
|
||||
self.yearindex_to_yearindex.mut_vec(),
|
||||
|(yi, ..)| (yi, Decadeindex::from(yi)),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.decadeindex_to_first_yearindex
|
||||
.compute_inverse_more_to_less(
|
||||
starting_yearindex,
|
||||
self.yearindex_to_decadeindex.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.decadeindex_to_last_yearindex
|
||||
.compute_last_index_from_first(
|
||||
starting_decadeindex,
|
||||
self.decadeindex_to_first_yearindex.mut_vec(),
|
||||
year_count,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.decadeindex_to_decadeindex.compute_transform(
|
||||
starting_decadeindex,
|
||||
self.decadeindex_to_first_yearindex.mut_vec(),
|
||||
|(di, ..)| (di, di),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// ---
|
||||
|
||||
let starting_difficultyepoch = self
|
||||
.height_to_difficultyepoch
|
||||
.get(starting_indexes.height)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
|
||||
self.height_to_difficultyepoch.compute_transform(
|
||||
starting_indexes.height,
|
||||
self.height_to_height.mut_vec(),
|
||||
|(h, ..)| (h, Difficultyepoch::from(h)),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.difficultyepoch_to_first_height
|
||||
.compute_inverse_more_to_less(
|
||||
starting_indexes.height,
|
||||
self.height_to_difficultyepoch.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.difficultyepoch_to_last_height
|
||||
.compute_last_index_from_first(
|
||||
starting_difficultyepoch,
|
||||
self.difficultyepoch_to_first_height.mut_vec(),
|
||||
height_count,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.difficultyepoch_to_difficultyepoch.compute_transform(
|
||||
starting_difficultyepoch,
|
||||
self.difficultyepoch_to_first_height.mut_vec(),
|
||||
|(de, ..)| (de, de),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// ---
|
||||
|
||||
let starting_halvingepoch = self
|
||||
.height_to_halvingepoch
|
||||
.get(starting_indexes.height)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
|
||||
self.height_to_halvingepoch.compute_transform(
|
||||
starting_indexes.height,
|
||||
self.height_to_height.mut_vec(),
|
||||
|(h, ..)| (h, Halvingepoch::from(h)),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.halvingepoch_to_first_height
|
||||
.compute_inverse_more_to_less(
|
||||
starting_indexes.height,
|
||||
self.height_to_halvingepoch.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.halvingepoch_to_last_height
|
||||
.compute_last_index_from_first(
|
||||
starting_halvingepoch,
|
||||
self.halvingepoch_to_first_height.mut_vec(),
|
||||
height_count,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.halvingepoch_to_halvingepoch.compute_transform(
|
||||
starting_halvingepoch,
|
||||
self.halvingepoch_to_first_height.mut_vec(),
|
||||
|(he, ..)| (he, he),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(Indexes {
|
||||
indexes: starting_indexes,
|
||||
dateindex: starting_dateindex,
|
||||
weekindex: starting_weekindex,
|
||||
monthindex: starting_monthindex,
|
||||
yearindex: starting_yearindex,
|
||||
decadeindex: starting_decadeindex,
|
||||
difficultyepoch: starting_difficultyepoch,
|
||||
halvingepoch: starting_halvingepoch,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn AnyStorableVec> {
|
||||
@@ -213,6 +606,30 @@ impl Vecs {
|
||||
self.height_to_real_date.any_vec(),
|
||||
self.txindex_to_last_txinindex.any_vec(),
|
||||
self.txindex_to_last_txoutindex.any_vec(),
|
||||
self.difficultyepoch_to_first_height.any_vec(),
|
||||
self.difficultyepoch_to_last_height.any_vec(),
|
||||
self.halvingepoch_to_first_height.any_vec(),
|
||||
self.halvingepoch_to_last_height.any_vec(),
|
||||
self.weekindex_to_first_dateindex.any_vec(),
|
||||
self.weekindex_to_last_dateindex.any_vec(),
|
||||
self.monthindex_to_first_dateindex.any_vec(),
|
||||
self.monthindex_to_last_dateindex.any_vec(),
|
||||
self.yearindex_to_first_monthindex.any_vec(),
|
||||
self.yearindex_to_last_monthindex.any_vec(),
|
||||
self.decadeindex_to_first_yearindex.any_vec(),
|
||||
self.decadeindex_to_last_yearindex.any_vec(),
|
||||
self.dateindex_to_weekindex.any_vec(),
|
||||
self.dateindex_to_monthindex.any_vec(),
|
||||
self.monthindex_to_yearindex.any_vec(),
|
||||
self.yearindex_to_decadeindex.any_vec(),
|
||||
self.height_to_difficultyepoch.any_vec(),
|
||||
self.height_to_halvingepoch.any_vec(),
|
||||
self.weekindex_to_weekindex.any_vec(),
|
||||
self.monthindex_to_monthindex.any_vec(),
|
||||
self.yearindex_to_yearindex.any_vec(),
|
||||
self.decadeindex_to_decadeindex.any_vec(),
|
||||
self.difficultyepoch_to_difficultyepoch.any_vec(),
|
||||
self.halvingepoch_to_halvingepoch.any_vec(),
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -220,6 +637,12 @@ impl Vecs {
|
||||
pub struct Indexes {
|
||||
indexes: brk_indexer::Indexes,
|
||||
pub dateindex: Dateindex,
|
||||
pub weekindex: Weekindex,
|
||||
pub monthindex: Monthindex,
|
||||
pub yearindex: Yearindex,
|
||||
pub decadeindex: Decadeindex,
|
||||
pub difficultyepoch: Difficultyepoch,
|
||||
pub halvingepoch: Halvingepoch,
|
||||
}
|
||||
|
||||
impl Deref for Indexes {
|
||||
@@ -228,8 +651,3 @@ impl Deref for Indexes {
|
||||
&self.indexes
|
||||
}
|
||||
}
|
||||
impl From<(brk_indexer::Indexes, Dateindex)> for Indexes {
|
||||
fn from((indexes, dateindex): (brk_indexer::Indexes, Dateindex)) -> Self {
|
||||
Self { indexes, dateindex }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,6 +66,7 @@ impl Vecs {
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn AnyStorableVec> {
|
||||
[
|
||||
self.indexes.as_any_vecs(),
|
||||
self.blocks.as_any_vecs(),
|
||||
self.transactions.as_any_vecs(),
|
||||
self.marketprice
|
||||
.as_ref()
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
use std::path::Path;
|
||||
|
||||
use brk_exit::Exit;
|
||||
use brk_vec::{AnyStorableVec, Compressed, Result, StoredIndex, Version};
|
||||
use brk_vec::{AnyStorableVec, Compressed, Result, StoredIndex, StoredType, Version};
|
||||
|
||||
use crate::storage::vecs::base::StorableVec;
|
||||
|
||||
use super::StoredType;
|
||||
use super::ComputedType;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StorableVecGeneator<I, T>
|
||||
pub struct StorableVecBuilder<I, T>
|
||||
where
|
||||
I: StoredIndex,
|
||||
T: StoredType,
|
||||
T: ComputedType,
|
||||
{
|
||||
// first: Option<StorableVec<I, T>>,
|
||||
first: Option<StorableVec<I, T>>,
|
||||
average: Option<StorableVec<I, T>>,
|
||||
sum: Option<StorableVec<I, T>>,
|
||||
max: Option<StorableVec<I, T>>,
|
||||
@@ -26,10 +26,10 @@ where
|
||||
last: Option<StorableVec<I, T>>,
|
||||
}
|
||||
|
||||
impl<I, T> StorableVecGeneator<I, T>
|
||||
impl<I, T> StorableVecBuilder<I, T>
|
||||
where
|
||||
I: StoredIndex,
|
||||
T: StoredType,
|
||||
T: ComputedType,
|
||||
{
|
||||
pub fn forced_import(
|
||||
path: &Path,
|
||||
@@ -37,17 +37,23 @@ where
|
||||
options: StorableVecGeneatorOptions,
|
||||
) -> color_eyre::Result<Self> {
|
||||
let name = path.file_name().unwrap().to_str().unwrap().to_string();
|
||||
let key = I::to_string().split("::").last().unwrap().to_lowercase();
|
||||
|
||||
// let prefix = |s: &str| path.with_file_name(format!("{s}_{name}"));
|
||||
let suffix = |s: &str| path.with_file_name(format!("{name}_{s}"));
|
||||
let prefix = |s: &str| path.with_file_name(format!("{key}_to_{s}_{name}"));
|
||||
let suffix = |s: &str| path.with_file_name(format!("{key}_to_{name}_{s}"));
|
||||
|
||||
let s = Self {
|
||||
// first: options.first.then(|| {
|
||||
// StorableVec::forced_import(&prefix("first"), Version::from(1), compressed).unwrap()
|
||||
// }),
|
||||
last: options
|
||||
.last
|
||||
.then(|| StorableVec::forced_import(path, Version::from(1), compressed).unwrap()),
|
||||
first: options.first.then(|| {
|
||||
StorableVec::forced_import(&prefix("first"), Version::from(1), compressed).unwrap()
|
||||
}),
|
||||
last: options.last.then(|| {
|
||||
StorableVec::forced_import(
|
||||
&path.with_file_name(format!("{key}_to_{name}")),
|
||||
Version::from(1),
|
||||
compressed,
|
||||
)
|
||||
.unwrap()
|
||||
}),
|
||||
min: options.min.then(|| {
|
||||
StorableVec::forced_import(&suffix("min"), Version::from(1), compressed).unwrap()
|
||||
}),
|
||||
@@ -91,7 +97,8 @@ where
|
||||
) -> Result<()>
|
||||
where
|
||||
I2: StoredIndex + StoredType,
|
||||
T: Ord,
|
||||
T: Ord + From<f64>,
|
||||
f64: From<T>,
|
||||
{
|
||||
let index = self.starting_index(max_from);
|
||||
|
||||
@@ -99,10 +106,10 @@ where
|
||||
let first_index = *first_index;
|
||||
let last_index = *last_indexes.get(i).unwrap().unwrap();
|
||||
|
||||
// if let Some(first) = self.first.as_mut() {
|
||||
// let v = source.read(first_index).unwrap().unwrap();
|
||||
// first.forced_push_at(index, v.clone(), exit);
|
||||
// }
|
||||
if let Some(first) = self.first.as_mut() {
|
||||
let v = source.get(first_index).unwrap().unwrap();
|
||||
first.forced_push_at(index, v.clone(), exit)?;
|
||||
}
|
||||
|
||||
if let Some(last) = self.last.as_mut() {
|
||||
let v = source.get(last_index).unwrap().unwrap();
|
||||
@@ -162,11 +169,13 @@ where
|
||||
let len = values.len();
|
||||
|
||||
if let Some(average) = self.average.as_mut() {
|
||||
let a = values
|
||||
let len = len as f64;
|
||||
let total = values
|
||||
.iter()
|
||||
.map(|v| v.clone() / len)
|
||||
.fold(T::from(0), |a, b| a + b);
|
||||
average.forced_push_at(i, a, exit)?;
|
||||
.map(|v| f64::from(v.clone()))
|
||||
.fold(0.0, |a, b| a + b);
|
||||
let avg = T::from(total / len);
|
||||
average.forced_push_at(i, avg, exit)?;
|
||||
}
|
||||
|
||||
if let Some(sum_vec) = self.sum.as_mut() {
|
||||
@@ -178,6 +187,127 @@ where
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
self.safe_flush(exit)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn from_aligned<I2>(
|
||||
&mut self,
|
||||
max_from: I,
|
||||
source: &mut StorableVecBuilder<I2, T>,
|
||||
first_indexes: &mut brk_vec::StorableVec<I, I2>,
|
||||
last_indexes: &mut brk_vec::StorableVec<I, I2>,
|
||||
exit: &Exit,
|
||||
) -> Result<()>
|
||||
where
|
||||
I2: StoredIndex + StoredType,
|
||||
T: Ord + From<f64>,
|
||||
f64: From<T>,
|
||||
{
|
||||
if self._90p.is_some()
|
||||
|| self._75p.is_some()
|
||||
|| self.median.is_some()
|
||||
|| self._25p.is_some()
|
||||
|| self._10p.is_some()
|
||||
{
|
||||
panic!("unsupported");
|
||||
}
|
||||
|
||||
let index = self.starting_index(max_from);
|
||||
|
||||
first_indexes.iter_from(index, |(i, first_index)| {
|
||||
let first_index = *first_index;
|
||||
let last_index = *last_indexes.get(i).unwrap().unwrap();
|
||||
|
||||
if let Some(first) = self.first.as_mut() {
|
||||
let v = source
|
||||
.first
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.get(last_index)
|
||||
.unwrap()
|
||||
.cloned()
|
||||
.unwrap();
|
||||
first.forced_push_at(index, v, exit)?;
|
||||
}
|
||||
|
||||
if let Some(last) = self.last.as_mut() {
|
||||
let v = source
|
||||
.last
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.get(last_index)
|
||||
.unwrap()
|
||||
.cloned()
|
||||
.unwrap();
|
||||
last.forced_push_at(index, v, exit)?;
|
||||
}
|
||||
|
||||
let first_index = Some(first_index.to_usize()? as i64);
|
||||
let last_index = Some(last_index.to_usize()? as i64);
|
||||
|
||||
let needs_sum_or_average = self.sum.is_some() || self.average.is_some();
|
||||
let needs_sorted = self.max.is_some() || self.min.is_some();
|
||||
let needs_values = needs_sorted || needs_sum_or_average;
|
||||
|
||||
if needs_values {
|
||||
if needs_sorted {
|
||||
if let Some(max) = self.max.as_mut() {
|
||||
let mut values = source
|
||||
.max
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.collect_range(first_index, last_index)?;
|
||||
values.sort_unstable();
|
||||
max.forced_push_at(i, values.last().unwrap().clone(), exit)?;
|
||||
}
|
||||
|
||||
if let Some(min) = self.min.as_mut() {
|
||||
let mut values = source
|
||||
.min
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.collect_range(first_index, last_index)?;
|
||||
values.sort_unstable();
|
||||
min.forced_push_at(i, values.first().unwrap().clone(), exit)?;
|
||||
}
|
||||
}
|
||||
|
||||
if needs_sum_or_average {
|
||||
if let Some(average) = self.average.as_mut() {
|
||||
let values = source
|
||||
.average
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.collect_range(first_index, last_index)?;
|
||||
let len = values.len() as f64;
|
||||
let total = values
|
||||
.into_iter()
|
||||
.map(|v| f64::from(v))
|
||||
.fold(0.0, |a, b| a + b);
|
||||
let avg = T::from(total / len);
|
||||
average.forced_push_at(i, avg, exit)?;
|
||||
}
|
||||
|
||||
if let Some(sum_vec) = self.sum.as_mut() {
|
||||
let values = source
|
||||
.sum
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.collect_range(first_index, last_index)?;
|
||||
let sum = values.into_iter().fold(T::from(0), |a, b| a + b);
|
||||
sum_vec.forced_push_at(i, sum, exit)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
self.safe_flush(exit)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -216,9 +346,9 @@ where
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn AnyStorableVec> {
|
||||
let mut v: Vec<&dyn AnyStorableVec> = vec![];
|
||||
|
||||
// if let Some(first) = self.first.as_ref() {
|
||||
// v.push(first.any_vec());
|
||||
// }
|
||||
if let Some(first) = self.first.as_ref() {
|
||||
v.push(first.any_vec());
|
||||
}
|
||||
if let Some(last) = self.last.as_ref() {
|
||||
v.push(last.any_vec());
|
||||
}
|
||||
@@ -252,6 +382,44 @@ where
|
||||
|
||||
v
|
||||
}
|
||||
|
||||
pub fn safe_flush(&mut self, exit: &Exit) -> Result<()> {
|
||||
if let Some(first) = self.first.as_mut() {
|
||||
first.safe_flush(exit)?;
|
||||
}
|
||||
if let Some(last) = self.last.as_mut() {
|
||||
last.safe_flush(exit)?;
|
||||
}
|
||||
if let Some(min) = self.min.as_mut() {
|
||||
min.safe_flush(exit)?;
|
||||
}
|
||||
if let Some(max) = self.max.as_mut() {
|
||||
max.safe_flush(exit)?;
|
||||
}
|
||||
if let Some(median) = self.median.as_mut() {
|
||||
median.safe_flush(exit)?;
|
||||
}
|
||||
if let Some(average) = self.average.as_mut() {
|
||||
average.safe_flush(exit)?;
|
||||
}
|
||||
if let Some(sum) = self.sum.as_mut() {
|
||||
sum.safe_flush(exit)?;
|
||||
}
|
||||
if let Some(_90p) = self._90p.as_mut() {
|
||||
_90p.safe_flush(exit)?;
|
||||
}
|
||||
if let Some(_75p) = self._75p.as_mut() {
|
||||
_75p.safe_flush(exit)?;
|
||||
}
|
||||
if let Some(_25p) = self._25p.as_mut() {
|
||||
_25p.safe_flush(exit)?;
|
||||
}
|
||||
if let Some(_10p) = self._10p.as_mut() {
|
||||
_10p.safe_flush(exit)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Copy)]
|
||||
@@ -265,15 +433,15 @@ pub struct StorableVecGeneatorOptions {
|
||||
_25p: bool,
|
||||
_10p: bool,
|
||||
min: bool,
|
||||
// first: bool,
|
||||
first: bool,
|
||||
last: bool,
|
||||
}
|
||||
|
||||
impl StorableVecGeneatorOptions {
|
||||
// pub fn add_first(mut self) -> Self {
|
||||
// self.first = true;
|
||||
// self
|
||||
// }
|
||||
pub fn add_first(mut self) -> Self {
|
||||
self.first = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_last(mut self) -> Self {
|
||||
self.last = true;
|
||||
|
||||
@@ -1,60 +1,108 @@
|
||||
use std::path::Path;
|
||||
|
||||
use brk_core::{
|
||||
Dateindex, Decadeindex, Difficultyepoch, Halvingepoch, Height, Monthindex, Weekindex, Yearindex,
|
||||
};
|
||||
use brk_indexer::{Indexer, Indexes};
|
||||
use brk_core::{Dateindex, Decadeindex, Difficultyepoch, Height, Monthindex, Weekindex, Yearindex};
|
||||
use brk_exit::Exit;
|
||||
use brk_vec::{AnyStorableVec, Compressed};
|
||||
|
||||
use crate::storage::vecs::{base::StorableVec, indexes};
|
||||
use crate::storage::vecs::{Indexes, base::StorableVec, indexes};
|
||||
|
||||
use super::{StorableVecGeneator, StorableVecGeneatorOptions, StoredType};
|
||||
use super::{ComputedType, StorableVecBuilder, StorableVecGeneatorOptions};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct StorableVecGeneatorByIndex<T>
|
||||
where
|
||||
T: StoredType,
|
||||
T: ComputedType + Ord,
|
||||
{
|
||||
pub dateindex: StorableVecGeneator<Dateindex, T>,
|
||||
pub weekindex: StorableVecGeneator<Weekindex, T>,
|
||||
pub difficultyepoch: StorableVecGeneator<Difficultyepoch, T>,
|
||||
pub monthindex: StorableVecGeneator<Monthindex, T>,
|
||||
pub yearindex: StorableVecGeneator<Yearindex, T>,
|
||||
pub halvingepoch: StorableVecGeneator<Halvingepoch, T>,
|
||||
pub decadeindex: StorableVecGeneator<Decadeindex, T>,
|
||||
pub dateindex: StorableVecBuilder<Dateindex, T>,
|
||||
pub weekindex: StorableVecBuilder<Weekindex, T>,
|
||||
pub difficultyepoch: StorableVecBuilder<Difficultyepoch, T>,
|
||||
pub monthindex: StorableVecBuilder<Monthindex, T>,
|
||||
pub yearindex: StorableVecBuilder<Yearindex, T>,
|
||||
// pub halvingepoch: StorableVecGeneator<Halvingepoch, T>, // TODO
|
||||
pub decadeindex: StorableVecBuilder<Decadeindex, T>,
|
||||
}
|
||||
|
||||
impl<T> StorableVecGeneatorByIndex<T>
|
||||
where
|
||||
T: StoredType,
|
||||
T: ComputedType + Ord + From<f64>,
|
||||
f64: From<T>,
|
||||
{
|
||||
pub fn forced_import(
|
||||
path: &Path,
|
||||
compressed: Compressed,
|
||||
options: StorableVecGeneatorOptions,
|
||||
) -> color_eyre::Result<Self> {
|
||||
let dateindex = StorableVecGeneator::forced_import(path, compressed, options)?;
|
||||
let dateindex = StorableVecBuilder::forced_import(path, compressed, options)?;
|
||||
|
||||
let options = options.remove_percentiles();
|
||||
|
||||
Ok(Self {
|
||||
dateindex,
|
||||
weekindex: StorableVecGeneator::forced_import(path, compressed, options)?,
|
||||
difficultyepoch: StorableVecGeneator::forced_import(path, compressed, options)?,
|
||||
monthindex: StorableVecGeneator::forced_import(path, compressed, options)?,
|
||||
yearindex: StorableVecGeneator::forced_import(path, compressed, options)?,
|
||||
halvingepoch: StorableVecGeneator::forced_import(path, compressed, options)?,
|
||||
decadeindex: StorableVecGeneator::forced_import(path, compressed, options)?,
|
||||
weekindex: StorableVecBuilder::forced_import(path, compressed, options)?,
|
||||
difficultyepoch: StorableVecBuilder::forced_import(path, compressed, options)?,
|
||||
monthindex: StorableVecBuilder::forced_import(path, compressed, options)?,
|
||||
yearindex: StorableVecBuilder::forced_import(path, compressed, options)?,
|
||||
// halvingepoch: StorableVecGeneator::forced_import(path, compressed, options)?,
|
||||
decadeindex: StorableVecBuilder::forced_import(path, compressed, options)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn compute(
|
||||
&mut self,
|
||||
source: &mut StorableVec<Height, T>,
|
||||
indexer: &mut Indexer,
|
||||
indexes: &mut indexes::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
) {
|
||||
exit: &Exit,
|
||||
) -> color_eyre::Result<()> {
|
||||
self.dateindex.compute(
|
||||
starting_indexes.dateindex,
|
||||
source,
|
||||
indexes.dateindex_to_first_height.mut_vec(),
|
||||
indexes.dateindex_to_last_height.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.weekindex.from_aligned(
|
||||
starting_indexes.weekindex,
|
||||
&mut self.dateindex,
|
||||
indexes.weekindex_to_first_dateindex.mut_vec(),
|
||||
indexes.weekindex_to_last_dateindex.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.monthindex.from_aligned(
|
||||
starting_indexes.monthindex,
|
||||
&mut self.dateindex,
|
||||
indexes.monthindex_to_first_dateindex.mut_vec(),
|
||||
indexes.monthindex_to_last_dateindex.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.yearindex.from_aligned(
|
||||
starting_indexes.yearindex,
|
||||
&mut self.monthindex,
|
||||
indexes.yearindex_to_first_monthindex.mut_vec(),
|
||||
indexes.yearindex_to_last_monthindex.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.decadeindex.from_aligned(
|
||||
starting_indexes.decadeindex,
|
||||
&mut self.yearindex,
|
||||
indexes.decadeindex_to_first_yearindex.mut_vec(),
|
||||
indexes.decadeindex_to_last_yearindex.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.difficultyepoch.compute(
|
||||
starting_indexes.difficultyepoch,
|
||||
source,
|
||||
indexes.difficultyepoch_to_first_height.mut_vec(),
|
||||
indexes.difficultyepoch_to_last_height.mut_vec(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn AnyStorableVec> {
|
||||
@@ -64,7 +112,7 @@ where
|
||||
self.difficultyepoch.as_any_vecs(),
|
||||
self.monthindex.as_any_vecs(),
|
||||
self.yearindex.as_any_vecs(),
|
||||
self.halvingepoch.as_any_vecs(),
|
||||
// self.halvingepoch.as_any_vecs(),
|
||||
self.decadeindex.as_any_vecs(),
|
||||
]
|
||||
.concat()
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
use std::ops::{Add, Div};
|
||||
|
||||
pub trait StoredType
|
||||
use brk_vec::StoredType;
|
||||
|
||||
pub trait ComputedType
|
||||
where
|
||||
Self: brk_vec::StoredType + From<usize> + Div<usize, Output = Self> + Add<Output = Self>,
|
||||
Self: StoredType + From<usize> + Div<usize, Output = Self> + Add<Output = Self>,
|
||||
{
|
||||
}
|
||||
impl<T> StoredType for T where
|
||||
T: brk_vec::StoredType + From<usize> + Div<usize, Output = Self> + Add<Output = Self>
|
||||
impl<T> ComputedType for T where
|
||||
T: StoredType + From<usize> + Div<usize, Output = Self> + Add<Output = Self>
|
||||
{
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user