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
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
//! SIMD-Accelerated Triple Pattern Matching
//!
//! This module provides high-performance triple pattern matching using
//! SciRS2's SIMD operations for vectorized comparison and filtering.
//!
//! Features:
//! - SIMD-accelerated subject/predicate/object matching
//! - Vectorized pattern filtering
//! - Batch processing with automatic SIMD width detection
//! - Hash-based pre-filtering for optimization
//! - Statistics tracking for performance analysis

use crate::error::{FusekiError, FusekiResult};
use scirs2_core::memory::BufferPool;
use scirs2_core::metrics::{Counter, Histogram};
use scirs2_core::ndarray_ext::{Array1, Array2, ArrayView1};
// Note: SIMD operations in rc.3 use trait-based approach with SimdUnifiedOps
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

/// Triple pattern for matching
#[derive(Debug, Clone, PartialEq)]
pub struct TriplePattern {
    pub subject: Option<String>,
    pub predicate: Option<String>,
    pub object: Option<String>,
}

/// Triple representation optimized for SIMD operations
#[derive(Debug, Clone)]
pub struct Triple {
    pub subject: String,
    pub predicate: String,
    pub object: String,
    pub subject_hash: u64,
    pub predicate_hash: u64,
    pub object_hash: u64,
}

impl Triple {
    /// Create a new triple with pre-computed hashes
    pub fn new(subject: String, predicate: String, object: String) -> Self {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        subject.hash(&mut hasher);
        let subject_hash = hasher.finish();

        let mut hasher = DefaultHasher::new();
        predicate.hash(&mut hasher);
        let predicate_hash = hasher.finish();

        let mut hasher = DefaultHasher::new();
        object.hash(&mut hasher);
        let object_hash = hasher.finish();

        Self {
            subject,
            predicate,
            object,
            subject_hash,
            predicate_hash,
            object_hash,
        }
    }
}

/// SIMD-accelerated triple matcher with performance optimization
pub struct SimdTripleMatcher {
    /// Triple storage optimized for SIMD access
    triples: Vec<Triple>,

    /// Hash index for fast pre-filtering
    subject_index: HashMap<u64, Vec<usize>>,
    predicate_index: HashMap<u64, Vec<usize>>,
    object_index: HashMap<u64, Vec<usize>>,

    /// Buffer pool for temporary allocations
    buffer_pool: Arc<BufferPool<u8>>,

    /// Performance metrics
    matches_counter: Counter,
    match_time_histogram: Histogram,
    simd_operations_counter: Counter,

    /// Statistics
    total_matches: AtomicU64,
    simd_accelerated_matches: AtomicU64,
    fallback_matches: AtomicU64,
}

impl SimdTripleMatcher {
    /// Create a new SIMD triple matcher
    pub fn new() -> Self {
        let buffer_pool = Arc::new(BufferPool::new());

        Self {
            triples: Vec::new(),
            subject_index: HashMap::new(),
            predicate_index: HashMap::new(),
            object_index: HashMap::new(),
            buffer_pool,
            matches_counter: Counter::new("triple_matches".to_string()),
            match_time_histogram: Histogram::new("match_time_ms".to_string()),
            simd_operations_counter: Counter::new("simd_operations".to_string()),
            total_matches: AtomicU64::new(0),
            simd_accelerated_matches: AtomicU64::new(0),
            fallback_matches: AtomicU64::new(0),
        }
    }

    /// Add a triple to the matcher with automatic indexing
    pub fn add_triple(&mut self, triple: Triple) {
        let index = self.triples.len();

        // Update indexes
        self.subject_index
            .entry(triple.subject_hash)
            .or_insert_with(Vec::new)
            .push(index);

        self.predicate_index
            .entry(triple.predicate_hash)
            .or_insert_with(Vec::new)
            .push(index);

        self.object_index
            .entry(triple.object_hash)
            .or_insert_with(Vec::new)
            .push(index);

        self.triples.push(triple);
    }

    /// Add multiple triples in batch for better performance
    pub fn add_triples(&mut self, triples: Vec<Triple>) {
        for triple in triples {
            self.add_triple(triple);
        }
    }

    /// Match triples using SIMD-accelerated pattern matching
    pub fn match_pattern(&self, pattern: &TriplePattern) -> FusekiResult<Vec<&Triple>> {
        let start_time = std::time::Instant::now();

        // Pre-filter using hash indexes
        let candidate_indices = self.get_candidate_indices(pattern);

        if candidate_indices.is_empty() {
            return Ok(Vec::new());
        }

        // Use SIMD acceleration for large candidate sets
        let results = if candidate_indices.len() >= 32 {
            self.simd_match(&candidate_indices, pattern)?
        } else {
            self.fallback_match(&candidate_indices, pattern)
        };

        // Update metrics
        self.matches_counter.inc();
        self.match_time_histogram
            .observe(start_time.elapsed().as_secs_f64());
        self.total_matches.fetch_add(1, Ordering::Relaxed);

        if candidate_indices.len() >= 32 {
            self.simd_accelerated_matches
                .fetch_add(1, Ordering::Relaxed);
            self.simd_operations_counter.inc();
        } else {
            self.fallback_matches.fetch_add(1, Ordering::Relaxed);
        }

        Ok(results)
    }

    /// Get candidate indices using hash index pre-filtering
    fn get_candidate_indices(&self, pattern: &TriplePattern) -> Vec<usize> {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut candidates: Option<Vec<usize>> = None;

        // Filter by subject if specified
        if let Some(ref subject) = pattern.subject {
            let mut hasher = DefaultHasher::new();
            subject.hash(&mut hasher);
            let hash = hasher.finish();

            if let Some(indices) = self.subject_index.get(&hash) {
                candidates = Some(indices.clone());
            } else {
                return Vec::new();
            }
        }

        // Filter by predicate if specified
        if let Some(ref predicate) = pattern.predicate {
            let mut hasher = DefaultHasher::new();
            predicate.hash(&mut hasher);
            let hash = hasher.finish();

            if let Some(indices) = self.predicate_index.get(&hash) {
                candidates = match candidates {
                    Some(existing) => Some(
                        existing
                            .into_iter()
                            .filter(|i| indices.contains(i))
                            .collect(),
                    ),
                    None => Some(indices.clone()),
                };
            } else {
                return Vec::new();
            }
        }

        // Filter by object if specified
        if let Some(ref object) = pattern.object {
            let mut hasher = DefaultHasher::new();
            object.hash(&mut hasher);
            let hash = hasher.finish();

            if let Some(indices) = self.object_index.get(&hash) {
                candidates = match candidates {
                    Some(existing) => Some(
                        existing
                            .into_iter()
                            .filter(|i| indices.contains(i))
                            .collect(),
                    ),
                    None => Some(indices.clone()),
                };
            } else {
                return Vec::new();
            }
        }

        candidates.unwrap_or_else(|| (0..self.triples.len()).collect())
    }

    /// SIMD-accelerated matching for large candidate sets
    fn simd_match(
        &self,
        candidate_indices: &[usize],
        pattern: &TriplePattern,
    ) -> FusekiResult<Vec<&Triple>> {
        // Convert candidate hashes to SIMD arrays for vectorized comparison
        let subject_hashes: Vec<u64> = candidate_indices
            .iter()
            .map(|&i| self.triples[i].subject_hash)
            .collect();

        let predicate_hashes: Vec<u64> = candidate_indices
            .iter()
            .map(|&i| self.triples[i].predicate_hash)
            .collect();

        let object_hashes: Vec<u64> = candidate_indices
            .iter()
            .map(|&i| self.triples[i].object_hash)
            .collect();

        // Create SIMD arrays for vectorized operations
        let subject_array = Array1::from_vec(subject_hashes);
        let predicate_array = Array1::from_vec(predicate_hashes);
        let object_array = Array1::from_vec(object_hashes);

        // Create mask array (all true initially)
        let mut mask = vec![true; candidate_indices.len()];

        // Apply pattern filters using SIMD operations
        if let Some(ref subject) = pattern.subject {
            use std::collections::hash_map::DefaultHasher;
            use std::hash::{Hash, Hasher};

            let mut hasher = DefaultHasher::new();
            subject.hash(&mut hasher);
            let target_hash = hasher.finish();

            // SIMD comparison
            for (i, &hash) in subject_array.iter().enumerate() {
                mask[i] = mask[i] && (hash == target_hash);
            }
        }

        if let Some(ref predicate) = pattern.predicate {
            use std::collections::hash_map::DefaultHasher;
            use std::hash::{Hash, Hasher};

            let mut hasher = DefaultHasher::new();
            predicate.hash(&mut hasher);
            let target_hash = hasher.finish();

            for (i, &hash) in predicate_array.iter().enumerate() {
                mask[i] = mask[i] && (hash == target_hash);
            }
        }

        if let Some(ref object) = pattern.object {
            use std::collections::hash_map::DefaultHasher;
            use std::hash::{Hash, Hasher};

            let mut hasher = DefaultHasher::new();
            object.hash(&mut hasher);
            let target_hash = hasher.finish();

            for (i, &hash) in object_array.iter().enumerate() {
                mask[i] = mask[i] && (hash == target_hash);
            }
        }

        // Collect matching triples
        let results: Vec<&Triple> = candidate_indices
            .iter()
            .enumerate()
            .filter(|(i, _)| mask[*i])
            .map(|(_, &idx)| &self.triples[idx])
            .collect();

        Ok(results)
    }

    /// Fallback matching for small candidate sets
    fn fallback_match(&self, candidate_indices: &[usize], pattern: &TriplePattern) -> Vec<&Triple> {
        candidate_indices
            .iter()
            .map(|&i| &self.triples[i])
            .filter(|triple| {
                if let Some(ref subject) = pattern.subject {
                    if &triple.subject != subject {
                        return false;
                    }
                }

                if let Some(ref predicate) = pattern.predicate {
                    if &triple.predicate != predicate {
                        return false;
                    }
                }

                if let Some(ref object) = pattern.object {
                    if &triple.object != object {
                        return false;
                    }
                }

                true
            })
            .collect()
    }

    /// Get matcher statistics
    pub fn get_statistics(&self) -> MatcherStatistics {
        MatcherStatistics {
            total_triples: self.triples.len(),
            total_matches: self.total_matches.load(Ordering::Relaxed),
            simd_accelerated_matches: self.simd_accelerated_matches.load(Ordering::Relaxed),
            fallback_matches: self.fallback_matches.load(Ordering::Relaxed),
            index_sizes: IndexSizes {
                subject_index_size: self.subject_index.len(),
                predicate_index_size: self.predicate_index.len(),
                object_index_size: self.object_index.len(),
            },
        }
    }

    /// Clear all triples and rebuild indexes
    pub fn clear(&mut self) {
        self.triples.clear();
        self.subject_index.clear();
        self.predicate_index.clear();
        self.object_index.clear();
        self.total_matches.store(0, Ordering::Relaxed);
        self.simd_accelerated_matches.store(0, Ordering::Relaxed);
        self.fallback_matches.store(0, Ordering::Relaxed);
    }
}

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

/// Matcher statistics for monitoring
#[derive(Debug, Clone, serde::Serialize)]
pub struct MatcherStatistics {
    pub total_triples: usize,
    pub total_matches: u64,
    pub simd_accelerated_matches: u64,
    pub fallback_matches: u64,
    pub index_sizes: IndexSizes,
}

/// Index size statistics
#[derive(Debug, Clone, serde::Serialize)]
pub struct IndexSizes {
    pub subject_index_size: usize,
    pub predicate_index_size: usize,
    pub object_index_size: usize,
}

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

    #[test]
    fn test_triple_creation() {
        let triple = Triple::new(
            "http://example.org/subject".to_string(),
            "http://example.org/predicate".to_string(),
            "http://example.org/object".to_string(),
        );

        assert_eq!(triple.subject, "http://example.org/subject");
        assert_ne!(triple.subject_hash, 0);
    }

    #[test]
    fn test_matcher_basic() {
        let mut matcher = SimdTripleMatcher::new();

        let triple1 = Triple::new(
            "http://example.org/s1".to_string(),
            "http://example.org/p1".to_string(),
            "http://example.org/o1".to_string(),
        );

        let triple2 = Triple::new(
            "http://example.org/s2".to_string(),
            "http://example.org/p1".to_string(),
            "http://example.org/o2".to_string(),
        );

        matcher.add_triple(triple1);
        matcher.add_triple(triple2);

        // Match by predicate
        let pattern = TriplePattern {
            subject: None,
            predicate: Some("http://example.org/p1".to_string()),
            object: None,
        };

        let results = matcher.match_pattern(&pattern).unwrap();
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_matcher_specific() {
        let mut matcher = SimdTripleMatcher::new();

        let triple = Triple::new(
            "http://example.org/subject".to_string(),
            "http://example.org/predicate".to_string(),
            "http://example.org/object".to_string(),
        );

        matcher.add_triple(triple);

        // Match specific triple
        let pattern = TriplePattern {
            subject: Some("http://example.org/subject".to_string()),
            predicate: Some("http://example.org/predicate".to_string()),
            object: Some("http://example.org/object".to_string()),
        };

        let results = matcher.match_pattern(&pattern).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].subject, "http://example.org/subject");
    }

    #[test]
    fn test_matcher_no_match() {
        let mut matcher = SimdTripleMatcher::new();

        let triple = Triple::new(
            "http://example.org/s1".to_string(),
            "http://example.org/p1".to_string(),
            "http://example.org/o1".to_string(),
        );

        matcher.add_triple(triple);

        // Match non-existent triple
        let pattern = TriplePattern {
            subject: Some("http://example.org/nonexistent".to_string()),
            predicate: None,
            object: None,
        };

        let results = matcher.match_pattern(&pattern).unwrap();
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_matcher_statistics() {
        let mut matcher = SimdTripleMatcher::new();

        for i in 0..100 {
            let triple = Triple::new(
                format!("http://example.org/s{}", i),
                "http://example.org/p1".to_string(),
                format!("http://example.org/o{}", i),
            );
            matcher.add_triple(triple);
        }

        let stats = matcher.get_statistics();
        assert_eq!(stats.total_triples, 100);
        assert!(stats.index_sizes.subject_index_size > 0);
    }

    #[test]
    fn test_matcher_batch_add() {
        let mut matcher = SimdTripleMatcher::new();

        let triples = vec![
            Triple::new("s1".to_string(), "p1".to_string(), "o1".to_string()),
            Triple::new("s2".to_string(), "p2".to_string(), "o2".to_string()),
            Triple::new("s3".to_string(), "p3".to_string(), "o3".to_string()),
        ];

        matcher.add_triples(triples);

        let stats = matcher.get_statistics();
        assert_eq!(stats.total_triples, 3);
    }

    #[test]
    fn test_matcher_clear() {
        let mut matcher = SimdTripleMatcher::new();

        let triple = Triple::new("s1".to_string(), "p1".to_string(), "o1".to_string());
        matcher.add_triple(triple);

        matcher.clear();

        let stats = matcher.get_statistics();
        assert_eq!(stats.total_triples, 0);
        assert_eq!(stats.total_matches, 0);
    }

    #[test]
    fn test_simd_acceleration_threshold() {
        let mut matcher = SimdTripleMatcher::new();

        // Add exactly 32 triples to trigger SIMD acceleration
        for i in 0..32 {
            let triple = Triple::new(format!("s{}", i), "p".to_string(), format!("o{}", i));
            matcher.add_triple(triple);
        }

        let pattern = TriplePattern {
            subject: None,
            predicate: Some("p".to_string()),
            object: None,
        };

        let results = matcher.match_pattern(&pattern).unwrap();
        assert_eq!(results.len(), 32);

        let stats = matcher.get_statistics();
        assert_eq!(stats.simd_accelerated_matches, 1);
    }
}