zipora 3.1.5

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Adaptive compression that automatically selects the best algorithm

use super::{Algorithm, CompressionStats, Compressor, CompressorFactory, PerformanceRequirements};
use crate::error::Result;
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

/// Configuration for adaptive compression
#[derive(Debug, Clone)]
pub struct AdaptiveConfig {
    /// Learning window size (number of operations to remember)
    pub learning_window: usize,
    /// Minimum number of operations before adaptation kicks in
    pub min_operations: usize,
    /// How often to re-evaluate the best algorithm (operations)
    pub evaluation_interval: usize,
    /// Threshold for switching algorithms (performance improvement required)
    pub switch_threshold: f64,
    /// Enable aggressive learning (test multiple algorithms)
    pub aggressive_learning: bool,
    /// Sample size for testing new algorithms
    pub test_sample_size: usize,
}

impl Default for AdaptiveConfig {
    fn default() -> Self {
        Self {
            learning_window: 1000,
            min_operations: 50,
            evaluation_interval: 100,
            switch_threshold: 0.1, // 10% improvement required
            aggressive_learning: false,
            test_sample_size: 10,
        }
    }
}

/// Compression profile for different data types
#[derive(Debug, Clone)]
pub struct CompressionProfile {
    /// Data type identifier
    pub data_type: String,
    /// Preferred algorithm for this data type
    pub preferred_algorithm: Algorithm,
    /// Performance statistics for this profile
    pub stats: CompressionStats,
    /// Learning confidence (0.0 to 1.0)
    pub confidence: f64,
}

impl CompressionProfile {
    /// Create a new compression profile for a specific data type
    ///
    /// # Arguments
    /// * `data_type` - Identifier for the type of data (e.g., "text", "binary")
    /// * `algorithm` - The preferred compression algorithm for this data type
    pub fn new(data_type: String, algorithm: Algorithm) -> Self {
        Self {
            data_type,
            preferred_algorithm: algorithm,
            stats: CompressionStats::default(),
            confidence: 0.0,
        }
    }
}

/// Performance measurement for a compression operation
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct PerformanceMeasurement {
    algorithm: Algorithm,
    input_size: usize,
    output_size: usize,
    duration: Duration,
    timestamp: Instant,
    data_hash: u64, // Simple hash to identify data patterns
}

impl PerformanceMeasurement {
    fn compression_ratio(&self) -> f64 {
        self.output_size as f64 / self.input_size as f64
    }

    fn throughput(&self) -> f64 {
        self.input_size as f64 / self.duration.as_secs_f64()
    }

    fn score(&self, requirements: &PerformanceRequirements) -> f64 {
        let ratio_score = 1.0 - self.compression_ratio();
        let speed_score = self.throughput() / 1_000_000_000.0; // Normalize to GB/s
        let latency_penalty = if self.duration > requirements.max_latency {
            -1.0
        } else {
            0.0
        };

        requirements.speed_vs_quality * ratio_score
            + (1.0 - requirements.speed_vs_quality) * speed_score
            + latency_penalty
    }
}

/// Adaptive compressor that learns and improves over time
pub struct AdaptiveCompressor {
    config: AdaptiveConfig,
    requirements: PerformanceRequirements,
    current_algorithm: Algorithm,
    current_compressor: Arc<RwLock<Box<dyn Compressor>>>,
    performance_history: Arc<RwLock<VecDeque<PerformanceMeasurement>>>,
    profiles: Arc<RwLock<HashMap<String, CompressionProfile>>>,
    operation_count: Arc<RwLock<usize>>,
    stats: Arc<RwLock<CompressionStats>>,
}

impl AdaptiveCompressor {
    /// Create a new adaptive compressor
    pub fn new(config: AdaptiveConfig, requirements: PerformanceRequirements) -> Result<Self> {
        let initial_algorithm = Algorithm::Lz4; // Conservative starting point
        let initial_compressor = CompressorFactory::create(initial_algorithm, None)?;

        Ok(Self {
            config,
            requirements,
            current_algorithm: initial_algorithm,
            current_compressor: Arc::new(RwLock::new(initial_compressor)),
            performance_history: Arc::new(RwLock::new(VecDeque::new())),
            profiles: Arc::new(RwLock::new(HashMap::new())),
            operation_count: Arc::new(RwLock::new(0)),
            stats: Arc::new(RwLock::new(CompressionStats::default())),
        })
    }

    /// Create with default configuration
    pub fn default_with_requirements(requirements: PerformanceRequirements) -> Result<Self> {
        Self::new(AdaptiveConfig::default(), requirements)
    }

    /// Compress data with adaptive algorithm selection
    pub fn compress(&self, data: &[u8]) -> Result<Vec<u8>> {
        let start_time = Instant::now();

        // Check if we should adapt the algorithm
        self.maybe_adapt(data)?;

        // Perform compression
        let compressed = {
            let compressor = self.current_compressor.read()
                .map_err(|e| crate::error::ZiporaError::system_error(
                    format!("AdaptiveCompressor: current_compressor RwLock poisoned: {}", e)
                ))?;
            compressor.compress(data)?
        };

        let duration = start_time.elapsed();

        // Record performance
        self.record_performance(data, &compressed, duration);

        Ok(compressed)
    }

    /// Decompress data
    pub fn decompress(&self, data: &[u8]) -> Result<Vec<u8>> {
        let compressor = self.current_compressor.read()
            .map_err(|e| crate::error::ZiporaError::system_error(
                format!("AdaptiveCompressor: current_compressor RwLock poisoned: {}", e)
            ))?;
        compressor.decompress(data)
    }

    /// Get current compression statistics
    pub fn stats(&self) -> CompressionStats {
        // SAFETY: Returns default stats if RwLock is poisoned (graceful degradation)
        // This is a read-only getter; returning empty stats is safe fallback
        self.stats.read()
            .map(|s| s.clone())
            .unwrap_or_default()
    }

    /// Get current algorithm
    pub fn current_algorithm(&self) -> Algorithm {
        self.current_algorithm
    }

    /// Force adaptation to a specific algorithm
    pub fn set_algorithm(&mut self, algorithm: Algorithm) -> Result<()> {
        let new_compressor = CompressorFactory::create(algorithm, None)?;

        {
            let mut compressor = self.current_compressor.write()
                .map_err(|e| crate::error::ZiporaError::system_error(
                    format!("AdaptiveCompressor: current_compressor RwLock poisoned: {}", e)
                ))?;
            *compressor = new_compressor;
        }

        self.current_algorithm = algorithm;
        Ok(())
    }

    /// Get compression profiles
    pub fn profiles(&self) -> HashMap<String, CompressionProfile> {
        // SAFETY: Returns empty HashMap if RwLock is poisoned (graceful degradation)
        // This is a read-only getter; returning empty map is safe fallback
        self.profiles.read()
            .map(|p| p.clone())
            .unwrap_or_default()
    }

    /// Train the compressor with sample data
    pub fn train(&self, samples: &[(&[u8], &str)]) -> Result<()> {
        for (data, data_type) in samples {
            let data_hash = self.calculate_hash(data);

            // Test multiple algorithms on this sample
            for algorithm in CompressorFactory::available_algorithms() {
                if let Ok(compressor) = CompressorFactory::create(algorithm, Some(data)) {
                    let start_time = Instant::now();
                    if let Ok(compressed) = compressor.compress(data) {
                        let duration = start_time.elapsed();

                        let measurement = PerformanceMeasurement {
                            algorithm,
                            input_size: data.len(),
                            output_size: compressed.len(),
                            duration,
                            timestamp: Instant::now(),
                            data_hash,
                        };

                        // Update profile for this data type
                        self.update_profile(data_type, &measurement);
                    }
                }
            }
        }

        Ok(())
    }

    /// Maybe adapt the algorithm based on recent performance
    fn maybe_adapt(&self, data: &[u8]) -> Result<()> {
        let mut operation_count = self.operation_count.write()
            .map_err(|e| crate::error::ZiporaError::system_error(
                format!("AdaptiveCompressor: operation_count RwLock poisoned: {}", e)
            ))?;
        *operation_count += 1;
        let count = *operation_count;
        drop(operation_count);

        // Don't adapt too early
        if count < self.config.min_operations {
            return Ok(());
        }

        // Don't adapt too frequently
        if count % self.config.evaluation_interval != 0 {
            return Ok(());
        }

        // Analyze recent performance
        let best_algorithm = self.find_best_algorithm(data)?;

        if best_algorithm != self.current_algorithm {
            let improvement = self.estimate_improvement(best_algorithm);

            if improvement > self.config.switch_threshold {
                // Can't call mutable method from immutable context
                // This would require a different design pattern
                log::info!(
                    "Would switch to algorithm: {:?} (improvement: {:.2}%)",
                    best_algorithm,
                    improvement * 100.0
                );
            }
        }

        Ok(())
    }

    /// Find the best algorithm for the current workload
    fn find_best_algorithm(&self, sample_data: &[u8]) -> Result<Algorithm> {
        let history = self.performance_history.read()
            .map_err(|e| crate::error::ZiporaError::system_error(
                format!("AdaptiveCompressor: performance_history RwLock poisoned: {}", e)
            ))?;

        if history.len() < self.config.min_operations {
            return Ok(self.current_algorithm);
        }

        // Analyze recent performance by algorithm
        let mut algorithm_scores: HashMap<Algorithm, Vec<f64>> = HashMap::new();

        let recent_window = history.iter().rev().take(self.config.learning_window);

        for measurement in recent_window {
            let score = measurement.score(&self.requirements);
            algorithm_scores
                .entry(measurement.algorithm)
                .or_insert_with(Vec::new)
                .push(score);
        }

        // Find algorithm with best average score
        let mut best_algorithm = self.current_algorithm;
        let mut best_score = f64::NEG_INFINITY;

        for (algorithm, scores) in algorithm_scores {
            if scores.len() >= 5 {
                // Minimum samples for confidence
                let avg_score: f64 = scores.iter().sum::<f64>() / scores.len() as f64;

                if avg_score > best_score {
                    best_score = avg_score;
                    best_algorithm = algorithm;
                }
            }
        }

        // If aggressive learning is enabled, occasionally test new algorithms
        let should_test = self.config.aggressive_learning && {
            let count = self.operation_count.read()
                .map_err(|e| crate::error::ZiporaError::system_error(
                    format!("AdaptiveCompressor: operation_count RwLock poisoned: {}", e)
                ))?;
            *count % (self.config.evaluation_interval * 5) == 0
        };
        if should_test {
            return self.test_new_algorithm(sample_data);
        }

        Ok(best_algorithm)
    }

    /// Test a new algorithm on sample data
    fn test_new_algorithm(&self, _sample_data: &[u8]) -> Result<Algorithm> {
        let available = CompressorFactory::available_algorithms();
        let current = self.current_algorithm;

        // Find an algorithm we haven't tested much recently
        let history = self.performance_history.read()
            .map_err(|e| crate::error::ZiporaError::system_error(
                format!("AdaptiveCompressor: performance_history RwLock poisoned: {}", e)
            ))?;
        let mut usage_counts: HashMap<Algorithm, usize> = HashMap::new();

        for measurement in history.iter().rev().take(100) {
            *usage_counts.entry(measurement.algorithm).or_insert(0) += 1;
        }

        // Select the least used algorithm
        let mut test_algorithm = current;
        let mut min_usage = usize::MAX;

        for algorithm in available {
            let usage = usage_counts.get(&algorithm).unwrap_or(&0);
            if *usage < min_usage {
                min_usage = *usage;
                test_algorithm = algorithm;
            }
        }

        Ok(test_algorithm)
    }

    /// Estimate performance improvement from switching algorithms
    fn estimate_improvement(&self, new_algorithm: Algorithm) -> f64 {
        // SAFETY: Returns 0.0 (no improvement) if RwLock is poisoned (graceful degradation)
        let history = match self.performance_history.read() {
            Ok(guard) => guard,
            Err(_) => return 0.0,
        };

        let current_scores: Vec<f64> = history
            .iter()
            .rev()
            .take(self.config.learning_window)
            .filter(|m| m.algorithm == self.current_algorithm)
            .map(|m| m.score(&self.requirements))
            .collect();

        let new_scores: Vec<f64> = history
            .iter()
            .rev()
            .take(self.config.learning_window)
            .filter(|m| m.algorithm == new_algorithm)
            .map(|m| m.score(&self.requirements))
            .collect();

        if current_scores.is_empty() || new_scores.is_empty() {
            return 0.0;
        }

        let current_avg = current_scores.iter().sum::<f64>() / current_scores.len() as f64;
        let new_avg = new_scores.iter().sum::<f64>() / new_scores.len() as f64;

        (new_avg - current_avg) / current_avg.abs()
    }

    /// Switch to a new algorithm
    #[allow(dead_code)]
    fn switch_algorithm(&mut self, algorithm: Algorithm) -> Result<()> {
        let new_compressor = CompressorFactory::create(algorithm, None)?;

        {
            let mut compressor = self.current_compressor.write()
                .map_err(|e| crate::error::ZiporaError::system_error(
                    format!("AdaptiveCompressor: current_compressor RwLock poisoned: {}", e)
                ))?;
            *compressor = new_compressor;
        }

        self.current_algorithm = algorithm;

        log::info!("Adaptive compressor switched to algorithm: {:?}", algorithm);

        Ok(())
    }

    /// Record performance measurement
    fn record_performance(&self, input: &[u8], output: &[u8], duration: Duration) {
        let measurement = PerformanceMeasurement {
            algorithm: self.current_algorithm,
            input_size: input.len(),
            output_size: output.len(),
            duration,
            timestamp: Instant::now(),
            data_hash: self.calculate_hash(input),
        };

        // Add to history
        // SAFETY: Skip recording if RwLock is poisoned (graceful degradation)
        // Performance recording is non-critical; missing data won't affect compression
        if let Ok(mut history) = self.performance_history.write() {
            history.push_back(measurement.clone());

            // Maintain window size
            while history.len() > self.config.learning_window {
                history.pop_front();
            }
        }

        // Update global stats
        // SAFETY: Skip update if RwLock is poisoned (graceful degradation)
        if let Ok(mut stats) = self.stats.write() {
            stats.update(
                measurement.input_size,
                measurement.output_size,
                measurement.duration,
                measurement.algorithm,
            );
        }
    }

    /// Update compression profile for a data type
    fn update_profile(&self, data_type: &str, measurement: &PerformanceMeasurement) {
        // SAFETY: Skip profile update if RwLock is poisoned (graceful degradation)
        // Profile updates are non-critical; missing data won't affect compression
        let mut profiles = match self.profiles.write() {
            Ok(guard) => guard,
            Err(_) => return,
        };

        let profile = profiles.entry(data_type.to_string()).or_insert_with(|| {
            CompressionProfile::new(data_type.to_string(), measurement.algorithm)
        });

        // Update statistics
        profile.stats.update(
            measurement.input_size,
            measurement.output_size,
            measurement.duration,
            measurement.algorithm,
        );

        // Update confidence
        profile.confidence = (profile.stats.operations as f64 / 100.0).min(1.0);

        // Maybe update preferred algorithm
        let score = measurement.score(&self.requirements);
        if score > 0.5 {
            // Arbitrary threshold for good performance
            profile.preferred_algorithm = measurement.algorithm;
        }
    }

    /// Calculate a simple hash of data for pattern recognition
    fn calculate_hash(&self, data: &[u8]) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();

        // Sample bytes from different parts of the data
        let sample_size = (data.len() / 10).max(1).min(1000);
        for i in 0..sample_size {
            let idx = (i * data.len()) / sample_size;
            data[idx].hash(&mut hasher);
        }

        hasher.finish()
    }
}

impl Compressor for AdaptiveCompressor {
    fn compress(&self, data: &[u8]) -> Result<Vec<u8>> {
        self.compress(data)
    }

    fn decompress(&self, data: &[u8]) -> Result<Vec<u8>> {
        self.decompress(data)
    }

    fn algorithm(&self) -> Algorithm {
        self.current_algorithm()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_adaptive_config() {
        let config = AdaptiveConfig::default();
        assert!(config.learning_window > 0);
        assert!(config.min_operations > 0);
        assert!(config.switch_threshold > 0.0);
    }

    #[test]
    fn test_compression_profile() {
        let profile = CompressionProfile::new("text".to_string(), Algorithm::Lz4);
        assert_eq!(profile.confidence, 0.0);
        assert_eq!(profile.data_type, "text");
        assert_eq!(profile.preferred_algorithm, Algorithm::Lz4);
    }

    #[test]
    fn test_performance_measurement() {
        let measurement = PerformanceMeasurement {
            algorithm: Algorithm::Lz4,
            input_size: 1000,
            output_size: 500,
            duration: Duration::from_millis(10),
            timestamp: Instant::now(),
            data_hash: 12345,
        };

        assert_eq!(measurement.compression_ratio(), 0.5);
        assert!(measurement.throughput() > 0.0);
    }

    #[test]
    fn test_adaptive_compressor_creation() {
        let requirements = PerformanceRequirements::default();
        let compressor = AdaptiveCompressor::default_with_requirements(requirements).unwrap();

        assert_eq!(compressor.current_algorithm(), Algorithm::Lz4);
        assert_eq!(compressor.stats().operations, 0);
    }

    #[test]
    #[cfg(feature = "lz4")]
    fn test_adaptive_compression() {
        let requirements = PerformanceRequirements::default();
        let compressor = AdaptiveCompressor::default_with_requirements(requirements).unwrap();

        let data = b"test data that should compress well";
        let compressed = compressor.compress(data).unwrap();
        let decompressed = compressor.decompress(&compressed).unwrap();

        assert_eq!(decompressed, data);
        assert_eq!(compressor.stats().operations, 1);
    }

    #[test]
    #[cfg(feature = "zstd")]
    fn test_algorithm_switching() {
        let requirements = PerformanceRequirements::default();
        let mut compressor = AdaptiveCompressor::default_with_requirements(requirements).unwrap();

        assert_eq!(compressor.current_algorithm(), Algorithm::Lz4);

        compressor.set_algorithm(Algorithm::Zstd(3)).unwrap();
        assert_eq!(compressor.current_algorithm(), Algorithm::Zstd(3));
    }

    #[test]
    fn test_training() {
        let requirements = PerformanceRequirements::default();
        let compressor = AdaptiveCompressor::default_with_requirements(requirements).unwrap();

        let samples = vec![
            (b"text data with lots of repetition".as_slice(), "text"),
            (b"binary data \x00\x01\x02\x03".as_slice(), "binary"),
        ];

        compressor.train(&samples).unwrap();

        let profiles = compressor.profiles();
        assert!(profiles.contains_key("text") || profiles.contains_key("binary"));
    }

    #[test]
    #[cfg(feature = "lz4")]
    fn test_performance_tracking() {
        let requirements = PerformanceRequirements::default();
        let compressor = AdaptiveCompressor::default_with_requirements(requirements).unwrap();

        // Perform several compression operations
        for i in 0..10 {
            let data = format!("test data iteration {}", i);
            let _ = compressor.compress(data.as_bytes()).unwrap();
        }

        let stats = compressor.stats();
        assert_eq!(stats.operations, 10);
        assert!(stats.bytes_processed > 0);
    }
}