typed_compression/
typed_compression.rs

1//! Example of TypedRef compression for typed structured data.
2//!
3//! TypedRef allows you to compress data with type information, enabling
4//! OpenZL to apply type-specific optimizations.
5
6use rust_openzl::{compress_typed_ref, decompress_typed_buffer, TypedRef};
7
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    println!("=== TypedRef Compression Examples ===\n");
10
11    // Example 1: Serial (untyped) data
12    println!("Example 1: Serial TypedRef");
13    let text = b"Hello from TypedRef! This is untyped byte data.";
14    let tref_serial = TypedRef::serial(text);
15
16    let compressed = compress_typed_ref(&tref_serial)?;
17    let tbuf = decompress_typed_buffer(&compressed)?;
18
19    println!("  Original size: {} bytes", text.len());
20    println!("  Compressed size: {} bytes", compressed.len());
21    println!("  Data type: {:?}", tbuf.data_type());
22
23    assert_eq!(text.as_slice(), tbuf.as_bytes());
24    println!("  ✓ Round-trip successful!\n");
25
26    // Example 2: Numeric u32 data
27    println!("Example 2: Numeric TypedRef (u32)");
28    let numbers: Vec<u32> = (0..1000).collect();
29    let tref_numeric = TypedRef::numeric(&numbers)?;
30
31    let compressed = compress_typed_ref(&tref_numeric)?;
32    let tbuf = decompress_typed_buffer(&compressed)?;
33
34    let original_size = numbers.len() * std::mem::size_of::<u32>();
35    println!("  Original size: {} bytes ({} elements)", original_size, numbers.len());
36    println!("  Compressed size: {} bytes", compressed.len());
37    println!(
38        "  Compression ratio: {:.2}%",
39        (compressed.len() as f64 / original_size as f64) * 100.0
40    );
41    println!("  Data type: {:?}", tbuf.data_type());
42    println!("  Element width: {} bytes", tbuf.elt_width());
43    println!("  Element count: {}", tbuf.num_elts());
44
45    let decompressed = tbuf.as_numeric::<u32>().expect("Failed to extract u32 data");
46    assert_eq!(numbers.as_slice(), decompressed);
47    println!("  ✓ Round-trip successful!\n");
48
49    // Example 3: Numeric u64 data
50    println!("Example 3: Numeric TypedRef (u64)");
51    let large_numbers: Vec<u64> = (0..500).map(|i| i * 1000000).collect();
52    let tref_numeric = TypedRef::numeric(&large_numbers)?;
53
54    let compressed = compress_typed_ref(&tref_numeric)?;
55    let tbuf = decompress_typed_buffer(&compressed)?;
56
57    let original_size = large_numbers.len() * std::mem::size_of::<u64>();
58    println!("  Original size: {} bytes ({} elements)", original_size, large_numbers.len());
59    println!("  Compressed size: {} bytes", compressed.len());
60    println!(
61        "  Compression ratio: {:.2}%",
62        (compressed.len() as f64 / original_size as f64) * 100.0
63    );
64
65    let decompressed = tbuf.as_numeric::<u64>().expect("Failed to extract u64 data");
66    assert_eq!(large_numbers.as_slice(), decompressed);
67    println!("  ✓ Round-trip successful!\n");
68
69    // Example 4: Struct data
70    println!("Example 4: Struct TypedRef");
71
72    // Simulate a simple struct as raw bytes: [id: u32, value: u32]
73    let struct_width = 8; // 2 * sizeof(u32)
74    let struct_count = 100;
75    let mut struct_data = Vec::with_capacity(struct_width * struct_count);
76
77    for i in 0..struct_count {
78        // Write id (u32)
79        struct_data.extend_from_slice(&(i as u32).to_le_bytes());
80        // Write value (u32)
81        struct_data.extend_from_slice(&((i * 10) as u32).to_le_bytes());
82    }
83
84    let tref_struct = TypedRef::structs(&struct_data, struct_width, struct_count)?;
85
86    let compressed = compress_typed_ref(&tref_struct)?;
87    let tbuf = decompress_typed_buffer(&compressed)?;
88
89    println!("  Original size: {} bytes ({} structs of {} bytes)",
90             struct_data.len(), struct_count, struct_width);
91    println!("  Compressed size: {} bytes", compressed.len());
92    println!(
93        "  Compression ratio: {:.2}%",
94        (compressed.len() as f64 / struct_data.len() as f64) * 100.0
95    );
96
97    assert_eq!(struct_data.as_slice(), tbuf.as_bytes());
98    println!("  ✓ Round-trip successful!\n");
99
100    println!("All TypedRef compression examples completed successfully!");
101    println!("\nNote: TypedRef provides type information to OpenZL, enabling");
102    println!("type-specific compression optimizations not available with serial compression.");
103
104    Ok(())
105}