Skip to main content

digdigdig3_station/
persistence.rs

1//! Persistence configuration for the Station builder.
2//!
3//! Actual disk I/O lives in [`crate::series::DiskStore`], generic over
4//! [`crate::series::DataPoint`]. This module only carries on/off toggles
5//! threaded through the builder.
6
7use serde::{Deserialize, Serialize};
8
9/// Per-kind persistence toggles. Master `enabled` gates everything.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct PersistenceConfig {
12    pub enabled: bool,
13    pub trades: bool,
14    pub agg_trades: bool,
15    pub klines: bool,
16    pub tickers: bool,
17    pub orderbook_snapshots: bool,
18    pub orderbook_deltas: bool,
19    pub mark_price: bool,
20    pub funding_rate: bool,
21    pub open_interest: bool,
22    pub liquidations: bool,
23}
24
25impl Default for PersistenceConfig {
26    fn default() -> Self {
27        Self {
28            enabled: false,
29            trades: false,
30            agg_trades: false,
31            klines: false,
32            tickers: false,
33            orderbook_snapshots: false,
34            orderbook_deltas: false,
35            mark_price: false,
36            funding_rate: false,
37            open_interest: false,
38            liquidations: false,
39        }
40    }
41}
42
43impl PersistenceConfig {
44    /// Enable persistence for every supported kind.
45    pub fn on() -> Self {
46        Self {
47            enabled: true,
48            trades: true,
49            agg_trades: true,
50            klines: true,
51            tickers: true,
52            orderbook_snapshots: true,
53            orderbook_deltas: true,
54            mark_price: true,
55            funding_rate: true,
56            open_interest: true,
57            liquidations: true,
58        }
59    }
60
61    pub fn trades(mut self, on: bool) -> Self { self.trades = on; self }
62    pub fn agg_trades(mut self, on: bool) -> Self { self.agg_trades = on; self }
63    pub fn klines(mut self, on: bool) -> Self { self.klines = on; self }
64    pub fn tickers(mut self, on: bool) -> Self { self.tickers = on; self }
65    pub fn orderbook_snapshots(mut self, on: bool) -> Self { self.orderbook_snapshots = on; self }
66    pub fn orderbook_deltas(mut self, on: bool) -> Self { self.orderbook_deltas = on; self }
67    pub fn mark_price(mut self, on: bool) -> Self { self.mark_price = on; self }
68    pub fn funding_rate(mut self, on: bool) -> Self { self.funding_rate = on; self }
69    pub fn open_interest(mut self, on: bool) -> Self { self.open_interest = on; self }
70    pub fn liquidations(mut self, on: bool) -> Self { self.liquidations = on; self }
71
72    /// Should `kind` be persisted given the current config?
73    pub fn is_enabled_for(&self, kind: &crate::series::Kind) -> bool {
74        if !self.enabled {
75            return false;
76        }
77        use crate::series::Kind::*;
78        match kind {
79            Trade => self.trades,
80            AggTrade => self.agg_trades,
81            Kline(_) => self.klines,
82            Ticker => self.tickers,
83            Orderbook => self.orderbook_snapshots,
84            OrderbookDelta => self.orderbook_deltas,
85            MarkPrice => self.mark_price,
86            FundingRate => self.funding_rate,
87            OpenInterest => self.open_interest,
88            Liquidation => self.liquidations,
89            // Extended types — numeric variants persist if globally enabled.
90            // String-bearing variants (BlockTrade, OrderbookL3, MarketWarning)
91            // use header + companion `.blob` storage via
92            // `DataPoint::blob_pointer_offset` and persist normally.
93            IndexPrice | CompositeIndex | VolatilityIndex | HistoricalVolatility
94            | LongShortRatio
95            | Basis | InsuranceFund | SettlementEvent | PredictedFunding
96            | FundingSettlement | RiskLimit | OptionGreeks
97            | MarkPriceKline(_) | IndexPriceKline(_) | PremiumIndexKline(_)
98            | BlockTrade | OrderbookL3 | MarketWarning => self.enabled,
99            // Mechanical bar aggregators — derived from Trade stream.
100            // Blob-backed (FootprintPoint) or fixed-record (BarPoint variants).
101            // Persist when globally enabled, same policy as extended numeric types.
102            RangeBar(_) | TickBar(_) | VolumeBar(_) | Footprint(_) => self.enabled,
103            // Private streams are ephemeral by default.  These are transient
104            // account-state notifications, not time-series data suitable for
105            // backtesting.  Persistence is opt-in via future dedicated toggles.
106            OrderUpdate | BalanceUpdate | PositionUpdate => false,
107        }
108    }
109}