blockchain_compression/core/
traits.rs1use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9pub trait CompressionStrategy {
11 type Error: std::error::Error + Send + Sync + 'static;
13
14 fn compress(&mut self, data: &[u8]) -> Result<Vec<u8>, Self::Error>;
16
17 fn decompress(&self, data: &[u8]) -> Result<Vec<u8>, Self::Error>;
19
20 fn metadata(&self) -> CompressionMetadata;
22
23 fn stats(&self) -> CompressionStats;
25
26 fn reset(&mut self);
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct CompressionMetadata {
33 pub name: String,
35 pub version: String,
37 pub description: String,
39 pub deterministic: bool,
41 pub memory_usage: usize,
43 pub domains: Vec<String>,
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct CompressionStats {
50 pub compressions: u64,
52 pub decompressions: u64,
54 pub total_input_bytes: u64,
56 pub total_output_bytes: u64,
58 pub best_ratio: f64,
60 pub average_ratio: f64,
62 pub compression_time_ns: u64,
64 pub decompression_time_ns: u64,
66 pub errors: u64,
68}
69
70impl CompressionStats {
71 pub fn new() -> Self {
73 Self {
74 compressions: 0,
75 decompressions: 0,
76 total_input_bytes: 0,
77 total_output_bytes: 0,
78 best_ratio: 1.0,
79 average_ratio: 1.0,
80 compression_time_ns: 0,
81 decompression_time_ns: 0,
82 errors: 0,
83 }
84 }
85
86 pub fn record_compression(&mut self, input_size: usize, output_size: usize, time_ns: u64) {
88 self.compressions += 1;
89 self.total_input_bytes += input_size as u64;
90 self.total_output_bytes += output_size as u64;
91 self.compression_time_ns += time_ns;
92
93 let ratio = input_size as f64 / output_size as f64;
94 if ratio > self.best_ratio {
95 self.best_ratio = ratio;
96 }
97
98 if self.total_output_bytes > 0 {
100 self.average_ratio = self.total_input_bytes as f64 / self.total_output_bytes as f64;
101 }
102 }
103
104 pub fn record_decompression(&mut self, time_ns: u64) {
106 self.decompressions += 1;
107 self.decompression_time_ns += time_ns;
108 }
109
110 pub fn record_error(&mut self) {
112 self.errors += 1;
113 }
114
115 pub fn compression_throughput_mbps(&self) -> f64 {
117 if self.compression_time_ns == 0 {
118 return 0.0;
119 }
120 let mb_processed = self.total_input_bytes as f64 / 1_000_000.0;
121 let seconds = self.compression_time_ns as f64 / 1_000_000_000.0;
122 mb_processed / seconds
123 }
124
125 pub fn decompression_throughput_mbps(&self) -> f64 {
127 if self.decompression_time_ns == 0 {
128 return 0.0;
129 }
130 let mb_processed = self.total_output_bytes as f64 / 1_000_000.0;
131 let seconds = self.decompression_time_ns as f64 / 1_000_000_000.0;
132 mb_processed / seconds
133 }
134}
135
136impl Default for CompressionStats {
137 fn default() -> Self {
138 Self::new()
139 }
140}
141
142pub trait PatternCompressionStrategy: CompressionStrategy {
144 type Pattern: Clone + Send + Sync;
146
147 type Config: Clone + Send + Sync;
149
150 fn with_config(config: Self::Config) -> Self;
152
153 fn add_pattern(&mut self, pattern: Self::Pattern) -> Result<(), Self::Error>;
155
156 fn remove_pattern(&mut self, pattern_id: &str) -> Result<(), Self::Error>;
158
159 fn pattern_info(&self) -> HashMap<String, PatternInfo>;
161
162 fn optimize_patterns(&mut self) -> Result<(), Self::Error>;
164}
165
166#[derive(Debug, Clone, Serialize, Deserialize)]
168pub struct PatternInfo {
169 pub id: String,
171 pub size: usize,
173 pub usage_count: u64,
175 pub bytes_saved: u64,
177 pub description: String,
179}
180
181pub trait PipelineCompressionStrategy: CompressionStrategy {
183 type Stage: CompressionStrategy;
185
186 fn add_stage(&mut self, stage: Self::Stage) -> Result<(), Self::Error>;
188
189 fn remove_stage(&mut self, index: usize) -> Result<Self::Stage, Self::Error>;
191
192 fn stage_count(&self) -> usize;
194
195 fn stage_stats(&self) -> Vec<CompressionStats>;
197
198 fn set_stage_enabled(&mut self, index: usize, enabled: bool) -> Result<(), Self::Error>;
200}
201
202pub trait AdaptiveCompressionStrategy: CompressionStrategy {
204 fn train(&mut self, training_data: &[&[u8]]) -> Result<(), Self::Error>;
206
207 fn learning_progress(&self) -> f64;
209
210 fn save_model(&self) -> Result<Vec<u8>, Self::Error>;
212
213 fn load_model(&mut self, model_data: &[u8]) -> Result<(), Self::Error>;
215
216 fn learning_info(&self) -> LearningInfo;
218}
219
220#[derive(Debug, Clone, Serialize, Deserialize)]
222pub struct LearningInfo {
223 pub training_samples: u64,
225 pub model_quality: f64,
227 pub discovered_features: Vec<String>,
229 pub model_size_bytes: usize,
231}
232
233#[derive(Debug, thiserror::Error)]
235pub enum CompressionError {
236 #[error("Invalid compression format")]
237 InvalidFormat,
238
239 #[error("Unsupported algorithm version: {version}")]
240 UnsupportedVersion { version: String },
241
242 #[error("Configuration error: {message}")]
243 Configuration { message: String },
244
245 #[error("Pattern error: {message}")]
246 Pattern { message: String },
247
248 #[error("Pipeline error at stage {stage}: {message}")]
249 Pipeline { stage: usize, message: String },
250
251 #[error("Training error: {message}")]
252 Training { message: String },
253
254 #[error("IO error: {0}")]
255 Io(#[from] std::io::Error),
256
257 #[error("Serialization error: {0}")]
258 Serialization(String),
259
260 #[error("Internal error: {message}")]
261 Internal { message: String },
262}