computer: unwrap

This commit is contained in:
nym21
2025-04-15 11:18:31 +02:00
parent b686d317a9
commit 4cf465f419
11 changed files with 215 additions and 341 deletions

4
Cargo.lock generated
View File

@@ -1549,9 +1549,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.171" version = "0.2.172"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"

View File

@@ -9,8 +9,7 @@ use std::{
use brk_core::CheckedSub; use brk_core::CheckedSub;
use brk_exit::Exit; use brk_exit::Exit;
use brk_vec::{ use brk_vec::{
Compressed, DynamicVec, Error, GenericVec, Result, StoredIndex, StoredType, StoredVec, Value, Compressed, DynamicVec, Error, GenericVec, Result, StoredIndex, StoredType, StoredVec, Version,
Version,
}; };
use log::info; use log::info;
@@ -120,8 +119,12 @@ where
&mut self.inner &mut self.inner
} }
pub fn cached_get(&mut self, index: I) -> Result<Option<Value<T>>> { pub fn unwrap_cached_get(&mut self, index: I) -> Option<T> {
self.inner.cached_get(index) self.inner.unwrap_cached_get(index)
}
#[inline]
pub fn double_unwrap_cached_get(&mut self, index: I) -> T {
self.inner.double_unwrap_cached_get(index)
} }
pub fn collect_inclusive_range(&self, from: I, to: I) -> Result<Vec<T>> { pub fn collect_inclusive_range(&self, from: I, to: I) -> Result<Vec<T>> {
@@ -196,7 +199,7 @@ where
.map_or_else(T::default, |v| v.into_inner()), .map_or_else(T::default, |v| v.into_inner()),
); );
other.iter_from(index, |(v, i, ..)| { other.iter_from(index, |(v, i, ..)| {
if self.cached_get(i).unwrap().is_none_or(|old_v| *old_v > v) { if self.unwrap_cached_get(i).is_none_or(|old_v| old_v > v) {
self.forced_push_at(i, v, exit) self.forced_push_at(i, v, exit)
} else { } else {
Ok(()) Ok(())
@@ -224,7 +227,7 @@ where
let index = max_from.min(T::from(self.len())); let index = max_from.min(T::from(self.len()));
first_indexes.iter_from(index, |(value, first_index, ..)| { first_indexes.iter_from(index, |(value, first_index, ..)| {
let first_index = (first_index).to_usize()?; let first_index = (first_index).to_usize()?;
let last_index = (last_indexes.cached_get(value)?.unwrap()).to_usize()?; let last_index = (last_indexes.double_unwrap_cached_get(value)).to_usize()?;
(first_index..last_index) (first_index..last_index)
.try_for_each(|index| self.forced_push_at(I::from(index), value, exit)) .try_for_each(|index| self.forced_push_at(I::from(index), value, exit))
})?; })?;
@@ -344,8 +347,8 @@ where
let index = max_from.min(I::from(self.len())); let index = max_from.min(I::from(self.len()));
first_indexes.iter_from(index, |(i, first_index, ..)| { first_indexes.iter_from(index, |(i, first_index, ..)| {
let last_index = last_indexes.cached_get(i)?.unwrap().into_inner(); let last_index = last_indexes.double_unwrap_cached_get(i);
let range = first_index.to_usize().unwrap()..=last_index.to_usize().unwrap(); let range = first_index.unwrap_to_usize()..=last_index.unwrap_to_usize();
let count = if let Some(filter) = filter.as_mut() { let count = if let Some(filter) = filter.as_mut() {
range.into_iter().filter(|i| filter(T2::from(*i))).count() range.into_iter().filter(|i| filter(T2::from(*i))).count()
} else { } else {
@@ -377,7 +380,7 @@ where
self_to_other.iter_from(index, |(i, other, ..)| { self_to_other.iter_from(index, |(i, other, ..)| {
self.forced_push_at( self.forced_push_at(
i, i,
T::from(other_to_self.cached_get(other)?.unwrap().into_inner() == i), T::from(other_to_self.double_unwrap_cached_get(other) == i),
exit, exit,
) )
})?; })?;
@@ -403,11 +406,11 @@ where
let index = max_from.min(I::from(self.len())); let index = max_from.min(I::from(self.len()));
first_indexes.iter_from(index, |(i, first_index, ..)| { first_indexes.iter_from(index, |(i, first_index, ..)| {
let last_index = last_indexes.cached_get(i)?.unwrap().into_inner(); let last_index = last_indexes.double_unwrap_cached_get(i);
let range = first_index.to_usize().unwrap()..=last_index.to_usize().unwrap(); let range = first_index.unwrap_to_usize()..=last_index.unwrap_to_usize();
let mut sum = T::from(0_usize); let mut sum = T::from(0_usize);
range.into_iter().for_each(|i| { range.into_iter().for_each(|i| {
sum = sum.clone() + source.cached_get_(i).unwrap().unwrap().into_inner(); sum = sum.clone() + source.double_unwrap_cached_get(T2::from(i));
}); });
self.forced_push_at(i, sum, exit) self.forced_push_at(i, sum, exit)
})?; })?;

View File

@@ -97,7 +97,7 @@ impl Vecs {
indexer.mut_vecs().height_to_timestamp.mut_vec(), indexer.mut_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 interval = height.decremented().map_or(Timestamp::ZERO, |prev_h| {
let prev_timestamp = *height_to_timestamp.cached_get(prev_h).unwrap().unwrap(); let prev_timestamp = height_to_timestamp.double_unwrap_cached_get(prev_h);
timestamp timestamp
.checked_sub(prev_timestamp) .checked_sub(prev_timestamp)
.unwrap_or(Timestamp::ZERO) .unwrap_or(Timestamp::ZERO)

View File

@@ -16,18 +16,18 @@ where
I: StoredIndex, I: StoredIndex,
T: ComputedType, T: ComputedType,
{ {
pub first: Option<ComputedVec<I, T>>, first: Option<ComputedVec<I, T>>,
pub average: Option<ComputedVec<I, T>>, average: Option<ComputedVec<I, T>>,
pub sum: Option<ComputedVec<I, T>>, sum: Option<ComputedVec<I, T>>,
pub max: Option<ComputedVec<I, T>>, max: Option<ComputedVec<I, T>>,
pub _90p: Option<ComputedVec<I, T>>, _90p: Option<ComputedVec<I, T>>,
pub _75p: Option<ComputedVec<I, T>>, _75p: Option<ComputedVec<I, T>>,
pub median: Option<ComputedVec<I, T>>, median: Option<ComputedVec<I, T>>,
pub _25p: Option<ComputedVec<I, T>>, _25p: Option<ComputedVec<I, T>>,
pub _10p: Option<ComputedVec<I, T>>, _10p: Option<ComputedVec<I, T>>,
pub min: Option<ComputedVec<I, T>>, min: Option<ComputedVec<I, T>>,
pub last: Option<ComputedVec<I, T>>, last: Option<ComputedVec<I, T>>,
pub total: Option<ComputedVec<I, T>>, total: Option<ComputedVec<I, T>>,
} }
impl<I, T> ComputedVecBuilder<I, T> impl<I, T> ComputedVecBuilder<I, T>
@@ -128,14 +128,12 @@ where
source.iter_from(index, |(i, v, ..)| { source.iter_from(index, |(i, v, ..)| {
let prev = i let prev = i
.to_usize() .unwrap_to_usize()
.unwrap()
.checked_sub(1) .checked_sub(1)
.map_or(T::from(0_usize), |prev_i| { .map_or(T::from(0_usize), |prev_i| {
total_vec total_vec
.cached_get(I::from(prev_i)) .unwrap_cached_get(I::from(prev_i))
.unwrap() .unwrap_or(T::from(0_usize))
.map_or(T::from(0_usize), |v| v.into_inner())
}); });
let value = v.clone() + prev; let value = v.clone() + prev;
total_vec.forced_push_at(i, value, exit)?; total_vec.forced_push_at(i, value, exit)?;
@@ -164,21 +162,15 @@ where
let index = self.starting_index(max_from); let index = self.starting_index(max_from);
first_indexes.iter_from(index, |(i, first_index, first_indexes)| { first_indexes.iter_from(index, |(i, first_index, first_indexes)| {
let last_index = last_indexes.cached_get(i)?.unwrap().into_inner(); let last_index = last_indexes.double_unwrap_cached_get(i);
if let Some(first) = self.first.as_mut() { if let Some(first) = self.first.as_mut() {
let v = source.cached_get(first_index)?.unwrap().into_inner(); let v = source.double_unwrap_cached_get(first_index);
first.forced_push_at(index, v, exit)?; first.forced_push_at(index, v, exit)?;
} }
if let Some(last) = self.last.as_mut() { if let Some(last) = self.last.as_mut() {
let v = source let v = source.double_unwrap_cached_get(last_index);
.cached_get(last_index)
.inspect_err(|_| {
dbg!(last.path(), last_index);
})?
.unwrap()
.into_inner();
last.forced_push_at(index, v, exit)?; last.forced_push_at(index, v, exit)?;
} }
@@ -269,17 +261,12 @@ where
} }
if let Some(total_vec) = self.total.as_mut() { if let Some(total_vec) = self.total.as_mut() {
let prev = i.to_usize().unwrap().checked_sub(1).map_or( let prev = i
T::from(0_usize), .unwrap_to_usize()
|prev_i| { .checked_sub(1)
total_vec .map_or(T::from(0_usize), |prev_i| {
.cached_get(I::from(prev_i)) total_vec.double_unwrap_cached_get(I::from(prev_i))
.unwrap() });
.unwrap()
.to_owned()
.into_inner()
},
);
total_vec.forced_push_at(i, prev + sum, exit)?; total_vec.forced_push_at(i, prev + sum, exit)?;
} }
} }
@@ -320,17 +307,14 @@ where
let index = self.starting_index(max_from); let index = self.starting_index(max_from);
first_indexes.iter_from(index, |(i, first_index, ..)| { first_indexes.iter_from(index, |(i, first_index, ..)| {
let last_index = *last_indexes.cached_get(i).unwrap().unwrap(); let last_index = last_indexes.double_unwrap_cached_get(i);
if let Some(first) = self.first.as_mut() { if let Some(first) = self.first.as_mut() {
let v = source let v = source
.first .first
.as_mut() .as_mut()
.unwrap() .unwrap()
.cached_get(first_index) .double_unwrap_cached_get(first_index);
.unwrap()
.unwrap()
.into_inner();
first.forced_push_at(index, v, exit)?; first.forced_push_at(index, v, exit)?;
} }
@@ -339,10 +323,7 @@ where
.last .last
.as_mut() .as_mut()
.unwrap() .unwrap()
.cached_get(last_index) .double_unwrap_cached_get(last_index);
.unwrap()
.unwrap()
.into_inner();
last.forced_push_at(index, v, exit)?; last.forced_push_at(index, v, exit)?;
} }
@@ -405,16 +386,12 @@ where
} }
if let Some(total_vec) = self.total.as_mut() { if let Some(total_vec) = self.total.as_mut() {
let prev = i.to_usize().unwrap().checked_sub(1).map_or( let prev = i
T::from(0_usize), .unwrap_to_usize()
|prev_i| { .checked_sub(1)
total_vec .map_or(T::from(0_usize), |prev_i| {
.cached_get(I::from(prev_i)) total_vec.double_unwrap_cached_get(I::from(prev_i))
.unwrap() });
.unwrap()
.into_inner()
},
);
total_vec.forced_push_at(i, prev + sum, exit)?; total_vec.forced_push_at(i, prev + sum, exit)?;
} }
} }
@@ -457,6 +434,43 @@ where
)) ))
} }
pub fn unwrap_first(&mut self) -> &mut ComputedVec<I, T> {
self.first.as_mut().unwrap()
}
pub fn unwrap_average(&mut self) -> &mut ComputedVec<I, T> {
self.average.as_mut().unwrap()
}
pub fn unwrap_sum(&mut self) -> &mut ComputedVec<I, T> {
self.sum.as_mut().unwrap()
}
pub fn unwrap_max(&mut self) -> &mut ComputedVec<I, T> {
self.max.as_mut().unwrap()
}
pub fn unwrap_90p(&mut self) -> &mut ComputedVec<I, T> {
self._90p.as_mut().unwrap()
}
pub fn unwrap_75p(&mut self) -> &mut ComputedVec<I, T> {
self._75p.as_mut().unwrap()
}
pub fn unwrap_median(&mut self) -> &mut ComputedVec<I, T> {
self.median.as_mut().unwrap()
}
pub fn unwrap_25p(&mut self) -> &mut ComputedVec<I, T> {
self._25p.as_mut().unwrap()
}
pub fn unwrap_10p(&mut self) -> &mut ComputedVec<I, T> {
self._10p.as_mut().unwrap()
}
pub fn unwrap_min(&mut self) -> &mut ComputedVec<I, T> {
self.min.as_mut().unwrap()
}
pub fn unwrap_last(&mut self) -> &mut ComputedVec<I, T> {
self.last.as_mut().unwrap()
}
pub fn unwrap_total(&mut self) -> &mut ComputedVec<I, T> {
self.total.as_mut().unwrap()
}
pub fn any_vecs(&self) -> Vec<&dyn brk_vec::AnyStoredVec> { pub fn any_vecs(&self) -> Vec<&dyn brk_vec::AnyStoredVec> {
let mut v: Vec<&dyn brk_vec::AnyStoredVec> = vec![]; let mut v: Vec<&dyn brk_vec::AnyStoredVec> = vec![];

View File

@@ -343,12 +343,8 @@ impl Vecs {
|(h, d, s, ..)| { |(h, d, s, ..)| {
let d = h let d = h
.decremented() .decremented()
.and_then(|h| s.cached_get(h).ok()) .and_then(|h| s.unwrap_cached_get(h))
.flatten() .map_or(d, |prev_d| prev_d.max(d));
.map_or(d, |prev_d| {
let prev_d = *prev_d;
if prev_d > d { prev_d } else { d }
});
(h, d) (h, d)
}, },
exit, exit,
@@ -365,8 +361,8 @@ impl Vecs {
let starting_dateindex = self let starting_dateindex = self
.height_to_dateindex .height_to_dateindex
.cached_get(decremented_starting_height)? .unwrap_cached_get(decremented_starting_height)
.map_or_else(Default::default, |v| v.into_inner()); .unwrap_or_default();
self.height_to_dateindex.compute_transform( self.height_to_dateindex.compute_transform(
starting_indexes.height, starting_indexes.height,
@@ -377,8 +373,7 @@ impl Vecs {
let starting_dateindex = if let Some(dateindex) = self let starting_dateindex = if let Some(dateindex) = self
.height_to_dateindex .height_to_dateindex
.cached_get(decremented_starting_height)? .unwrap_cached_get(decremented_starting_height)
.map(|v| v.into_inner())
{ {
starting_dateindex.min(dateindex) starting_dateindex.min(dateindex)
} else { } else {
@@ -450,8 +445,8 @@ impl Vecs {
let starting_weekindex = self let starting_weekindex = self
.dateindex_to_weekindex .dateindex_to_weekindex
.cached_get(starting_dateindex)? .unwrap_cached_get(starting_dateindex)
.map_or_else(Default::default, |v| v.into_inner()); .unwrap_or_default();
self.dateindex_to_weekindex.compute_transform( self.dateindex_to_weekindex.compute_transform(
starting_dateindex, starting_dateindex,
@@ -485,12 +480,7 @@ impl Vecs {
self.weekindex_to_timestamp.compute_transform( self.weekindex_to_timestamp.compute_transform(
starting_weekindex, starting_weekindex,
self.weekindex_to_first_dateindex.mut_vec(), self.weekindex_to_first_dateindex.mut_vec(),
|(i, d, ..)| { |(i, d, ..)| (i, self.dateindex_to_timestamp.double_unwrap_cached_get(d)),
(
i,
*self.dateindex_to_timestamp.cached_get(d).unwrap().unwrap(),
)
},
exit, exit,
)?; )?;
@@ -498,8 +488,8 @@ impl Vecs {
let starting_monthindex = self let starting_monthindex = self
.dateindex_to_monthindex .dateindex_to_monthindex
.cached_get(starting_dateindex)? .unwrap_cached_get(starting_dateindex)
.map_or_else(Default::default, |v| v.into_inner()); .unwrap_or_default();
self.dateindex_to_monthindex.compute_transform( self.dateindex_to_monthindex.compute_transform(
starting_dateindex, starting_dateindex,
@@ -535,12 +525,7 @@ impl Vecs {
self.monthindex_to_timestamp.compute_transform( self.monthindex_to_timestamp.compute_transform(
starting_monthindex, starting_monthindex,
self.monthindex_to_first_dateindex.mut_vec(), self.monthindex_to_first_dateindex.mut_vec(),
|(i, d, ..)| { |(i, d, ..)| (i, self.dateindex_to_timestamp.double_unwrap_cached_get(d)),
(
i,
*self.dateindex_to_timestamp.cached_get(d).unwrap().unwrap(),
)
},
exit, exit,
)?; )?;
@@ -548,8 +533,8 @@ impl Vecs {
let starting_quarterindex = self let starting_quarterindex = self
.monthindex_to_quarterindex .monthindex_to_quarterindex
.cached_get(starting_monthindex)? .unwrap_cached_get(starting_monthindex)
.map_or_else(Default::default, |v| v.into_inner()); .unwrap_or_default();
self.monthindex_to_quarterindex.compute_transform( self.monthindex_to_quarterindex.compute_transform(
starting_monthindex, starting_monthindex,
@@ -585,12 +570,7 @@ impl Vecs {
self.quarterindex_to_timestamp.compute_transform( self.quarterindex_to_timestamp.compute_transform(
starting_quarterindex, starting_quarterindex,
self.quarterindex_to_first_monthindex.mut_vec(), self.quarterindex_to_first_monthindex.mut_vec(),
|(i, m, ..)| { |(i, m, ..)| (i, self.monthindex_to_timestamp.double_unwrap_cached_get(m)),
(
i,
*self.monthindex_to_timestamp.cached_get(m).unwrap().unwrap(),
)
},
exit, exit,
)?; )?;
@@ -598,8 +578,8 @@ impl Vecs {
let starting_yearindex = self let starting_yearindex = self
.monthindex_to_yearindex .monthindex_to_yearindex
.cached_get(starting_monthindex)? .unwrap_cached_get(starting_monthindex)
.map_or_else(Default::default, |v| v.into_inner()); .unwrap_or_default();
self.monthindex_to_yearindex.compute_transform( self.monthindex_to_yearindex.compute_transform(
starting_monthindex, starting_monthindex,
@@ -635,12 +615,7 @@ impl Vecs {
self.yearindex_to_timestamp.compute_transform( self.yearindex_to_timestamp.compute_transform(
starting_yearindex, starting_yearindex,
self.yearindex_to_first_monthindex.mut_vec(), self.yearindex_to_first_monthindex.mut_vec(),
|(i, m, ..)| { |(i, m, ..)| (i, self.monthindex_to_timestamp.double_unwrap_cached_get(m)),
(
i,
*self.monthindex_to_timestamp.cached_get(m).unwrap().unwrap(),
)
},
exit, exit,
)?; )?;
@@ -648,8 +623,8 @@ impl Vecs {
let starting_decadeindex = self let starting_decadeindex = self
.yearindex_to_decadeindex .yearindex_to_decadeindex
.cached_get(starting_yearindex)? .unwrap_cached_get(starting_yearindex)
.map_or_else(Default::default, |v| v.into_inner()); .unwrap_or_default();
self.yearindex_to_decadeindex.compute_transform( self.yearindex_to_decadeindex.compute_transform(
starting_yearindex, starting_yearindex,
@@ -683,12 +658,7 @@ impl Vecs {
self.decadeindex_to_timestamp.compute_transform( self.decadeindex_to_timestamp.compute_transform(
starting_decadeindex, starting_decadeindex,
self.decadeindex_to_first_yearindex.mut_vec(), self.decadeindex_to_first_yearindex.mut_vec(),
|(i, y, ..)| { |(i, y, ..)| (i, self.yearindex_to_timestamp.double_unwrap_cached_get(y)),
(
i,
*self.yearindex_to_timestamp.cached_get(y).unwrap().unwrap(),
)
},
exit, exit,
)?; )?;
@@ -696,8 +666,8 @@ impl Vecs {
let starting_difficultyepoch = self let starting_difficultyepoch = self
.height_to_difficultyepoch .height_to_difficultyepoch
.cached_get(decremented_starting_height)? .unwrap_cached_get(decremented_starting_height)
.map_or_else(Default::default, |v| v.into_inner()); .unwrap_or_default();
self.height_to_difficultyepoch.compute_transform( self.height_to_difficultyepoch.compute_transform(
starting_indexes.height, starting_indexes.height,
@@ -734,11 +704,7 @@ impl Vecs {
|(i, h, ..)| { |(i, h, ..)| {
( (
i, i,
*indexer_vecs indexer_vecs.height_to_timestamp.double_unwrap_cached_get(h),
.height_to_timestamp
.cached_get(h)
.unwrap()
.unwrap(),
) )
}, },
exit, exit,
@@ -748,8 +714,8 @@ impl Vecs {
let starting_halvingepoch = self let starting_halvingepoch = self
.height_to_halvingepoch .height_to_halvingepoch
.cached_get(decremented_starting_height)? .unwrap_cached_get(decremented_starting_height)
.map_or_else(Default::default, |v| v.into_inner()); .unwrap_or_default();
self.height_to_halvingepoch.compute_transform( self.height_to_halvingepoch.compute_transform(
starting_indexes.height, starting_indexes.height,
@@ -786,7 +752,7 @@ impl Vecs {
// |(i, h, ..)| { // |(i, h, ..)| {
// ( // (
// i, // i,
// *indexer_vecs.height_to_timestamp.cached_get(h).unwrap().unwrap(), // *indexer_vecs.height_to_timestamp.unwraped_cached_get(h).unwrap().unwrap(),
// ) // )
// }, // },
// exit, // exit,

View File

@@ -242,9 +242,8 @@ impl Vecs {
.get_height( .get_height(
h, h,
t, t,
h.decremented().map(|prev_h| { h.decremented()
*height_to_timestamp.cached_get(prev_h).unwrap().unwrap() .map(|prev_h| height_to_timestamp.double_unwrap_cached_get(prev_h)),
}),
) )
.unwrap(); .unwrap();
(h, ohlc) (h, ohlc)
@@ -454,43 +453,26 @@ impl Vecs {
self.weekindex_to_ohlc.compute_transform( self.weekindex_to_ohlc.compute_transform(
starting_indexes.weekindex, starting_indexes.weekindex,
self.timeindexes_to_close self.timeindexes_to_close.weekindex.unwrap_last().mut_vec(),
.weekindex
.last
.as_mut()
.unwrap()
.mut_vec(),
|(i, close, ..)| { |(i, close, ..)| {
( (
i, i,
OHLCDollars { OHLCDollars {
open: *self open: self
.timeindexes_to_open .timeindexes_to_open
.weekindex .weekindex
.first .unwrap_first()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() high: self
.cached_get(i)
.unwrap()
.unwrap(),
high: *self
.timeindexes_to_high .timeindexes_to_high
.weekindex .weekindex
.max .unwrap_max()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() low: self
.cached_get(i)
.unwrap()
.unwrap(),
low: *self
.timeindexes_to_low .timeindexes_to_low
.weekindex .weekindex
.min .unwrap_min()
.as_mut() .double_unwrap_cached_get(i),
.unwrap()
.cached_get(i)
.unwrap()
.unwrap(),
close, close,
}, },
) )
@@ -502,41 +484,27 @@ impl Vecs {
starting_indexes.difficultyepoch, starting_indexes.difficultyepoch,
self.chainindexes_to_close self.chainindexes_to_close
.difficultyepoch .difficultyepoch
.last .unwrap_last()
.as_mut()
.unwrap()
.mut_vec(), .mut_vec(),
|(i, close, ..)| { |(i, close, ..)| {
( (
i, i,
OHLCDollars { OHLCDollars {
open: *self open: self
.chainindexes_to_open .chainindexes_to_open
.difficultyepoch .difficultyepoch
.first .unwrap_first()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() high: self
.cached_get(i)
.unwrap()
.unwrap(),
high: *self
.chainindexes_to_high .chainindexes_to_high
.difficultyepoch .difficultyepoch
.max .unwrap_max()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() low: self
.cached_get(i)
.unwrap()
.unwrap(),
low: *self
.chainindexes_to_low .chainindexes_to_low
.difficultyepoch .difficultyepoch
.min .unwrap_min()
.as_mut() .double_unwrap_cached_get(i),
.unwrap()
.cached_get(i)
.unwrap()
.unwrap(),
close, close,
}, },
) )
@@ -546,43 +514,26 @@ impl Vecs {
self.monthindex_to_ohlc.compute_transform( self.monthindex_to_ohlc.compute_transform(
starting_indexes.monthindex, starting_indexes.monthindex,
self.timeindexes_to_close self.timeindexes_to_close.monthindex.unwrap_last().mut_vec(),
.monthindex
.last
.as_mut()
.unwrap()
.mut_vec(),
|(i, close, ..)| { |(i, close, ..)| {
( (
i, i,
OHLCDollars { OHLCDollars {
open: *self open: self
.timeindexes_to_open .timeindexes_to_open
.monthindex .monthindex
.first .unwrap_first()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() high: self
.cached_get(i)
.unwrap()
.unwrap(),
high: *self
.timeindexes_to_high .timeindexes_to_high
.monthindex .monthindex
.max .unwrap_max()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() low: self
.cached_get(i)
.unwrap()
.unwrap(),
low: *self
.timeindexes_to_low .timeindexes_to_low
.monthindex .monthindex
.min .unwrap_min()
.as_mut() .double_unwrap_cached_get(i),
.unwrap()
.cached_get(i)
.unwrap()
.unwrap(),
close, close,
}, },
) )
@@ -594,41 +545,27 @@ impl Vecs {
starting_indexes.quarterindex, starting_indexes.quarterindex,
self.timeindexes_to_close self.timeindexes_to_close
.quarterindex .quarterindex
.last .unwrap_last()
.as_mut()
.unwrap()
.mut_vec(), .mut_vec(),
|(i, close, ..)| { |(i, close, ..)| {
( (
i, i,
OHLCDollars { OHLCDollars {
open: *self open: self
.timeindexes_to_open .timeindexes_to_open
.quarterindex .quarterindex
.first .unwrap_first()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() high: self
.cached_get(i)
.unwrap()
.unwrap(),
high: *self
.timeindexes_to_high .timeindexes_to_high
.quarterindex .quarterindex
.max .unwrap_max()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() low: self
.cached_get(i)
.unwrap()
.unwrap(),
low: *self
.timeindexes_to_low .timeindexes_to_low
.quarterindex .quarterindex
.min .unwrap_min()
.as_mut() .double_unwrap_cached_get(i),
.unwrap()
.cached_get(i)
.unwrap()
.unwrap(),
close, close,
}, },
) )
@@ -638,43 +575,26 @@ impl Vecs {
self.yearindex_to_ohlc.compute_transform( self.yearindex_to_ohlc.compute_transform(
starting_indexes.yearindex, starting_indexes.yearindex,
self.timeindexes_to_close self.timeindexes_to_close.yearindex.unwrap_last().mut_vec(),
.yearindex
.last
.as_mut()
.unwrap()
.mut_vec(),
|(i, close, ..)| { |(i, close, ..)| {
( (
i, i,
OHLCDollars { OHLCDollars {
open: *self open: self
.timeindexes_to_open .timeindexes_to_open
.yearindex .yearindex
.first .unwrap_first()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() high: self
.cached_get(i)
.unwrap()
.unwrap(),
high: *self
.timeindexes_to_high .timeindexes_to_high
.yearindex .yearindex
.max .unwrap_max()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() low: self
.cached_get(i)
.unwrap()
.unwrap(),
low: *self
.timeindexes_to_low .timeindexes_to_low
.yearindex .yearindex
.min .unwrap_min()
.as_mut() .double_unwrap_cached_get(i),
.unwrap()
.cached_get(i)
.unwrap()
.unwrap(),
close, close,
}, },
) )
@@ -689,42 +609,27 @@ impl Vecs {
starting_indexes.decadeindex, starting_indexes.decadeindex,
self.timeindexes_to_close self.timeindexes_to_close
.decadeindex .decadeindex
.last .unwrap_last()
.as_mut()
.as_mut()
.unwrap()
.mut_vec(), .mut_vec(),
|(i, close, ..)| { |(i, close, ..)| {
( (
i, i,
OHLCDollars { OHLCDollars {
open: *self open: self
.timeindexes_to_open .timeindexes_to_open
.decadeindex .decadeindex
.first .unwrap_first()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() high: self
.cached_get(i)
.unwrap()
.unwrap(),
high: *self
.timeindexes_to_high .timeindexes_to_high
.decadeindex .decadeindex
.max .unwrap_max()
.as_mut() .double_unwrap_cached_get(i),
.unwrap() low: self
.cached_get(i)
.unwrap()
.unwrap(),
low: *self
.timeindexes_to_low .timeindexes_to_low
.decadeindex .decadeindex
.min .unwrap_min()
.as_mut() .double_unwrap_cached_get(i),
.unwrap()
.cached_get(i)
.unwrap()
.unwrap(),
close, close,
}, },
) )

View File

@@ -295,10 +295,7 @@ impl Vecs {
|txindex| { |txindex| {
let v = indexer_vecs let v = indexer_vecs
.txindex_to_txversion .txindex_to_txversion
.cached_get(txindex) .double_unwrap_cached_get(txindex);
.unwrap()
.unwrap()
.into_inner();
v == txversion v == txversion
}, },
exit, exit,
@@ -326,10 +323,7 @@ impl Vecs {
let total_size = indexer_vecs let total_size = indexer_vecs
.txindex_to_total_size .txindex_to_total_size
.mut_vec() .mut_vec()
.cached_get(txindex) .double_unwrap_cached_get(txindex);
.unwrap()
.unwrap()
.into_inner();
// This is the exact definition of a weight unit, as defined by BIP-141 (quote above). // This is the exact definition of a weight unit, as defined by BIP-141 (quote above).
let wu = base_size * 3 + total_size; let wu = base_size * 3 + total_size;
@@ -357,12 +351,12 @@ impl Vecs {
|(txinindex, txoutindex, slf, other)| { |(txinindex, txoutindex, slf, other)| {
let value = if txoutindex == Txoutindex::COINBASE { let value = if txoutindex == Txoutindex::COINBASE {
Sats::ZERO Sats::ZERO
} else if let Ok(Some(value)) = indexer_vecs } else if let Some(value) = indexer_vecs
.txoutindex_to_value .txoutindex_to_value
.mut_vec() .mut_vec()
.cached_get(txoutindex) .unwrap_cached_get(txoutindex)
{ {
*value value
} else { } else {
dbg!(txinindex, txoutindex, slf.len(), other.len()); dbg!(txinindex, txoutindex, slf.len(), other.len());
panic!() panic!()
@@ -429,11 +423,8 @@ impl Vecs {
if input_value.is_zero() { if input_value.is_zero() {
(txindex, input_value) (txindex, input_value)
} else { } else {
let output_value = txindex_to_output_value let output_value =
.cached_get(txindex) txindex_to_output_value.double_unwrap_cached_get(txindex);
.unwrap()
.unwrap()
.into_inner();
(txindex, input_value.checked_sub(output_value).unwrap()) (txindex, input_value.checked_sub(output_value).unwrap())
} }
}, },
@@ -455,10 +446,7 @@ impl Vecs {
let vsize = self let vsize = self
.txindex_to_vsize .txindex_to_vsize
.mut_vec() .mut_vec()
.cached_get(txindex) .double_unwrap_cached_get(txindex);
.unwrap()
.unwrap()
.into_inner();
(txindex, Feerate::from((fee, vsize))) (txindex, Feerate::from((fee, vsize)))
}, },
@@ -496,29 +484,18 @@ impl Vecs {
|(height, txindex, ..)| { |(height, txindex, ..)| {
let first_txoutindex = indexer_vecs let first_txoutindex = indexer_vecs
.txindex_to_first_txoutindex .txindex_to_first_txoutindex
.cached_get(txindex) .double_unwrap_cached_get(txindex)
.unwrap() .unwrap_to_usize();
.unwrap()
.into_inner()
.to_usize()
.unwrap();
let last_txoutindex = indexes let last_txoutindex = indexes
.txindex_to_last_txoutindex .txindex_to_last_txoutindex
.mut_vec() .mut_vec()
.cached_get(txindex) .double_unwrap_cached_get(txindex)
.unwrap() .unwrap_to_usize();
.unwrap()
.into_inner()
.to_usize()
.unwrap();
let mut sats = Sats::ZERO; let mut sats = Sats::ZERO;
(first_txoutindex..=last_txoutindex).for_each(|txoutindex| { (first_txoutindex..=last_txoutindex).for_each(|txoutindex| {
sats += indexer_vecs sats += indexer_vecs
.txoutindex_to_value .txoutindex_to_value
.cached_get_(txoutindex) .double_unwrap_cached_get(Txoutindex::from(txoutindex));
.unwrap()
.unwrap()
.into_inner();
}); });
(height, sats) (height, sats)
}, },
@@ -540,14 +517,9 @@ impl Vecs {
let fees = self let fees = self
.indexes_to_fee .indexes_to_fee
.height .height
.sum .unwrap_sum()
.as_mut()
.unwrap()
.mut_vec() .mut_vec()
.cached_get(height) .double_unwrap_cached_get(height);
.unwrap()
.unwrap()
.into_inner();
(height, subsidy.checked_sub(fees).unwrap()) (height, subsidy.checked_sub(fees).unwrap())
}, },
exit, exit,

View File

@@ -46,12 +46,12 @@ where
self.inner.get(index) self.inner.get(index)
} }
#[inline] #[inline]
pub fn cached_get(&mut self, index: I) -> Result<Option<Value<'_, T>>> { pub fn unwrap_cached_get(&mut self, index: I) -> Option<T> {
self.inner.cached_get(index) self.inner.unwrap_cached_get(index)
} }
#[inline] #[inline]
pub fn cached_get_(&mut self, index: usize) -> Result<Option<Value<'_, T>>> { pub fn double_unwrap_cached_get(&mut self, index: I) -> T {
self.inner.cached_get_(index) self.inner.double_unwrap_cached_get(index)
} }
pub fn iter_from<F>(&mut self, index: I, f: F) -> Result<()> pub fn iter_from<F>(&mut self, index: I, f: F) -> Result<()>

View File

@@ -41,16 +41,16 @@ The element returned by the iterator is a tuple which includes the:
- Block: `Block` (from `bitcoin-rust`) - Block: `Block` (from `bitcoin-rust`)
- Block's Hash: `BlockHash` (also from `bitcoin-rust`) - Block's Hash: `BlockHash` (also from `bitcoin-rust`)
## Example
`src/main.rs`
## Requirements ## Requirements
Even though it reads *blkXXXXX.dat* files, it **needs** `bitcoind` to run with the RPC server to filter out block forks. Even though it reads *blkXXXXX.dat* files, it **needs** `bitcoind` to run with the RPC server to filter out block forks.
Peak memory should be around 500MB. Peak memory should be around 500MB.
## Disclaimer
A state is saved in `{bitcoindir}/blocks/blk_index_to_blk_recap.json` to allow for faster starts (see benchmark below) but doesn't yet support locking. Thus it is recommended to run one instance of `brk_parser` at a time.
## Comparaison ## Comparaison
| | [brk_parser](https://crates.io/crates/brk_parser) | [bitcoin-explorer (deprecated)](https://crates.io/crates/bitcoin-explorer) | [blocks_iterator](https://crates.io/crates/blocks_iterator) | | | [brk_parser](https://crates.io/crates/brk_parser) | [bitcoin-explorer (deprecated)](https://crates.io/crates/bitcoin-explorer) | [blocks_iterator](https://crates.io/crates/blocks_iterator) |

View File

@@ -17,7 +17,15 @@ pub trait DynamicVec: Send + Sync {
} }
#[inline] #[inline]
fn cached_get(&mut self, index: Self::I) -> Result<Option<Value<Self::T>>> { fn cached_get(&mut self, index: Self::I) -> Result<Option<Value<Self::T>>> {
self.get_(index.to_usize()?) self.cached_get_(index.to_usize()?)
}
#[inline]
fn unwrap_cached_get(&mut self, index: Self::I) -> Option<Self::T> {
self.cached_get(index).unwrap().map(Value::into_inner)
}
#[inline]
fn double_unwrap_cached_get(&mut self, index: Self::I) -> Self::T {
self.unwrap_cached_get(index).unwrap()
} }
#[inline] #[inline]
fn get_(&self, index: usize) -> Result<Option<Value<Self::T>>> { fn get_(&self, index: usize) -> Result<Option<Value<Self::T>>> {

View File

@@ -18,6 +18,7 @@ where
+ Send + Send
+ Sync, + Sync,
{ {
fn unwrap_to_usize(self) -> usize;
fn to_usize(self) -> Result<usize>; fn to_usize(self) -> Result<usize>;
fn to_string<'a>() -> &'a str; fn to_string<'a>() -> &'a str;
} }
@@ -37,7 +38,12 @@ where
+ Send + Send
+ Sync, + Sync,
{ {
#[inline(always)] #[inline]
fn unwrap_to_usize(self) -> usize {
self.to_usize().unwrap()
}
#[inline]
fn to_usize(self) -> Result<usize> { fn to_usize(self) -> Result<usize> {
self.try_into().map_err(|_| Error::FailedKeyTryIntoUsize) self.try_into().map_err(|_| Error::FailedKeyTryIntoUsize)
} }