computer: convert ComputedFrom to LazyFrom

This commit is contained in:
nym21
2025-08-13 10:46:28 +02:00
parent 00c316c35d
commit 8648d3131a
28 changed files with 642 additions and 1562 deletions

View File

@@ -0,0 +1,395 @@
use brk_structs::Version;
use vecdb::{
AnyBoxedIterableVec, AnyCloneableIterableVec, AnyCollectableVec, FromCoarserIndex,
LazyVecFrom2, StoredIndex,
};
use crate::grouped::{EagerVecBuilder, VecBuilderOptions};
use super::ComputedType;
#[allow(clippy::type_complexity)]
#[derive(Clone)]
pub struct LazyVecBuilder<I, T, S1I, S2T>
where
I: StoredIndex,
T: ComputedType,
S2T: ComputedType,
{
pub first: Option<Box<LazyVecFrom2<I, T, S1I, T, I, S2T>>>,
pub average: Option<Box<LazyVecFrom2<I, T, S1I, T, I, S2T>>>,
pub sum: Option<Box<LazyVecFrom2<I, T, S1I, T, I, S2T>>>,
pub max: Option<Box<LazyVecFrom2<I, T, S1I, T, I, S2T>>>,
pub min: Option<Box<LazyVecFrom2<I, T, S1I, T, I, S2T>>>,
pub last: Option<Box<LazyVecFrom2<I, T, S1I, T, I, S2T>>>,
pub cumulative: Option<Box<LazyVecFrom2<I, T, S1I, T, I, S2T>>>,
}
const VERSION: Version = Version::ZERO;
impl<I, T, S1I, S2T> LazyVecBuilder<I, T, S1I, S2T>
where
I: StoredIndex,
T: ComputedType + 'static,
S1I: StoredIndex + 'static + FromCoarserIndex<I>,
S2T: ComputedType,
{
#[allow(clippy::too_many_arguments)]
pub fn forced_import(
name: &str,
version: Version,
source: Option<AnyBoxedIterableVec<S1I, T>>,
source_extra: &EagerVecBuilder<S1I, T>,
len_source: AnyBoxedIterableVec<I, S2T>,
options: LazyVecBuilderOptions,
) -> Self {
let only_one_active = options.is_only_one_active();
let suffix = |s: &str| format!("{name}_{s}");
let maybe_suffix = |s: &str| {
if only_one_active {
name.to_string()
} else {
suffix(s)
}
};
Self {
first: options.first.then(|| {
Box::new(LazyVecFrom2::init(
&maybe_suffix("first"),
version + VERSION + Version::ZERO,
source_extra
.first
.as_ref()
.map_or_else(|| source.as_ref().unwrap().clone(), |v| v.clone()),
len_source.clone(),
|i: I, source, len_source| {
if i.unwrap_to_usize() >= len_source.len() {
return None;
}
source
.next_at(S1I::min_from(i))
.map(|(_, cow)| cow.into_owned())
},
))
}),
last: options.last.then(|| {
Box::new(LazyVecFrom2::init(
name,
version + VERSION + Version::ZERO,
source_extra.last.as_ref().map_or_else(
|| {
source
.as_ref()
.unwrap_or_else(|| {
dbg!(name, I::to_string());
panic!()
})
.clone()
},
|v| v.clone(),
),
len_source.clone(),
|i: I, source, len_source| {
if i.unwrap_to_usize() >= len_source.len() {
return None;
}
source
.next_at(S1I::max_from(i, source.len()))
.map(|(_, cow)| cow.into_owned())
},
))
}),
min: options.min.then(|| {
Box::new(LazyVecFrom2::init(
&maybe_suffix("min"),
version + VERSION + Version::ZERO,
source_extra
.min
.as_ref()
.map_or_else(|| source.as_ref().unwrap().clone(), |v| v.clone()),
len_source.clone(),
|i: I, source, len_source| {
if i.unwrap_to_usize() >= len_source.len() {
return None;
}
S1I::inclusive_range_from(i, source.len())
.flat_map(|i| source.next_at(i).map(|(_, cow)| cow.into_owned()))
.min()
},
))
}),
max: options.max.then(|| {
Box::new(LazyVecFrom2::init(
&maybe_suffix("max"),
version + VERSION + Version::ZERO,
source_extra
.max
.as_ref()
.map_or_else(|| source.as_ref().unwrap().clone(), |v| v.clone()),
len_source.clone(),
|i: I, source, len_source| {
if i.unwrap_to_usize() >= len_source.len() {
return None;
}
S1I::inclusive_range_from(i, source.len())
.flat_map(|i| source.next_at(i).map(|(_, cow)| cow.into_owned()))
.max()
},
))
}),
average: options.average.then(|| {
Box::new(LazyVecFrom2::init(
&maybe_suffix("average"),
version + VERSION + Version::ZERO,
source_extra
.average
.as_ref()
.map_or_else(|| source.as_ref().unwrap().clone(), |v| v.clone()),
len_source.clone(),
|i: I, source, len_source| {
if i.unwrap_to_usize() >= len_source.len() {
return None;
}
let vec = S1I::inclusive_range_from(i, source.len())
.flat_map(|i| source.next_at(i).map(|(_, cow)| cow.into_owned()))
.collect::<Vec<_>>();
if vec.is_empty() {
return None;
}
let mut sum = T::from(0);
let len = vec.len();
vec.into_iter().for_each(|v| sum += v);
Some(sum / len)
},
))
}),
sum: options.sum.then(|| {
Box::new(LazyVecFrom2::init(
&(if !options.last && !options.average && !options.min && !options.max {
name.to_string()
} else {
maybe_suffix("sum")
}),
version + VERSION + Version::ZERO,
source_extra
.sum
.as_ref()
.map_or_else(|| source.as_ref().unwrap().clone(), |v| v.clone()),
len_source.clone(),
|i: I, source, len_source| {
if i.unwrap_to_usize() >= len_source.len() {
return None;
}
let vec = S1I::inclusive_range_from(i, source.len())
.flat_map(|i| source.next_at(i).map(|(_, cow)| cow.into_owned()))
.collect::<Vec<_>>();
if vec.is_empty() {
return None;
}
let mut sum = T::from(0);
vec.into_iter().for_each(|v| sum += v);
Some(sum)
},
))
}),
cumulative: options.cumulative.then(|| {
Box::new(LazyVecFrom2::init(
&suffix("cumulative"),
version + VERSION + Version::ZERO,
source_extra.cumulative.as_ref().unwrap().boxed_clone(),
len_source.clone(),
|i: I, source, len_source| {
if i.unwrap_to_usize() >= len_source.len() {
return None;
}
source
.next_at(S1I::max_from(i, source.len()))
.map(|(_, cow)| cow.into_owned())
},
))
}),
}
}
pub fn starting_index(&self, max_from: I) -> I {
max_from.min(I::from(
self.vecs().into_iter().map(|v| v.len()).min().unwrap(),
))
}
pub fn unwrap_first(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.first.as_ref().unwrap()
}
#[allow(unused)]
pub fn unwrap_average(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.average.as_ref().unwrap()
}
pub fn unwrap_sum(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.sum.as_ref().unwrap()
}
pub fn unwrap_max(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.max.as_ref().unwrap()
}
pub fn unwrap_min(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.min.as_ref().unwrap()
}
pub fn unwrap_last(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.last.as_ref().unwrap()
}
#[allow(unused)]
pub fn unwrap_cumulative(&self) -> &LazyVecFrom2<I, T, S1I, T, I, S2T> {
self.cumulative.as_ref().unwrap()
}
pub fn vecs(&self) -> Vec<&dyn AnyCollectableVec> {
let mut v: Vec<&dyn AnyCollectableVec> = vec![];
if let Some(first) = self.first.as_ref() {
v.push(first.as_ref());
}
if let Some(last) = self.last.as_ref() {
v.push(last.as_ref());
}
if let Some(min) = self.min.as_ref() {
v.push(min.as_ref());
}
if let Some(max) = self.max.as_ref() {
v.push(max.as_ref());
}
if let Some(average) = self.average.as_ref() {
v.push(average.as_ref());
}
if let Some(sum) = self.sum.as_ref() {
v.push(sum.as_ref());
}
if let Some(cumulative) = self.cumulative.as_ref() {
v.push(cumulative.as_ref());
}
v
}
}
#[derive(Default, Clone, Copy)]
pub struct LazyVecBuilderOptions {
average: bool,
sum: bool,
max: bool,
min: bool,
first: bool,
last: bool,
cumulative: bool,
}
impl From<VecBuilderOptions> for LazyVecBuilderOptions {
fn from(value: VecBuilderOptions) -> Self {
Self {
average: value.average(),
sum: value.sum(),
max: value.max(),
min: value.min(),
first: value.first(),
last: value.last(),
cumulative: value.cumulative(),
}
}
}
impl LazyVecBuilderOptions {
pub fn add_first(mut self) -> Self {
self.first = true;
self
}
pub fn add_last(mut self) -> Self {
self.last = true;
self
}
pub fn add_min(mut self) -> Self {
self.min = true;
self
}
pub fn add_max(mut self) -> Self {
self.max = true;
self
}
pub fn add_average(mut self) -> Self {
self.average = true;
self
}
pub fn add_sum(mut self) -> Self {
self.sum = true;
self
}
pub fn add_cumulative(mut self) -> Self {
self.cumulative = true;
self
}
#[allow(unused)]
pub fn rm_min(mut self) -> Self {
self.min = false;
self
}
#[allow(unused)]
pub fn rm_max(mut self) -> Self {
self.max = false;
self
}
#[allow(unused)]
pub fn rm_average(mut self) -> Self {
self.average = false;
self
}
#[allow(unused)]
pub fn rm_sum(mut self) -> Self {
self.sum = false;
self
}
#[allow(unused)]
pub fn rm_cumulative(mut self) -> Self {
self.cumulative = false;
self
}
pub fn add_minmax(mut self) -> Self {
self.min = true;
self.max = true;
self
}
pub fn is_only_one_active(&self) -> bool {
[
self.average,
self.sum,
self.max,
self.min,
self.first,
self.last,
self.cumulative,
]
.iter()
.filter(|b| **b)
.count()
== 1
}
pub fn copy_self_extra(&self) -> Self {
Self {
cumulative: self.cumulative,
..Self::default()
}
}
}

View File

@@ -5,11 +5,10 @@ use brk_structs::{
DateIndex, DecadeIndex, MonthIndex, QuarterIndex, SemesterIndex, Version, WeekIndex, YearIndex,
};
use vecdb::{
AnyCloneableIterableVec, AnyCollectableVec, AnyIterableVec, Computation, Database, EagerVec,
Exit, Format,
AnyCloneableIterableVec, AnyCollectableVec, AnyIterableVec, Database, EagerVec, Exit, Format,
};
use crate::{Indexes, grouped::ComputedVecBuilder, indexes};
use crate::{Indexes, grouped::LazyVecBuilder, indexes};
use super::{ComputedType, EagerVecBuilder, Source, VecBuilderOptions};
@@ -20,12 +19,12 @@ where
{
pub dateindex: Option<EagerVec<DateIndex, T>>,
pub dateindex_extra: EagerVecBuilder<DateIndex, T>,
pub weekindex: ComputedVecBuilder<WeekIndex, T, DateIndex, WeekIndex>,
pub monthindex: ComputedVecBuilder<MonthIndex, T, DateIndex, MonthIndex>,
pub quarterindex: ComputedVecBuilder<QuarterIndex, T, DateIndex, QuarterIndex>,
pub semesterindex: ComputedVecBuilder<SemesterIndex, T, DateIndex, SemesterIndex>,
pub yearindex: ComputedVecBuilder<YearIndex, T, DateIndex, YearIndex>,
pub decadeindex: ComputedVecBuilder<DecadeIndex, T, DateIndex, DecadeIndex>,
pub weekindex: LazyVecBuilder<WeekIndex, T, DateIndex, WeekIndex>,
pub monthindex: LazyVecBuilder<MonthIndex, T, DateIndex, MonthIndex>,
pub quarterindex: LazyVecBuilder<QuarterIndex, T, DateIndex, QuarterIndex>,
pub semesterindex: LazyVecBuilder<SemesterIndex, T, DateIndex, SemesterIndex>,
pub yearindex: LazyVecBuilder<YearIndex, T, DateIndex, YearIndex>,
pub decadeindex: LazyVecBuilder<DecadeIndex, T, DateIndex, DecadeIndex>,
}
const VERSION: Version = Version::ZERO;
@@ -40,11 +39,11 @@ where
name: &str,
source: Source<DateIndex, T>,
version: Version,
format: Format,
computation: Computation,
indexes: &indexes::Vecs,
options: VecBuilderOptions,
) -> Result<Self> {
let format = Format::Compressed;
let dateindex = source.is_compute().then(|| {
EagerVec::forced_import(db, name, version + VERSION + Version::ZERO, format).unwrap()
});
@@ -62,72 +61,54 @@ where
let dateindex_source = source.vec().or(dateindex.as_ref().map(|v| v.boxed_clone()));
Ok(Self {
weekindex: ComputedVecBuilder::forced_import(
db,
weekindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
computation,
dateindex_source.clone(),
&dateindex_extra,
indexes.weekindex_to_weekindex.boxed_clone(),
options.into(),
)?,
monthindex: ComputedVecBuilder::forced_import(
db,
),
monthindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
dateindex_source.clone(),
&dateindex_extra,
indexes.monthindex_to_monthindex.boxed_clone(),
options.into(),
)?,
quarterindex: ComputedVecBuilder::forced_import(
db,
),
quarterindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
dateindex_source.clone(),
&dateindex_extra,
indexes.quarterindex_to_quarterindex.boxed_clone(),
options.into(),
)?,
semesterindex: ComputedVecBuilder::forced_import(
db,
),
semesterindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
dateindex_source.clone(),
&dateindex_extra,
indexes.semesterindex_to_semesterindex.boxed_clone(),
options.into(),
)?,
yearindex: ComputedVecBuilder::forced_import(
db,
),
yearindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
dateindex_source.clone(),
&dateindex_extra,
indexes.yearindex_to_yearindex.boxed_clone(),
options.into(),
)?,
decadeindex: ComputedVecBuilder::forced_import(
db,
),
decadeindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
dateindex_source.clone(),
&dateindex_extra,
indexes.decadeindex_to_decadeindex.boxed_clone(),
options.into(),
)?,
),
dateindex,
dateindex_extra,
})
@@ -159,12 +140,11 @@ where
)?;
let dateindex: Option<&EagerVec<DateIndex, T>> = None;
self.compute_rest(indexes, starting_indexes, exit, dateindex)
self.compute_rest(starting_indexes, exit, dateindex)
}
pub fn compute_rest(
&mut self,
indexes: &indexes::Vecs,
starting_indexes: &Indexes,
exit: &Exit,
dateindex: Option<&impl AnyIterableVec<DateIndex, T>>,
@@ -179,42 +159,6 @@ where
.extend(starting_indexes.dateindex, dateindex, exit)?;
}
self.weekindex.compute_if_necessary(
starting_indexes.weekindex,
&indexes.weekindex_to_dateindex_count,
exit,
)?;
self.monthindex.compute_if_necessary(
starting_indexes.monthindex,
&indexes.monthindex_to_dateindex_count,
exit,
)?;
self.quarterindex.compute_if_necessary(
starting_indexes.quarterindex,
&indexes.quarterindex_to_monthindex_count,
exit,
)?;
self.semesterindex.compute_if_necessary(
starting_indexes.semesterindex,
&indexes.semesterindex_to_monthindex_count,
exit,
)?;
self.yearindex.compute_if_necessary(
starting_indexes.yearindex,
&indexes.yearindex_to_monthindex_count,
exit,
)?;
self.decadeindex.compute_if_necessary(
starting_indexes.decadeindex,
&indexes.decadeindex_to_yearindex_count,
exit,
)?;
Ok(())
}

View File

@@ -6,13 +6,12 @@ use brk_structs::{
Version, WeekIndex, YearIndex,
};
use vecdb::{
AnyCloneableIterableVec, AnyCollectableVec, AnyIterableVec, Computation, Database, EagerVec,
Exit, Format,
AnyCloneableIterableVec, AnyCollectableVec, AnyIterableVec, Database, EagerVec, Exit, Format,
};
use crate::{
Indexes,
grouped::{ComputedVecBuilder, Source},
grouped::{LazyVecBuilder, Source},
indexes,
};
@@ -26,14 +25,14 @@ where
pub height: Option<EagerVec<Height, T>>,
pub height_extra: EagerVecBuilder<Height, T>,
pub dateindex: EagerVecBuilder<DateIndex, T>,
pub weekindex: ComputedVecBuilder<WeekIndex, T, DateIndex, WeekIndex>,
pub weekindex: LazyVecBuilder<WeekIndex, T, DateIndex, WeekIndex>,
pub difficultyepoch: EagerVecBuilder<DifficultyEpoch, T>,
pub monthindex: ComputedVecBuilder<MonthIndex, T, DateIndex, MonthIndex>,
pub quarterindex: ComputedVecBuilder<QuarterIndex, T, DateIndex, QuarterIndex>,
pub semesterindex: ComputedVecBuilder<SemesterIndex, T, DateIndex, SemesterIndex>,
pub yearindex: ComputedVecBuilder<YearIndex, T, DateIndex, YearIndex>,
pub monthindex: LazyVecBuilder<MonthIndex, T, DateIndex, MonthIndex>,
pub quarterindex: LazyVecBuilder<QuarterIndex, T, DateIndex, QuarterIndex>,
pub semesterindex: LazyVecBuilder<SemesterIndex, T, DateIndex, SemesterIndex>,
pub yearindex: LazyVecBuilder<YearIndex, T, DateIndex, YearIndex>,
// TODO: pub halvingepoch: StorableVecGeneator<Halvingepoch, T>,
pub decadeindex: ComputedVecBuilder<DecadeIndex, T, DateIndex, DecadeIndex>,
pub decadeindex: LazyVecBuilder<DecadeIndex, T, DateIndex, DecadeIndex>,
}
const VERSION: Version = Version::ZERO;
@@ -49,11 +48,11 @@ where
name: &str,
source: Source<Height, T>,
version: Version,
format: Format,
computation: Computation,
indexes: &indexes::Vecs,
options: VecBuilderOptions,
) -> Result<Self> {
let format = Format::Compressed;
let height = source.is_compute().then(|| {
EagerVec::forced_import(db, name, version + VERSION + Version::ZERO, format).unwrap()
});
@@ -77,72 +76,54 @@ where
let options = options.remove_percentiles();
Ok(Self {
weekindex: ComputedVecBuilder::forced_import(
db,
weekindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
computation,
None,
&dateindex,
indexes.weekindex_to_weekindex.boxed_clone(),
options.into(),
)?,
monthindex: ComputedVecBuilder::forced_import(
db,
),
monthindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
None,
&dateindex,
indexes.monthindex_to_monthindex.boxed_clone(),
options.into(),
)?,
quarterindex: ComputedVecBuilder::forced_import(
db,
),
quarterindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
None,
&dateindex,
indexes.quarterindex_to_quarterindex.boxed_clone(),
options.into(),
)?,
semesterindex: ComputedVecBuilder::forced_import(
db,
),
semesterindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
None,
&dateindex,
indexes.semesterindex_to_semesterindex.boxed_clone(),
options.into(),
)?,
yearindex: ComputedVecBuilder::forced_import(
db,
),
yearindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
None,
&dateindex,
indexes.yearindex_to_yearindex.boxed_clone(),
options.into(),
)?,
decadeindex: ComputedVecBuilder::forced_import(
db,
),
decadeindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
None,
&dateindex,
indexes.decadeindex_to_decadeindex.boxed_clone(),
options.into(),
)?,
),
// halvingepoch: StorableVecGeneator::forced_import(db, name, version + VERSION + Version::ZERO, format, options)?,
height,
height_extra,
@@ -229,42 +210,6 @@ where
)?;
}
self.weekindex.compute_if_necessary(
starting_indexes.weekindex,
&indexes.weekindex_to_dateindex_count,
exit,
)?;
self.monthindex.compute_if_necessary(
starting_indexes.monthindex,
&indexes.monthindex_to_dateindex_count,
exit,
)?;
self.quarterindex.compute_if_necessary(
starting_indexes.quarterindex,
&indexes.quarterindex_to_monthindex_count,
exit,
)?;
self.semesterindex.compute_if_necessary(
starting_indexes.semesterindex,
&indexes.semesterindex_to_monthindex_count,
exit,
)?;
self.yearindex.compute_if_necessary(
starting_indexes.yearindex,
&indexes.yearindex_to_monthindex_count,
exit,
)?;
self.decadeindex.compute_if_necessary(
starting_indexes.decadeindex,
&indexes.decadeindex_to_yearindex_count,
exit,
)?;
Ok(())
}

View File

@@ -30,9 +30,10 @@ where
db: &Database,
name: &str,
version: Version,
format: Format,
options: VecBuilderOptions,
) -> Result<Self> {
let format = Format::Compressed;
let height = EagerVec::forced_import(db, name, version + VERSION + Version::ZERO, format)?;
let height_extra = EagerVecBuilder::forced_import(

View File

@@ -5,13 +5,13 @@ use brk_structs::{
Sats, SemesterIndex, TxIndex, Version, WeekIndex, YearIndex,
};
use vecdb::{
AnyCloneableIterableVec, AnyCollectableVec, AnyVec, CollectableVec, Computation, Database,
EagerVec, Exit, Format, GenericStoredVec, StoredIndex, VecIterator,
AnyCloneableIterableVec, AnyCollectableVec, AnyVec, CollectableVec, Database, EagerVec, Exit,
Format, GenericStoredVec, StoredIndex, VecIterator,
};
use crate::{
Indexes,
grouped::{ComputedVecBuilder, Source},
grouped::{LazyVecBuilder, Source},
indexes, price,
};
@@ -25,14 +25,14 @@ where
pub txindex: Option<Box<EagerVec<TxIndex, T>>>,
pub height: EagerVecBuilder<Height, T>,
pub dateindex: EagerVecBuilder<DateIndex, T>,
pub weekindex: ComputedVecBuilder<WeekIndex, T, DateIndex, WeekIndex>,
pub weekindex: LazyVecBuilder<WeekIndex, T, DateIndex, WeekIndex>,
pub difficultyepoch: EagerVecBuilder<DifficultyEpoch, T>,
pub monthindex: ComputedVecBuilder<MonthIndex, T, DateIndex, MonthIndex>,
pub quarterindex: ComputedVecBuilder<QuarterIndex, T, DateIndex, QuarterIndex>,
pub semesterindex: ComputedVecBuilder<SemesterIndex, T, DateIndex, SemesterIndex>,
pub yearindex: ComputedVecBuilder<YearIndex, T, DateIndex, YearIndex>,
pub monthindex: LazyVecBuilder<MonthIndex, T, DateIndex, MonthIndex>,
pub quarterindex: LazyVecBuilder<QuarterIndex, T, DateIndex, QuarterIndex>,
pub semesterindex: LazyVecBuilder<SemesterIndex, T, DateIndex, SemesterIndex>,
pub yearindex: LazyVecBuilder<YearIndex, T, DateIndex, YearIndex>,
// TODO: pub halvingepoch: StorableVecGeneator<Halvingepoch, T>,
pub decadeindex: ComputedVecBuilder<DecadeIndex, T, DateIndex, DecadeIndex>,
pub decadeindex: LazyVecBuilder<DecadeIndex, T, DateIndex, DecadeIndex>,
}
const VERSION: Version = Version::ZERO;
@@ -49,7 +49,6 @@ where
source: Source<TxIndex, T>,
version: Version,
format: Format,
computation: Computation,
indexes: &indexes::Vecs,
options: VecBuilderOptions,
) -> Result<Self> {
@@ -79,72 +78,54 @@ where
)?;
Ok(Self {
weekindex: ComputedVecBuilder::forced_import(
db,
weekindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
computation,
None,
&dateindex,
indexes.weekindex_to_weekindex.boxed_clone(),
options.into(),
)?,
monthindex: ComputedVecBuilder::forced_import(
db,
),
monthindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
None,
&dateindex,
indexes.monthindex_to_monthindex.boxed_clone(),
options.into(),
)?,
quarterindex: ComputedVecBuilder::forced_import(
db,
),
quarterindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
None,
&dateindex,
indexes.quarterindex_to_quarterindex.boxed_clone(),
options.into(),
)?,
semesterindex: ComputedVecBuilder::forced_import(
db,
),
semesterindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
None,
&dateindex,
indexes.semesterindex_to_semesterindex.boxed_clone(),
options.into(),
)?,
yearindex: ComputedVecBuilder::forced_import(
db,
),
yearindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
None,
&dateindex,
indexes.yearindex_to_yearindex.boxed_clone(),
options.into(),
)?,
decadeindex: ComputedVecBuilder::forced_import(
db,
),
decadeindex: LazyVecBuilder::forced_import(
name,
version + VERSION + Version::ZERO,
format,
Computation::Lazy,
None,
&dateindex,
indexes.decadeindex_to_decadeindex.boxed_clone(),
options.into(),
)?,
),
txindex,
height,
@@ -237,42 +218,6 @@ where
exit,
)?;
self.weekindex.compute_if_necessary(
starting_indexes.weekindex,
&indexes.weekindex_to_first_dateindex,
exit,
)?;
self.monthindex.compute_if_necessary(
starting_indexes.monthindex,
&indexes.monthindex_to_dateindex_count,
exit,
)?;
self.quarterindex.compute_if_necessary(
starting_indexes.quarterindex,
&indexes.quarterindex_to_monthindex_count,
exit,
)?;
self.semesterindex.compute_if_necessary(
starting_indexes.semesterindex,
&indexes.semesterindex_to_monthindex_count,
exit,
)?;
self.yearindex.compute_if_necessary(
starting_indexes.yearindex,
&indexes.yearindex_to_monthindex_count,
exit,
)?;
self.decadeindex.compute_if_necessary(
starting_indexes.decadeindex,
&indexes.decadeindex_to_yearindex_count,
exit,
)?;
self.difficultyepoch.from_aligned(
starting_indexes.difficultyepoch,
&self.height,

View File

@@ -1,5 +1,5 @@
mod builder_computed;
mod builder_eager;
mod builder_lazy;
mod computed;
mod from_dateindex;
mod from_height;
@@ -12,8 +12,8 @@ mod value_from_height;
mod value_from_txindex;
mod value_height;
pub use builder_computed::*;
pub use builder_eager::*;
pub use builder_lazy::*;
use computed::*;
pub use from_dateindex::*;
pub use from_height::*;

View File

@@ -2,8 +2,8 @@ use brk_error::Result;
use brk_indexer::Indexer;
use brk_structs::{Date, DateIndex, Dollars, StoredF32, Version};
use vecdb::{
AnyCollectableVec, AnyIterableVec, AnyStoredVec, AnyVec, CollectableVec, Computation, Database,
EagerVec, Exit, Format, GenericStoredVec, StoredIndex, VecIterator,
AnyCollectableVec, AnyIterableVec, AnyStoredVec, AnyVec, CollectableVec, Database, EagerVec,
Exit, GenericStoredVec, StoredIndex, VecIterator,
};
use crate::{Indexes, grouped::source::Source, indexes, price, utils::get_percentile};
@@ -62,8 +62,6 @@ impl ComputedRatioVecsFromDateIndex {
name: &str,
source: Source<DateIndex, Dollars>,
version: Version,
format: Format,
computation: Computation,
indexes: &indexes::Vecs,
extended: bool,
) -> Result<Self> {
@@ -76,8 +74,6 @@ impl ComputedRatioVecsFromDateIndex {
name,
Source::Compute,
version + VERSION,
format,
computation,
indexes,
options,
)
@@ -88,8 +84,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)?,
@@ -99,8 +93,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_sma"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -112,8 +104,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_1w_sma"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -125,8 +115,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_1m_sma"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -138,8 +126,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_1y_sma"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -151,8 +137,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_4y_sma"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -164,8 +148,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_1y_sma_momentum_oscillator"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -177,8 +159,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_sd"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -190,8 +170,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_4y_sd"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -203,8 +181,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_1y_sd"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -216,8 +192,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p99_9"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -229,8 +203,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p99_5"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -242,8 +214,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p99"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -255,8 +225,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p1"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -268,8 +236,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p0_5"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -281,8 +247,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p0_1"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -294,8 +258,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p1sd"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -307,8 +269,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p2sd"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -320,8 +280,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p3sd"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -333,8 +291,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_m1sd"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -346,8 +302,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_m2sd"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -359,8 +313,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_m3sd"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -372,8 +324,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p99_9_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -385,8 +335,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p99_5_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -398,8 +346,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p99_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -411,8 +357,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p1_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -424,8 +368,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p0_5_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -437,8 +379,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p0_1_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -450,8 +390,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p1sd_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -463,8 +401,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p2sd_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -476,8 +412,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_p3sd_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -489,8 +423,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_m1sd_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -502,8 +434,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_m2sd_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -515,8 +445,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_m3sd_as_price"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -528,8 +456,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_zscore"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -541,8 +467,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_4y_zscore"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -554,8 +478,6 @@ impl ComputedRatioVecsFromDateIndex {
&format!("{name}_ratio_1y_zscore"),
Source::Compute,
version + VERSION + Version::ZERO,
format,
computation,
indexes,
options,
)
@@ -1084,91 +1006,76 @@ impl ComputedRatioVecsFromDateIndex {
.try_for_each(|v| v.safe_flush(exit))?;
self.ratio_p99_9.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_p99_5.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_p99.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_p1.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_p0_5.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_p0_1.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_sd.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_4y_sd.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_1y_sd.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_p1sd.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_p2sd.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_p3sd.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_m1sd.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_m2sd.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,
)?;
self.ratio_m3sd.as_mut().unwrap().compute_rest(
indexes,
starting_indexes,
exit,
None as Option<&EagerVec<_, _>>,

View File

@@ -1,9 +1,7 @@
use brk_error::Result;
use brk_indexer::Indexer;
use brk_structs::{Bitcoin, DateIndex, Dollars, Sats, Version};
use vecdb::{
AnyCollectableVec, CollectableVec, Computation, Database, EagerVec, Exit, Format, StoredVec,
};
use vecdb::{AnyCollectableVec, CollectableVec, Database, EagerVec, Exit, StoredVec};
use crate::{
Indexes,
@@ -30,8 +28,6 @@ impl ComputedValueVecsFromDateIndex {
name: &str,
source: Source<DateIndex, Sats>,
version: Version,
format: Format,
computation: Computation,
options: VecBuilderOptions,
compute_dollars: bool,
indexes: &indexes::Vecs,
@@ -42,8 +38,6 @@ impl ComputedValueVecsFromDateIndex {
name,
source,
version + VERSION,
format,
computation,
indexes,
options,
)?,
@@ -52,8 +46,6 @@ impl ComputedValueVecsFromDateIndex {
&format!("{name}_in_btc"),
Source::Compute,
version + VERSION,
format,
computation,
indexes,
options,
)?,
@@ -63,8 +55,6 @@ impl ComputedValueVecsFromDateIndex {
&format!("{name}_in_usd"),
Source::Compute,
version + VERSION,
format,
computation,
indexes,
options,
)
@@ -116,7 +106,7 @@ impl ComputedValueVecsFromDateIndex {
) -> Result<()> {
if let Some(dateindex) = dateindex {
self.sats
.compute_rest(indexes, starting_indexes, exit, Some(dateindex))?;
.compute_rest(starting_indexes, exit, Some(dateindex))?;
self.bitcoin.compute_all(
indexer,
@@ -130,8 +120,7 @@ impl ComputedValueVecsFromDateIndex {
} else {
let dateindex: Option<&StoredVec<DateIndex, Sats>> = None;
self.sats
.compute_rest(indexes, starting_indexes, exit, dateindex)?;
self.sats.compute_rest(starting_indexes, exit, dateindex)?;
self.bitcoin.compute_all(
indexer,

View File

@@ -1,9 +1,7 @@
use brk_error::Result;
use brk_indexer::Indexer;
use brk_structs::{Bitcoin, Dollars, Height, Sats, Version};
use vecdb::{
AnyCollectableVec, CollectableVec, Computation, Database, EagerVec, Exit, Format, StoredVec,
};
use vecdb::{AnyCollectableVec, CollectableVec, Database, EagerVec, Exit, StoredVec};
use crate::{
Indexes,
@@ -30,8 +28,6 @@ impl ComputedValueVecsFromHeight {
name: &str,
source: Source<Height, Sats>,
version: Version,
format: Format,
computation: Computation,
options: VecBuilderOptions,
compute_dollars: bool,
indexes: &indexes::Vecs,
@@ -42,8 +38,6 @@ impl ComputedValueVecsFromHeight {
name,
source,
version + VERSION,
format,
computation,
indexes,
options,
)?,
@@ -52,8 +46,6 @@ impl ComputedValueVecsFromHeight {
&format!("{name}_in_btc"),
Source::Compute,
version + VERSION,
format,
computation,
indexes,
options,
)?,
@@ -63,8 +55,6 @@ impl ComputedValueVecsFromHeight {
&format!("{name}_in_usd"),
Source::Compute,
version + VERSION,
format,
computation,
indexes,
options,
)

View File

@@ -2,8 +2,8 @@ use brk_error::Result;
use brk_indexer::Indexer;
use brk_structs::{Bitcoin, Close, Dollars, Height, Sats, TxIndex, Version};
use vecdb::{
AnyCloneableIterableVec, AnyCollectableVec, CollectableVec, Computation, ComputedVecFrom3,
Database, Exit, Format, LazyVecFrom1, StoredIndex, StoredVec,
AnyCloneableIterableVec, AnyCollectableVec, CollectableVec, Database, Exit, Format,
LazyVecFrom1, LazyVecFrom3, StoredIndex, StoredVec,
};
use crate::{Indexes, grouped::Source, indexes, price};
@@ -17,16 +17,7 @@ pub struct ComputedValueVecsFromTxindex {
pub bitcoin: ComputedVecsFromTxindex<Bitcoin>,
#[allow(clippy::type_complexity)]
pub dollars_txindex: Option<
ComputedVecFrom3<
TxIndex,
Dollars,
TxIndex,
Bitcoin,
TxIndex,
Height,
Height,
Close<Dollars>,
>,
LazyVecFrom3<TxIndex, Dollars, TxIndex, Bitcoin, TxIndex, Height, Height, Close<Dollars>>,
>,
pub dollars: Option<ComputedVecsFromTxindex<Dollars>>,
}
@@ -41,7 +32,6 @@ impl ComputedValueVecsFromTxindex {
indexes: &indexes::Vecs,
source: Source<TxIndex, Sats>,
version: Version,
computation: Computation,
format: Format,
price: Option<&price::Vecs>,
options: VecBuilderOptions,
@@ -57,7 +47,6 @@ impl ComputedValueVecsFromTxindex {
source.clone(),
version + VERSION,
format,
computation,
indexes,
options,
)?;
@@ -82,18 +71,14 @@ impl ComputedValueVecsFromTxindex {
Source::None,
version + VERSION,
format,
computation,
indexes,
options,
)?;
let dollars_txindex = price.map(|price| {
ComputedVecFrom3::forced_import_or_init_from_3(
computation,
db,
LazyVecFrom3::init(
&name_in_usd,
version + VERSION,
format,
bitcoin_txindex.boxed_clone(),
indexes.txindex_to_height.boxed_clone(),
price.chainindexes_to_close.height.boxed_clone(),
@@ -115,7 +100,6 @@ impl ComputedValueVecsFromTxindex {
})
},
)
.unwrap()
});
Ok(Self {
@@ -130,7 +114,6 @@ impl ComputedValueVecsFromTxindex {
Source::None,
version + VERSION,
format,
computation,
indexes,
options,
)
@@ -208,12 +191,6 @@ impl ComputedValueVecsFromTxindex {
if let Some(dollars) = self.dollars.as_mut() {
let dollars_txindex = self.dollars_txindex.as_mut().unwrap();
dollars_txindex.compute_if_necessary(
starting_indexes.txindex,
&indexer.vecs.txindex_to_txid,
exit,
)?;
dollars.compute_rest_from_bitcoin(
indexer,
indexes,