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
//! Snappy compression algorithm implementation
//!
//! Snappy is a compression algorithm developed by Google that prioritizes
//! speed over compression ratio. It's designed for very fast compression
//! and decompression with reasonable compression ratios.
//!
//! # Performance Characteristics
//!
//! - Compression speed: ~250-500 MB/s
//! - Decompression speed: ~500-1500 MB/s
//! - Compression ratio: 1.5-2x on typical data
//!
//! # Use Cases
//!
//! - Real-time data processing
//! - Network protocols requiring low latency
//! - Cache compression where speed is paramount
//! - Google BigTable-compatible storage

use super::{
    AdvancedCompressionType, CompressedData, CompressionAlgorithm, CompressionMetadata,
    MAX_COMPRESSION_INPUT_SIZE,
};
use anyhow::{bail, Result};
use std::collections::HashMap;
use std::time::Instant;

/// Snappy compression algorithm
///
/// Uses the oxiarc-snappy crate for pure Rust Snappy compression.
/// Optimized for speed with reasonable compression ratios.
#[derive(Debug, Clone)]
pub struct SnappyCompressor {
    /// Use framing format (compatible with streaming)
    use_framing: bool,
}

impl SnappyCompressor {
    /// Create a new Snappy compressor with default settings
    pub fn new() -> Self {
        Self {
            use_framing: false, // Raw format by default
        }
    }

    /// Create a Snappy compressor with framing format
    ///
    /// The framing format is compatible with the Snappy streaming format
    /// and includes checksums for data integrity.
    pub fn with_framing() -> Self {
        Self { use_framing: true }
    }

    /// Create a Snappy compressor with raw format (no framing)
    pub fn raw() -> Self {
        Self { use_framing: false }
    }
}

impl Default for SnappyCompressor {
    fn default() -> Self {
        Self::new()
    }
}

impl CompressionAlgorithm for SnappyCompressor {
    fn compress(&self, data: &[u8]) -> Result<CompressedData> {
        if data.is_empty() {
            return Ok(CompressedData {
                data: Vec::new(),
                metadata: CompressionMetadata {
                    algorithm: AdvancedCompressionType::Adaptive, // Will add Snappy type
                    original_size: 0,
                    compressed_size: 0,
                    compression_time_us: 0,
                    metadata: HashMap::new(),
                },
            });
        }

        if data.len() > MAX_COMPRESSION_INPUT_SIZE {
            bail!(
                "Input data too large: {} bytes (max: {})",
                data.len(),
                MAX_COMPRESSION_INPUT_SIZE
            );
        }

        let start = Instant::now();

        // Compress using oxiarc-snappy (Pure Rust). Both the framed and raw
        // Snappy formats are standard and preserved across the migration.
        let compressed = if self.use_framing {
            // Use framing format with checksums
            use std::io::Write;
            let mut encoder = oxiarc_snappy::FrameEncoder::new(Vec::new());
            encoder.write_all(data)?;
            encoder
                .finish()
                .map_err(|e| anyhow::anyhow!("Snappy frame finish failed: {}", e))?
        } else {
            // Use raw format (faster, no checksums). Raw Snappy compression is
            // infallible in oxiarc-snappy.
            oxiarc_snappy::compress(data)
        };

        let compression_time = start.elapsed();

        let mut metadata_map = HashMap::new();
        metadata_map.insert("framing".to_string(), self.use_framing.to_string());
        metadata_map.insert(
            "compression_ratio".to_string(),
            format!("{:.2}", compressed.len() as f64 / data.len() as f64),
        );

        let compressed_size = compressed.len() as u64;

        Ok(CompressedData {
            data: compressed,
            metadata: CompressionMetadata {
                algorithm: AdvancedCompressionType::Adaptive, // Will add Snappy type
                original_size: data.len() as u64,
                compressed_size,
                compression_time_us: compression_time.as_micros() as u64,
                metadata: metadata_map,
            },
        })
    }

    fn decompress(&self, compressed: &CompressedData) -> Result<Vec<u8>> {
        if compressed.data.is_empty() {
            return Ok(Vec::new());
        }

        let start = Instant::now();

        // Determine if data was compressed with framing
        let use_framing = compressed
            .metadata
            .metadata
            .get("framing")
            .and_then(|v| v.parse().ok())
            .unwrap_or(self.use_framing);

        // Decompress using oxiarc-snappy (Pure Rust).
        let decompressed = if use_framing {
            // Use framing format with checksums
            use std::io::Read;
            let mut decoder = oxiarc_snappy::FrameDecoder::new(&compressed.data[..]);
            let mut result = Vec::new();
            decoder.read_to_end(&mut result)?;
            result
        } else {
            // Use raw format
            oxiarc_snappy::decompress(&compressed.data)
                .map_err(|e| anyhow::anyhow!("Snappy decompression failed: {}", e))?
        };

        let decompression_time = start.elapsed();

        // Verify decompressed size matches metadata
        if decompressed.len() != compressed.metadata.original_size as usize {
            bail!(
                "Decompressed size mismatch: expected {}, got {}",
                compressed.metadata.original_size,
                decompressed.len()
            );
        }

        log::trace!(
            "Snappy decompression: {} bytes -> {} bytes in {:?}",
            compressed.data.len(),
            decompressed.len(),
            decompression_time
        );

        Ok(decompressed)
    }

    fn algorithm_type(&self) -> AdvancedCompressionType {
        AdvancedCompressionType::Adaptive // Will add Snappy type
    }
}

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

    #[test]
    fn test_snappy_compress_empty() {
        let compressor = SnappyCompressor::new();
        let result = compressor.compress(&[]).unwrap();
        assert_eq!(result.data.len(), 0);
        assert_eq!(result.metadata.original_size, 0);
        assert_eq!(result.metadata.compressed_size, 0);
    }

    #[test]
    fn test_snappy_compress_decompress_raw() {
        let compressor = SnappyCompressor::raw();
        let original = b"Hello, World! This is a test of Snappy compression. ".repeat(100);

        let compressed = compressor.compress(&original).unwrap();
        assert!(compressed.data.len() < original.len());
        assert_eq!(compressed.metadata.original_size, original.len() as u64);

        let decompressed = compressor.decompress(&compressed).unwrap();
        assert_eq!(decompressed, original);
    }

    #[test]
    fn test_snappy_compress_decompress_framed() {
        let compressor = SnappyCompressor::with_framing();
        let original = b"Hello, World! This is a test of Snappy framed compression. ".repeat(100);

        let compressed = compressor.compress(&original).unwrap();
        assert!(compressed.data.len() < original.len());
        assert_eq!(compressed.metadata.original_size, original.len() as u64);

        let decompressed = compressor.decompress(&compressed).unwrap();
        assert_eq!(decompressed, original);
    }

    #[test]
    fn test_snappy_raw_format_fidelity() {
        // The raw compressor must emit a standard raw Snappy block that the
        // low-level oxiarc-snappy decoder can decompress directly.
        let compressor = SnappyCompressor::raw();
        let original = b"oxiarc raw snappy fidelity check ".repeat(40);

        let compressed = compressor.compress(&original).unwrap();
        let decoded = oxiarc_snappy::decompress(&compressed.data).unwrap();
        assert_eq!(decoded, original);

        // And a blob produced directly by oxiarc must decode via the compressor.
        let direct = oxiarc_snappy::compress(&original);
        let cd = CompressedData {
            data: direct,
            metadata: CompressionMetadata {
                algorithm: AdvancedCompressionType::Adaptive,
                original_size: original.len() as u64,
                compressed_size: 0,
                compression_time_us: 0,
                metadata: {
                    let mut m = std::collections::HashMap::new();
                    m.insert("framing".to_string(), "false".to_string());
                    m
                },
            },
        };
        assert_eq!(compressor.decompress(&cd).unwrap(), original);
    }

    #[test]
    fn test_snappy_framed_format_fidelity() {
        // Framed output must begin with the standard Snappy stream identifier
        // chunk (0xFF followed by length 6 and the ASCII "sNaPpY" magic).
        let compressor = SnappyCompressor::with_framing();
        let original = b"oxiarc framed snappy fidelity check ".repeat(40);

        let compressed = compressor.compress(&original).unwrap();
        assert!(compressed.data.len() >= 10);
        assert_eq!(compressed.data[0], 0xFF);
        assert_eq!(&compressed.data[4..10], b"sNaPpY");

        let decompressed = compressor.decompress(&compressed).unwrap();
        assert_eq!(decompressed, original);
    }

    #[test]
    fn test_snappy_format_comparison() {
        let data = b"The quick brown fox jumps over the lazy dog. ".repeat(50);

        let raw = SnappyCompressor::raw();
        let framed = SnappyCompressor::with_framing();

        let raw_result = raw.compress(&data).unwrap();
        let framed_result = framed.compress(&data).unwrap();

        // Both should decompress to the same data
        assert_eq!(raw.decompress(&raw_result).unwrap(), data);
        assert_eq!(framed.decompress(&framed_result).unwrap(), data);

        // Raw format should be slightly smaller (no framing overhead)
        println!("Raw: {} bytes", raw_result.data.len());
        println!("Framed: {} bytes", framed_result.data.len());
        assert!(raw_result.data.len() <= framed_result.data.len());
    }

    #[test]
    fn test_snappy_highly_compressible_data() {
        let compressor = SnappyCompressor::new();
        let data = vec![b'A'; 10000]; // Highly repetitive data

        let compressed = compressor.compress(&data).unwrap();
        let decompressed = compressor.decompress(&compressed).unwrap();

        assert_eq!(decompressed, data);
        // Should achieve good compression on repetitive data
        assert!(compressed.data.len() < data.len() / 5);
        println!(
            "Snappy highly compressible: {} -> {} bytes ({:.2}x)",
            data.len(),
            compressed.data.len(),
            data.len() as f64 / compressed.data.len() as f64
        );
    }

    #[test]
    fn test_snappy_incompressible_data() {
        let compressor = SnappyCompressor::new();
        // Use scirs2_core::random for random data generation
        use scirs2_core::random::rng;
        use scirs2_core::RngExt;

        let mut rng = rng();
        let data: Vec<u8> = (0..1000).map(|_| rng.random_range(0..256) as u8).collect();

        let compressed = compressor.compress(&data).unwrap();
        let decompressed = compressor.decompress(&compressed).unwrap();

        assert_eq!(decompressed, data);
        // Random data should not compress well
        println!(
            "Snappy incompressible: {} -> {} bytes",
            data.len(),
            compressed.data.len()
        );
    }

    #[test]
    fn test_snappy_too_large_input() {
        let compressor = SnappyCompressor::new();
        let data = vec![0u8; MAX_COMPRESSION_INPUT_SIZE + 1];

        let result = compressor.compress(&data);
        assert!(result.is_err());
    }

    #[test]
    fn test_snappy_metadata() {
        let compressor = SnappyCompressor::with_framing();
        let data = b"Testing metadata collection with Snappy".repeat(10);

        let compressed = compressor.compress(&data).unwrap();

        assert_eq!(compressed.metadata.metadata.get("framing").unwrap(), "true");
        assert!(compressed
            .metadata
            .metadata
            .contains_key("compression_ratio"));
        assert!(compressed.metadata.compression_time_us > 0);
    }

    #[test]
    fn test_snappy_small_data() {
        let compressor = SnappyCompressor::new();
        let data = b"Small";

        let compressed = compressor.compress(data).unwrap();
        let decompressed = compressor.decompress(&compressed).unwrap();

        assert_eq!(decompressed, data);
        // Very small data might not compress
        println!(
            "Snappy small data: {} -> {} bytes",
            data.len(),
            compressed.data.len()
        );
    }

    #[test]
    fn test_snappy_rdf_data_compression() {
        let compressor = SnappyCompressor::new();
        // Simulate RDF triple data with typical patterns
        let rdf_data = b"<http://example.org/resource/1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/Class> .\n"
            .repeat(100);

        let compressed = compressor.compress(&rdf_data).unwrap();
        let decompressed = compressor.decompress(&compressed).unwrap();

        assert_eq!(decompressed, rdf_data);

        // RDF data with repetitive URIs should compress well
        let ratio = rdf_data.len() as f64 / compressed.data.len() as f64;
        println!("Snappy RDF compression ratio: {:.2}x", ratio);
        assert!(ratio > 1.5); // Should achieve >1.5x compression
    }

    #[test]
    fn test_snappy_roundtrip_consistency() {
        let compressor = SnappyCompressor::new();
        let data = b"Consistency test data".repeat(50);

        // Compress and decompress multiple times
        let mut current = data.to_vec();
        for _ in 0..5 {
            let compressed = compressor.compress(&current).unwrap();
            current = compressor.decompress(&compressed).unwrap();
        }

        assert_eq!(current, data);
    }
}