mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-25 15:19:58 -07:00
vec: rework part 2
This commit is contained in:
@@ -10,12 +10,20 @@ use std::{
|
||||
use brk_core::CheckedSub;
|
||||
use brk_exit::Exit;
|
||||
use brk_vec::{
|
||||
AnyStoredVec, Compressed, Error, MAX_CACHE_SIZE, Result, StoredIndex, StoredType, StoredVec,
|
||||
Compressed, DynamicVec, Error, GenericVec, Result, StoredIndex, StoredType, StoredVec, Value,
|
||||
Version,
|
||||
};
|
||||
|
||||
const ONE_KIB: usize = 1024;
|
||||
const ONE_MIB: usize = ONE_KIB * ONE_KIB;
|
||||
const MAX_CACHE_SIZE: usize = 100 * ONE_MIB;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ComputedVec<I, T> {
|
||||
pub struct ComputedVec<I, T>
|
||||
where
|
||||
I: StoredIndex,
|
||||
T: StoredType,
|
||||
{
|
||||
computed_version: Option<Version>,
|
||||
vec: StoredVec<I, T>,
|
||||
}
|
||||
@@ -71,7 +79,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub fn safe_flush(&mut self, exit: &Exit) -> io::Result<()> {
|
||||
pub fn safe_flush(&mut self, exit: &Exit) -> Result<()> {
|
||||
if exit.triggered() {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -97,21 +105,21 @@ where
|
||||
&mut self.vec
|
||||
}
|
||||
|
||||
pub fn any_vec(&self) -> &dyn AnyStoredVec {
|
||||
pub fn any_vec(&self) -> &dyn brk_vec::AnyStoredVec {
|
||||
&self.vec
|
||||
}
|
||||
|
||||
pub fn mut_any_vec(&mut self) -> &mut dyn AnyStoredVec {
|
||||
pub fn mut_any_vec(&mut self) -> &mut dyn brk_vec::AnyStoredVec {
|
||||
&mut self.vec
|
||||
}
|
||||
|
||||
pub fn get(&mut self, index: I) -> Result<Option<&T>> {
|
||||
pub fn get(&mut self, index: I) -> Result<Option<Value<T>>> {
|
||||
self.vec.get(index)
|
||||
}
|
||||
|
||||
pub fn collect_range(&self, from: Option<i64>, to: Option<i64>) -> Result<Vec<T>> {
|
||||
self.vec.collect_range(from, to)
|
||||
}
|
||||
// pub fn collect_range(&self, from: Option<i64>, to: Option<i64>) -> Result<Vec<T>> {
|
||||
// self.vec.collect_range(from, to)
|
||||
// }
|
||||
|
||||
#[inline]
|
||||
fn path_computed_version(&self) -> PathBuf {
|
||||
@@ -137,14 +145,14 @@ where
|
||||
where
|
||||
A: StoredIndex,
|
||||
B: StoredType,
|
||||
F: FnMut((A, B, &mut Self, &mut StoredVec<A, B>)) -> (I, T),
|
||||
F: FnMut((A, B, &mut Self, &mut dyn DynamicVec<I = A, T = B>)) -> (I, T),
|
||||
{
|
||||
self.validate_computed_version_or_reset_file(
|
||||
Version::ZERO + self.version() + other.version(),
|
||||
)?;
|
||||
|
||||
let index = max_from.min(A::from(self.len()));
|
||||
other.iter_from_cloned(index, |(a, b, other)| {
|
||||
other.iter_from(index, |(a, b, other)| {
|
||||
let (i, v) = t((a, b, self, other));
|
||||
self.forced_push_at(i, v, exit)
|
||||
})?;
|
||||
@@ -166,9 +174,12 @@ where
|
||||
Version::ZERO + self.version() + other.version(),
|
||||
)?;
|
||||
|
||||
let index = max_from.min(self.vec.get_last()?.cloned().unwrap_or_default());
|
||||
let index = max_from.min(
|
||||
self.vec
|
||||
.get_last()?
|
||||
.map_or_else(T::default, |v| v.into_inner()),
|
||||
);
|
||||
other.iter_from(index, |(v, i, ..)| {
|
||||
let i = *i;
|
||||
if self.get(i).unwrap().is_none_or(|old_v| *old_v > v) {
|
||||
self.forced_push_at(i, v, exit)
|
||||
} else {
|
||||
@@ -260,7 +271,7 @@ where
|
||||
first_indexes.iter_from(index, |(i, first_index, ..)| {
|
||||
let last_index = last_indexes.get(i)?.unwrap();
|
||||
let count = (*last_index + 1_usize)
|
||||
.checked_sub(*first_index)
|
||||
.checked_sub(first_index)
|
||||
.unwrap_or_default();
|
||||
self.forced_push_at(i, count.into(), exit)
|
||||
})?;
|
||||
@@ -286,7 +297,11 @@ where
|
||||
|
||||
let index = max_from.min(I::from(self.len()));
|
||||
self_to_other.iter_from(index, |(i, other, ..)| {
|
||||
self.forced_push_at(i, T::from(other_to_self.get(*other)?.unwrap() == &i), exit)
|
||||
self.forced_push_at(
|
||||
i,
|
||||
T::from(other_to_self.get(other)?.unwrap().into_inner() == i),
|
||||
exit,
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(self.safe_flush(exit)?)
|
||||
@@ -311,7 +326,7 @@ where
|
||||
let index = max_from.min(I::from(self.len()));
|
||||
first_indexes.iter_from(index, |(index, first_index, ..)| {
|
||||
let last_index = last_indexes.get(index)?.unwrap();
|
||||
let count = *last_index + 1_usize - *first_index;
|
||||
let count = *last_index + 1_usize - first_index;
|
||||
self.forced_push_at(index, count.into(), exit)
|
||||
})?;
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ use brk_core::{CheckedSub, StoredU32, StoredU64, StoredUsize, Timestamp, Weight}
|
||||
use brk_exit::Exit;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_parser::bitcoin;
|
||||
use brk_vec::{AnyStoredVec, Compressed, Version};
|
||||
use brk_vec::{Compressed, DynamicVec, Version};
|
||||
|
||||
use super::{
|
||||
Indexes,
|
||||
@@ -156,7 +156,7 @@ impl Vecs {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn AnyStoredVec> {
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn brk_vec::AnyStoredVec> {
|
||||
[
|
||||
self.indexes_to_block_interval.any_vecs(),
|
||||
self.indexes_to_block_count.any_vecs(),
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use std::path::Path;
|
||||
|
||||
use brk_exit::Exit;
|
||||
use brk_vec::{AnyStoredVec, Compressed, Result, StoredIndex, StoredType, StoredVec, Version};
|
||||
use brk_vec::{
|
||||
AnyStoredVec, Compressed, DynamicVec, GenericVec, Result, StoredIndex, StoredType, StoredVec,
|
||||
Version,
|
||||
};
|
||||
|
||||
use crate::storage::vecs::base::ComputedVec;
|
||||
|
||||
@@ -123,7 +126,7 @@ where
|
||||
|
||||
let total_vec = self.total.as_mut().unwrap();
|
||||
|
||||
source.iter_from(index, |(i, v)| {
|
||||
source.iter_from(index, |(i, v, ..)| {
|
||||
let prev = i
|
||||
.to_usize()
|
||||
.unwrap()
|
||||
@@ -132,8 +135,7 @@ where
|
||||
total_vec
|
||||
.get(I::from(prev_i))
|
||||
.unwrap()
|
||||
.unwrap_or(&T::from(0_usize))
|
||||
.to_owned()
|
||||
.map_or(T::from(0_usize), |v| v.into_inner())
|
||||
});
|
||||
let value = v.clone() + prev;
|
||||
total_vec.forced_push_at(i, value, exit)?;
|
||||
@@ -161,18 +163,18 @@ where
|
||||
{
|
||||
let index = self.starting_index(max_from);
|
||||
|
||||
first_indexes.iter_from(index, |(i, first_index)| {
|
||||
let first_index = *first_index;
|
||||
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.get(first_index).unwrap().unwrap();
|
||||
first.forced_push_at(index, v.clone(), exit)?;
|
||||
let v = source.get(first_index).unwrap().unwrap().into_inner();
|
||||
first.forced_push_at(index, v, exit)?;
|
||||
}
|
||||
|
||||
if let Some(last) = self.last.as_mut() {
|
||||
let v = source.get(last_index).unwrap().unwrap();
|
||||
last.forced_push_at(index, v.clone(), exit)?;
|
||||
let v = source.get(last_index).unwrap().unwrap().into_inner();
|
||||
last.forced_push_at(index, v, exit)?;
|
||||
}
|
||||
|
||||
let first_index = first_index.to_usize()?;
|
||||
@@ -249,7 +251,12 @@ where
|
||||
let prev = i.to_usize().unwrap().checked_sub(1).map_or(
|
||||
T::from(0_usize),
|
||||
|prev_i| {
|
||||
total_vec.get(I::from(prev_i)).unwrap().unwrap().to_owned()
|
||||
total_vec
|
||||
.get(I::from(prev_i))
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.to_owned()
|
||||
.into_inner()
|
||||
},
|
||||
);
|
||||
total_vec.forced_push_at(i, prev + sum, exit)?;
|
||||
@@ -302,8 +309,8 @@ where
|
||||
.unwrap()
|
||||
.get(first_index)
|
||||
.unwrap()
|
||||
.cloned()
|
||||
.unwrap();
|
||||
.unwrap()
|
||||
.into_inner();
|
||||
first.forced_push_at(index, v, exit)?;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ use brk_core::{
|
||||
};
|
||||
use brk_exit::Exit;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_vec::{AnyStoredVec, Compressed, Version};
|
||||
use brk_vec::{Compressed, Version};
|
||||
|
||||
use super::ComputedVec;
|
||||
|
||||
@@ -366,8 +366,7 @@ impl Vecs {
|
||||
let starting_dateindex = self
|
||||
.height_to_dateindex
|
||||
.get(starting_indexes.height.decremented().unwrap_or_default())?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
.map_or_else(Default::default, |v| v.into_inner());
|
||||
|
||||
self.height_to_dateindex.compute_transform(
|
||||
starting_indexes.height,
|
||||
@@ -379,7 +378,7 @@ impl Vecs {
|
||||
let starting_dateindex = if let Some(dateindex) = self
|
||||
.height_to_dateindex
|
||||
.get(starting_indexes.height.decremented().unwrap_or_default())?
|
||||
.copied()
|
||||
.map(|v| v.into_inner())
|
||||
{
|
||||
starting_dateindex.min(dateindex)
|
||||
} else {
|
||||
@@ -452,8 +451,7 @@ impl Vecs {
|
||||
let starting_weekindex = self
|
||||
.dateindex_to_weekindex
|
||||
.get(starting_dateindex)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
.map_or_else(Default::default, |v| v.into_inner());
|
||||
|
||||
self.dateindex_to_weekindex.compute_transform(
|
||||
starting_dateindex,
|
||||
@@ -496,8 +494,7 @@ impl Vecs {
|
||||
let starting_monthindex = self
|
||||
.dateindex_to_monthindex
|
||||
.get(starting_dateindex)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
.map_or_else(Default::default, |v| v.into_inner());
|
||||
|
||||
self.dateindex_to_monthindex.compute_transform(
|
||||
starting_dateindex,
|
||||
@@ -542,8 +539,7 @@ impl Vecs {
|
||||
let starting_quarterindex = self
|
||||
.monthindex_to_quarterindex
|
||||
.get(starting_monthindex)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
.map_or_else(Default::default, |v| v.into_inner());
|
||||
|
||||
self.monthindex_to_quarterindex.compute_transform(
|
||||
starting_monthindex,
|
||||
@@ -588,8 +584,7 @@ impl Vecs {
|
||||
let starting_yearindex = self
|
||||
.monthindex_to_yearindex
|
||||
.get(starting_monthindex)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
.map_or_else(Default::default, |v| v.into_inner());
|
||||
|
||||
self.monthindex_to_yearindex.compute_transform(
|
||||
starting_monthindex,
|
||||
@@ -634,8 +629,7 @@ impl Vecs {
|
||||
let starting_decadeindex = self
|
||||
.yearindex_to_decadeindex
|
||||
.get(starting_yearindex)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
.map_or_else(Default::default, |v| v.into_inner());
|
||||
|
||||
self.yearindex_to_decadeindex.compute_transform(
|
||||
starting_yearindex,
|
||||
@@ -678,8 +672,7 @@ impl Vecs {
|
||||
let starting_difficultyepoch = self
|
||||
.height_to_difficultyepoch
|
||||
.get(starting_indexes.height)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
.map_or_else(Default::default, |v| v.into_inner());
|
||||
|
||||
self.height_to_difficultyepoch.compute_transform(
|
||||
starting_indexes.height,
|
||||
@@ -727,8 +720,7 @@ impl Vecs {
|
||||
let starting_halvingepoch = self
|
||||
.height_to_halvingepoch
|
||||
.get(starting_indexes.height)?
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
.map_or_else(Default::default, |v| v.into_inner());
|
||||
|
||||
self.height_to_halvingepoch.compute_transform(
|
||||
starting_indexes.height,
|
||||
@@ -784,7 +776,7 @@ impl Vecs {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn AnyStoredVec> {
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn brk_vec::AnyStoredVec> {
|
||||
vec![
|
||||
self.dateindex_to_date.any_vec(),
|
||||
self.dateindex_to_dateindex.any_vec(),
|
||||
|
||||
@@ -7,7 +7,7 @@ use brk_core::{
|
||||
use brk_exit::Exit;
|
||||
use brk_fetcher::Fetcher;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_vec::{AnyStoredVec, Compressed, Version};
|
||||
use brk_vec::{Compressed, DynamicVec, Version};
|
||||
|
||||
use super::{
|
||||
ComputedVec, Indexes,
|
||||
@@ -765,7 +765,7 @@ impl Vecs {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn AnyStoredVec> {
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn brk_vec::AnyStoredVec> {
|
||||
vec![
|
||||
vec![
|
||||
self.dateindex_to_close_in_cents.any_vec(),
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::{fs, path::Path};
|
||||
use brk_core::{Sats, StoredU64, Txindex, Txinindex, Txoutindex};
|
||||
use brk_exit::Exit;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_vec::{AnyStoredVec, Compressed, Version};
|
||||
use brk_vec::{Compressed, DynamicVec, Version};
|
||||
|
||||
use super::{
|
||||
ComputedVec, Indexes,
|
||||
@@ -203,7 +203,7 @@ impl Vecs {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn AnyStoredVec> {
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn brk_vec::AnyStoredVec> {
|
||||
[
|
||||
vec![
|
||||
self.txindex_to_is_coinbase.any_vec(),
|
||||
|
||||
Reference in New Issue
Block a user