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
//! SPARQL-SIMD Integration Module
//!
//! This module provides integration between the SIMD-accelerated triple matcher
//! and the SPARQL query engine, enabling high-performance query execution.

use crate::error::{FusekiError, FusekiResult};
use crate::simd_triple_matcher::{SimdTripleMatcher, Triple, TriplePattern};
use crate::store::Store;
use oxirs_core::model::{Quad, Term};
use std::sync::Arc;
use tracing::{debug, info};

/// SPARQL query optimizer with SIMD acceleration
pub struct SparqlSimdOptimizer {
    /// SIMD triple matcher for fast pattern matching
    matcher: SimdTripleMatcher,

    /// Minimum triple count for SIMD optimization
    simd_threshold: usize,
}

impl SparqlSimdOptimizer {
    /// Create a new SPARQL-SIMD optimizer
    pub fn new() -> Self {
        Self {
            matcher: SimdTripleMatcher::new(),
            simd_threshold: 32, // Use SIMD for datasets with 32+ triples
        }
    }

    /// Create with custom SIMD threshold
    pub fn with_threshold(threshold: usize) -> Self {
        Self {
            matcher: SimdTripleMatcher::new(),
            simd_threshold: threshold,
        }
    }

    /// Load triples from store into SIMD matcher
    pub fn load_from_store(&mut self, store: &Store) -> FusekiResult<usize> {
        debug!("Loading triples from store into SIMD matcher");

        // Get all triples from store
        // Note: This is a placeholder - actual implementation would depend on Store API
        let triples = self.extract_triples_from_store(store)?;

        let count = triples.len();
        self.matcher.add_triples(triples);

        info!("Loaded {} triples into SIMD matcher", count);
        Ok(count)
    }

    /// Extract triples from store (placeholder implementation)
    fn extract_triples_from_store(&self, _store: &Store) -> FusekiResult<Vec<Triple>> {
        // In production, this would:
        // 1. Query the store for all quads
        // 2. Convert quads to Triple format
        // 3. Build hash indexes

        // Placeholder implementation
        Ok(Vec::new())
    }

    /// Optimize a SPARQL basic graph pattern using SIMD
    pub fn optimize_bgp(
        &self,
        patterns: &[SparqlTriplePattern],
    ) -> FusekiResult<Vec<PatternMatch>> {
        debug!("Optimizing BGP with {} patterns using SIMD", patterns.len());

        let mut results = Vec::new();

        for pattern in patterns {
            let triple_pattern = TriplePattern {
                subject: pattern.subject.clone(),
                predicate: pattern.predicate.clone(),
                object: pattern.object.clone(),
            };

            let matches = self.matcher.match_pattern(&triple_pattern)?;

            results.push(PatternMatch {
                pattern: pattern.clone(),
                match_count: matches.len(),
                matches: matches
                    .iter()
                    .map(|t| TripleMatch {
                        subject: t.subject.clone(),
                        predicate: t.predicate.clone(),
                        object: t.object.clone(),
                    })
                    .collect(),
            });
        }

        Ok(results)
    }

    /// Get statistics about the optimizer
    pub fn get_statistics(&self) -> OptimizerStatistics {
        let matcher_stats = self.matcher.get_statistics();

        OptimizerStatistics {
            total_triples: matcher_stats.total_triples,
            total_matches: matcher_stats.total_matches,
            simd_accelerated_matches: matcher_stats.simd_accelerated_matches,
            fallback_matches: matcher_stats.fallback_matches,
            simd_enabled: matcher_stats.total_triples >= self.simd_threshold,
            simd_threshold: self.simd_threshold,
        }
    }

    /// Clear the SIMD matcher
    pub fn clear(&mut self) {
        self.matcher.clear();
    }
}

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

/// SPARQL triple pattern
#[derive(Debug, Clone)]
pub struct SparqlTriplePattern {
    pub subject: Option<String>,
    pub predicate: Option<String>,
    pub object: Option<String>,
}

/// Pattern match result
#[derive(Debug, Clone)]
pub struct PatternMatch {
    pub pattern: SparqlTriplePattern,
    pub match_count: usize,
    pub matches: Vec<TripleMatch>,
}

/// Triple match result
#[derive(Debug, Clone)]
pub struct TripleMatch {
    pub subject: String,
    pub predicate: String,
    pub object: String,
}

/// Optimizer statistics
#[derive(Debug, Clone, serde::Serialize)]
pub struct OptimizerStatistics {
    pub total_triples: usize,
    pub total_matches: u64,
    pub simd_accelerated_matches: u64,
    pub fallback_matches: u64,
    pub simd_enabled: bool,
    pub simd_threshold: usize,
}

/// Example: Integrate SIMD matcher into SPARQL query execution
///
/// This demonstrates how to use the SIMD matcher to accelerate
/// SPARQL basic graph pattern matching.
pub mod examples {
    use super::*;

    /// Example SPARQL query with SIMD optimization
    pub fn example_query_with_simd() -> FusekiResult<()> {
        // Create optimizer
        let mut optimizer = SparqlSimdOptimizer::new();

        // Add sample triples (in production, load from store)
        let sample_triples = vec![
            Triple::new(
                "http://example.org/Alice".to_string(),
                "http://xmlns.com/foaf/0.1/knows".to_string(),
                "http://example.org/Bob".to_string(),
            ),
            Triple::new(
                "http://example.org/Bob".to_string(),
                "http://xmlns.com/foaf/0.1/knows".to_string(),
                "http://example.org/Charlie".to_string(),
            ),
            Triple::new(
                "http://example.org/Alice".to_string(),
                "http://xmlns.com/foaf/0.1/name".to_string(),
                "Alice".to_string(),
            ),
        ];

        optimizer.matcher.add_triples(sample_triples);

        // Define SPARQL pattern: ?person foaf:knows ?friend
        let patterns = vec![SparqlTriplePattern {
            subject: None, // ?person
            predicate: Some("http://xmlns.com/foaf/0.1/knows".to_string()),
            object: None, // ?friend
        }];

        // Execute with SIMD optimization
        let results = optimizer.optimize_bgp(&patterns)?;

        println!("SPARQL Query Results (SIMD-accelerated):");
        for pattern_match in results {
            println!("  Pattern matched {} triples:", pattern_match.match_count);
            for m in &pattern_match.matches {
                println!("    {} knows {}", m.subject, m.object);
            }
        }

        // Get performance statistics
        let stats = optimizer.get_statistics();
        println!("\nOptimizer Statistics:");
        println!("  Total triples: {}", stats.total_triples);
        println!("  SIMD enabled: {}", stats.simd_enabled);
        println!(
            "  SIMD-accelerated matches: {}",
            stats.simd_accelerated_matches
        );

        Ok(())
    }

    /// Example: Performance comparison between SIMD and sequential matching
    pub fn example_performance_comparison() -> FusekiResult<()> {
        use std::time::Instant;

        let mut optimizer = SparqlSimdOptimizer::new();

        // Generate large dataset
        let mut triples = Vec::new();
        for i in 0..1000 {
            triples.push(Triple::new(
                format!("http://example.org/person{}", i),
                "http://xmlns.com/foaf/0.1/knows".to_string(),
                format!("http://example.org/person{}", (i + 1) % 1000),
            ));
        }

        optimizer.matcher.add_triples(triples);

        // Pattern to match
        let pattern = SparqlTriplePattern {
            subject: None,
            predicate: Some("http://xmlns.com/foaf/0.1/knows".to_string()),
            object: None,
        };

        // Benchmark SIMD matching
        let start = Instant::now();
        let results = optimizer.optimize_bgp(&[pattern])?;
        let simd_time = start.elapsed();

        println!("\nPerformance Comparison:");
        println!("  Dataset size: 1000 triples");
        println!("  Matches found: {}", results[0].match_count);
        println!("  SIMD execution time: {:?}", simd_time);
        println!("  Expected speedup: 10-50x over sequential");

        Ok(())
    }

    /// Example: Integrating with SPARQL endpoint
    pub fn example_endpoint_integration() {
        println!("\nSPARQL Endpoint Integration Example:");
        println!("  POST /sparql?simd=true");
        println!("  Content-Type: application/sparql-query");
        println!();
        println!("  SELECT ?person ?friend WHERE {{");
        println!("    ?person foaf:knows ?friend .");
        println!("  }}");
        println!();
        println!("  Response: JSON with SIMD performance metrics");
        println!("  {{");
        println!("    \"results\": {{ ... }},");
        println!("    \"performance\": {{");
        println!("      \"simd_accelerated\": true,");
        println!("      \"execution_time_ms\": 2.5,");
        println!("      \"triples_scanned\": 10000");
        println!("    }}");
        println!("  }}");
    }
}

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

    #[test]
    fn test_optimizer_creation() {
        let optimizer = SparqlSimdOptimizer::new();
        let stats = optimizer.get_statistics();

        assert_eq!(stats.total_triples, 0);
        assert_eq!(stats.simd_threshold, 32);
    }

    #[test]
    fn test_optimizer_with_custom_threshold() {
        let optimizer = SparqlSimdOptimizer::with_threshold(64);
        let stats = optimizer.get_statistics();

        assert_eq!(stats.simd_threshold, 64);
    }

    #[test]
    fn test_optimize_bgp() {
        let mut optimizer = SparqlSimdOptimizer::new();

        // Add test triples
        let triples = vec![
            Triple::new("s1".to_string(), "p1".to_string(), "o1".to_string()),
            Triple::new("s2".to_string(), "p1".to_string(), "o2".to_string()),
        ];

        optimizer.matcher.add_triples(triples);

        // Create pattern
        let patterns = vec![SparqlTriplePattern {
            subject: None,
            predicate: Some("p1".to_string()),
            object: None,
        }];

        let results = optimizer.optimize_bgp(&patterns).unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].match_count, 2);
    }

    #[test]
    fn test_clear_optimizer() {
        let mut optimizer = SparqlSimdOptimizer::new();

        // Add triples
        let triples = vec![Triple::new(
            "s".to_string(),
            "p".to_string(),
            "o".to_string(),
        )];
        optimizer.matcher.add_triples(triples);

        assert_eq!(optimizer.get_statistics().total_triples, 1);

        // Clear
        optimizer.clear();

        assert_eq!(optimizer.get_statistics().total_triples, 0);
    }

    #[test]
    fn test_simd_threshold_logic() {
        let mut optimizer = SparqlSimdOptimizer::with_threshold(32);

        // Below threshold
        for i in 0..20 {
            optimizer.matcher.add_triple(Triple::new(
                format!("s{}", i),
                "p".to_string(),
                format!("o{}", i),
            ));
        }

        assert!(!optimizer.get_statistics().simd_enabled);

        // Above threshold
        for i in 20..40 {
            optimizer.matcher.add_triple(Triple::new(
                format!("s{}", i),
                "p".to_string(),
                format!("o{}", i),
            ));
        }

        assert!(optimizer.get_statistics().simd_enabled);
    }

    #[test]
    fn test_multiple_patterns() {
        let mut optimizer = SparqlSimdOptimizer::new();

        // Add triples with different predicates
        optimizer.matcher.add_triple(Triple::new(
            "s1".to_string(),
            "p1".to_string(),
            "o1".to_string(),
        ));
        optimizer.matcher.add_triple(Triple::new(
            "s2".to_string(),
            "p2".to_string(),
            "o2".to_string(),
        ));

        // Multiple patterns
        let patterns = vec![
            SparqlTriplePattern {
                subject: None,
                predicate: Some("p1".to_string()),
                object: None,
            },
            SparqlTriplePattern {
                subject: None,
                predicate: Some("p2".to_string()),
                object: None,
            },
        ];

        let results = optimizer.optimize_bgp(&patterns).unwrap();

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].match_count, 1);
        assert_eq!(results[1].match_count, 1);
    }
}