mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 06:39:58 -07:00
global: snapshot
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use brk_exit::Exit;
|
||||
use brk_fetcher::Fetcher;
|
||||
use brk_indexer::{Indexer, Indexes};
|
||||
pub use brk_parser::rpc;
|
||||
|
||||
@@ -17,25 +18,30 @@ use storage::{Stores, Vecs};
|
||||
#[derive(Clone)]
|
||||
pub struct Computer {
|
||||
path: PathBuf,
|
||||
fetcher: Option<Fetcher>,
|
||||
vecs: Option<Vecs>,
|
||||
stores: Option<Stores>,
|
||||
}
|
||||
|
||||
impl Computer {
|
||||
pub fn new(computed_dir: PathBuf) -> Self {
|
||||
pub fn new(computed_dir: PathBuf, fetcher: Option<Fetcher>) -> Self {
|
||||
Self {
|
||||
path: computed_dir,
|
||||
fetcher,
|
||||
vecs: None,
|
||||
stores: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn import_vecs(&mut self) -> color_eyre::Result<()> {
|
||||
self.vecs = Some(Vecs::import(&self.path.join("vecs"))?);
|
||||
self.vecs = Some(Vecs::import(
|
||||
&self.path.join("vecs"),
|
||||
self.fetcher.is_some(),
|
||||
)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Do NOT import multiple times are things will break !!!
|
||||
/// Do NOT import multiple times or things will break !!!
|
||||
/// Clone struct instead
|
||||
pub fn import_stores(&mut self) -> color_eyre::Result<()> {
|
||||
self.stores = Some(Stores::import(&self.path.join("stores"))?);
|
||||
@@ -52,7 +58,12 @@ impl Computer {
|
||||
) -> color_eyre::Result<()> {
|
||||
info!("Computing...");
|
||||
|
||||
self.mut_vecs().compute(indexer, starting_indexes, exit)?;
|
||||
self.vecs.as_mut().unwrap().compute(
|
||||
indexer,
|
||||
starting_indexes,
|
||||
self.fetcher.as_mut(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use brk_core::{Cents, Close, Dateindex, Dollars, Height, High, Low, OHLCCents, OHLCDollars, Open};
|
||||
use brk_core::{
|
||||
Cents, Close, Dateindex, Dollars, Height, High, Low, OHLCCents, OHLCDollars, Open, Sats,
|
||||
};
|
||||
use brk_exit::Exit;
|
||||
use brk_fetcher::Fetcher;
|
||||
use brk_indexer::Indexer;
|
||||
@@ -20,6 +22,7 @@ pub struct Vecs {
|
||||
pub dateindex_to_low: StorableVec<Dateindex, Low<Dollars>>,
|
||||
pub dateindex_to_open_in_cents: StorableVec<Dateindex, Open<Cents>>,
|
||||
pub dateindex_to_open: StorableVec<Dateindex, Open<Dollars>>,
|
||||
pub dateindex_to_sats_per_dollar: StorableVec<Dateindex, Close<Sats>>,
|
||||
pub height_to_ohlc_in_cents: StorableVec<Height, OHLCCents>,
|
||||
pub height_to_ohlc: StorableVec<Height, OHLCDollars>,
|
||||
pub height_to_close_in_cents: StorableVec<Height, Close<Cents>>,
|
||||
@@ -30,6 +33,7 @@ pub struct Vecs {
|
||||
pub height_to_low: StorableVec<Height, Low<Dollars>>,
|
||||
pub height_to_open_in_cents: StorableVec<Height, Open<Cents>>,
|
||||
pub height_to_open: StorableVec<Height, Open<Dollars>>,
|
||||
pub height_to_sats_per_dollar: StorableVec<Height, Close<Sats>>,
|
||||
}
|
||||
|
||||
impl Vecs {
|
||||
@@ -77,6 +81,10 @@ impl Vecs {
|
||||
&path.join("dateindex_to_open"),
|
||||
Version::from(1),
|
||||
)?,
|
||||
dateindex_to_sats_per_dollar: StorableVec::import(
|
||||
&path.join("dateindex_to_sats_per_dollar"),
|
||||
Version::from(1),
|
||||
)?,
|
||||
height_to_ohlc_in_cents: StorableVec::import(
|
||||
&path.join("height_to_ohlc_in_cents"),
|
||||
Version::from(1),
|
||||
@@ -102,6 +110,10 @@ impl Vecs {
|
||||
Version::from(1),
|
||||
)?,
|
||||
height_to_open: StorableVec::import(&path.join("height_to_open"), Version::from(1))?,
|
||||
height_to_sats_per_dollar: StorableVec::import(
|
||||
&path.join("height_to_sats_per_dollar"),
|
||||
Version::from(1),
|
||||
)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -110,10 +122,9 @@ impl Vecs {
|
||||
indexer: &mut Indexer,
|
||||
indexes: &mut indexes::Vecs,
|
||||
starting_indexes: Indexes,
|
||||
fetcher: &mut Fetcher,
|
||||
exit: &Exit,
|
||||
) -> color_eyre::Result<()> {
|
||||
let mut fetcher = Fetcher::import(None)?;
|
||||
|
||||
self.height_to_ohlc_in_cents.compute_transform(
|
||||
starting_indexes.height,
|
||||
&mut indexer.mut_vecs().height_to_timestamp,
|
||||
@@ -199,6 +210,13 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.height_to_sats_per_dollar.compute_transform(
|
||||
starting_indexes.height,
|
||||
&mut self.height_to_close,
|
||||
|(di, close, ..)| (di, Close::from(Sats::ONE_BTC / **close)),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.dateindex_to_ohlc_in_cents.compute_transform(
|
||||
starting_indexes.dateindex,
|
||||
&mut indexes.dateindex_to_date,
|
||||
@@ -272,6 +290,13 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.dateindex_to_sats_per_dollar.compute_transform(
|
||||
starting_indexes.dateindex,
|
||||
&mut self.dateindex_to_close,
|
||||
|(di, close, ..)| (di, Close::from(Sats::ONE_BTC / **close)),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -287,6 +312,7 @@ impl Vecs {
|
||||
&self.dateindex_to_ohlc_in_cents,
|
||||
&self.dateindex_to_open,
|
||||
&self.dateindex_to_open_in_cents,
|
||||
&self.dateindex_to_sats_per_dollar,
|
||||
&self.height_to_close,
|
||||
&self.height_to_close_in_cents,
|
||||
&self.height_to_high,
|
||||
@@ -297,6 +323,7 @@ impl Vecs {
|
||||
&self.height_to_ohlc_in_cents,
|
||||
&self.height_to_open,
|
||||
&self.height_to_open_in_cents,
|
||||
&self.height_to_sats_per_dollar,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ use std::{fs, path::Path};
|
||||
|
||||
use brk_core::{Height, Sats, Txindex, Txinindex, Txoutindex};
|
||||
use brk_exit::Exit;
|
||||
use brk_fetcher::Fetcher;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_vec::{AnyStorableVec, StorableVec, Version};
|
||||
|
||||
@@ -11,7 +12,7 @@ mod marketprice;
|
||||
#[derive(Clone)]
|
||||
pub struct Vecs {
|
||||
pub indexes: indexes::Vecs,
|
||||
pub marketprice: marketprice::Vecs,
|
||||
pub marketprice: Option<marketprice::Vecs>,
|
||||
// pub height_to_block_interval: StorableVec<Height, Timestamp>,
|
||||
// pub height_to_fee: StorableVec<Txindex, Amount>,
|
||||
// pub height_to_inputcount: StorableVec<Height, u32>,
|
||||
@@ -38,13 +39,13 @@ pub struct Vecs {
|
||||
}
|
||||
|
||||
impl Vecs {
|
||||
pub fn import(path: &Path) -> color_eyre::Result<Self> {
|
||||
pub fn import(path: &Path, fetch: bool) -> color_eyre::Result<Self> {
|
||||
fs::create_dir_all(path)?;
|
||||
|
||||
Ok(Self {
|
||||
// height_to_block_interval: StorableVec::forced_import(&path.join("height_to_block_interval"), Version::from(1))?,
|
||||
indexes: indexes::Vecs::import(path)?,
|
||||
marketprice: marketprice::Vecs::import(path)?,
|
||||
marketprice: fetch.then(|| marketprice::Vecs::import(path).unwrap()),
|
||||
// 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(
|
||||
@@ -107,12 +108,20 @@ impl Vecs {
|
||||
&mut self,
|
||||
indexer: &mut Indexer,
|
||||
starting_indexes: brk_indexer::Indexes,
|
||||
fetcher: Option<&mut Fetcher>,
|
||||
exit: &Exit,
|
||||
) -> color_eyre::Result<()> {
|
||||
let starting_indexes = self.indexes.compute(indexer, starting_indexes, exit)?;
|
||||
|
||||
self.marketprice
|
||||
.compute(indexer, &mut self.indexes, starting_indexes, exit)?;
|
||||
if let Some(marketprice) = self.marketprice.as_mut() {
|
||||
marketprice.compute(
|
||||
indexer,
|
||||
&mut self.indexes,
|
||||
starting_indexes,
|
||||
fetcher.unwrap(),
|
||||
exit,
|
||||
)?;
|
||||
}
|
||||
|
||||
// self.mut_vecs().height_to_ohlc
|
||||
|
||||
@@ -197,6 +206,12 @@ impl Vecs {
|
||||
}
|
||||
|
||||
pub fn as_any_vecs(&self) -> Vec<&dyn AnyStorableVec> {
|
||||
[self.indexes.as_any_vecs(), self.marketprice.as_any_vecs()].concat()
|
||||
[
|
||||
self.indexes.as_any_vecs(),
|
||||
self.marketprice
|
||||
.as_ref()
|
||||
.map_or(vec![], |v| v.as_any_vecs()),
|
||||
]
|
||||
.concat()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user