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
//! # Temporal Joins for Stream Processing
//!
//! Advanced temporal join operations supporting event-time and processing-time semantics
//! with watermarks, late data handling, and various join strategies.

use anyhow::{anyhow, Result};
use chrono::{DateTime, Duration as ChronoDuration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, warn};

use crate::event::StreamEvent;

/// Temporal join configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalJoinConfig {
    /// Join type
    pub join_type: TemporalJoinType,
    /// Time semantics
    pub time_semantics: TimeSemantics,
    /// Join window configuration
    pub window: TemporalWindow,
    /// Watermark configuration
    pub watermark: WatermarkConfig,
    /// Late data handling
    pub late_data: LateDataConfig,
}

impl Default for TemporalJoinConfig {
    fn default() -> Self {
        Self {
            join_type: TemporalJoinType::Inner,
            time_semantics: TimeSemantics::EventTime,
            window: TemporalWindow::default(),
            watermark: WatermarkConfig::default(),
            late_data: LateDataConfig::default(),
        }
    }
}

/// Temporal join types
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum TemporalJoinType {
    /// Inner temporal join
    Inner,
    /// Left temporal join
    Left,
    /// Right temporal join
    Right,
    /// Full outer temporal join
    FullOuter,
    /// Interval join
    Interval,
}

/// Time semantics for temporal operations
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum TimeSemantics {
    /// Event time (based on event timestamps)
    EventTime,
    /// Processing time (based on system clock)
    ProcessingTime,
    /// Ingestion time (based on arrival time)
    IngestionTime,
}

/// Temporal window configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalWindow {
    /// Lower bound offset (negative duration before event)
    pub lower_bound: ChronoDuration,
    /// Upper bound offset (positive duration after event)
    pub upper_bound: ChronoDuration,
    /// Allow exact timestamp matches
    pub allow_exact: bool,
}

impl Default for TemporalWindow {
    fn default() -> Self {
        Self {
            lower_bound: ChronoDuration::minutes(-5),
            upper_bound: ChronoDuration::minutes(5),
            allow_exact: true,
        }
    }
}

/// Watermark configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WatermarkConfig {
    /// Watermark strategy
    pub strategy: WatermarkStrategy,
    /// Maximum allowed lateness
    pub max_lateness: ChronoDuration,
    /// Emit watermarks periodically
    pub periodic_emit: bool,
    /// Periodic emit interval
    pub emit_interval: ChronoDuration,
}

impl Default for WatermarkConfig {
    fn default() -> Self {
        Self {
            strategy: WatermarkStrategy::BoundedOutOfOrder {
                max_delay: ChronoDuration::seconds(10),
            },
            max_lateness: ChronoDuration::minutes(1),
            periodic_emit: true,
            emit_interval: ChronoDuration::seconds(1),
        }
    }
}

/// Watermark strategies
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum WatermarkStrategy {
    /// Ascending timestamps (no out-of-order)
    Ascending,
    /// Bounded out-of-order with maximum delay
    BoundedOutOfOrder { max_delay: ChronoDuration },
    /// Periodic watermarks
    Periodic { interval: ChronoDuration },
    /// Custom watermark generator
    Custom,
}

/// Late data handling configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LateDataConfig {
    /// Strategy for handling late data
    pub strategy: LateDataStrategy,
    /// Side output for late data
    pub side_output_enabled: bool,
}

impl Default for LateDataConfig {
    fn default() -> Self {
        Self {
            strategy: LateDataStrategy::Drop,
            side_output_enabled: true,
        }
    }
}

/// Late data strategies
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum LateDataStrategy {
    /// Drop late data
    Drop,
    /// Emit late data with special marker
    EmitWithMarker,
    /// Reprocess affected windows
    ReprocessWindows,
}

/// Temporal join operator
pub struct TemporalJoin {
    config: TemporalJoinConfig,
    left_buffer: Arc<RwLock<EventBuffer>>,
    right_buffer: Arc<RwLock<EventBuffer>>,
    watermarks: Arc<RwLock<Watermarks>>,
    metrics: Arc<RwLock<TemporalJoinMetrics>>,
}

/// Event buffer for temporal join
#[derive(Debug)]
struct EventBuffer {
    events: VecDeque<TimestampedEvent>,
    max_size: usize,
}

/// Timestamped event wrapper
#[derive(Debug, Clone)]
struct TimestampedEvent {
    event: StreamEvent,
    event_time: DateTime<Utc>,
    processing_time: DateTime<Utc>,
}

impl EventBuffer {
    fn new(max_size: usize) -> Self {
        Self {
            events: VecDeque::new(),
            max_size,
        }
    }

    fn add_event(&mut self, event: TimestampedEvent) {
        if self.events.len() >= self.max_size {
            self.events.pop_front();
        }
        self.events.push_back(event);
    }

    fn get_events_in_window(
        &self,
        timestamp: DateTime<Utc>,
        window: &TemporalWindow,
    ) -> Vec<TimestampedEvent> {
        let lower = timestamp + window.lower_bound;
        let upper = timestamp + window.upper_bound;

        self.events
            .iter()
            .filter(|e| {
                let t = e.event_time;
                (t > lower && t < upper) || (window.allow_exact && t == timestamp)
            })
            .cloned()
            .collect()
    }

    fn purge_before_watermark(&mut self, watermark: DateTime<Utc>) {
        while let Some(event) = self.events.front() {
            if event.event_time < watermark {
                self.events.pop_front();
            } else {
                break;
            }
        }
    }
}

/// Watermark tracking
#[derive(Debug, Clone)]
struct Watermarks {
    left_watermark: Option<DateTime<Utc>>,
    right_watermark: Option<DateTime<Utc>>,
}

impl Watermarks {
    fn new() -> Self {
        Self {
            left_watermark: None,
            right_watermark: None,
        }
    }

    fn update_left(&mut self, watermark: DateTime<Utc>) {
        self.left_watermark = Some(watermark);
    }

    fn update_right(&mut self, watermark: DateTime<Utc>) {
        self.right_watermark = Some(watermark);
    }

    fn min_watermark(&self) -> Option<DateTime<Utc>> {
        match (self.left_watermark, self.right_watermark) {
            (Some(l), Some(r)) => Some(l.min(r)),
            (Some(l), None) => Some(l),
            (None, Some(r)) => Some(r),
            (None, None) => None,
        }
    }
}

/// Temporal join metrics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TemporalJoinMetrics {
    /// Total left events processed
    pub left_events_processed: u64,
    /// Total right events processed
    pub right_events_processed: u64,
    /// Total join matches
    pub join_matches: u64,
    /// Late events dropped
    pub late_events_dropped: u64,
    /// Watermarks emitted
    pub watermarks_emitted: u64,
    /// Average join latency (ms)
    pub avg_join_latency_ms: f64,
}

impl TemporalJoin {
    /// Create a new temporal join operator
    pub fn new(config: TemporalJoinConfig) -> Self {
        Self {
            config,
            left_buffer: Arc::new(RwLock::new(EventBuffer::new(10000))),
            right_buffer: Arc::new(RwLock::new(EventBuffer::new(10000))),
            watermarks: Arc::new(RwLock::new(Watermarks::new())),
            metrics: Arc::new(RwLock::new(TemporalJoinMetrics::default())),
        }
    }

    /// Process left stream event
    pub async fn process_left(&self, event: StreamEvent) -> Result<Vec<JoinResult>> {
        let start_time = std::time::Instant::now();

        let timestamped = self.create_timestamped_event(event).await?;

        // Check for late data
        if self.is_late_event(&timestamped, true).await {
            return self.handle_late_event(timestamped, true).await;
        }

        // Add to buffer
        self.left_buffer
            .write()
            .await
            .add_event(timestamped.clone());

        // Perform join
        let results = self.join_with_right(&timestamped).await?;

        // Update metrics
        {
            let mut metrics = self.metrics.write().await;
            metrics.left_events_processed += 1;
            metrics.join_matches += results.len() as u64;
            let latency = start_time.elapsed().as_millis() as f64;
            metrics.avg_join_latency_ms = (metrics.avg_join_latency_ms + latency) / 2.0;
        }

        // Update watermark
        self.update_watermark(&timestamped, true).await;

        debug!("Processed left event, found {} matches", results.len());
        Ok(results)
    }

    /// Process right stream event
    pub async fn process_right(&self, event: StreamEvent) -> Result<Vec<JoinResult>> {
        let start_time = std::time::Instant::now();

        let timestamped = self.create_timestamped_event(event).await?;

        // Check for late data
        if self.is_late_event(&timestamped, false).await {
            return self.handle_late_event(timestamped, false).await;
        }

        // Add to buffer
        self.right_buffer
            .write()
            .await
            .add_event(timestamped.clone());

        // Perform join
        let results = self.join_with_left(&timestamped).await?;

        // Update metrics
        {
            let mut metrics = self.metrics.write().await;
            metrics.right_events_processed += 1;
            metrics.join_matches += results.len() as u64;
            let latency = start_time.elapsed().as_millis() as f64;
            metrics.avg_join_latency_ms = (metrics.avg_join_latency_ms + latency) / 2.0;
        }

        // Update watermark
        self.update_watermark(&timestamped, false).await;

        debug!("Processed right event, found {} matches", results.len());
        Ok(results)
    }

    /// Create timestamped event
    async fn create_timestamped_event(&self, event: StreamEvent) -> Result<TimestampedEvent> {
        let event_time = match self.config.time_semantics {
            TimeSemantics::EventTime => self.extract_event_time(&event)?,
            TimeSemantics::ProcessingTime => Utc::now(),
            TimeSemantics::IngestionTime => Utc::now(),
        };

        Ok(TimestampedEvent {
            event,
            event_time,
            processing_time: Utc::now(),
        })
    }

    /// Extract event time from event
    fn extract_event_time(&self, event: &StreamEvent) -> Result<DateTime<Utc>> {
        match event {
            StreamEvent::TripleAdded { metadata, .. } => Ok(metadata.timestamp),
            StreamEvent::TripleRemoved { metadata, .. } => Ok(metadata.timestamp),
            StreamEvent::GraphCreated { metadata, .. } => Ok(metadata.timestamp),
            StreamEvent::GraphDeleted { metadata, .. } => Ok(metadata.timestamp),
            StreamEvent::TransactionBegin { metadata, .. } => Ok(metadata.timestamp),
            StreamEvent::TransactionCommit { metadata, .. } => Ok(metadata.timestamp),
            StreamEvent::TransactionAbort { metadata, .. } => Ok(metadata.timestamp),
            _ => Err(anyhow!("Cannot extract event time from event")),
        }
    }

    /// Check if event is late
    async fn is_late_event(&self, event: &TimestampedEvent, is_left: bool) -> bool {
        let watermarks = self.watermarks.read().await;
        let watermark = if is_left {
            watermarks.left_watermark
        } else {
            watermarks.right_watermark
        };

        if let Some(wm) = watermark {
            event.event_time < wm - self.config.watermark.max_lateness
        } else {
            false
        }
    }

    /// Handle late event
    async fn handle_late_event(
        &self,
        _event: TimestampedEvent,
        _is_left: bool,
    ) -> Result<Vec<JoinResult>> {
        match self.config.late_data.strategy {
            LateDataStrategy::Drop => {
                self.metrics.write().await.late_events_dropped += 1;
                warn!("Dropped late event");
                Ok(Vec::new())
            }
            LateDataStrategy::EmitWithMarker => {
                // Emit with late marker
                Ok(Vec::new())
            }
            LateDataStrategy::ReprocessWindows => {
                // Reprocess affected windows
                Ok(Vec::new())
            }
        }
    }

    /// Join with right buffer
    async fn join_with_right(&self, left_event: &TimestampedEvent) -> Result<Vec<JoinResult>> {
        let right_buffer = self.right_buffer.read().await;
        let matches = right_buffer.get_events_in_window(left_event.event_time, &self.config.window);

        let results = matches
            .into_iter()
            .map(|right_event| JoinResult {
                left_event: left_event.event.clone(),
                right_event: Some(right_event.event),
                join_time: Utc::now(),
                time_diff: (right_event.event_time - left_event.event_time).num_milliseconds(),
            })
            .collect();

        Ok(results)
    }

    /// Join with left buffer
    async fn join_with_left(&self, right_event: &TimestampedEvent) -> Result<Vec<JoinResult>> {
        let left_buffer = self.left_buffer.read().await;
        let matches = left_buffer.get_events_in_window(right_event.event_time, &self.config.window);

        let results = matches
            .into_iter()
            .map(|left_event| JoinResult {
                left_event: left_event.event,
                right_event: Some(right_event.event.clone()),
                join_time: Utc::now(),
                time_diff: (right_event.event_time - left_event.event_time).num_milliseconds(),
            })
            .collect();

        Ok(results)
    }

    /// Update watermark
    async fn update_watermark(&self, event: &TimestampedEvent, is_left: bool) {
        let watermark = match self.config.watermark.strategy {
            WatermarkStrategy::Ascending => event.event_time,
            WatermarkStrategy::BoundedOutOfOrder { max_delay } => event.event_time - max_delay,
            WatermarkStrategy::Periodic { .. } => {
                // Handled by periodic task
                return;
            }
            WatermarkStrategy::Custom => {
                // Custom logic
                event.event_time
            }
        };

        let mut watermarks = self.watermarks.write().await;
        if is_left {
            watermarks.update_left(watermark);
        } else {
            watermarks.update_right(watermark);
        }

        self.metrics.write().await.watermarks_emitted += 1;

        // Purge old events
        if let Some(min_wm) = watermarks.min_watermark() {
            drop(watermarks);
            self.left_buffer
                .write()
                .await
                .purge_before_watermark(min_wm);
            self.right_buffer
                .write()
                .await
                .purge_before_watermark(min_wm);
        }
    }

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

/// Join result
#[derive(Debug, Clone)]
pub struct JoinResult {
    /// Left stream event
    pub left_event: StreamEvent,
    /// Right stream event (None for outer joins)
    pub right_event: Option<StreamEvent>,
    /// Join timestamp
    pub join_time: DateTime<Utc>,
    /// Time difference between events (milliseconds)
    pub time_diff: i64,
}

/// Interval join operator for asymmetric temporal joins
pub struct IntervalJoin {
    config: TemporalJoinConfig,
    join: TemporalJoin,
}

impl IntervalJoin {
    /// Create a new interval join
    pub fn new(config: TemporalJoinConfig) -> Self {
        let mut join_config = config.clone();
        join_config.join_type = TemporalJoinType::Interval;

        Self {
            config,
            join: TemporalJoin::new(join_config),
        }
    }

    /// Process event with interval constraints
    pub async fn process(
        &self,
        left_event: StreamEvent,
        right_event: StreamEvent,
    ) -> Result<Vec<JoinResult>> {
        // Process both events
        let left_results = self.join.process_left(left_event).await?;
        let right_results = self.join.process_right(right_event).await?;

        // Combine results
        let mut all_results = left_results;
        all_results.extend(right_results);

        Ok(all_results)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event::EventMetadata;
    use std::collections::HashMap;

    #[tokio::test]
    async fn test_temporal_join_creation() {
        let config = TemporalJoinConfig::default();
        let join = TemporalJoin::new(config);
        let metrics = join.get_metrics().await;
        assert_eq!(metrics.left_events_processed, 0);
    }

    #[tokio::test]
    async fn test_event_buffer() {
        let mut buffer = EventBuffer::new(100);
        let metadata = EventMetadata {
            event_id: "test".to_string(),
            timestamp: Utc::now(),
            source: "test".to_string(),
            user: None,
            context: None,
            caused_by: None,
            version: "1.0".to_string(),
            properties: HashMap::new(),
            checksum: None,
        };

        let event = TimestampedEvent {
            event: StreamEvent::GraphCreated {
                graph: "test".to_string(),
                metadata,
            },
            event_time: Utc::now(),
            processing_time: Utc::now(),
        };

        buffer.add_event(event);
        assert_eq!(buffer.events.len(), 1);
    }

    #[tokio::test]
    async fn test_watermark_strategy() {
        let strategy = WatermarkStrategy::BoundedOutOfOrder {
            max_delay: ChronoDuration::seconds(5),
        };

        match strategy {
            WatermarkStrategy::BoundedOutOfOrder { max_delay } => {
                assert_eq!(max_delay, ChronoDuration::seconds(5));
            }
            _ => panic!("Wrong strategy"),
        }
    }
}