oxirs-tdb 0.3.1

Apache Jena TDB/TDB2 compatible RDF storage engine with B+Tree indexes
Documentation
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
//! Adaptive compression algorithm selection
//!
//! This module automatically selects the best compression algorithm based on
//! runtime performance metrics, data characteristics, and compression goals.

use super::AdvancedCompressionType;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Simplified compression type for adaptive selection
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressionType {
    /// No compression
    None,
    /// LZ4 compression (very fast)
    Lz4,
    /// Zstandard compression (excellent ratio)
    Zstd,
    /// Snappy compression (fast with good ratio)
    Snappy,
    /// Brotli compression (high compression ratio)
    Brotli,
    /// Zlib/DEFLATE compression
    Zlib,
}

impl From<CompressionType> for AdvancedCompressionType {
    fn from(ct: CompressionType) -> Self {
        match ct {
            CompressionType::None => AdvancedCompressionType::RunLength, // Fallback
            CompressionType::Lz4 => AdvancedCompressionType::Lz4,
            CompressionType::Zstd => AdvancedCompressionType::Zstd,
            CompressionType::Snappy => AdvancedCompressionType::Snappy,
            CompressionType::Brotli => AdvancedCompressionType::Brotli,
            CompressionType::Zlib => AdvancedCompressionType::RunLength, // Fallback
        }
    }
}

/// Compression selection strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SelectionStrategy {
    /// Maximize compression ratio (smallest size)
    MaximizeRatio,
    /// Minimize compression time (fastest)
    MinimizeTime,
    /// Balance between ratio and time
    Balanced,
    /// Adaptive based on data size
    Adaptive,
}

/// Compression performance metrics for an algorithm
#[derive(Debug)]
pub struct AlgorithmMetrics {
    /// Total bytes compressed
    pub bytes_compressed: AtomicU64,
    /// Total compression time (microseconds)
    pub total_time_us: AtomicU64,
    /// Total compressed size
    pub total_compressed_size: AtomicU64,
    /// Number of compression operations
    pub operations: AtomicU64,
}

impl Default for AlgorithmMetrics {
    fn default() -> Self {
        Self {
            bytes_compressed: AtomicU64::new(0),
            total_time_us: AtomicU64::new(0),
            total_compressed_size: AtomicU64::new(0),
            operations: AtomicU64::new(0),
        }
    }
}

impl AlgorithmMetrics {
    /// Record a compression operation
    pub fn record(&self, input_size: usize, compressed_size: usize, duration: Duration) {
        self.bytes_compressed
            .fetch_add(input_size as u64, Ordering::Relaxed);
        self.total_compressed_size
            .fetch_add(compressed_size as u64, Ordering::Relaxed);
        self.total_time_us
            .fetch_add(duration.as_micros() as u64, Ordering::Relaxed);
        self.operations.fetch_add(1, Ordering::Relaxed);
    }

    /// Get average compression ratio (original_size / compressed_size)
    pub fn avg_ratio(&self) -> f64 {
        let total_in = self.bytes_compressed.load(Ordering::Relaxed) as f64;
        let total_out = self.total_compressed_size.load(Ordering::Relaxed) as f64;
        if total_out == 0.0 {
            1.0
        } else {
            total_in / total_out
        }
    }

    /// Get average compression throughput (MB/s)
    pub fn avg_throughput_mbps(&self) -> f64 {
        let bytes = self.bytes_compressed.load(Ordering::Relaxed) as f64;
        let time_s = self.total_time_us.load(Ordering::Relaxed) as f64 / 1_000_000.0;
        if time_s == 0.0 {
            0.0
        } else {
            (bytes / (1024.0 * 1024.0)) / time_s
        }
    }

    /// Get average time per operation (microseconds)
    pub fn avg_time_us(&self) -> f64 {
        let ops = self.operations.load(Ordering::Relaxed);
        if ops == 0 {
            0.0
        } else {
            self.total_time_us.load(Ordering::Relaxed) as f64 / ops as f64
        }
    }
}

/// Adaptive compression selector
pub struct AdaptiveCompressionSelector {
    /// Selection strategy
    strategy: SelectionStrategy,
    /// Metrics for each compression algorithm
    metrics: Vec<(AdvancedCompressionType, Arc<AlgorithmMetrics>)>,
    /// Minimum data size to enable compression
    min_compression_size: usize,
}

impl AdaptiveCompressionSelector {
    /// Create a new adaptive selector
    pub fn new(strategy: SelectionStrategy) -> Self {
        let compression_types = vec![
            AdvancedCompressionType::Lz4,
            AdvancedCompressionType::Zstd,
            AdvancedCompressionType::Snappy,
            AdvancedCompressionType::Brotli,
            AdvancedCompressionType::RunLength, // Using RunLength as Zlib alternative
        ];

        let metrics = compression_types
            .into_iter()
            .map(|ctype| (ctype, Arc::new(AlgorithmMetrics::default())))
            .collect();

        Self {
            strategy,
            metrics,
            min_compression_size: 128, // Don't compress data < 128 bytes
        }
    }

    /// Select the best compression algorithm for the given data
    pub fn select(&self, data_size: usize) -> AdvancedCompressionType {
        // Don't compress small data - use RunLength as "no compression" fallback
        if data_size < self.min_compression_size {
            return AdvancedCompressionType::RunLength;
        }

        match self.strategy {
            SelectionStrategy::MaximizeRatio => self.select_max_ratio(),
            SelectionStrategy::MinimizeTime => self.select_min_time(),
            SelectionStrategy::Balanced => self.select_balanced(),
            SelectionStrategy::Adaptive => self.select_adaptive(data_size),
        }
    }

    /// Select algorithm with best compression ratio
    fn select_max_ratio(&self) -> AdvancedCompressionType {
        self.metrics
            .iter()
            .max_by(|(_, a), (_, b)| {
                a.avg_ratio()
                    .partial_cmp(&b.avg_ratio())
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|(ctype, _)| *ctype)
            .unwrap_or(AdvancedCompressionType::Zstd)
    }

    /// Select algorithm with best throughput
    fn select_min_time(&self) -> AdvancedCompressionType {
        self.metrics
            .iter()
            .max_by(|(_, a), (_, b)| {
                a.avg_throughput_mbps()
                    .partial_cmp(&b.avg_throughput_mbps())
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|(ctype, _)| *ctype)
            .unwrap_or(AdvancedCompressionType::Lz4)
    }

    /// Select algorithm with balanced ratio and speed
    fn select_balanced(&self) -> AdvancedCompressionType {
        self.metrics
            .iter()
            .max_by(|(_, a), (_, b)| {
                let score_a = a.avg_ratio() * a.avg_throughput_mbps().sqrt();
                let score_b = b.avg_ratio() * b.avg_throughput_mbps().sqrt();
                score_a
                    .partial_cmp(&score_b)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|(ctype, _)| *ctype)
            .unwrap_or(AdvancedCompressionType::Snappy)
    }

    /// Select algorithm based on data size
    fn select_adaptive(&self, data_size: usize) -> AdvancedCompressionType {
        match data_size {
            0..=1024 => AdvancedCompressionType::Lz4, // Small: fast compression
            1025..=10240 => AdvancedCompressionType::Snappy, // Medium: balanced
            10241..=102400 => AdvancedCompressionType::Zstd, // Large: good ratio
            _ => AdvancedCompressionType::Zstd,       // Very large: best ratio
        }
    }

    /// Record compression metrics
    pub fn record_compression(
        &self,
        compression_type: AdvancedCompressionType,
        input_size: usize,
        compressed_size: usize,
        duration: Duration,
    ) {
        if let Some((_, metrics)) = self.metrics.iter().find(|(ct, _)| *ct == compression_type) {
            metrics.record(input_size, compressed_size, duration);
        }
    }

    /// Get metrics for an algorithm
    pub fn get_metrics(
        &self,
        compression_type: AdvancedCompressionType,
    ) -> Option<Arc<AlgorithmMetrics>> {
        self.metrics
            .iter()
            .find(|(ct, _)| *ct == compression_type)
            .map(|(_, m)| Arc::clone(m))
    }

    /// Get comprehensive statistics
    pub fn stats(&self) -> AdaptiveCompressionStats {
        let algorithm_stats = self
            .metrics
            .iter()
            .map(|(ctype, metrics)| AlgorithmStats {
                compression_type: *ctype,
                avg_ratio: metrics.avg_ratio(),
                avg_throughput_mbps: metrics.avg_throughput_mbps(),
                operations: metrics.operations.load(Ordering::Relaxed),
                total_bytes: metrics.bytes_compressed.load(Ordering::Relaxed),
            })
            .collect();

        AdaptiveCompressionStats {
            strategy: self.strategy,
            algorithm_stats,
        }
    }
}

/// Statistics for adaptive compression
#[derive(Debug)]
pub struct AdaptiveCompressionStats {
    /// Current selection strategy
    pub strategy: SelectionStrategy,
    /// Per-algorithm statistics
    pub algorithm_stats: Vec<AlgorithmStats>,
}

/// Statistics for a single algorithm
#[derive(Debug)]
pub struct AlgorithmStats {
    /// Compression type
    pub compression_type: AdvancedCompressionType,
    /// Average compression ratio
    pub avg_ratio: f64,
    /// Average throughput (MB/s)
    pub avg_throughput_mbps: f64,
    /// Number of operations
    pub operations: u64,
    /// Total bytes compressed
    pub total_bytes: u64,
}

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

    #[test]
    fn test_algorithm_metrics() {
        let metrics = AlgorithmMetrics::default();

        metrics.record(1000, 500, Duration::from_micros(100));
        metrics.record(2000, 800, Duration::from_micros(200));

        assert_eq!(metrics.operations.load(Ordering::Relaxed), 2);
        assert_eq!(metrics.bytes_compressed.load(Ordering::Relaxed), 3000);
        assert_eq!(metrics.total_compressed_size.load(Ordering::Relaxed), 1300);

        // Ratio = 3000 / 1300 ≈ 2.31
        assert!((metrics.avg_ratio() - 2.31).abs() < 0.01);

        // Avg time = 300 / 2 = 150 us
        assert_eq!(metrics.avg_time_us(), 150.0);
    }

    #[test]
    fn test_adaptive_selector_creation() {
        let selector = AdaptiveCompressionSelector::new(SelectionStrategy::Balanced);

        assert_eq!(selector.strategy, SelectionStrategy::Balanced);
        assert_eq!(selector.metrics.len(), 5); // 5 compression types
    }

    #[test]
    fn test_small_data_not_compressed() {
        let selector = AdaptiveCompressionSelector::new(SelectionStrategy::MaximizeRatio);

        let selected = selector.select(50); // Small data
        assert_eq!(selected, AdvancedCompressionType::RunLength); // Fallback for small data
    }

    #[test]
    fn test_adaptive_selection_by_size() {
        let selector = AdaptiveCompressionSelector::new(SelectionStrategy::Adaptive);

        // Small data: Lz4
        assert_eq!(selector.select(500), AdvancedCompressionType::Lz4);

        // Medium data: Snappy
        assert_eq!(selector.select(5000), AdvancedCompressionType::Snappy);

        // Large data: Zstd
        assert_eq!(selector.select(50000), AdvancedCompressionType::Zstd);

        // Very large data: Zstd
        assert_eq!(selector.select(500000), AdvancedCompressionType::Zstd);
    }

    #[test]
    fn test_record_and_select() {
        let selector = AdaptiveCompressionSelector::new(SelectionStrategy::MaximizeRatio);

        // Record some operations with different ratios
        selector.record_compression(
            AdvancedCompressionType::Lz4,
            1000,
            800,
            Duration::from_micros(50),
        );
        selector.record_compression(
            AdvancedCompressionType::Zstd,
            1000,
            400,
            Duration::from_micros(200),
        );

        // Zstd has better ratio (1000/400 = 2.5 vs 1000/800 = 1.25)
        let selected = selector.select_max_ratio();
        assert_eq!(selected, AdvancedCompressionType::Zstd);
    }

    #[test]
    fn test_min_time_selection() {
        let selector = AdaptiveCompressionSelector::new(SelectionStrategy::MinimizeTime);

        // Record operations with different speeds
        selector.record_compression(
            AdvancedCompressionType::Lz4,
            10000,
            5000,
            Duration::from_micros(100), // Fast
        );
        selector.record_compression(
            AdvancedCompressionType::Zstd,
            10000,
            3000,
            Duration::from_micros(500), // Slow but better ratio
        );

        // Should select Lz4 (faster)
        let selected = selector.select_min_time();
        assert_eq!(selected, AdvancedCompressionType::Lz4);
    }

    #[test]
    fn test_get_metrics() {
        let selector = AdaptiveCompressionSelector::new(SelectionStrategy::Balanced);

        selector.record_compression(
            AdvancedCompressionType::Lz4,
            1000,
            500,
            Duration::from_micros(100),
        );

        let metrics = selector.get_metrics(AdvancedCompressionType::Lz4).unwrap();
        assert_eq!(metrics.operations.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn test_stats() {
        let selector = AdaptiveCompressionSelector::new(SelectionStrategy::Balanced);

        selector.record_compression(
            AdvancedCompressionType::Lz4,
            1000,
            500,
            Duration::from_micros(100),
        );
        selector.record_compression(
            AdvancedCompressionType::Zstd,
            2000,
            600,
            Duration::from_micros(300),
        );

        let stats = selector.stats();
        assert_eq!(stats.strategy, SelectionStrategy::Balanced);
        assert!(stats.algorithm_stats.len() >= 2);

        // Find Lz4 stats
        let lz4_stats = stats
            .algorithm_stats
            .iter()
            .find(|s| s.compression_type == AdvancedCompressionType::Lz4)
            .unwrap();

        assert_eq!(lz4_stats.operations, 1);
        assert_eq!(lz4_stats.total_bytes, 1000);
    }
}