server: smart generic vec routes build

This commit is contained in:
nym21
2025-02-12 14:11:04 +01:00
parent eaf76e27f5
commit 269c64e4ed
21 changed files with 278 additions and 411 deletions

View File

@@ -83,9 +83,11 @@ impl Computer<SINGLE_THREAD> {
let date_count = self.vecs.height_to_date.len();
// self.vecs.height_to_dateindex.compute(...)
self.vecs
.date_to_first_height
.compute_inverse_more_to_less(&mut self.vecs.height_to_date)?;
.dateindex_to_first_height
.compute_inverse_more_to_less(&mut self.vecs.height_to_dateindex)?;
// ---
// Date to X

View File

@@ -3,17 +3,21 @@ use std::{fs, path::Path};
use indexer::{Addressindex, Amount, Height, Timestamp, Txindex, Txinindex, Txoutindex};
use storable_vec::{StorableVec, Version};
use crate::structs::{Date, Feerate};
use crate::{
structs::{Date, Feerate},
Dateindex,
};
// mod base;
// use base::*;
pub struct StorableVecs<const MODE: u8> {
pub date_to_first_height: StorableVec<Date, Height, MODE>,
// pub date_to_last_height: StorableVec<Date, Height, MODE>,
pub dateindex_to_first_height: StorableVec<Dateindex, Height, MODE>,
// pub dateindex_to_last_height: StorableVec<Dateindex, Height, MODE>,
// pub height_to_block_interval: StorableVec<Height, Timestamp, MODE>,
pub height_to_date: StorableVec<Height, Date, MODE>,
pub height_to_dateindex: StorableVec<Height, Dateindex, MODE>,
// pub height_to_fee: StorableVec<Txindex, Amount, MODE>,
// pub height_to_inputcount: StorableVec<Height, u32, MODE>,
// pub height_to_last_addressindex: StorableVec<Height, Addressindex, MODE>,
@@ -43,9 +47,13 @@ impl<const MODE: u8> StorableVecs<MODE> {
fs::create_dir_all(path)?;
Ok(Self {
date_to_first_height: StorableVec::forced_import(&path.join("date_to_first_height"), Version::from(1))?,
dateindex_to_first_height: StorableVec::forced_import(
&path.join("dateindex_to_first_height"),
Version::from(1),
)?,
// height_to_block_interval: StorableVec::forced_import(&path.join("height_to_block_interval"), Version::from(1))?,
height_to_date: StorableVec::forced_import(&path.join("height_to_date"), Version::from(1))?,
height_to_dateindex: StorableVec::forced_import(&path.join("height_to_dateindex"), Version::from(1))?,
// height_to_fee: StorableVec::forced_import(&path.join("height_to_fee"), Version::from(1))?,
// height_to_inputcount: StorableVec::forced_import(&path.join("height_to_inputcount"), Version::from(1))?,
// height_to_last_addressindex: StorableVec::forced_import(
@@ -87,10 +95,10 @@ impl<const MODE: u8> StorableVecs<MODE> {
}
// pub fn as_slice(&self) -> [&dyn AnyComputedStorableVec; 1] {
// [&self.date_to_first_height]
// [&self.dateindex_to_first_height]
// }
// pub fn as_mut_slice(&mut self) -> [&mut dyn AnyComputedStorableVec; 1] {
// [&mut self.date_to_first_height]
// [&mut self.dateindex_to_first_height]
// }
}

View File

@@ -6,25 +6,25 @@ use jiff::{civil::Date as Date_, tz::TimeZone, Span};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromBytes, Immutable, IntoBytes, KnownLayout)]
pub struct Date {
year: u16,
month: u8,
day: u8,
}
pub struct Date(u32);
impl Date {
const INDEX_ZERO: Self = Self {
year: 2009,
month: 1,
day: 3,
};
const INDEX_ZERO: Self = Self(20090103);
const INDEX_ZERO_: Date_ = Date_::constant(2009, 1, 3);
const INDEX_ONE: Self = Self {
year: 2009,
month: 1,
day: 9,
};
const INDEX_ONE: Self = Self(20090109);
const INDEX_ONE_: Date_ = Date_::constant(2009, 1, 9);
pub fn year(&self) -> u16 {
(self.0 / 1_00_00) as u16
}
pub fn month(&self) -> u8 {
((self.0 % 1_00_00) / 1_00) as u8
}
pub fn day(&self) -> u8 {
(self.0 % 1_00) as u8
}
}
impl Default for Date {
@@ -33,31 +33,15 @@ impl Default for Date {
}
}
impl PartialOrd for Date {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Date {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
Date_::from(*self).cmp(&Date_::from(*other))
}
}
impl From<Date_> for Date {
fn from(value: Date_) -> Self {
Self {
year: value.year() as u16,
month: value.month() as u8,
day: value.day() as u8,
}
Self(value.year() as u32 * 1_00_00 + value.month() as u32 * 1_00 + value.day() as u32)
}
}
impl From<Date> for Date_ {
fn from(value: Date) -> Self {
Self::new(value.year as i16, value.month as i8, value.day as i8).unwrap()
Self::new(value.year() as i16, value.month() as i8, value.day() as i8).unwrap()
}
}
@@ -67,33 +51,33 @@ impl From<Timestamp> for Date {
}
}
impl TryFrom<Date> for usize {
type Error = color_eyre::Report;
fn try_from(value: Date) -> Result<Self, Self::Error> {
let value_ = Date_::from(value);
if value_ < Date::INDEX_ZERO_ {
Err(eyre!("Date is too early"))
} else if value == Date::INDEX_ZERO {
Ok(0)
} else if value_ < Date::INDEX_ONE_ {
Err(eyre!("Date is between first and second"))
} else if value == Date::INDEX_ONE {
Ok(1)
} else {
Ok(Date_::from(Date::INDEX_ONE).until(value_)?.get_days() as usize + 1)
}
}
}
// impl TryFrom<Date> for usize {
// type Error = color_eyre::Report;
// fn try_from(value: Date) -> Result<Self, Self::Error> {
// let value_ = Date_::from(value);
// if value_ < Date::INDEX_ZERO_ {
// Err(eyre!("Date is too early"))
// } else if value == Date::INDEX_ZERO {
// Ok(0)
// } else if value_ < Date::INDEX_ONE_ {
// Err(eyre!("Date is between first and second"))
// } else if value == Date::INDEX_ONE {
// Ok(1)
// } else {
// Ok(Date_::from(Date::INDEX_ONE).until(value_)?.get_days() as usize + 1)
// }
// }
// }
impl From<usize> for Date {
fn from(value: usize) -> Self {
Self::from(Self::INDEX_ZERO_.checked_add(Span::new().days(value as i64)).unwrap())
}
}
// impl From<usize> for Date {
// fn from(value: usize) -> Self {
// Self::from(Self::INDEX_ZERO_.checked_add(Span::new().days(value as i64)).unwrap())
// }
// }
impl Add<usize> for Date {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(Date_::from(self).checked_add(Span::new().days(rhs as i64)).unwrap())
}
}
// impl Add<usize> for Date {
// type Output = Self;
// fn add(self, rhs: usize) -> Self::Output {
// Self::from(Date_::from(self).checked_add(Span::new().days(rhs as i64)).unwrap())
// }
// }

View File

@@ -0,0 +1,25 @@
use std::ops::Add;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromBytes, Immutable, IntoBytes, KnownLayout)]
pub struct Dateindex(u16);
impl From<Dateindex> for usize {
fn from(value: Dateindex) -> Self {
value.0 as usize
}
}
impl From<usize> for Dateindex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl Add<usize> for Dateindex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs as u16)
}
}

View File

@@ -1,9 +1,11 @@
mod addressindextxoutindex;
mod date;
mod dateindex;
mod feerate;
mod unit;
pub use addressindextxoutindex::*;
pub use date::*;
pub use dateindex::*;
pub use feerate::*;
pub use unit::*;