rgen-core 0.1.0

Core graph-aware code generation engine
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
use anyhow::{bail, Result};
use fxhash::FxHasher;
use lru::LruCache;
use oxigraph::io::RdfFormat;
use oxigraph::model::{GraphName, NamedNode, Quad, Subject, Term};
use oxigraph::sparql::{Query, QueryOptions, QueryResults};
use oxigraph::store::Store;
use serde_json::Value as JsonValue;
use std::collections::BTreeMap;
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::BufReader;
use std::num::NonZeroUsize;
use std::path::Path;
use std::sync::{
    atomic::{AtomicU64, Ordering},
    Arc, Mutex,
};

#[derive(Clone, Debug)]
pub enum CachedResult {
    Boolean(bool),
    Solutions(Vec<BTreeMap<String, String>>),
    Graph(Vec<String>), // Serialized triples
}

impl CachedResult {
    /// Convert to serde_json::Value for Tera consumption
    pub fn to_json(&self) -> JsonValue {
        match self {
            CachedResult::Boolean(b) => JsonValue::Bool(*b),
            CachedResult::Solutions(rows) => {
                let arr: Vec<JsonValue> = rows
                    .iter()
                    .map(|row| {
                        let mut obj = serde_json::Map::new();
                        for (k, v) in row {
                            obj.insert(k.clone(), JsonValue::String(v.clone()));
                        }
                        JsonValue::Object(obj)
                    })
                    .collect();
                JsonValue::Array(arr)
            }
            CachedResult::Graph(_triples) => JsonValue::String(String::new()),
        }
    }
}

/// Thread-safe Oxigraph wrapper with SPARQL caching. Clone is cheap (shared store).
pub struct Graph {
    inner: Store,
    epoch: Arc<AtomicU64>,
    plan_cache: Arc<Mutex<LruCache<u64, Query>>>,
    result_cache: Arc<Mutex<LruCache<(u64, u64), CachedResult>>>,
}

impl Graph {
    pub fn new() -> Result<Self> {
        let plan_cache_size = NonZeroUsize::new(100)
            .ok_or_else(|| anyhow::anyhow!("Invalid cache size"))?;
        let result_cache_size = NonZeroUsize::new(1000)
            .ok_or_else(|| anyhow::anyhow!("Invalid cache size"))?;

        Ok(Self {
            inner: Store::new()?,
            epoch: Arc::new(AtomicU64::new(1)),
            plan_cache: Arc::new(Mutex::new(LruCache::new(plan_cache_size))),
            result_cache: Arc::new(Mutex::new(LruCache::new(result_cache_size))),
        })
    }

    fn current_epoch(&self) -> u64 {
        self.epoch.load(Ordering::Relaxed)
    }

    fn bump_epoch(&self) {
        self.epoch.fetch_add(1, Ordering::Relaxed);
    }

    fn hash_query(&self, sparql: &str) -> u64 {
        let mut hasher = FxHasher::default();
        sparql.hash(&mut hasher);
        hasher.finish()
    }

    fn materialize_results(&self, results: QueryResults) -> Result<CachedResult> {
        match results {
            QueryResults::Boolean(b) => Ok(CachedResult::Boolean(b)),
            QueryResults::Solutions(solutions) => {
                let mut rows = Vec::new();
                for solution in solutions {
                    let solution = solution?;
                    let mut row = BTreeMap::new();
                    for (var, term) in solution.iter() {
                        row.insert(var.as_str().to_string(), term.to_string());
                    }
                    rows.push(row);
                }
                Ok(CachedResult::Solutions(rows))
            }
            QueryResults::Graph(quads) => {
                let triples: Result<Vec<String>> = quads
                    .map(|q| q.map(|quad| quad.to_string()).map_err(Into::into))
                    .collect();
                Ok(CachedResult::Graph(triples?))
            }
        }
    }

    pub fn insert_turtle(&self, turtle: &str) -> Result<()> {
        self.inner
            .load_from_reader(RdfFormat::Turtle, turtle.as_bytes())?;
        self.bump_epoch();
        Ok(())
    }

    pub fn insert_turtle_with_base(&self, turtle: &str, _base_iri: &str) -> Result<()> {
        // Note: The new Oxigraph API doesn't support base IRI in load_from_reader
        // We'll need to handle this differently or use a different approach
        self.inner
            .load_from_reader(RdfFormat::Turtle, turtle.as_bytes())?;
        self.bump_epoch();
        Ok(())
    }

    pub fn insert_turtle_in(&self, turtle: &str, _graph_iri: &str) -> Result<()> {
        // Note: The new Oxigraph API doesn't support named graphs in load_from_reader
        // We'll need to handle this differently or use a different approach
        self.inner
            .load_from_reader(RdfFormat::Turtle, turtle.as_bytes())?;
        self.bump_epoch();
        Ok(())
    }

    pub fn insert_quad(&self, s: &str, p: &str, o: &str) -> Result<()> {
        let s = NamedNode::new(s)?;
        let p = NamedNode::new(p)?;
        let o = NamedNode::new(o)?;
        self.inner
            .insert(&Quad::new(s, p, o, GraphName::DefaultGraph))?;
        self.bump_epoch();
        Ok(())
    }

    pub fn load_path<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let path = path.as_ref();
        let ext = path
            .extension()
            .and_then(|e| e.to_str())
            .map(|s| s.to_ascii_lowercase())
            .unwrap_or_default();

        let fmt = match ext.as_str() {
            "ttl" | "turtle" => RdfFormat::Turtle,
            "nt" | "ntriples" => RdfFormat::NTriples,
            "rdf" | "xml" => RdfFormat::RdfXml,
            other => bail!("unsupported RDF format: {}", other),
        };

        let file = File::open(path)?;
        let reader = BufReader::new(file);
        self.inner.load_from_reader(fmt, reader)?;
        self.bump_epoch();
        Ok(())
    }

    pub fn query_cached(&self, sparql: &str) -> Result<CachedResult> {
        let query_hash = self.hash_query(sparql);
        let epoch = self.current_epoch();
        let cache_key = (query_hash, epoch);

        // Check result cache
        if let Some(cached) = self.result_cache
            .lock()
            .map_err(|e| anyhow::anyhow!("Cache lock poisoned: {}", e))?
            .get(&cache_key)
            .cloned()
        {
            return Ok(cached);
        }

        // Check plan cache or parse
        let query = {
            let mut cache = self.plan_cache
                .lock()
                .map_err(|e| anyhow::anyhow!("Cache lock poisoned: {}", e))?;
            if let Some(q) = cache.get(&query_hash).cloned() {
                q
            } else {
                let q = Query::parse(sparql, None)?;
                cache.put(query_hash, q.clone());
                q
            }
        };

        // Execute and materialize
        let results = self.inner.query_opt(query, QueryOptions::default())?;
        let cached = self.materialize_results(results)?;

        // Store in cache
        self.result_cache
            .lock()
            .map_err(|e| anyhow::anyhow!("Cache lock poisoned: {}", e))?
            .put(cache_key, cached.clone());

        Ok(cached)
    }

    pub fn query(&self, sparql: &str) -> Result<QueryResults> {
        // For backward compatibility, we need to reconstruct QueryResults
        // This is inefficient but maintains API compatibility
        let cached = self.query_cached(sparql)?;

        match cached {
            CachedResult::Boolean(b) => Ok(QueryResults::Boolean(b)),
            CachedResult::Solutions(_) | CachedResult::Graph(_) => {
                // Fall back to direct query for non-boolean results
                // since we can't reconstruct the iterator properly
                Ok(self.inner.query(sparql)?)
            }
        }
    }

    pub fn query_with_prolog(
        &self, sparql: &str, prefixes: &BTreeMap<String, String>, base: Option<&str>,
    ) -> Result<QueryResults> {
        let head = build_prolog(prefixes, base);
        let q = if head.is_empty() {
            sparql.into()
        } else {
            format!("{head}\n{sparql}")
        };
        self.query(&q)
    }

    pub fn query_prepared(&self, q: &Query) -> Result<QueryResults> {
        Ok(self.inner.query_opt(q.clone(), QueryOptions::default())?)
    }

    /// Typed pattern filter (no extra allocs).
    pub fn quads_for_pattern(
        &self, s: Option<&Subject>, p: Option<&NamedNode>, o: Option<&Term>, g: Option<&GraphName>,
    ) -> Result<Vec<Quad>> {
        Ok(self
            .inner
            .quads_for_pattern(
                s.map(|x| x.as_ref()),
                p.map(|x| x.as_ref()),
                o.map(|x| x.as_ref()),
                g.map(|x| x.as_ref()),
            )
            .collect::<Result<Vec<_>, _>>()?)
    }

    pub fn clear(&self) -> Result<()> {
        self.inner.clear()?;
        self.bump_epoch();
        Ok(())
    }

    pub fn len(&self) -> usize {
        #[allow(deprecated)]
        {
            self.inner.len().unwrap_or(0)
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl Clone for Graph {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            epoch: Arc::clone(&self.epoch),
            plan_cache: Arc::clone(&self.plan_cache),
            result_cache: Arc::clone(&self.result_cache),
        }
    }
}

pub fn build_prolog(prefixes: &BTreeMap<String, String>, base: Option<&str>) -> String {
    let mut s = String::new();
    if let Some(b) = base {
        let _ = std::fmt::Write::write_fmt(&mut s, format_args!("BASE <{}>\n", b));
    }
    for (pfx, iri) in prefixes {
        let _ = std::fmt::Write::write_fmt(&mut s, format_args!("PREFIX {}: <{}>\n", pfx, iri));
    }
    s
}

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

    #[test]
    fn insert_turtle_and_query() -> Result<()> {
        let g = Graph::new()?;
        let ttl = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:knows ex:bob .
        "#;
        g.insert_turtle(ttl)?;

        let res = g.query("SELECT ?s WHERE { ?s ?p ?o }")?;
        if let QueryResults::Solutions(mut it) = res {
            let first = it.next().unwrap().unwrap();
            let s = first.get("s").unwrap().to_string();
            assert_eq!(s, "<http://example.org/alice>");
        } else {
            return Err(anyhow::anyhow!("Expected Solutions results"));
        }
        Ok(())
    }

    #[test]
    fn insert_quad_and_filter() -> Result<()> {
        let g = Graph::new()?;
        g.insert_quad(
            "http://example.org/A",
            "http://example.org/rel",
            "http://example.org/B",
        )?;
        let a = NamedNode::new("http://example.org/A")?;
        let list = g.quads_for_pattern(Some(&a.into()), None, None, None)?;
        assert_eq!(list.len(), 1);
        Ok(())
    }

    #[test]
    fn insert_turtle_with_base() -> Result<()> {
        let g = Graph::new()?;
        let ttl = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:knows ex:bob .
        "#;
        g.insert_turtle_with_base(ttl, "http://example.org/")?;

        let res = g.query("SELECT ?s WHERE { ?s ?p ?o }")?;
        if let QueryResults::Solutions(mut it) = res {
            let first = it.next().unwrap().unwrap();
            let s = first.get("s").unwrap().to_string();
            assert_eq!(s, "<http://example.org/alice>");
        } else {
            return Err(anyhow::anyhow!("Expected Solutions results"));
        }
        Ok(())
    }

    #[test]
    fn query_with_prolog_works() -> Result<()> {
        let g = Graph::new()?;
        g.insert_turtle("@prefix ex: <http://example/> . ex:x a ex:T .")?;
        let mut p = BTreeMap::new();
        p.insert("ex".to_string(), "http://example/".to_string());
        let q = "SELECT ?s WHERE { ?s a ex:T }";
        let res = g.query_with_prolog(q, &p, None)?;
        if let QueryResults::Solutions(mut it) = res {
            let first = it.next().unwrap().unwrap();
            let s = first.get("s").unwrap().to_string();
            assert_eq!(s, "<http://example/x>");
        } else {
            return Err(anyhow::anyhow!("Expected Solutions results"));
        }
        Ok(())
    }

    #[test]
    fn test_cached_result_to_json() {
        // Test Boolean variant
        let bool_result = CachedResult::Boolean(true);
        let json = bool_result.to_json();
        assert_eq!(json, JsonValue::Bool(true));

        // Test Solutions variant
        let mut solutions = Vec::new();
        let mut row = BTreeMap::new();
        row.insert("name".to_string(), "Alice".to_string());
        row.insert("age".to_string(), "30".to_string());
        solutions.push(row);
        let solutions_result = CachedResult::Solutions(solutions);
        let json = solutions_result.to_json();

        if let JsonValue::Array(arr) = json {
            assert_eq!(arr.len(), 1);
            if let JsonValue::Object(obj) = &arr[0] {
                assert_eq!(obj.get("name").unwrap(), "Alice");
                assert_eq!(obj.get("age").unwrap(), "30");
            } else {
                panic!("Expected object in array");
            }
        } else {
            panic!("Expected array");
        }

        // Test Graph variant
        let graph_result = CachedResult::Graph(vec!["<http://example.org/subject> <http://example.org/predicate> <http://example.org/object> .".to_string()]);
        let json = graph_result.to_json();
        assert_eq!(json, JsonValue::String(String::new()));
    }

    #[test]
    fn test_graph_creation_and_basic_properties() -> Result<()> {
        let g = Graph::new()?;
        assert!(g.is_empty());
        assert_eq!(g.len(), 0);
        assert_eq!(g.current_epoch(), 1); // Epoch starts at 1

        // Test epoch bumping
        g.bump_epoch();
        assert_eq!(g.current_epoch(), 2);

        Ok(())
    }

    #[test]
    fn test_insert_turtle_in() -> Result<()> {
        let g = Graph::new()?;
        let ttl = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:knows ex:bob .
        "#;
        g.insert_turtle_in(ttl, "http://example.org/graph1")?;

        // Should have inserted the triple
        assert!(!g.is_empty());
        assert_eq!(g.len(), 1);

        Ok(())
    }

    #[test]
    fn test_query_cached() -> Result<()> {
        let g = Graph::new()?;
        let ttl = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:knows ex:bob .
            ex:bob ex:knows ex:charlie .
        "#;
        g.insert_turtle(ttl)?;

        // First query should execute and cache
        let result1 = g.query_cached("SELECT ?s WHERE { ?s <http://example.org/knows> ?o }")?;

        // Second query should use cache
        let result2 = g.query_cached("SELECT ?s WHERE { ?s <http://example.org/knows> ?o }")?;

        // Results should be identical
        match (&result1, &result2) {
            (CachedResult::Solutions(sol1), CachedResult::Solutions(sol2)) => {
                assert_eq!(sol1.len(), sol2.len());
                assert_eq!(sol1.len(), 2); // alice->bob, bob->charlie
            }
            _ => return Err(anyhow::anyhow!("Expected Solutions results")),
        }

        Ok(())
    }

    #[test]
    fn test_query_prepared() -> Result<()> {
        let g = Graph::new()?;
        let ttl = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:knows ex:bob .
        "#;
        g.insert_turtle(ttl)?;

        let query = Query::parse("SELECT ?s WHERE { ?s ?p ?o }", None)?;
        let results = g.query_prepared(&query)?;

        if let QueryResults::Solutions(mut it) = results {
            let first = it.next().unwrap().unwrap();
            let s = first.get("s").unwrap().to_string();
            assert_eq!(s, "<http://example.org/alice>");
        } else {
            return Err(anyhow::anyhow!("Expected Solutions results"));
        }

        Ok(())
    }

    #[test]
    fn test_quads_for_pattern() -> Result<()> {
        let g = Graph::new()?;
        g.insert_quad(
            "http://example.org/A",
            "http://example.org/rel",
            "http://example.org/B",
        )?;
        g.insert_quad(
            "http://example.org/A",
            "http://example.org/rel2",
            "http://example.org/C",
        )?;

        let a = NamedNode::new("http://example.org/A")?;
        let rel = NamedNode::new("http://example.org/rel")?;

        // Test filtering by subject only
        let quads = g.quads_for_pattern(Some(&a.clone().into()), None, None, None)?;
        assert_eq!(quads.len(), 2);

        // Test filtering by subject and predicate
        let quads = g.quads_for_pattern(Some(&a.into()), Some(&rel.into()), None, None)?;
        assert_eq!(quads.len(), 1);

        Ok(())
    }

    #[test]
    fn test_clear() -> Result<()> {
        let g = Graph::new()?;
        g.insert_quad(
            "http://example.org/A",
            "http://example.org/rel",
            "http://example.org/B",
        )?;

        assert!(!g.is_empty());
        assert_eq!(g.len(), 1);

        g.clear()?;

        assert!(g.is_empty());
        assert_eq!(g.len(), 0);

        Ok(())
    }

    #[test]
    fn test_hash_query() -> Result<()> {
        let g = Graph::new()?;

        let hash1 = g.hash_query("SELECT ?s WHERE { ?s ?p ?o }");
        let hash2 = g.hash_query("SELECT ?s WHERE { ?s ?p ?o }");
        let hash3 = g.hash_query("SELECT ?o WHERE { ?s ?p ?o }");

        // Same query should produce same hash
        assert_eq!(hash1, hash2);

        // Different query should produce different hash
        assert_ne!(hash1, hash3);

        Ok(())
    }

    #[test]
    fn test_materialize_results() -> Result<()> {
        let g = Graph::new()?;
        let ttl = r#"
            @prefix ex: <http://example.org/> .
            ex:alice ex:knows ex:bob .
        "#;
        g.insert_turtle(ttl)?;

        let query = "SELECT ?s WHERE { ?s ?p ?o }";
        let results = g.query(query)?;

        let cached = g.materialize_results(results)?;

        match cached {
            CachedResult::Solutions(solutions) => {
                assert_eq!(solutions.len(), 1);
                let row = &solutions[0];
                assert_eq!(row.get("s").unwrap(), "<http://example.org/alice>");
            }
            _ => return Err(anyhow::anyhow!("Expected Solutions result")),
        }

        Ok(())
    }

    #[test]
    fn test_build_prolog() {
        let mut prefixes = BTreeMap::new();
        prefixes.insert("ex".to_string(), "http://example.org/".to_string());
        prefixes.insert(
            "rdf".to_string(),
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#".to_string(),
        );

        let prolog = build_prolog(&prefixes, Some("http://example.org/base"));

        assert!(prolog.contains("PREFIX ex: <http://example.org/>"));
        assert!(prolog.contains("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"));
        assert!(prolog.contains("BASE <http://example.org/base>"));
    }

    #[test]
    fn test_build_prolog_no_base() {
        let mut prefixes = BTreeMap::new();
        prefixes.insert("ex".to_string(), "http://example.org/".to_string());

        let prolog = build_prolog(&prefixes, None);

        assert!(prolog.contains("PREFIX ex: <http://example.org/>"));
        assert!(!prolog.contains("BASE"));
    }

    #[test]
    fn test_clone_graph() -> Result<()> {
        let g1 = Graph::new()?;
        g1.insert_quad(
            "http://example.org/A",
            "http://example.org/rel",
            "http://example.org/B",
        )?;

        let g2 = g1.clone();

        // Both should have the same data
        assert_eq!(g1.len(), g2.len());
        assert_eq!(g1.is_empty(), g2.is_empty());

        // Both should be able to query the same data
        let results1 = g1.query("SELECT ?s WHERE { ?s ?p ?o }")?;
        let results2 = g2.query("SELECT ?s WHERE { ?s ?p ?o }")?;

        // Results should be identical
        match (results1, results2) {
            (QueryResults::Solutions(mut it1), QueryResults::Solutions(mut it2)) => {
                let row1 = it1.next().unwrap().unwrap();
                let row2 = it2.next().unwrap().unwrap();
                assert_eq!(row1.get("s").unwrap(), row2.get("s").unwrap());
            }
            _ => return Err(anyhow::anyhow!("Expected Solutions results")),
        }

        Ok(())
    }
}