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]]
name = "libc"
version = "0.2.171"
version = "0.2.172"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6"
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
[[package]]
name = "linux-raw-sys"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -46,12 +46,12 @@ where
self.inner.get(index)
}
#[inline]
pub fn cached_get(&mut self, index: I) -> Result<Option<Value<'_, T>>> {
self.inner.cached_get(index)
pub fn unwrap_cached_get(&mut self, index: I) -> Option<T> {
self.inner.unwrap_cached_get(index)
}
#[inline]
pub fn cached_get_(&mut self, index: usize) -> Result<Option<Value<'_, T>>> {
self.inner.cached_get_(index)
pub fn double_unwrap_cached_get(&mut self, index: I) -> T {
self.inner.double_unwrap_cached_get(index)
}
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's Hash: `BlockHash` (also from `bitcoin-rust`)
## Example
`src/main.rs`
## Requirements
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.
## 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
| | [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]
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]
fn get_(&self, index: usize) -> Result<Option<Value<Self::T>>> {

View File

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