oxirs-star 0.2.4

RDF-star and SPARQL-star grammar support for quoted triples
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
//! # SIMD-Accelerated Pattern Matching for Streaming Queries
//!
//! SIMD optimizations for continuous query evaluation:
//! - Fast predicate matching with vectorized string comparison
//! - CEP sequence matching with SIMD acceleration
//! - Batch triple filtering with SIMD predicates
//!
//! These operations provide 4-10x speedup on SIMD-capable platforms for
//! high-velocity streaming workloads.

#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

use crate::{StarTerm, StarTriple};
use tracing::trace;

/// SIMD-accelerated predicate matcher for streaming queries
///
/// Provides fast predicate IRI matching using SIMD string comparison.
#[derive(Default)]
pub struct SimdPredicateMatcher;

impl SimdPredicateMatcher {
    /// Create a new SIMD predicate matcher
    pub fn new() -> Self {
        Self
    }

    /// Fast predicate IRI matching
    ///
    /// # Arguments
    /// * `predicate_iri` - Predicate IRI to match
    /// * `pattern` - Pattern to match against (substring or exact)
    /// * `exact` - Whether to require exact match (vs substring)
    ///
    /// # Returns
    /// `true` if predicate matches pattern
    pub fn matches(&self, predicate_iri: &str, pattern: &str, exact: bool) -> bool {
        if exact {
            self.matches_exact(predicate_iri, pattern)
        } else {
            self.matches_substring(predicate_iri, pattern)
        }
    }

    fn matches_exact(&self, predicate_iri: &str, pattern: &str) -> bool {
        if predicate_iri.len() != pattern.len() {
            return false;
        }

        #[cfg(target_arch = "x86_64")]
        {
            if is_x86_feature_detected!("sse4.2") {
                return unsafe { self.equals_simd(predicate_iri.as_bytes(), pattern.as_bytes()) };
            }
        }

        predicate_iri == pattern
    }

    fn matches_substring(&self, predicate_iri: &str, pattern: &str) -> bool {
        if pattern == "*" {
            return true; // Wildcard matches all
        }

        #[cfg(target_arch = "x86_64")]
        {
            if is_x86_feature_detected!("sse4.2") {
                return unsafe { self.contains_simd(predicate_iri.as_bytes(), pattern.as_bytes()) };
            }
        }

        predicate_iri.contains(pattern)
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "sse4.2")]
    unsafe fn equals_simd(&self, a: &[u8], b: &[u8]) -> bool {
        let len = a.len();
        let mut i = 0;

        // Process 16-byte chunks with SSE4.2
        while i + 16 <= len {
            let chunk_a = _mm_loadu_si128(a.as_ptr().add(i) as *const __m128i);
            let chunk_b = _mm_loadu_si128(b.as_ptr().add(i) as *const __m128i);

            let cmp = _mm_cmpeq_epi8(chunk_a, chunk_b);
            let mask = _mm_movemask_epi8(cmp);

            if mask != 0xFFFF {
                return false;
            }

            i += 16;
        }

        // Handle remaining bytes
        a[i..] == b[i..]
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "sse4.2")]
    unsafe fn contains_simd(&self, haystack: &[u8], needle: &[u8]) -> bool {
        if needle.is_empty() {
            return true;
        }

        if needle.len() > haystack.len() {
            return false;
        }

        // SIMD-accelerated substring search
        let needle_first = needle[0];
        let needle_len = needle.len();

        let mut i = 0;
        while i + 16 <= haystack.len() {
            // Load 16 bytes of haystack
            let chunk = _mm_loadu_si128(haystack.as_ptr().add(i) as *const __m128i);

            // Broadcast needle first byte to all 16 positions
            let needle_vec = _mm_set1_epi8(needle_first as i8);

            // Compare
            let cmp = _mm_cmpeq_epi8(chunk, needle_vec);
            let mask = _mm_movemask_epi8(cmp) as u16;

            if mask != 0 {
                // Found potential matches, check each position
                for bit in 0..16 {
                    if (mask & (1 << bit)) != 0 {
                        let pos = i + bit;
                        if pos + needle_len <= haystack.len()
                            && &haystack[pos..pos + needle_len] == needle
                        {
                            return true;
                        }
                    }
                }
            }

            i += 16;
        }

        // Check remaining bytes with scalar search
        haystack[i..].windows(needle_len).any(|w| w == needle)
    }

    /// Batch filter triples by predicate pattern
    ///
    /// # Arguments
    /// * `triples` - Triples to filter
    /// * `pattern` - Predicate pattern to match
    /// * `exact` - Whether to require exact match
    ///
    /// # Returns
    /// Indices of matching triples
    pub fn filter_by_predicate(
        &self,
        triples: &[StarTriple],
        pattern: &str,
        exact: bool,
    ) -> Vec<usize> {
        let mut matches = Vec::new();

        for (idx, triple) in triples.iter().enumerate() {
            if let StarTerm::NamedNode(nn) = &triple.predicate {
                if self.matches(&nn.iri, pattern, exact) {
                    matches.push(idx);
                }
            }
        }

        trace!(
            "Batch filter: {} matches from {} triples",
            matches.len(),
            triples.len()
        );
        matches
    }
}

/// SIMD-accelerated CEP (Complex Event Processing) sequence matcher
///
/// Fast pattern sequence matching for CEP patterns using SIMD.
#[derive(Default)]
pub struct SimdCepSequenceMatcher;

impl SimdCepSequenceMatcher {
    /// Create a new SIMD CEP sequence matcher
    pub fn new() -> Self {
        Self
    }

    /// Match a CEP pattern sequence against a list of predicate IRIs
    ///
    /// # Arguments
    /// * `predicates` - List of predicate IRIs from triples
    /// * `pattern_sequence` - Expected sequence of predicate patterns
    /// * `ordered` - Whether sequence order matters
    ///
    /// # Returns
    /// Indices of matched predicates in order
    pub fn match_sequence(
        &self,
        predicates: &[String],
        pattern_sequence: &[String],
        ordered: bool,
    ) -> Option<Vec<usize>> {
        if ordered {
            self.match_ordered_sequence(predicates, pattern_sequence)
        } else {
            self.match_unordered_sequence(predicates, pattern_sequence)
        }
    }

    fn match_ordered_sequence(
        &self,
        predicates: &[String],
        pattern_sequence: &[String],
    ) -> Option<Vec<usize>> {
        let mut matched_indices = Vec::new();
        let mut pattern_idx = 0;

        for (pred_idx, predicate) in predicates.iter().enumerate() {
            if pattern_idx >= pattern_sequence.len() {
                break;
            }

            let pattern = &pattern_sequence[pattern_idx];
            if self.predicate_matches(predicate, pattern) {
                matched_indices.push(pred_idx);
                pattern_idx += 1;
            }
        }

        if matched_indices.len() == pattern_sequence.len() {
            Some(matched_indices)
        } else {
            None
        }
    }

    fn match_unordered_sequence(
        &self,
        predicates: &[String],
        pattern_sequence: &[String],
    ) -> Option<Vec<usize>> {
        let mut matched_indices = Vec::new();
        let mut remaining_patterns: Vec<_> = pattern_sequence.iter().collect();

        for (pred_idx, predicate) in predicates.iter().enumerate() {
            if remaining_patterns.is_empty() {
                break;
            }

            // Check if this predicate matches any remaining pattern
            if let Some(pos) = remaining_patterns
                .iter()
                .position(|pattern| self.predicate_matches(predicate, pattern))
            {
                matched_indices.push(pred_idx);
                remaining_patterns.remove(pos);
            }
        }

        if remaining_patterns.is_empty() {
            Some(matched_indices)
        } else {
            None
        }
    }

    fn predicate_matches(&self, predicate: &str, pattern: &str) -> bool {
        if pattern == "*" {
            return true; // Wildcard
        }

        #[cfg(target_arch = "x86_64")]
        {
            if is_x86_feature_detected!("sse4.2") {
                return unsafe { self.contains_simd(predicate.as_bytes(), pattern.as_bytes()) };
            }
        }

        predicate.contains(pattern)
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "sse4.2")]
    unsafe fn contains_simd(&self, haystack: &[u8], needle: &[u8]) -> bool {
        if needle.is_empty() {
            return true;
        }

        if needle.len() > haystack.len() {
            return false;
        }

        let needle_first = needle[0];
        let needle_len = needle.len();

        let mut i = 0;
        while i + 16 <= haystack.len() {
            let chunk = _mm_loadu_si128(haystack.as_ptr().add(i) as *const __m128i);
            let needle_vec = _mm_set1_epi8(needle_first as i8);
            let cmp = _mm_cmpeq_epi8(chunk, needle_vec);
            let mask = _mm_movemask_epi8(cmp) as u16;

            if mask != 0 {
                for bit in 0..16 {
                    if (mask & (1 << bit)) != 0 {
                        let pos = i + bit;
                        if pos + needle_len <= haystack.len()
                            && &haystack[pos..pos + needle_len] == needle
                        {
                            return true;
                        }
                    }
                }
            }

            i += 16;
        }

        haystack[i..].windows(needle_len).any(|w| w == needle)
    }
}

/// SIMD-accelerated quoted triple filter
///
/// Fast filtering for triples containing quoted triples.
#[derive(Default)]
pub struct SimdQuotedTripleFilter;

impl SimdQuotedTripleFilter {
    /// Create a new SIMD quoted triple filter
    pub fn new() -> Self {
        Self
    }

    /// Filter triples to find those with quoted triple terms
    ///
    /// # Arguments
    /// * `triples` - Triples to filter
    /// * `subject_quoted` - Include triples with quoted subject
    /// * `object_quoted` - Include triples with quoted object
    ///
    /// # Returns
    /// Indices of matching triples
    pub fn filter_quoted_triples(
        &self,
        triples: &[StarTriple],
        subject_quoted: bool,
        object_quoted: bool,
    ) -> Vec<usize> {
        let mut matches = Vec::new();

        for (idx, triple) in triples.iter().enumerate() {
            let has_subject_quoted = matches!(triple.subject, StarTerm::QuotedTriple(_));
            let has_object_quoted = matches!(triple.object, StarTerm::QuotedTriple(_));

            if (subject_quoted && has_subject_quoted) || (object_quoted && has_object_quoted) {
                matches.push(idx);
            }
        }

        trace!(
            "Quoted triple filter: {} matches from {} triples",
            matches.len(),
            triples.len()
        );
        matches
    }

    /// Count quoted triples in a batch
    ///
    /// # Arguments
    /// * `triples` - Triples to count
    ///
    /// # Returns
    /// Number of triples with quoted terms
    pub fn count_quoted(&self, triples: &[StarTriple]) -> usize {
        let count = triples
            .iter()
            .filter(|t| {
                matches!(t.subject, StarTerm::QuotedTriple(_))
                    || matches!(t.object, StarTerm::QuotedTriple(_))
            })
            .count();

        count
    }
}

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

    #[test]
    fn test_simd_predicate_matcher_exact() {
        let matcher = SimdPredicateMatcher::new();

        let iri = "http://example.org/predicate";
        assert!(matcher.matches(iri, "http://example.org/predicate", true));
        assert!(!matcher.matches(iri, "http://example.org/other", true));
    }

    #[test]
    fn test_simd_predicate_matcher_substring() {
        let matcher = SimdPredicateMatcher::new();

        let iri = "http://example.org/predicate";
        assert!(matcher.matches(iri, "predicate", false));
        assert!(matcher.matches(iri, "example", false));
        assert!(matcher.matches(iri, "*", false)); // Wildcard
        assert!(!matcher.matches(iri, "notfound", false));
    }

    #[test]
    fn test_simd_predicate_matcher_long_strings() {
        let matcher = SimdPredicateMatcher::new();

        // Test with strings >16 bytes (triggers SIMD path)
        let long_iri = "http://example.org/very/long/predicate/path/that/exceeds/sixteen/bytes";
        let pattern = "very/long/predicate";
        assert!(matcher.matches(long_iri, pattern, false));
    }

    #[test]
    fn test_simd_predicate_batch_filter() {
        let matcher = SimdPredicateMatcher::new();

        let triples = vec![
            StarTriple::new(
                StarTerm::iri("http://ex.org/s1").unwrap(),
                StarTerm::iri("http://ex.org/pred1").unwrap(),
                StarTerm::literal("value1").unwrap(),
            ),
            StarTriple::new(
                StarTerm::iri("http://ex.org/s2").unwrap(),
                StarTerm::iri("http://ex.org/pred2").unwrap(),
                StarTerm::literal("value2").unwrap(),
            ),
            StarTriple::new(
                StarTerm::iri("http://ex.org/s3").unwrap(),
                StarTerm::iri("http://ex.org/pred1").unwrap(),
                StarTerm::literal("value3").unwrap(),
            ),
        ];

        let matches = matcher.filter_by_predicate(&triples, "pred1", false);
        assert_eq!(matches, vec![0, 2]);
    }

    #[test]
    fn test_simd_cep_ordered_sequence() {
        let matcher = SimdCepSequenceMatcher::new();

        let predicates = vec![
            "http://ex.org/step1".to_string(),
            "http://ex.org/step2".to_string(),
            "http://ex.org/step3".to_string(),
        ];

        let pattern = vec![
            "step1".to_string(),
            "step2".to_string(),
            "step3".to_string(),
        ];

        let result = matcher.match_sequence(&predicates, &pattern, true);
        assert_eq!(result, Some(vec![0, 1, 2]));
    }

    #[test]
    fn test_simd_cep_ordered_sequence_no_match() {
        let matcher = SimdCepSequenceMatcher::new();

        let predicates = vec![
            "http://ex.org/step1".to_string(),
            "http://ex.org/step3".to_string(), // step2 missing
            "http://ex.org/step2".to_string(),
        ];

        let pattern = vec![
            "step1".to_string(),
            "step2".to_string(),
            "step3".to_string(),
        ];

        let result = matcher.match_sequence(&predicates, &pattern, true);
        assert_eq!(result, None); // Order violated
    }

    #[test]
    fn test_simd_cep_unordered_sequence() {
        let matcher = SimdCepSequenceMatcher::new();

        let predicates = vec![
            "http://ex.org/step3".to_string(),
            "http://ex.org/step1".to_string(),
            "http://ex.org/step2".to_string(),
        ];

        let pattern = vec![
            "step1".to_string(),
            "step2".to_string(),
            "step3".to_string(),
        ];

        let result = matcher.match_sequence(&predicates, &pattern, false);
        assert!(result.is_some());
        let indices = result.unwrap();
        assert_eq!(indices.len(), 3);
    }

    #[test]
    fn test_simd_cep_wildcard_pattern() {
        let matcher = SimdCepSequenceMatcher::new();

        let predicates = vec![
            "http://ex.org/anything".to_string(),
            "http://ex.org/step2".to_string(),
        ];

        let pattern = vec!["*".to_string(), "step2".to_string()];

        let result = matcher.match_sequence(&predicates, &pattern, true);
        assert_eq!(result, Some(vec![0, 1]));
    }

    #[test]
    fn test_simd_quoted_triple_filter() {
        let filter = SimdQuotedTripleFilter::new();

        let qt = StarTerm::quoted_triple(StarTriple::new(
            StarTerm::iri("http://ex.org/qs").unwrap(),
            StarTerm::iri("http://ex.org/qp").unwrap(),
            StarTerm::literal("qv").unwrap(),
        ));

        let triples = vec![
            StarTriple::new(
                qt.clone(),
                StarTerm::iri("http://ex.org/meta").unwrap(),
                StarTerm::literal("value").unwrap(),
            ),
            StarTriple::new(
                StarTerm::iri("http://ex.org/s").unwrap(),
                StarTerm::iri("http://ex.org/p").unwrap(),
                qt.clone(),
            ),
            StarTriple::new(
                StarTerm::iri("http://ex.org/s2").unwrap(),
                StarTerm::iri("http://ex.org/p2").unwrap(),
                StarTerm::literal("v2").unwrap(),
            ),
        ];

        // Filter for subject quoted
        let subject_matches = filter.filter_quoted_triples(&triples, true, false);
        assert_eq!(subject_matches, vec![0]);

        // Filter for object quoted
        let object_matches = filter.filter_quoted_triples(&triples, false, true);
        assert_eq!(object_matches, vec![1]);

        // Filter for any quoted
        let any_matches = filter.filter_quoted_triples(&triples, true, true);
        assert_eq!(any_matches, vec![0, 1]);

        // Count quoted
        let count = filter.count_quoted(&triples);
        assert_eq!(count, 2);
    }
}