oxirs-stream 0.2.2

Real-time streaming support with Kafka/NATS/MQTT/OPC-UA I/O, RDF Patch, and SPARQL Update delta
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
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
//! Patch compression and optimization

use super::{PatchParser, PatchSerializer};
use crate::{PatchOperation, RdfPatch};
use anyhow::Result;
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
use std::collections::HashMap;
use std::io::{Read, Write};
use tracing::info;

pub struct PatchCompressor {
    compression_level: u32,
    enable_dictionary: bool,
    prefix_compression: bool,
}

impl PatchCompressor {
    pub fn new() -> Self {
        Self {
            compression_level: 6,
            enable_dictionary: true,
            prefix_compression: true,
        }
    }

    pub fn with_compression_level(mut self, level: u32) -> Self {
        self.compression_level = level.min(9);
        self
    }

    pub fn with_dictionary_compression(mut self, enabled: bool) -> Self {
        self.enable_dictionary = enabled;
        self
    }

    pub fn with_prefix_compression(mut self, enabled: bool) -> Self {
        self.prefix_compression = enabled;
        self
    }

    /// Compress patch using gzip compression
    pub fn compress_patch(&self, patch: &RdfPatch) -> Result<Vec<u8>> {
        // Serialize patch to string
        let serializer = PatchSerializer::new().with_pretty_print(false);
        let patch_str = serializer.serialize(patch)?;
        let original_len = patch_str.len();

        // Apply dictionary compression if enabled
        let optimized_str = if self.enable_dictionary {
            self.apply_dictionary_compression(&patch_str)?
        } else {
            patch_str
        };

        // Apply gzip compression
        let mut encoder = GzEncoder::new(Vec::new(), Compression::new(self.compression_level));
        encoder.write_all(optimized_str.as_bytes())?;
        let compressed = encoder.finish()?;

        info!(
            "Compressed patch: {} -> {} bytes ({:.1}% reduction)",
            original_len,
            compressed.len(),
            (1.0 - compressed.len() as f64 / original_len as f64) * 100.0
        );

        Ok(compressed)
    }

    /// Decompress patch from compressed bytes
    pub fn decompress_patch(&self, compressed_data: &[u8]) -> Result<RdfPatch> {
        // Decompress gzip
        let mut decoder = GzDecoder::new(compressed_data);
        let mut decompressed = String::new();
        decoder.read_to_string(&mut decompressed)?;

        // Apply dictionary decompression if needed
        let patch_str = if self.enable_dictionary {
            self.apply_dictionary_decompression(&decompressed)?
        } else {
            decompressed
        };

        // Parse patch
        let mut parser = PatchParser::new();
        parser.parse(&patch_str)
    }

    fn apply_dictionary_compression(&self, patch_str: &str) -> Result<String> {
        // Build frequency dictionary of common terms
        let mut word_freq = HashMap::new();
        for word in patch_str.split_whitespace() {
            *word_freq.entry(word.to_string()).or_insert(0) += 1;
        }

        // Create dictionary of most frequent terms
        let mut freq_words: Vec<_> = word_freq.into_iter().collect();
        freq_words.sort_by(|a, b| b.1.cmp(&a.1));

        let mut dictionary = HashMap::new();
        let mut compressed = patch_str.to_string();

        // Replace most frequent words with short codes
        for (i, (word, freq)) in freq_words.iter().take(256).enumerate() {
            if word.len() > 3 && *freq > 2 {
                let code = format!("#{i:02x}");
                dictionary.insert(code.clone(), word.clone());
                compressed = compressed.replace(word, &code);
            }
        }

        // Prepend dictionary to compressed string
        let mut dict_header = String::new();
        for (code, word) in dictionary {
            dict_header.push_str(&format!("{code}={word}\n"));
        }
        dict_header.push_str("---\n");
        dict_header.push_str(&compressed);

        Ok(dict_header)
    }

    fn apply_dictionary_decompression(&self, compressed_str: &str) -> Result<String> {
        if let Some(separator_pos) = compressed_str.find("---\n") {
            let (dict_part, content_part) = compressed_str.split_at(separator_pos);
            let content = &content_part[4..]; // Skip "---\n"

            let mut dictionary = HashMap::new();
            for line in dict_part.lines() {
                if let Some(eq_pos) = line.find('=') {
                    let code = &line[..eq_pos];
                    let word = &line[eq_pos + 1..];
                    dictionary.insert(code, word);
                }
            }

            let mut decompressed = content.to_string();
            for (code, word) in dictionary {
                decompressed = decompressed.replace(code, word);
            }

            Ok(decompressed)
        } else {
            Ok(compressed_str.to_string())
        }
    }

    /// Compress using prefix compression for common namespaces
    pub fn compress_with_prefixes(&self, patch: &RdfPatch) -> Result<RdfPatch> {
        let mut compressed = patch.clone();
        compressed.id = format!("{}-prefix-compressed", patch.id);

        if !self.prefix_compression {
            return Ok(compressed);
        }

        // Build frequency map of URI prefixes
        let mut prefix_freq = HashMap::new();
        for operation in &patch.operations {
            self.collect_uris_from_operation(operation, &mut prefix_freq);
        }

        // Find common prefixes
        let mut common_prefixes = HashMap::new();
        for (uri, freq) in prefix_freq {
            if freq > 2 {
                if let Some(prefix) = self.extract_namespace_prefix(&uri) {
                    if prefix.len() > 10 {
                        let short_prefix = format!("ns{}", common_prefixes.len());
                        common_prefixes.insert(prefix, short_prefix);
                    }
                }
            }
        }

        // Add prefix declarations to patch
        for (namespace, prefix) in &common_prefixes {
            compressed.add_operation(PatchOperation::AddPrefix {
                prefix: prefix.clone(),
                namespace: namespace.clone(),
            });
        }

        // Replace URIs with prefixed forms
        for operation in &mut compressed.operations {
            self.apply_prefix_compression_to_operation(operation, &common_prefixes);
        }

        info!(
            "Applied prefix compression: {} prefixes defined",
            common_prefixes.len()
        );
        Ok(compressed)
    }

    fn collect_uris_from_operation(
        &self,
        operation: &PatchOperation,
        prefix_freq: &mut HashMap<String, usize>,
    ) {
        match operation {
            PatchOperation::Add {
                subject,
                predicate,
                object,
            } => {
                *prefix_freq.entry(subject.clone()).or_insert(0) += 1;
                *prefix_freq.entry(predicate.clone()).or_insert(0) += 1;
                *prefix_freq.entry(object.clone()).or_insert(0) += 1;
            }
            PatchOperation::Delete {
                subject,
                predicate,
                object,
            } => {
                *prefix_freq.entry(subject.clone()).or_insert(0) += 1;
                *prefix_freq.entry(predicate.clone()).or_insert(0) += 1;
                *prefix_freq.entry(object.clone()).or_insert(0) += 1;
            }
            PatchOperation::AddGraph { graph } | PatchOperation::DeleteGraph { graph } => {
                *prefix_freq.entry(graph.clone()).or_insert(0) += 1;
            }
            _ => {}
        }
    }

    fn extract_namespace_prefix(&self, uri: &str) -> Option<String> {
        // Extract namespace part of URI (everything up to last # or /)
        if let Some(pos) = uri.rfind('#') {
            Some(uri[..pos + 1].to_string())
        } else {
            uri.rfind('/').map(|pos| uri[..pos + 1].to_string())
        }
    }

    fn apply_prefix_compression_to_operation(
        &self,
        operation: &mut PatchOperation,
        prefixes: &HashMap<String, String>,
    ) {
        match operation {
            PatchOperation::Add {
                subject,
                predicate,
                object,
            } => {
                *subject = self.compress_uri_with_prefixes(subject, prefixes);
                *predicate = self.compress_uri_with_prefixes(predicate, prefixes);
                *object = self.compress_uri_with_prefixes(object, prefixes);
            }
            PatchOperation::Delete {
                subject,
                predicate,
                object,
            } => {
                *subject = self.compress_uri_with_prefixes(subject, prefixes);
                *predicate = self.compress_uri_with_prefixes(predicate, prefixes);
                *object = self.compress_uri_with_prefixes(object, prefixes);
            }
            PatchOperation::AddGraph { graph } | PatchOperation::DeleteGraph { graph } => {
                *graph = self.compress_uri_with_prefixes(graph, prefixes);
            }
            _ => {}
        }
    }

    fn compress_uri_with_prefixes(&self, uri: &str, prefixes: &HashMap<String, String>) -> String {
        for (namespace, prefix) in prefixes {
            if uri.starts_with(namespace) {
                let local_name = &uri[namespace.len()..];
                return format!("{prefix}:{local_name}");
            }
        }
        uri.to_string()
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::patch::context::{apply_patch_with_context, PatchContext};
    use crate::patch::result::{
        create_reverse_patch, create_transactional_patch, optimize_patch, validate_patch,
    };

    #[test]
    fn test_patch_serialization() {
        let mut patch = RdfPatch::new();
        patch.add_operation(PatchOperation::Header {
            key: "creator".to_string(),
            value: "test-suite".to_string(),
        });
        patch.add_operation(PatchOperation::TransactionBegin {
            transaction_id: Some("tx-123".to_string()),
        });
        patch.add_operation(PatchOperation::AddPrefix {
            prefix: "ex".to_string(),
            namespace: "http://example.org/".to_string(),
        });
        patch.add_operation(PatchOperation::Add {
            subject: "http://example.org/subject".to_string(),
            predicate: "http://example.org/predicate".to_string(),
            object: "\"Object literal\"".to_string(),
        });
        patch.add_operation(PatchOperation::Delete {
            subject: "http://example.org/subject2".to_string(),
            predicate: "http://example.org/predicate2".to_string(),
            object: "http://example.org/object2".to_string(),
        });
        patch.add_operation(PatchOperation::TransactionCommit);

        let serializer = PatchSerializer::new();
        let result = serializer.serialize(&patch);

        assert!(result.is_ok());
        let serialized = result.unwrap();
        assert!(serialized.contains("H creator test-suite"));
        assert!(serialized.contains("TX tx-123"));
        assert!(serialized.contains("PA ex:"));
        assert!(serialized.contains("A "));
        assert!(serialized.contains("D "));
        assert!(serialized.contains("TC"));
        assert!(serialized.contains("@prefix"));
    }

    #[test]
    fn test_patch_parsing() {
        let patch_content = r#"
@prefix ex: <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

H creator test-parser .
TX tx-456 .
PA ex2: <http://example2.org/> .
A ex:subject ex:predicate "Object literal" .
D ex:subject2 ex:predicate2 ex:object2 .
GA ex:graph1 .
GD ex:graph2 .
PD old: .
TC .
"#;

        let mut parser = PatchParser::new();
        let result = parser.parse(patch_content);

        assert!(result.is_ok());
        let patch = result.unwrap();
        assert_eq!(patch.operations.len(), 9);

        // Check header was captured
        assert_eq!(
            patch.headers.get("creator"),
            Some(&"test-parser".to_string())
        );

        // Check transaction ID was captured
        assert_eq!(patch.transaction_id, Some("tx-456".to_string()));

        // Check prefix was captured
        assert_eq!(
            patch.prefixes.get("ex2"),
            Some(&"http://example2.org/".to_string())
        );

        match &patch.operations[0] {
            PatchOperation::Header { key, value } => {
                assert_eq!(key, "creator");
                assert_eq!(value, "test-parser");
            }
            _ => panic!("Expected Header operation"),
        }

        match &patch.operations[1] {
            PatchOperation::TransactionBegin { transaction_id } => {
                assert_eq!(transaction_id, &Some("tx-456".to_string()));
            }
            _ => panic!("Expected TransactionBegin operation"),
        }
    }

    #[test]
    fn test_patch_round_trip() {
        let mut original_patch = RdfPatch::new();
        original_patch.add_operation(PatchOperation::Add {
            subject: "http://example.org/subject".to_string(),
            predicate: "http://example.org/predicate".to_string(),
            object: "\"Test object\"".to_string(),
        });

        // Serialize to string
        let serialized = original_patch.to_rdf_patch_format().unwrap();

        // Parse back from string
        let parsed_patch = RdfPatch::from_rdf_patch_format(&serialized).unwrap();

        // Check that we get the same operations
        assert_eq!(
            original_patch.operations.len(),
            parsed_patch.operations.len()
        );

        match (&original_patch.operations[0], &parsed_patch.operations[0]) {
            (
                PatchOperation::Add {
                    subject: s1,
                    predicate: p1,
                    object: o1,
                },
                PatchOperation::Add {
                    subject: s2,
                    predicate: p2,
                    object: o2,
                },
            ) => {
                assert_eq!(s1, s2);
                assert_eq!(p1, p2);
                assert_eq!(o1, o2);
            }
            _ => panic!("Operations don't match"),
        }
    }

    #[test]
    fn test_reverse_patch() {
        let mut patch = RdfPatch::new();
        patch.add_operation(PatchOperation::TransactionBegin {
            transaction_id: Some("tx-789".to_string()),
        });
        patch.add_operation(PatchOperation::Add {
            subject: "http://example.org/s".to_string(),
            predicate: "http://example.org/p".to_string(),
            object: "http://example.org/o".to_string(),
        });
        patch.add_operation(PatchOperation::AddGraph {
            graph: "http://example.org/graph".to_string(),
        });
        patch.add_operation(PatchOperation::TransactionCommit);
        patch.transaction_id = Some("tx-789".to_string());

        let reverse = create_reverse_patch(&patch).unwrap();

        // Should have TX, two reversed operations, and TC
        assert!(reverse.operations.len() >= 4);

        // First should be transaction begin (reversing the commit)
        match &reverse.operations[0] {
            PatchOperation::TransactionBegin { .. } => {}
            _ => panic!("Expected TransactionBegin operation"),
        }

        // Find the reversed operations
        let has_delete_graph = reverse.operations.iter().any(|op| {
            matches!(op, PatchOperation::DeleteGraph { graph } if graph == "http://example.org/graph")
        });
        let has_delete_triple = reverse.operations.iter().any(|op| {
            matches!(op, PatchOperation::Delete { subject, .. } if subject == "http://example.org/s")
        });

        assert!(has_delete_graph);
        assert!(has_delete_triple);

        // Last should be transaction commit
        match reverse.operations.last() {
            Some(PatchOperation::TransactionCommit) => {}
            _ => panic!("Expected TransactionCommit as last operation"),
        }
    }

    #[test]
    fn test_patch_optimization() {
        let mut patch = RdfPatch::new();
        let operation = PatchOperation::Add {
            subject: "http://example.org/s".to_string(),
            predicate: "http://example.org/p".to_string(),
            object: "http://example.org/o".to_string(),
        };

        // Add the same operation twice
        patch.add_operation(operation.clone());
        patch.add_operation(operation);

        let optimized = optimize_patch(&patch).unwrap();

        // Should remove duplicate
        assert_eq!(optimized.operations.len(), 1);
    }

    #[test]
    fn test_transactional_patch() {
        let operations = vec![
            PatchOperation::Add {
                subject: "s1".to_string(),
                predicate: "p1".to_string(),
                object: "o1".to_string(),
            },
            PatchOperation::Delete {
                subject: "s2".to_string(),
                predicate: "p2".to_string(),
                object: "o2".to_string(),
            },
        ];

        let patch = create_transactional_patch(operations);

        // Should have TX + 2 operations + TC = 4 total
        assert_eq!(patch.operations.len(), 4);

        // First should be transaction begin
        assert!(matches!(
            &patch.operations[0],
            PatchOperation::TransactionBegin { .. }
        ));

        // Last should be transaction commit
        assert!(matches!(
            &patch.operations[3],
            PatchOperation::TransactionCommit
        ));

        // Should have transaction ID set
        assert!(patch.transaction_id.is_some());
    }

    #[test]
    fn test_patch_validation() {
        let mut patch = RdfPatch::new();
        patch.add_operation(PatchOperation::Delete {
            subject: "http://example.org/s".to_string(),
            predicate: "http://example.org/p".to_string(),
            object: "http://example.org/o".to_string(),
        });

        let warnings = validate_patch(&patch).unwrap();

        // Should warn about deleting without prior addition
        assert!(!warnings.is_empty());
        assert!(warnings[0].contains("deleted without prior addition"));
    }

    #[test]
    fn test_patch_application() {
        let mut patch = RdfPatch::new();
        patch.add_operation(PatchOperation::Add {
            subject: "http://example.org/s".to_string(),
            predicate: "http://example.org/p".to_string(),
            object: "http://example.org/o".to_string(),
        });

        let context = PatchContext {
            strict_mode: false,
            validate_operations: true,
            dry_run: true,
        };

        let result = apply_patch_with_context(&patch, &context).unwrap();

        assert_eq!(result.total_operations, 1);
        assert_eq!(result.operations_applied, 1);
        assert!(result.is_success());
        assert_eq!(result.success_rate(), 1.0);
    }
}