nyxd_scraper_shared/block_processor/
pruning.rs1use crate::error::ScraperError;
5use serde::{Deserialize, Serialize};
6
7pub const DEFAULT_PRUNING_KEEP_RECENT: u32 = 362880;
8pub const DEFAULT_PRUNING_INTERVAL: u32 = 10;
9pub const EVERYTHING_PRUNING_KEEP_RECENT: u32 = 2;
10pub const EVERYTHING_PRUNING_INTERVAL: u32 = 10;
11
12#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
14#[serde(rename_all = "snake_case")]
15pub enum PruningStrategy {
16 #[default]
21 Default,
22
23 Everything,
27
28 Nothing,
30
31 Custom,
33}
34
35impl PruningStrategy {
36 pub fn is_custom(&self) -> bool {
37 matches!(self, PruningStrategy::Custom)
38 }
39
40 pub fn is_nothing(&self) -> bool {
41 matches!(self, PruningStrategy::Nothing)
42 }
43
44 pub fn is_everything(&self) -> bool {
45 matches!(self, PruningStrategy::Everything)
46 }
47}
48
49#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
50pub struct PruningOptions {
51 pub keep_recent: u32,
53
54 pub interval: u32,
56
57 pub strategy: PruningStrategy,
59}
60
61impl PruningOptions {
62 pub fn validate(&self) -> Result<(), ScraperError> {
63 if !self.strategy.is_custom() {
65 return Ok(());
66 }
67
68 if self.interval == 0 {
69 return Err(ScraperError::ZeroPruningInterval);
70 }
71
72 if self.interval < EVERYTHING_PRUNING_INTERVAL {
73 return Err(ScraperError::TooSmallPruningInterval {
74 interval: self.interval,
75 });
76 }
77
78 if self.keep_recent < EVERYTHING_PRUNING_KEEP_RECENT {
79 return Err(ScraperError::TooSmallKeepRecent {
80 keep_recent: self.keep_recent,
81 });
82 }
83
84 Ok(())
85 }
86
87 pub fn nothing() -> Self {
88 PruningOptions {
89 keep_recent: 0,
90 interval: 0,
91 strategy: PruningStrategy::Nothing,
92 }
93 }
94
95 pub fn strategy_interval(&self) -> u32 {
96 match self.strategy {
97 PruningStrategy::Default => DEFAULT_PRUNING_INTERVAL,
98 PruningStrategy::Everything => EVERYTHING_PRUNING_INTERVAL,
99 PruningStrategy::Nothing => 0,
100 PruningStrategy::Custom => self.interval,
101 }
102 }
103
104 pub fn strategy_keep_recent(&self) -> u32 {
105 match self.strategy {
106 PruningStrategy::Default => DEFAULT_PRUNING_KEEP_RECENT,
107 PruningStrategy::Everything => EVERYTHING_PRUNING_KEEP_RECENT,
108 PruningStrategy::Nothing => 0,
109 PruningStrategy::Custom => self.keep_recent,
110 }
111 }
112}
113
114impl Default for PruningOptions {
115 fn default() -> Self {
116 PruningOptions {
117 keep_recent: DEFAULT_PRUNING_KEEP_RECENT,
118 interval: DEFAULT_PRUNING_INTERVAL,
119 strategy: Default::default(),
120 }
121 }
122}