oxirs-stream 0.2.4

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
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
624
625
626
627
628
629
//! # Change Data Capture (CDC) Stream Processor
//!
//! Advanced CDC capabilities for streaming database changes, event sourcing,
//! and real-time data integration with full support for inserts, updates, deletes,
//! and schema evolution.
//!
//! ## Features
//! - Multiple CDC patterns (Debezium, Maxwell, Canal compatibility)
//! - Transaction boundary detection
//! - Schema evolution tracking
//! - Snapshot + incremental sync
//! - Deduplication and idempotency
//! - Backpressure handling for large transactions
//! - Metrics and monitoring

use crate::error::StreamResult;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};
use uuid::Uuid;

/// CDC operation types following industry standards
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CdcOperation {
    /// Insert/Create operation
    Insert,
    /// Update/Modify operation
    Update,
    /// Delete/Remove operation
    Delete,
    /// Snapshot read (initial state capture)
    Snapshot,
    /// Truncate table operation
    Truncate,
    /// Schema change (DDL)
    SchemaChange,
}

/// CDC event representing a database change
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CdcEvent {
    /// Unique event identifier
    pub id: Uuid,
    /// Source database/table identifier
    pub source: CdcSource,
    /// Operation type
    pub operation: CdcOperation,
    /// Data before the change (for updates/deletes)
    pub before: Option<HashMap<String, serde_json::Value>>,
    /// Data after the change (for inserts/updates)
    pub after: Option<HashMap<String, serde_json::Value>>,
    /// Transaction identifier
    pub transaction_id: Option<String>,
    /// Transaction sequence number
    pub sequence: Option<u64>,
    /// Log position (LSN/binlog position/etc)
    pub position: Option<String>,
    /// Event timestamp
    pub timestamp: DateTime<Utc>,
    /// Schema version
    pub schema_version: Option<u32>,
    /// Metadata
    pub metadata: HashMap<String, String>,
}

/// CDC source identifier
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CdcSource {
    /// Database name
    pub database: String,
    /// Schema name (optional)
    pub schema: Option<String>,
    /// Table name
    pub table: String,
    /// Source connector type
    pub connector: CdcConnector,
}

/// CDC connector types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CdcConnector {
    /// Debezium-compatible format
    Debezium,
    /// Maxwell format (MySQL)
    Maxwell,
    /// Canal format (MySQL)
    Canal,
    /// AWS DMS format
    AwsDms,
    /// Custom connector
    Custom,
}

/// CDC processor configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CdcConfig {
    /// Enable transaction boundary detection
    pub detect_transactions: bool,
    /// Buffer size for transaction assembly
    pub transaction_buffer_size: usize,
    /// Transaction timeout (commit incomplete transactions)
    pub transaction_timeout_ms: u64,
    /// Enable deduplication based on event ID
    pub enable_deduplication: bool,
    /// Deduplication window size
    pub dedup_window_size: usize,
    /// Enable schema evolution tracking
    pub track_schema_evolution: bool,
    /// Enable snapshot mode
    pub enable_snapshot: bool,
    /// Snapshot batch size
    pub snapshot_batch_size: usize,
    /// Enable metrics collection
    pub enable_metrics: bool,
}

impl Default for CdcConfig {
    fn default() -> Self {
        Self {
            detect_transactions: true,
            transaction_buffer_size: 10000,
            transaction_timeout_ms: 30000,
            enable_deduplication: true,
            dedup_window_size: 100000,
            track_schema_evolution: true,
            enable_snapshot: true,
            snapshot_batch_size: 1000,
            enable_metrics: true,
        }
    }
}

/// Transaction assembler for multi-event transactions
#[derive(Debug)]
struct Transaction {
    id: String,
    events: Vec<CdcEvent>,
    started_at: DateTime<Utc>,
    last_event_at: DateTime<Utc>,
}

/// Deduplication cache entry type (event ID, timestamp)
type DedupCacheEntry = (Uuid, DateTime<Utc>);

/// CDC processor with transaction handling and deduplication
pub struct CdcProcessor {
    config: CdcConfig,
    /// Active transactions being assembled
    active_transactions: Arc<RwLock<HashMap<String, Transaction>>>,
    /// Deduplication cache (event ID -> timestamp)
    dedup_cache: Arc<RwLock<VecDeque<DedupCacheEntry>>>,
    /// Schema versions by source
    schema_versions: Arc<RwLock<HashMap<CdcSource, u32>>>,
    /// Processing metrics
    metrics: Arc<RwLock<CdcMetrics>>,
}

/// CDC processing metrics
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct CdcMetrics {
    pub events_processed: u64,
    pub transactions_committed: u64,
    pub transactions_rolled_back: u64,
    pub deduplicated_events: u64,
    pub schema_changes_detected: u64,
    pub snapshot_events: u64,
    pub inserts: u64,
    pub updates: u64,
    pub deletes: u64,
    pub avg_transaction_size: f64,
    pub max_transaction_size: usize,
}

impl CdcProcessor {
    /// Create a new CDC processor
    pub fn new(config: CdcConfig) -> Self {
        Self {
            config,
            active_transactions: Arc::new(RwLock::new(HashMap::new())),
            dedup_cache: Arc::new(RwLock::new(VecDeque::new())),
            schema_versions: Arc::new(RwLock::new(HashMap::new())),
            metrics: Arc::new(RwLock::new(CdcMetrics::default())),
        }
    }

    /// Process a CDC event
    pub async fn process_event(&self, event: CdcEvent) -> StreamResult<Vec<CdcEvent>> {
        // Deduplication check
        if self.config.enable_deduplication && self.is_duplicate(&event).await? {
            let mut metrics = self.metrics.write().await;
            metrics.deduplicated_events += 1;
            debug!("Deduplicated CDC event: {}", event.id);
            return Ok(vec![]);
        }

        // Update schema version tracking
        if self.config.track_schema_evolution {
            self.track_schema_version(&event).await?;
        }

        // Update metrics
        self.update_metrics(&event).await;

        // Handle transaction boundaries
        if self.config.detect_transactions && event.transaction_id.is_some() {
            self.handle_transaction_event(event).await
        } else {
            // Non-transactional event - emit immediately
            Ok(vec![event])
        }
    }

    /// Check if event is a duplicate
    async fn is_duplicate(&self, event: &CdcEvent) -> StreamResult<bool> {
        let cache = self.dedup_cache.read().await;
        Ok(cache.iter().any(|(id, _)| *id == event.id))
    }

    /// Track schema version changes
    async fn track_schema_version(&self, event: &CdcEvent) -> StreamResult<()> {
        if event.operation == CdcOperation::SchemaChange {
            if let Some(version) = event.schema_version {
                let mut versions = self.schema_versions.write().await;
                let old_version = versions.insert(event.source.clone(), version);

                if old_version != Some(version) {
                    info!(
                        "Schema version changed for {}.{}: {:?} -> {}",
                        event.source.database, event.source.table, old_version, version
                    );

                    let mut metrics = self.metrics.write().await;
                    metrics.schema_changes_detected += 1;
                }
            }
        }
        Ok(())
    }

    /// Handle transaction event assembly
    async fn handle_transaction_event(&self, event: CdcEvent) -> StreamResult<Vec<CdcEvent>> {
        let tx_id = event
            .transaction_id
            .clone()
            .expect("transaction_id should be present for transaction events");
        let mut transactions = self.active_transactions.write().await;

        let now = Utc::now();

        // Get or create transaction
        let transaction = transactions
            .entry(tx_id.clone())
            .or_insert_with(|| Transaction {
                id: tx_id.clone(),
                events: Vec::new(),
                started_at: now,
                last_event_at: now,
            });

        transaction.events.push(event.clone());
        transaction.last_event_at = now;

        // Check for transaction timeout
        let timeout_ms = self.config.transaction_timeout_ms as i64;
        if (now - transaction.started_at).num_milliseconds() > timeout_ms {
            warn!(
                "Transaction {} timed out after {} events",
                tx_id,
                transaction.events.len()
            );

            // Commit incomplete transaction
            let events = transaction.events.clone();
            transactions.remove(&tx_id);

            let mut metrics = self.metrics.write().await;
            let prev_count = metrics.transactions_committed;
            metrics.transactions_committed += 1;
            metrics.avg_transaction_size = (metrics.avg_transaction_size * prev_count as f64
                + events.len() as f64)
                / metrics.transactions_committed as f64;
            metrics.max_transaction_size = metrics.max_transaction_size.max(events.len());

            return Ok(events);
        }

        // Transaction assembly continues
        Ok(vec![])
    }

    /// Commit a transaction (call when transaction end marker received)
    pub async fn commit_transaction(&self, transaction_id: &str) -> StreamResult<Vec<CdcEvent>> {
        let mut transactions = self.active_transactions.write().await;

        if let Some(transaction) = transactions.remove(transaction_id) {
            info!(
                "Committing transaction {} with {} events",
                transaction_id,
                transaction.events.len()
            );

            let mut metrics = self.metrics.write().await;
            let prev_count = metrics.transactions_committed;
            metrics.transactions_committed += 1;
            metrics.avg_transaction_size = (metrics.avg_transaction_size * prev_count as f64
                + transaction.events.len() as f64)
                / metrics.transactions_committed as f64;
            metrics.max_transaction_size =
                metrics.max_transaction_size.max(transaction.events.len());

            Ok(transaction.events)
        } else {
            warn!(
                "Attempted to commit unknown transaction: {}",
                transaction_id
            );
            Ok(vec![])
        }
    }

    /// Rollback a transaction (discard events)
    pub async fn rollback_transaction(&self, transaction_id: &str) -> StreamResult<()> {
        let mut transactions = self.active_transactions.write().await;

        if let Some(transaction) = transactions.remove(transaction_id) {
            warn!(
                "Rolling back transaction {} with {} events",
                transaction_id,
                transaction.events.len()
            );

            let mut metrics = self.metrics.write().await;
            metrics.transactions_rolled_back += 1;
        }

        Ok(())
    }

    /// Update processing metrics
    async fn update_metrics(&self, event: &CdcEvent) {
        let mut metrics = self.metrics.write().await;
        metrics.events_processed += 1;

        match event.operation {
            CdcOperation::Insert => metrics.inserts += 1,
            CdcOperation::Update => metrics.updates += 1,
            CdcOperation::Delete => metrics.deletes += 1,
            CdcOperation::Snapshot => metrics.snapshot_events += 1,
            _ => {}
        }

        // Maintain deduplication cache
        if self.config.enable_deduplication {
            let mut cache = self.dedup_cache.write().await;
            cache.push_back((event.id, event.timestamp));

            // Trim cache to configured size
            while cache.len() > self.config.dedup_window_size {
                cache.pop_front();
            }
        }
    }

    /// Get current processing metrics
    pub async fn get_metrics(&self) -> CdcMetrics {
        self.metrics.read().await.clone()
    }

    /// Convert CDC event to StreamEvent (Custom event variant)
    /// Note: Since StreamEvent is an enum focused on RDF operations,
    /// CDC events are best handled separately or through a custom event type.
    /// This is a placeholder for potential future integration.
    pub fn to_custom_event_data(cdc_event: &CdcEvent) -> serde_json::Value {
        serde_json::to_value(cdc_event).unwrap_or(serde_json::Value::Null)
    }

    /// Parse CDC event from JSON data
    pub fn from_json(data: &serde_json::Value) -> StreamResult<CdcEvent> {
        serde_json::from_value(data.clone())
            .map_err(|e| crate::error::StreamError::Deserialization(e.to_string()))
    }
}

/// CDC event builder for convenient event construction
pub struct CdcEventBuilder {
    event: CdcEvent,
}

impl CdcEventBuilder {
    pub fn new(source: CdcSource, operation: CdcOperation) -> Self {
        Self {
            event: CdcEvent {
                id: Uuid::new_v4(),
                source,
                operation,
                before: None,
                after: None,
                transaction_id: None,
                sequence: None,
                position: None,
                timestamp: Utc::now(),
                schema_version: None,
                metadata: HashMap::new(),
            },
        }
    }

    pub fn before(mut self, data: HashMap<String, serde_json::Value>) -> Self {
        self.event.before = Some(data);
        self
    }

    pub fn after(mut self, data: HashMap<String, serde_json::Value>) -> Self {
        self.event.after = Some(data);
        self
    }

    pub fn transaction(mut self, tx_id: String, sequence: u64) -> Self {
        self.event.transaction_id = Some(tx_id);
        self.event.sequence = Some(sequence);
        self
    }

    pub fn position(mut self, pos: String) -> Self {
        self.event.position = Some(pos);
        self
    }

    pub fn schema_version(mut self, version: u32) -> Self {
        self.event.schema_version = Some(version);
        self
    }

    pub fn metadata(mut self, key: String, value: String) -> Self {
        self.event.metadata.insert(key, value);
        self
    }

    pub fn build(self) -> CdcEvent {
        self.event
    }
}

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

    fn create_test_source() -> CdcSource {
        CdcSource {
            database: "testdb".to_string(),
            schema: Some("public".to_string()),
            table: "users".to_string(),
            connector: CdcConnector::Debezium,
        }
    }

    #[tokio::test]
    async fn test_cdc_processor_creation() {
        let config = CdcConfig::default();
        let processor = CdcProcessor::new(config);
        let metrics = processor.get_metrics().await;
        assert_eq!(metrics.events_processed, 0);
    }

    #[tokio::test]
    async fn test_single_event_processing() {
        let processor = CdcProcessor::new(CdcConfig::default());

        let event = CdcEventBuilder::new(create_test_source(), CdcOperation::Insert)
            .after(HashMap::from([
                ("id".to_string(), serde_json::json!(1)),
                ("name".to_string(), serde_json::json!("Alice")),
            ]))
            .build();

        let result = processor.process_event(event).await.unwrap();
        assert_eq!(result.len(), 1);

        let metrics = processor.get_metrics().await;
        assert_eq!(metrics.events_processed, 1);
        assert_eq!(metrics.inserts, 1);
    }

    #[tokio::test]
    async fn test_transaction_assembly() {
        let processor = CdcProcessor::new(CdcConfig::default());

        // Event 1 in transaction
        let event1 = CdcEventBuilder::new(create_test_source(), CdcOperation::Insert)
            .transaction("tx123".to_string(), 1)
            .after(HashMap::from([("id".to_string(), serde_json::json!(1))]))
            .build();

        // Event 2 in same transaction
        let event2 = CdcEventBuilder::new(create_test_source(), CdcOperation::Update)
            .transaction("tx123".to_string(), 2)
            .before(HashMap::from([("id".to_string(), serde_json::json!(1))]))
            .after(HashMap::from([
                ("id".to_string(), serde_json::json!(1)),
                ("status".to_string(), serde_json::json!("active")),
            ]))
            .build();

        // Process events - should buffer them
        let result1 = processor.process_event(event1).await.unwrap();
        let result2 = processor.process_event(event2).await.unwrap();

        assert_eq!(result1.len(), 0); // Buffered
        assert_eq!(result2.len(), 0); // Buffered

        // Commit transaction
        let committed = processor.commit_transaction("tx123").await.unwrap();
        assert_eq!(committed.len(), 2);

        let metrics = processor.get_metrics().await;
        assert_eq!(metrics.transactions_committed, 1);
        assert_eq!(metrics.avg_transaction_size, 2.0);
    }

    #[tokio::test]
    async fn test_deduplication() {
        let processor = CdcProcessor::new(CdcConfig {
            enable_deduplication: true,
            dedup_window_size: 100,
            detect_transactions: false,
            ..Default::default()
        });

        let event = CdcEventBuilder::new(create_test_source(), CdcOperation::Insert)
            .after(HashMap::from([("id".to_string(), serde_json::json!(1))]))
            .build();

        // First processing
        let result1 = processor.process_event(event.clone()).await.unwrap();
        assert_eq!(result1.len(), 1);

        // Duplicate - should be filtered
        let result2 = processor.process_event(event).await.unwrap();
        assert_eq!(result2.len(), 0);

        let metrics = processor.get_metrics().await;
        assert_eq!(metrics.deduplicated_events, 1);
    }

    #[tokio::test]
    async fn test_schema_version_tracking() {
        let processor = CdcProcessor::new(CdcConfig {
            track_schema_evolution: true,
            ..Default::default()
        });

        let source = create_test_source();

        let schema_event = CdcEventBuilder::new(source.clone(), CdcOperation::SchemaChange)
            .schema_version(2)
            .build();

        processor.process_event(schema_event).await.unwrap();

        let metrics = processor.get_metrics().await;
        assert_eq!(metrics.schema_changes_detected, 1);
    }

    #[tokio::test]
    async fn test_transaction_rollback() {
        let processor = CdcProcessor::new(CdcConfig::default());

        let event = CdcEventBuilder::new(create_test_source(), CdcOperation::Insert)
            .transaction("tx456".to_string(), 1)
            .after(HashMap::from([("id".to_string(), serde_json::json!(1))]))
            .build();

        processor.process_event(event).await.unwrap();
        processor.rollback_transaction("tx456").await.unwrap();

        let metrics = processor.get_metrics().await;
        assert_eq!(metrics.transactions_rolled_back, 1);
        assert_eq!(metrics.transactions_committed, 0);
    }

    #[tokio::test]
    async fn test_event_builder() {
        let source = create_test_source();
        let event = CdcEventBuilder::new(source.clone(), CdcOperation::Update)
            .before(HashMap::from([(
                "status".to_string(),
                serde_json::json!("inactive"),
            )]))
            .after(HashMap::from([(
                "status".to_string(),
                serde_json::json!("active"),
            )]))
            .transaction("tx789".to_string(), 5)
            .position("mysql-bin.000001:1234".to_string())
            .schema_version(3)
            .metadata("connector".to_string(), "debezium".to_string())
            .build();

        assert_eq!(event.source, source);
        assert_eq!(event.operation, CdcOperation::Update);
        assert!(event.before.is_some());
        assert!(event.after.is_some());
        assert_eq!(event.transaction_id, Some("tx789".to_string()));
        assert_eq!(event.sequence, Some(5));
        assert_eq!(event.position, Some("mysql-bin.000001:1234".to_string()));
        assert_eq!(event.schema_version, Some(3));
        assert_eq!(
            event.metadata.get("connector"),
            Some(&"debezium".to_string())
        );
    }

    #[tokio::test]
    async fn test_json_conversion() {
        let cdc_event = CdcEventBuilder::new(create_test_source(), CdcOperation::Insert)
            .after(HashMap::from([("id".to_string(), serde_json::json!(1))]))
            .build();

        let json_data = CdcProcessor::to_custom_event_data(&cdc_event);
        assert!(json_data.is_object());

        let converted_back = CdcProcessor::from_json(&json_data).unwrap();
        assert_eq!(converted_back.id, cdc_event.id);
        assert_eq!(converted_back.operation, cdc_event.operation);
    }
}