oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
//! Stream processing pipelines for real-time analytics

use async_trait::async_trait;
use std::{
    collections::{HashMap, VecDeque},
    sync::Arc,
    time::{Duration, Instant},
};
use tokio::sync::RwLock;

use crate::{
    error::Result,
    streaming::{PipelineConfig, RDFEvent},
};

/// Type alias for aggregation function
type AggregationFn = Box<dyn Fn(&[RDFEvent]) -> Option<RDFEvent> + Send + Sync>;

/// Pipeline stage for processing events
#[async_trait]
pub trait PipelineStage: Send + Sync {
    /// Process an event and optionally produce output events
    async fn process(&self, event: RDFEvent) -> Result<Vec<RDFEvent>>;

    /// Get stage name for monitoring
    fn name(&self) -> &str;
}

/// Window types for time-based aggregation
#[derive(Debug, Clone)]
pub enum WindowType {
    /// Fixed time windows (e.g., every 5 minutes)
    Fixed(Duration),
    /// Sliding windows with size and slide interval
    Sliding { size: Duration, slide: Duration },
    /// Session windows with gap duration
    Session(Duration),
}

/// Time window for event aggregation
#[derive(Debug, Clone)]
pub struct TimeWindow {
    pub start: Instant,
    pub end: Instant,
    pub events: Vec<RDFEvent>,
}

impl TimeWindow {
    /// Create a new time window
    pub fn new(start: Instant, duration: Duration) -> Self {
        Self {
            start,
            end: start + duration,
            events: Vec::new(),
        }
    }

    /// Check if an event falls within this window
    pub fn contains(&self, timestamp: Instant) -> bool {
        timestamp >= self.start && timestamp < self.end
    }

    /// Add an event to the window
    pub fn add_event(&mut self, event: RDFEvent) {
        self.events.push(event);
    }
}

/// Windowed aggregation stage
pub struct WindowAggregator {
    window_type: WindowType,
    aggregation_fn: AggregationFn,
    windows: Arc<RwLock<VecDeque<TimeWindow>>>,
    watermark: Arc<RwLock<Instant>>,
}

impl WindowAggregator {
    /// Create a new window aggregator
    pub fn new<F>(window_type: WindowType, aggregation_fn: F) -> Self
    where
        F: Fn(&[RDFEvent]) -> Option<RDFEvent> + Send + Sync + 'static,
    {
        Self {
            window_type,
            aggregation_fn: Box::new(aggregation_fn),
            windows: Arc::new(RwLock::new(VecDeque::new())),
            watermark: Arc::new(RwLock::new(Instant::now())),
        }
    }

    /// Update watermark and emit completed windows
    async fn update_watermark(&self, event_time: Instant) -> Vec<RDFEvent> {
        let mut watermark = self.watermark.write().await;
        *watermark = (*watermark).max(event_time);

        let mut results = Vec::new();
        let mut windows = self.windows.write().await;

        // Check for completed windows
        while let Some(window) = windows.front() {
            if window.end <= *watermark {
                let completed = windows
                    .pop_front()
                    .expect("window should exist after front() check");
                if let Some(result) = (self.aggregation_fn)(&completed.events) {
                    results.push(result);
                }
            } else {
                break;
            }
        }

        results
    }

    /// Get or create window for a timestamp
    async fn get_window(&self, timestamp: Instant) -> Result<()> {
        let mut windows = self.windows.write().await;

        match &self.window_type {
            WindowType::Fixed(duration) => {
                // Calculate window start
                let window_start = timestamp
                    - Duration::from_millis(
                        timestamp.duration_since(Instant::now()).as_millis() as u64
                            % duration.as_millis() as u64,
                    );

                // Check if window exists
                let exists = windows.iter().any(|w| w.contains(timestamp));
                if !exists {
                    windows.push_back(TimeWindow::new(window_start, *duration));
                }
            }
            WindowType::Sliding { size, slide } => {
                // Create multiple overlapping windows
                let mut window_start = timestamp - *size;
                while window_start <= timestamp {
                    let exists = windows
                        .iter()
                        .any(|w| w.start == window_start && w.contains(timestamp));
                    if !exists {
                        windows.push_back(TimeWindow::new(window_start, *size));
                    }
                    window_start += *slide;
                }
            }
            WindowType::Session(_gap) => {
                // Session windows are created dynamically
                // TODO: Implement session window logic
            }
        }

        Ok(())
    }
}

#[async_trait]
impl PipelineStage for WindowAggregator {
    async fn process(&self, event: RDFEvent) -> Result<Vec<RDFEvent>> {
        let event_time = Instant::now(); // TODO: Extract actual event time

        // Create window if needed
        self.get_window(event_time).await?;

        // Add event to appropriate windows
        let mut windows = self.windows.write().await;
        for window in windows.iter_mut() {
            if window.contains(event_time) {
                window.add_event(event.clone());
            }
        }
        drop(windows);

        // Update watermark and emit results
        Ok(self.update_watermark(event_time).await)
    }

    fn name(&self) -> &str {
        "WindowAggregator"
    }
}

/// Filter stage for event filtering
pub struct FilterStage {
    name: String,
    predicate: Box<dyn Fn(&RDFEvent) -> bool + Send + Sync>,
}

impl FilterStage {
    /// Create a new filter stage
    pub fn new<F>(name: String, predicate: F) -> Self
    where
        F: Fn(&RDFEvent) -> bool + Send + Sync + 'static,
    {
        Self {
            name,
            predicate: Box::new(predicate),
        }
    }
}

#[async_trait]
impl PipelineStage for FilterStage {
    async fn process(&self, event: RDFEvent) -> Result<Vec<RDFEvent>> {
        if (self.predicate)(&event) {
            Ok(vec![event])
        } else {
            Ok(vec![])
        }
    }

    fn name(&self) -> &str {
        &self.name
    }
}

/// Transform stage for event transformation
pub struct TransformStage {
    name: String,
    transform_fn: Box<dyn Fn(RDFEvent) -> Result<RDFEvent> + Send + Sync>,
}

impl TransformStage {
    /// Create a new transform stage
    pub fn new<F>(name: String, transform_fn: F) -> Self
    where
        F: Fn(RDFEvent) -> Result<RDFEvent> + Send + Sync + 'static,
    {
        Self {
            name,
            transform_fn: Box::new(transform_fn),
        }
    }
}

#[async_trait]
impl PipelineStage for TransformStage {
    async fn process(&self, event: RDFEvent) -> Result<Vec<RDFEvent>> {
        Ok(vec![(self.transform_fn)(event)?])
    }

    fn name(&self) -> &str {
        &self.name
    }
}

/// Stream processing pipeline
pub struct StreamPipeline {
    name: String,
    stages: Vec<Box<dyn PipelineStage>>,
    #[allow(dead_code)]
    config: PipelineConfig,
    metrics: Arc<RwLock<PipelineMetrics>>,
}

/// Pipeline execution metrics
#[derive(Debug, Default, Clone)]
pub struct PipelineMetrics {
    pub events_processed: u64,
    pub events_dropped: u64,
    pub processing_time: Duration,
    pub stage_metrics: HashMap<String, StageMetrics>,
}

#[derive(Debug, Default, Clone)]
pub struct StageMetrics {
    pub events_in: u64,
    pub events_out: u64,
    pub processing_time: Duration,
    pub errors: u64,
}

impl StreamPipeline {
    /// Create a new stream pipeline
    pub fn new(name: String, config: PipelineConfig) -> Self {
        Self {
            name,
            stages: Vec::new(),
            config,
            metrics: Arc::new(RwLock::new(PipelineMetrics::default())),
        }
    }

    /// Add a stage to the pipeline
    pub fn add_stage(&mut self, stage: Box<dyn PipelineStage>) {
        self.stages.push(stage);
    }

    /// Process an event through the pipeline
    pub async fn process(&self, event: RDFEvent) -> Result<Vec<RDFEvent>> {
        let start = Instant::now();
        let mut current_events = vec![event];
        let mut metrics = self.metrics.write().await;

        for stage in &self.stages {
            let stage_start = Instant::now();
            let mut next_events = Vec::new();

            let stage_name = stage.name().to_string();
            let stage_metrics = metrics
                .stage_metrics
                .entry(stage_name.clone())
                .or_insert_with(StageMetrics::default);

            stage_metrics.events_in += current_events.len() as u64;

            for event in current_events {
                match stage.process(event).await {
                    Ok(outputs) => {
                        next_events.extend(outputs);
                    }
                    Err(e) => {
                        tracing::error!("Pipeline stage {} error: {}", stage.name(), e);
                        stage_metrics.errors += 1;
                    }
                }
            }

            stage_metrics.events_out += next_events.len() as u64;
            stage_metrics.processing_time += stage_start.elapsed();

            current_events = next_events;
            if current_events.is_empty() {
                break;
            }
        }

        metrics.events_processed += 1;
        if current_events.is_empty() {
            metrics.events_dropped += 1;
        }
        metrics.processing_time += start.elapsed();

        Ok(current_events)
    }

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

/// Pipeline builder for constructing processing pipelines
pub struct PipelineBuilder {
    name: String,
    config: PipelineConfig,
    stages: Vec<Box<dyn PipelineStage>>,
}

impl PipelineBuilder {
    /// Create a new pipeline builder
    pub fn new(name: String) -> Self {
        Self {
            name,
            config: PipelineConfig::default(),
            stages: Vec::new(),
        }
    }

    /// Set pipeline configuration
    pub fn with_config(mut self, config: PipelineConfig) -> Self {
        self.config = config;
        self
    }

    /// Add a filter stage
    pub fn filter<F>(mut self, name: &str, predicate: F) -> Self
    where
        F: Fn(&RDFEvent) -> bool + Send + Sync + 'static,
    {
        self.stages
            .push(Box::new(FilterStage::new(name.to_string(), predicate)));
        self
    }

    /// Add a transform stage
    pub fn transform<F>(mut self, name: &str, transform_fn: F) -> Self
    where
        F: Fn(RDFEvent) -> Result<RDFEvent> + Send + Sync + 'static,
    {
        self.stages.push(Box::new(TransformStage::new(
            name.to_string(),
            transform_fn,
        )));
        self
    }

    /// Add a window aggregation stage
    pub fn window<F>(mut self, window_type: WindowType, aggregation_fn: F) -> Self
    where
        F: Fn(&[RDFEvent]) -> Option<RDFEvent> + Send + Sync + 'static,
    {
        self.stages
            .push(Box::new(WindowAggregator::new(window_type, aggregation_fn)));
        self
    }

    /// Build the pipeline
    pub fn build(self) -> StreamPipeline {
        let mut pipeline = StreamPipeline::new(self.name, self.config);
        for stage in self.stages {
            pipeline.add_stage(stage);
        }
        pipeline
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use oxirs_core::{Literal, NamedNode};

    #[tokio::test]
    async fn test_filter_stage() {
        let stage = FilterStage::new("test_filter".to_string(), |event| {
            matches!(event, RDFEvent::GraphCleared { .. })
        });

        let event1 = RDFEvent::GraphCleared {
            graph: "test".to_string(),
            timestamp: 12345,
        };

        let event2 = RDFEvent::TripleAdded {
            triple: oxirs_core::Triple::new(
                NamedNode::new("http://example.org/subject").unwrap(),
                NamedNode::new("http://example.org/predicate").unwrap(),
                Literal::new("object"),
            ),
            graph: None,
            timestamp: 12345,
        };

        let result1 = stage.process(event1).await.unwrap();
        assert_eq!(result1.len(), 1);

        let result2 = stage.process(event2).await.unwrap();
        assert_eq!(result2.len(), 0);
    }

    #[test]
    fn test_pipeline_builder() {
        let pipeline = PipelineBuilder::new("test_pipeline".to_string())
            .filter("graph_events", |event| {
                matches!(event, RDFEvent::GraphCleared { .. })
            })
            .transform("add_metadata", |event| {
                // Add metadata to event
                Ok(event)
            })
            .build();

        assert_eq!(pipeline.name, "test_pipeline");
        assert_eq!(pipeline.stages.len(), 2);
    }
}