use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub trait CompressionStrategy {
type Error: std::error::Error + Send + Sync + 'static;
fn compress(&mut self, data: &[u8]) -> Result<Vec<u8>, Self::Error>;
fn decompress(&self, data: &[u8]) -> Result<Vec<u8>, Self::Error>;
fn metadata(&self) -> CompressionMetadata;
fn stats(&self) -> CompressionStats;
fn reset(&mut self);
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressionMetadata {
pub name: String,
pub version: String,
pub description: String,
pub deterministic: bool,
pub memory_usage: usize,
pub domains: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressionStats {
pub compressions: u64,
pub decompressions: u64,
pub total_input_bytes: u64,
pub total_output_bytes: u64,
pub best_ratio: f64,
pub average_ratio: f64,
pub compression_time_ns: u64,
pub decompression_time_ns: u64,
pub errors: u64,
}
impl CompressionStats {
pub fn new() -> Self {
Self {
compressions: 0,
decompressions: 0,
total_input_bytes: 0,
total_output_bytes: 0,
best_ratio: 1.0,
average_ratio: 1.0,
compression_time_ns: 0,
decompression_time_ns: 0,
errors: 0,
}
}
pub fn record_compression(&mut self, input_size: usize, output_size: usize, time_ns: u64) {
self.compressions += 1;
self.total_input_bytes += input_size as u64;
self.total_output_bytes += output_size as u64;
self.compression_time_ns += time_ns;
let ratio = input_size as f64 / output_size as f64;
if ratio > self.best_ratio {
self.best_ratio = ratio;
}
if self.total_output_bytes > 0 {
self.average_ratio = self.total_input_bytes as f64 / self.total_output_bytes as f64;
}
}
pub fn record_decompression(&mut self, time_ns: u64) {
self.decompressions += 1;
self.decompression_time_ns += time_ns;
}
pub fn record_error(&mut self) {
self.errors += 1;
}
pub fn compression_throughput_mbps(&self) -> f64 {
if self.compression_time_ns == 0 {
return 0.0;
}
let mb_processed = self.total_input_bytes as f64 / 1_000_000.0;
let seconds = self.compression_time_ns as f64 / 1_000_000_000.0;
mb_processed / seconds
}
pub fn decompression_throughput_mbps(&self) -> f64 {
if self.decompression_time_ns == 0 {
return 0.0;
}
let mb_processed = self.total_output_bytes as f64 / 1_000_000.0;
let seconds = self.decompression_time_ns as f64 / 1_000_000_000.0;
mb_processed / seconds
}
}
impl Default for CompressionStats {
fn default() -> Self {
Self::new()
}
}
pub trait PatternCompressionStrategy: CompressionStrategy {
type Pattern: Clone + Send + Sync;
type Config: Clone + Send + Sync;
fn with_config(config: Self::Config) -> Self;
fn add_pattern(&mut self, pattern: Self::Pattern) -> Result<(), Self::Error>;
fn remove_pattern(&mut self, pattern_id: &str) -> Result<(), Self::Error>;
fn pattern_info(&self) -> HashMap<String, PatternInfo>;
fn optimize_patterns(&mut self) -> Result<(), Self::Error>;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatternInfo {
pub id: String,
pub size: usize,
pub usage_count: u64,
pub bytes_saved: u64,
pub description: String,
}
pub trait PipelineCompressionStrategy: CompressionStrategy {
type Stage: CompressionStrategy;
fn add_stage(&mut self, stage: Self::Stage) -> Result<(), Self::Error>;
fn remove_stage(&mut self, index: usize) -> Result<Self::Stage, Self::Error>;
fn stage_count(&self) -> usize;
fn stage_stats(&self) -> Vec<CompressionStats>;
fn set_stage_enabled(&mut self, index: usize, enabled: bool) -> Result<(), Self::Error>;
}
pub trait AdaptiveCompressionStrategy: CompressionStrategy {
fn train(&mut self, training_data: &[&[u8]]) -> Result<(), Self::Error>;
fn learning_progress(&self) -> f64;
fn save_model(&self) -> Result<Vec<u8>, Self::Error>;
fn load_model(&mut self, model_data: &[u8]) -> Result<(), Self::Error>;
fn learning_info(&self) -> LearningInfo;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LearningInfo {
pub training_samples: u64,
pub model_quality: f64,
pub discovered_features: Vec<String>,
pub model_size_bytes: usize,
}
#[derive(Debug, thiserror::Error)]
pub enum CompressionError {
#[error("Invalid compression format")]
InvalidFormat,
#[error("Unsupported algorithm version: {version}")]
UnsupportedVersion { version: String },
#[error("Configuration error: {message}")]
Configuration { message: String },
#[error("Pattern error: {message}")]
Pattern { message: String },
#[error("Pipeline error at stage {stage}: {message}")]
Pipeline { stage: usize, message: String },
#[error("Training error: {message}")]
Training { message: String },
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Internal error: {message}")]
Internal { message: String },
}