oxirs-wasm 0.2.4

WebAssembly bindings for OxiRS - Run RDF/SPARQL in the browser
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//! SPARQL UPDATE operations for OxiRS WASM
//!
//! Implements a subset of SPARQL 1.1 Update:
//! - INSERT DATA { triples }
//! - DELETE DATA { triples }
//! - INSERT { template } WHERE { pattern }
//! - DELETE { template } WHERE { pattern }
//! - CLEAR
//! - DROP

use crate::error::{WasmError, WasmResult};
use crate::store::OxiRSStore;
use std::collections::HashMap;

// -----------------------------------------------------------------------
// Public types
// -----------------------------------------------------------------------

/// A parsed SPARQL UPDATE operation
#[derive(Debug, Clone)]
pub enum UpdateOperation {
    /// INSERT DATA { triples }
    InsertData(Vec<RawTriple>),
    /// DELETE DATA { triples }
    DeleteData(Vec<RawTriple>),
    /// INSERT { template } WHERE { pattern }
    InsertWhere {
        template: Vec<TemplateTriple>,
        where_patterns: Vec<RawPattern>,
    },
    /// DELETE { template } WHERE { pattern }
    DeleteWhere {
        template: Vec<TemplateTriple>,
        where_patterns: Vec<RawPattern>,
    },
    /// CLEAR – removes all triples
    Clear,
    /// DROP – alias for CLEAR in the embedded store
    Drop,
}

/// A fully resolved triple (no variables)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RawTriple {
    pub subject: String,
    pub predicate: String,
    pub object: String,
}

impl RawTriple {
    /// Create a new raw triple
    pub fn new(
        subject: impl Into<String>,
        predicate: impl Into<String>,
        object: impl Into<String>,
    ) -> Self {
        Self {
            subject: subject.into(),
            predicate: predicate.into(),
            object: object.into(),
        }
    }
}

/// A triple that may contain variable references (`?var`) in subject/predicate/object
#[derive(Debug, Clone)]
pub struct TemplateTriple {
    pub subject: TemplateTerm,
    pub predicate: TemplateTerm,
    pub object: TemplateTerm,
}

/// A template term is either a variable or a concrete value
#[derive(Debug, Clone)]
pub enum TemplateTerm {
    Variable(String),
    Value(String),
}

impl TemplateTerm {
    /// Resolve against a binding map, returning the concrete value.
    /// Variables not found in the map are left as the raw variable string.
    pub fn resolve(&self, binding: &HashMap<String, String>) -> String {
        match self {
            TemplateTerm::Variable(v) => {
                binding.get(v).cloned().unwrap_or_else(|| format!("?{}", v))
            }
            TemplateTerm::Value(v) => v.clone(),
        }
    }
}

/// A triple pattern used in WHERE clauses (may contain variables)
#[derive(Debug, Clone)]
pub struct RawPattern {
    pub subject: TemplateTerm,
    pub predicate: TemplateTerm,
    pub object: TemplateTerm,
}

// -----------------------------------------------------------------------
// UpdateParser
// -----------------------------------------------------------------------

/// Parses SPARQL UPDATE strings into [`UpdateOperation`] values
pub struct UpdateParser;

impl UpdateParser {
    /// Parse a SPARQL UPDATE string into an [`UpdateOperation`].
    pub fn parse(sparql: &str) -> WasmResult<UpdateOperation> {
        let s = sparql.trim();
        let upper = s.to_uppercase();

        if upper.starts_with("INSERT DATA") {
            let body = extract_brace_body(s, "INSERT DATA")?;
            let triples = parse_data_block(&body)?;
            Ok(UpdateOperation::InsertData(triples))
        } else if upper.starts_with("DELETE DATA") {
            let body = extract_brace_body(s, "DELETE DATA")?;
            let triples = parse_data_block(&body)?;
            Ok(UpdateOperation::DeleteData(triples))
        } else if upper.starts_with("INSERT") && upper.contains("WHERE") {
            let (template_body, where_body) = split_template_where(s, "INSERT")?;
            let template = parse_template_block(&template_body)?;
            let where_patterns = parse_pattern_block(&where_body)?;
            Ok(UpdateOperation::InsertWhere {
                template,
                where_patterns,
            })
        } else if upper.starts_with("DELETE") && upper.contains("WHERE") {
            let (template_body, where_body) = split_template_where(s, "DELETE")?;
            let template = parse_template_block(&template_body)?;
            let where_patterns = parse_pattern_block(&where_body)?;
            Ok(UpdateOperation::DeleteWhere {
                template,
                where_patterns,
            })
        } else if upper.starts_with("CLEAR") {
            Ok(UpdateOperation::Clear)
        } else if upper.starts_with("DROP") {
            Ok(UpdateOperation::Drop)
        } else {
            Err(WasmError::QueryError(format!(
                "Unknown or unsupported UPDATE operation: {}",
                &s[..s.len().min(80)]
            )))
        }
    }
}

// -----------------------------------------------------------------------
// UpdateExecutor
// -----------------------------------------------------------------------

/// Executes [`UpdateOperation`] values against an [`OxiRSStore`]
pub struct UpdateExecutor;

impl UpdateExecutor {
    /// Apply an already-parsed [`UpdateOperation`] and return the number of
    /// triples affected (inserted or deleted).
    pub fn execute(op: &UpdateOperation, store: &mut OxiRSStore) -> WasmResult<u32> {
        match op {
            UpdateOperation::InsertData(triples) => {
                let mut count = 0u32;
                for t in triples {
                    if store.insert(&t.subject, &t.predicate, &t.object) {
                        count += 1;
                    }
                }
                Ok(count)
            }

            UpdateOperation::DeleteData(triples) => {
                let mut count = 0u32;
                for t in triples {
                    if store.delete(&t.subject, &t.predicate, &t.object) {
                        count += 1;
                    }
                }
                Ok(count)
            }

            UpdateOperation::InsertWhere {
                template,
                where_patterns,
            } => {
                let bindings = evaluate_where_patterns(where_patterns, store)?;
                let mut count = 0u32;
                for binding in &bindings {
                    for t in template {
                        let s = t.subject.resolve(binding);
                        let p = t.predicate.resolve(binding);
                        let o = t.object.resolve(binding);
                        if store.insert(&s, &p, &o) {
                            count += 1;
                        }
                    }
                }
                Ok(count)
            }

            UpdateOperation::DeleteWhere {
                template,
                where_patterns,
            } => {
                let bindings = evaluate_where_patterns(where_patterns, store)?;
                // Collect all triples to delete first to avoid borrow conflicts
                let mut to_delete: Vec<RawTriple> = Vec::new();
                for binding in &bindings {
                    for t in template {
                        let s = t.subject.resolve(binding);
                        let p = t.predicate.resolve(binding);
                        let o = t.object.resolve(binding);
                        to_delete.push(RawTriple::new(s, p, o));
                    }
                }
                let mut count = 0u32;
                for t in to_delete {
                    if store.delete(&t.subject, &t.predicate, &t.object) {
                        count += 1;
                    }
                }
                Ok(count)
            }

            UpdateOperation::Clear | UpdateOperation::Drop => {
                let before = store.size() as u32;
                store.clear();
                Ok(before)
            }
        }
    }
}

// -----------------------------------------------------------------------
// Top-level convenience function
// -----------------------------------------------------------------------

/// Parse and execute a SPARQL UPDATE string against the given store.
///
/// Returns the number of triples affected (inserted or deleted).
pub fn execute_update(sparql: &str, store: &mut OxiRSStore) -> WasmResult<u32> {
    let op = UpdateParser::parse(sparql)?;
    UpdateExecutor::execute(&op, store)
}

// -----------------------------------------------------------------------
// Internal helpers – pattern evaluation
// -----------------------------------------------------------------------

/// Simple SPARQL pattern evaluation for WHERE clauses in UPDATE.
/// Returns all bindings that match the pattern list.
fn evaluate_where_patterns(
    patterns: &[RawPattern],
    store: &OxiRSStore,
) -> WasmResult<Vec<HashMap<String, String>>> {
    let mut results: Vec<HashMap<String, String>> = vec![HashMap::new()];

    for pattern in patterns {
        let mut new_results = Vec::new();
        for binding in &results {
            for triple in store.all_triples() {
                if matches_term(&pattern.subject, &triple.subject, binding)
                    && matches_term(&pattern.predicate, &triple.predicate, binding)
                    && matches_term(&pattern.object, &triple.object, binding)
                {
                    let mut new_binding = binding.clone();
                    bind_variable(&pattern.subject, &triple.subject, &mut new_binding);
                    bind_variable(&pattern.predicate, &triple.predicate, &mut new_binding);
                    bind_variable(&pattern.object, &triple.object, &mut new_binding);
                    new_results.push(new_binding);
                }
            }
        }
        results = new_results;
    }

    Ok(results)
}

fn matches_term(term: &TemplateTerm, value: &str, binding: &HashMap<String, String>) -> bool {
    match term {
        TemplateTerm::Variable(var) => {
            if let Some(bound) = binding.get(var) {
                bound == value
            } else {
                true
            }
        }
        TemplateTerm::Value(v) => v == value,
    }
}

fn bind_variable(term: &TemplateTerm, value: &str, binding: &mut HashMap<String, String>) {
    if let TemplateTerm::Variable(var) = term {
        binding
            .entry(var.clone())
            .or_insert_with(|| value.to_string());
    }
}

// -----------------------------------------------------------------------
// Internal helpers – parsing
// -----------------------------------------------------------------------

/// Extract the content of the `{ }` block that follows a keyword
fn extract_brace_body(sparql: &str, keyword: &str) -> WasmResult<String> {
    let upper = sparql.to_uppercase();
    let kw_upper = keyword.to_uppercase();
    let start = upper
        .find(&kw_upper)
        .ok_or_else(|| WasmError::QueryError(format!("Expected keyword '{}'", keyword)))?
        + keyword.len();

    let after = &sparql[start..];
    let open = after
        .find('{')
        .ok_or_else(|| WasmError::QueryError(format!("Missing '{{' after '{}'", keyword)))?;
    let inner_start = open + 1;

    let chars: Vec<char> = after[inner_start..].chars().collect();
    let mut depth = 1usize;
    let mut pos = 0usize;
    while pos < chars.len() && depth > 0 {
        match chars[pos] {
            '{' => depth += 1,
            '}' => depth -= 1,
            _ => {}
        }
        if depth > 0 {
            pos += 1;
        }
    }

    if depth != 0 {
        return Err(WasmError::QueryError("Unmatched '{' in UPDATE".to_string()));
    }

    Ok(chars[..pos].iter().collect())
}

/// Split `KEYWORD { template } WHERE { where_body }` into the two bodies
fn split_template_where(sparql: &str, keyword: &str) -> WasmResult<(String, String)> {
    let upper = sparql.to_uppercase();
    let kw_upper = keyword.to_uppercase();

    let kw_start = upper
        .find(&kw_upper)
        .ok_or_else(|| WasmError::QueryError(format!("Expected keyword '{}'", keyword)))?;
    let after_kw = &sparql[kw_start + keyword.len()..];

    // First brace block = template
    let open1 = after_kw
        .find('{')
        .ok_or_else(|| WasmError::QueryError(format!("Missing '{{' after '{}'", keyword)))?;
    let after_kw_inner = &after_kw[open1 + 1..];
    let chars1: Vec<char> = after_kw_inner.chars().collect();
    let mut depth = 1usize;
    let mut pos1 = 0usize;
    while pos1 < chars1.len() && depth > 0 {
        match chars1[pos1] {
            '{' => depth += 1,
            '}' => depth -= 1,
            _ => {}
        }
        if depth > 0 {
            pos1 += 1;
        }
    }
    let template_body: String = chars1[..pos1].iter().collect();

    // Everything after the closing '}' of the template
    let consumed = kw_start + keyword.len() + open1 + 1 + pos1 + 1;
    let rest = &sparql[consumed..];

    // Find WHERE keyword then second brace block
    let rest_upper = rest.to_uppercase();
    let where_pos = rest_upper
        .find("WHERE")
        .ok_or_else(|| WasmError::QueryError("Missing WHERE clause".to_string()))?;
    let after_where = &rest[where_pos + 5..];
    let open2 = after_where
        .find('{')
        .ok_or_else(|| WasmError::QueryError("Missing '{{' after WHERE".to_string()))?;
    let after_where_inner = &after_where[open2 + 1..];
    let chars2: Vec<char> = after_where_inner.chars().collect();
    let mut depth2 = 1usize;
    let mut pos2 = 0usize;
    while pos2 < chars2.len() && depth2 > 0 {
        match chars2[pos2] {
            '{' => depth2 += 1,
            '}' => depth2 -= 1,
            _ => {}
        }
        if depth2 > 0 {
            pos2 += 1;
        }
    }
    let where_body: String = chars2[..pos2].iter().collect();

    Ok((template_body, where_body))
}

/// Parse a DATA block (no variables) into [`RawTriple`] values
fn parse_data_block(body: &str) -> WasmResult<Vec<RawTriple>> {
    let mut triples = Vec::new();

    for stmt in body.split('.') {
        let stmt = stmt.trim();
        if stmt.is_empty() {
            continue;
        }
        let tokens: Vec<&str> = stmt.split_whitespace().collect();
        if tokens.len() >= 3 {
            let s = iri_from_token(tokens[0]);
            let p = if tokens[1] == "a" {
                "http://www.w3.org/1999/02/22-rdf-syntax-ns#type".to_string()
            } else {
                iri_from_token(tokens[1])
            };
            let obj_raw = tokens[2..].join(" ");
            let o = object_from_token(&obj_raw);
            triples.push(RawTriple::new(s, p, o));
        }
    }

    Ok(triples)
}

/// Parse a template block into [`TemplateTriple`] values (may contain variables)
fn parse_template_block(body: &str) -> WasmResult<Vec<TemplateTriple>> {
    let mut triples = Vec::new();

    for stmt in body.split('.') {
        let stmt = stmt.trim();
        if stmt.is_empty() {
            continue;
        }
        let tokens: Vec<&str> = stmt.split_whitespace().collect();
        if tokens.len() >= 3 {
            let s = template_term_from(tokens[0]);
            let p = if tokens[1] == "a" {
                TemplateTerm::Value("http://www.w3.org/1999/02/22-rdf-syntax-ns#type".to_string())
            } else {
                template_term_from(tokens[1])
            };
            let obj_raw = tokens[2..].join(" ");
            let o = template_term_from(&obj_raw);
            triples.push(TemplateTriple {
                subject: s,
                predicate: p,
                object: o,
            });
        }
    }

    Ok(triples)
}

/// Parse a WHERE pattern block into [`RawPattern`] values (may contain variables)
fn parse_pattern_block(body: &str) -> WasmResult<Vec<RawPattern>> {
    let mut patterns = Vec::new();

    for stmt in body.split('.') {
        let stmt = stmt.trim();
        if stmt.is_empty() || stmt.to_uppercase().starts_with("FILTER") {
            continue;
        }
        let tokens: Vec<&str> = stmt.split_whitespace().collect();
        if tokens.len() >= 3 {
            let s = template_term_from(tokens[0]);
            let p = if tokens[1] == "a" {
                TemplateTerm::Value("http://www.w3.org/1999/02/22-rdf-syntax-ns#type".to_string())
            } else {
                template_term_from(tokens[1])
            };
            let obj_raw = tokens[2..].join(" ");
            let o = template_term_from(&obj_raw);
            patterns.push(RawPattern {
                subject: s,
                predicate: p,
                object: o,
            });
        }
    }

    Ok(patterns)
}

fn template_term_from(token: &str) -> TemplateTerm {
    let t = token.trim();
    if t.starts_with('?') || t.starts_with('$') {
        TemplateTerm::Variable(t.trim_start_matches(['?', '$']).to_string())
    } else {
        TemplateTerm::Value(iri_from_token(t))
    }
}

fn iri_from_token(token: &str) -> String {
    let t = token.trim();
    if t.starts_with('<') && t.ends_with('>') {
        t[1..t.len() - 1].to_string()
    } else {
        t.to_string()
    }
}

fn object_from_token(token: &str) -> String {
    let t = token.trim();
    if t.starts_with('<') && t.ends_with('>') {
        t[1..t.len() - 1].to_string()
    } else {
        // Literals are kept as-is (including surrounding quotes)
        t.to_string()
    }
}

// -----------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------

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

    fn make_store() -> OxiRSStore {
        OxiRSStore::new()
    }

    // INSERT DATA

    #[test]
    fn test_insert_data_single() {
        let mut store = make_store();
        let n = execute_update(
            "INSERT DATA { <http://a> <http://b> <http://c> }",
            &mut store,
        )
        .expect("execute");
        assert_eq!(n, 1);
        assert!(store.contains("http://a", "http://b", "http://c"));
    }

    #[test]
    fn test_insert_data_multiple() {
        let mut store = make_store();
        let n = execute_update(
            "INSERT DATA { <http://a> <http://b> <http://c> . <http://x> <http://y> <http://z> }",
            &mut store,
        )
        .expect("execute");
        assert_eq!(n, 2);
        assert_eq!(store.size(), 2);
    }

    #[test]
    fn test_insert_data_no_duplicates() {
        let mut store = make_store();
        execute_update(
            "INSERT DATA { <http://a> <http://b> <http://c> }",
            &mut store,
        )
        .expect("first");
        let n = execute_update(
            "INSERT DATA { <http://a> <http://b> <http://c> }",
            &mut store,
        )
        .expect("second");
        assert_eq!(n, 0);
        assert_eq!(store.size(), 1);
    }

    #[test]
    fn test_insert_data_type_shortcut() {
        let mut store = make_store();
        let n = execute_update(
            "INSERT DATA { <http://alice> a <http://Person> }",
            &mut store,
        )
        .expect("execute");
        assert_eq!(n, 1);
        assert!(store.contains(
            "http://alice",
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
            "http://Person"
        ));
    }

    // DELETE DATA

    #[test]
    fn test_delete_data_existing() {
        let mut store = make_store();
        store.insert("http://a", "http://b", "http://c");
        let n = execute_update(
            "DELETE DATA { <http://a> <http://b> <http://c> }",
            &mut store,
        )
        .expect("execute");
        assert_eq!(n, 1);
        assert_eq!(store.size(), 0);
    }

    #[test]
    fn test_delete_data_nonexistent() {
        let mut store = make_store();
        let n = execute_update(
            "DELETE DATA { <http://a> <http://b> <http://c> }",
            &mut store,
        )
        .expect("execute");
        assert_eq!(n, 0);
    }

    // INSERT WHERE

    #[test]
    fn test_insert_where() {
        let mut store = make_store();
        store.insert("http://alice", "http://knows", "http://bob");

        let n = execute_update(
            "INSERT { ?s <http://friend> ?o } WHERE { ?s <http://knows> ?o }",
            &mut store,
        )
        .expect("execute");
        assert_eq!(n, 1);
        assert!(store.contains("http://alice", "http://friend", "http://bob"));
    }

    // DELETE WHERE

    #[test]
    fn test_delete_where() {
        let mut store = make_store();
        store.insert("http://alice", "http://name", "Alice");
        store.insert("http://bob", "http://name", "Bob");
        store.insert("http://alice", "http://age", "30");

        let n = execute_update(
            "DELETE { ?s <http://name> ?o } WHERE { ?s <http://name> ?o }",
            &mut store,
        )
        .expect("execute");
        assert_eq!(n, 2);
        assert!(!store.contains("http://alice", "http://name", "Alice"));
        assert!(!store.contains("http://bob", "http://name", "Bob"));
        assert!(store.contains("http://alice", "http://age", "30"));
    }

    // CLEAR

    #[test]
    fn test_clear() {
        let mut store = make_store();
        store.insert("http://a", "http://b", "http://c");
        store.insert("http://x", "http://y", "http://z");

        let n = execute_update("CLEAR", &mut store).expect("execute");
        assert_eq!(n, 2);
        assert_eq!(store.size(), 0);
    }

    #[test]
    fn test_drop() {
        let mut store = make_store();
        store.insert("http://a", "http://b", "http://c");

        let n = execute_update("DROP", &mut store).expect("execute");
        assert_eq!(n, 1);
        assert_eq!(store.size(), 0);
    }

    #[test]
    fn test_unknown_operation() {
        let mut store = make_store();
        assert!(execute_update("LOAD <http://example.org/data.ttl>", &mut store).is_err());
    }
}