crdf 0.1.0

A CRDT-based RDF graph implementation in Rust, built on top of crdt-graph.
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
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;

use uuid::Uuid;

use crate::error::CrdfError;
use crate::term::RdfTerm;
use crate::triple::Triple;
use crate::types::{remove_edge_op, AddEdge, AddVertex, Graph, RemoveEdge, RemoveVertex};

/// A compound operation representing an RDF triple addition.
///
/// May include vertex creation for subject and/or object if they are new terms.
#[derive(Clone, Debug)]
pub struct AddTripleOp {
    pub subject_vertex: Option<AddVertex>,
    pub object_vertex: Option<AddVertex>,
    pub edge: AddEdge,
}

/// A compound operation representing an RDF triple removal.
#[derive(Clone, Debug)]
pub struct RemoveTripleOp {
    pub edge_remove: RemoveEdge,
}

/// A high-level RDF operation that can be broadcast to other replicas.
#[derive(Clone, Debug)]
pub enum RdfOperation {
    /// A triple was added to the graph.
    AddTriple(AddTripleOp),
    /// A triple was removed from the graph.
    RemoveTriple(RemoveTripleOp),
}

/// RDF file serialization formats supported by [`RdfGraph::write_rdf_file`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RdfFileFormat {
    /// N-Triples syntax (`.nt`).
    NTriples,
    /// FlatBuffers binary format (`.crdf`) preserving full CRDT operation history.
    FlatBuffers,
}

/// An RDF graph backed by a 2P2P-Graph CRDT.
///
/// Each unique RDF term (IRI, blank node, or literal) that appears as a subject
/// or object becomes a vertex in the underlying CRDT graph. Each triple becomes
/// a directed edge from the subject vertex to the object vertex, with the
/// predicate IRI stored as edge data.
pub struct RdfGraph {
    graph: Graph,
    term_to_vertex: HashMap<RdfTerm, HashSet<Uuid>>,
}

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

impl RdfGraph {
    /// Creates an empty RDF graph.
    pub fn new() -> Self {
        Self {
            graph: Graph::new(),
            term_to_vertex: HashMap::new(),
        }
    }

    /// Constructs an `RdfGraph` from pre-built CRDT graph and term mapping.
    ///
    /// Used by the FlatBuffers decoder to reconstruct graphs from serialized data.
    pub(crate) fn from_raw(graph: Graph, term_to_vertex: HashMap<RdfTerm, HashSet<Uuid>>) -> Self {
        Self {
            graph,
            term_to_vertex,
        }
    }

    /// Returns a reverse map from vertex UUID to term.
    fn vertex_to_term_map(&self) -> HashMap<&Uuid, &RdfTerm> {
        self.term_to_vertex
            .iter()
            .flat_map(|(term, ids)| ids.iter().map(move |id| (id, term)))
            .collect()
    }

    /// Adds a triple to the graph (atSource).
    ///
    /// Returns the operation to broadcast to other replicas.
    ///
    /// # Errors
    /// - [`CrdfError::LiteralSubject`] if the subject is a literal.
    pub fn add_triple(
        &mut self,
        subject: RdfTerm,
        predicate: impl Into<String>,
        object: RdfTerm,
    ) -> Result<RdfOperation, CrdfError> {
        if subject.is_literal() {
            return Err(CrdfError::LiteralSubject);
        }

        let predicate = predicate.into();

        if predicate.is_empty() {
            return Err(CrdfError::InvalidPredicate);
        }

        let (subject_vertex_op, subject_id) = self.get_or_create_vertex(subject)?;
        let (object_vertex_op, object_id) = self.get_or_create_vertex(object)?;

        let edge = AddEdge {
            id: Uuid::now_v7(),
            source: subject_id,
            target: object_id,
            predicate,
        };

        self.graph.prepare(edge.clone().into())?;

        Ok(RdfOperation::AddTriple(AddTripleOp {
            subject_vertex: subject_vertex_op,
            object_vertex: object_vertex_op,
            edge,
        }))
    }

    /// Removes a triple from the graph (atSource).
    ///
    /// Returns the operation to broadcast to other replicas.
    pub fn remove_triple(
        &mut self,
        subject: &RdfTerm,
        predicate: &str,
        object: &RdfTerm,
    ) -> Result<RdfOperation, CrdfError> {
        let subject_ids = self
            .term_to_vertex
            .get(subject)
            .ok_or(CrdfError::TripleNotFound)?;
        let object_ids = self
            .term_to_vertex
            .get(object)
            .ok_or(CrdfError::TripleNotFound)?;

        let edge = self
            .graph
            .edges()
            .find(|ea| {
                subject_ids.contains(&ea.source)
                    && object_ids.contains(&ea.target)
                    && ea.predicate == predicate
            })
            .ok_or(CrdfError::TripleNotFound)?;

        let remove = RemoveEdge {
            id: Uuid::now_v7(),
            add_edge_id: edge.id,
        };

        self.graph.prepare(remove_edge_op(remove.clone()))?;

        Ok(RdfOperation::RemoveTriple(RemoveTripleOp {
            edge_remove: remove,
        }))
    }

    /// Applies an operation received from a remote replica (downstream).
    pub fn apply_downstream(&mut self, op: RdfOperation) -> Result<(), CrdfError> {
        match op {
            RdfOperation::AddTriple(add) => {
                if let Some(sv) = add.subject_vertex {
                    self.term_to_vertex
                        .entry(sv.term.clone())
                        .or_default()
                        .insert(sv.id);
                    self.graph.apply_downstream(sv.into())?;
                }
                if let Some(ov) = add.object_vertex {
                    self.term_to_vertex
                        .entry(ov.term.clone())
                        .or_default()
                        .insert(ov.id);
                    self.graph.apply_downstream(ov.into())?;
                }
                self.graph.apply_downstream(add.edge.into())?;
            }
            RdfOperation::RemoveTriple(remove) => {
                self.graph
                    .apply_downstream(remove_edge_op(remove.edge_remove))?;
            }
        }
        Ok(())
    }

    /// Returns all active triples in the graph.
    pub fn triples(&self) -> Vec<Triple> {
        let vertex_to_term = self.vertex_to_term_map();

        self.graph
            .edges()
            .filter_map(|ea| {
                let subject = vertex_to_term.get(&ea.source)?;
                let object = vertex_to_term.get(&ea.target)?;
                Some(Triple::new(
                    (*subject).clone(),
                    ea.predicate.clone(),
                    (*object).clone(),
                ))
            })
            .collect()
    }

    /// Finds triples matching a pattern. `None` means "match any".
    pub fn triples_matching(
        &self,
        subject: Option<&RdfTerm>,
        predicate: Option<&str>,
        object: Option<&RdfTerm>,
    ) -> Vec<Triple> {
        self.triples()
            .into_iter()
            .filter(|t| {
                subject.is_none_or(|s| t.subject == *s)
                    && predicate.is_none_or(|p| t.predicate == p)
                    && object.is_none_or(|o| t.object == *o)
            })
            .collect()
    }

    /// Returns `true` if the graph contains the specified triple.
    pub fn contains_triple(&self, subject: &RdfTerm, predicate: &str, object: &RdfTerm) -> bool {
        let Some(subject_ids) = self.term_to_vertex.get(subject) else {
            return false;
        };
        let Some(object_ids) = self.term_to_vertex.get(object) else {
            return false;
        };

        self.graph.edges().any(|ea| {
            subject_ids.contains(&ea.source)
                && object_ids.contains(&ea.target)
                && ea.predicate == predicate
        })
    }

    /// Returns the number of active triples in the graph.
    pub fn len(&self) -> usize {
        self.graph.edge_count()
    }

    /// Returns `true` if the graph contains no active triples.
    pub fn is_empty(&self) -> bool {
        self.graph.is_empty()
    }

    /// Returns all vertex-add operations (`V_A`) in the underlying CRDT graph.
    pub fn all_vertices_added(&self) -> &[AddVertex] {
        self.graph.all_vertices_added()
    }

    /// Returns all vertex-remove operations (`V_R`) in the underlying CRDT graph.
    pub fn all_vertices_removed(&self) -> &[RemoveVertex] {
        self.graph.all_vertices_removed()
    }

    /// Returns all edge-add operations (`E_A`) in the underlying CRDT graph.
    pub fn all_edges_added(&self) -> &[AddEdge] {
        self.graph.all_edges_added()
    }

    /// Returns all edge-remove operations (`E_R`) in the underlying CRDT graph.
    pub fn all_edges_removed(&self) -> &[RemoveEdge] {
        self.graph.all_edges_removed()
    }

    /// Returns all unique subjects that appear in active triples.
    pub fn subjects(&self) -> Vec<&RdfTerm> {
        let vertex_to_term = self.vertex_to_term_map();

        let mut seen = HashSet::new();
        self.graph
            .edges()
            .filter_map(|ea| vertex_to_term.get(&ea.source).copied())
            .filter(|term| seen.insert(*term as *const RdfTerm))
            .collect()
    }

    /// Returns all unique predicates that appear in active triples.
    pub fn predicates(&self) -> Vec<&str> {
        let mut predicates: Vec<&str> =
            self.graph.edges().map(|ea| ea.predicate.as_str()).collect();
        predicates.sort();
        predicates.dedup();
        predicates
    }

    /// Returns all unique objects that appear in active triples.
    pub fn objects(&self) -> Vec<&RdfTerm> {
        let vertex_to_term = self.vertex_to_term_map();

        let mut seen = HashSet::new();
        self.graph
            .edges()
            .filter_map(|ea| vertex_to_term.get(&ea.target).copied())
            .filter(|term| seen.insert(*term as *const RdfTerm))
            .collect()
    }

    /// Returns all triples with the given subject.
    pub fn triples_for_subject(&self, subject: &RdfTerm) -> Vec<Triple> {
        self.triples_matching(Some(subject), None, None)
    }

    /// Returns all triples with the given predicate.
    pub fn triples_for_predicate(&self, predicate: &str) -> Vec<Triple> {
        self.triples_matching(None, Some(predicate), None)
    }

    /// Returns all triples with the given object.
    pub fn triples_for_object(&self, object: &RdfTerm) -> Vec<Triple> {
        self.triples_matching(None, None, Some(object))
    }

    /// Returns all objects for triples matching the given subject and predicate.
    pub fn objects_for_subject_predicate(
        &self,
        subject: &RdfTerm,
        predicate: &str,
    ) -> Vec<RdfTerm> {
        self.triples_matching(Some(subject), Some(predicate), None)
            .into_iter()
            .map(|t| t.object)
            .collect()
    }

    /// Returns all subjects for triples matching the given predicate and object.
    pub fn subjects_for_predicate_object(&self, predicate: &str, object: &RdfTerm) -> Vec<RdfTerm> {
        self.triples_matching(None, Some(predicate), Some(object))
            .into_iter()
            .map(|t| t.subject)
            .collect()
    }

    /// Returns all predicates for triples matching the given subject and object.
    pub fn predicates_for_subject_object(
        &self,
        subject: &RdfTerm,
        object: &RdfTerm,
    ) -> Vec<String> {
        self.triples_matching(Some(subject), None, Some(object))
            .into_iter()
            .map(|t| t.predicate)
            .collect()
    }

    fn get_or_create_vertex(
        &mut self,
        term: RdfTerm,
    ) -> Result<(Option<AddVertex>, Uuid), CrdfError> {
        if let Some(ids) = self.term_to_vertex.get(&term) {
            // Return any existing UUID for this term
            Ok((None, *ids.iter().next().unwrap()))
        } else {
            let vertex = AddVertex {
                id: Uuid::now_v7(),
                term: term.clone(),
            };
            let id = vertex.id;
            self.graph.prepare(vertex.clone().into())?;
            self.term_to_vertex.entry(term).or_default().insert(id);
            Ok((Some(vertex), id))
        }
    }

    /// Converts this graph into an `oxrdf::Graph`.
    pub fn to_oxrdf(&self) -> Result<oxrdf::Graph, CrdfError> {
        let mut graph = oxrdf::Graph::default();
        for triple in self.triples() {
            let oxrdf_triple = triple.to_oxrdf()?;
            graph.insert(oxrdf_triple.as_ref());
        }
        Ok(graph)
    }

    /// Writes this graph as an RDF file.
    pub fn write_rdf_file<P: AsRef<Path>>(
        &self,
        path: P,
        format: RdfFileFormat,
    ) -> Result<(), CrdfError> {
        let file = File::create(path)?;
        let mut writer = BufWriter::new(file);

        match format {
            RdfFileFormat::NTriples => {
                let graph = self.to_oxrdf()?;
                for triple in &graph {
                    writeln!(writer, "{triple}")?;
                }
            }
            RdfFileFormat::FlatBuffers => {
                let buf = crate::flatbuffers::encode(self);
                writer.write_all(&buf)?;
            }
        }

        writer.flush()?;
        Ok(())
    }

    /// Reads an RDF graph from a FlatBuffers file.
    pub fn read_flatbuffers_file<P: AsRef<Path>>(path: P) -> Result<Self, CrdfError> {
        let buf = std::fs::read(path)?;
        Ok(crate::flatbuffers::decode(&buf)?)
    }
}

impl TryFrom<&RdfGraph> for oxrdf::Graph {
    type Error = CrdfError;

    fn try_from(value: &RdfGraph) -> Result<Self, Self::Error> {
        value.to_oxrdf()
    }
}

impl RdfGraph {
    /// Creates an `RdfGraph` by importing all triples from an `oxrdf::Graph`.
    pub fn from_oxrdf(graph: &oxrdf::Graph) -> Result<Self, CrdfError> {
        let mut rdf_graph = Self::new();
        for triple_ref in graph {
            let subject: RdfTerm = triple_ref.subject.into();
            let predicate = triple_ref.predicate.as_str();
            let object: RdfTerm = triple_ref.object.into();
            rdf_graph.add_triple(subject, predicate, object)?;
        }
        Ok(rdf_graph)
    }
}

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

    const FOAF_NAME: &str = "http://xmlns.com/foaf/0.1/name";
    const FOAF_KNOWS: &str = "http://xmlns.com/foaf/0.1/knows";
    const RDF_TYPE: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
    const FOAF_PERSON: &str = "http://xmlns.com/foaf/0.1/Person";

    fn alice() -> RdfTerm {
        RdfTerm::iri("http://example.org/alice")
    }

    fn bob() -> RdfTerm {
        RdfTerm::iri("http://example.org/bob")
    }

    #[test]
    fn add_and_query_triple() {
        let mut g = RdfGraph::new();
        g.add_triple(alice(), FOAF_NAME, RdfTerm::literal("Alice"))
            .unwrap();

        assert_eq!(g.len(), 1);
        assert!(g.contains_triple(&alice(), FOAF_NAME, &RdfTerm::literal("Alice")));
    }

    #[test]
    fn literal_subject_rejected() {
        let mut g = RdfGraph::new();
        let result = g.add_triple(RdfTerm::literal("bad"), FOAF_NAME, alice());
        assert!(result.is_err());
    }

    #[test]
    fn remove_triple() {
        let mut g = RdfGraph::new();
        g.add_triple(alice(), FOAF_NAME, RdfTerm::literal("Alice"))
            .unwrap();

        g.remove_triple(&alice(), FOAF_NAME, &RdfTerm::literal("Alice"))
            .unwrap();

        assert_eq!(g.len(), 0);
        assert!(!g.contains_triple(&alice(), FOAF_NAME, &RdfTerm::literal("Alice")));
    }

    #[test]
    fn triples_matching_by_subject() {
        let mut g = RdfGraph::new();
        g.add_triple(alice(), FOAF_NAME, RdfTerm::literal("Alice"))
            .unwrap();
        g.add_triple(alice(), FOAF_KNOWS, bob()).unwrap();
        g.add_triple(bob(), FOAF_NAME, RdfTerm::literal("Bob"))
            .unwrap();

        let alice_triples = g.triples_matching(Some(&alice()), None, None);
        assert_eq!(alice_triples.len(), 2);
    }

    #[test]
    fn triples_matching_by_predicate() {
        let mut g = RdfGraph::new();
        g.add_triple(alice(), FOAF_NAME, RdfTerm::literal("Alice"))
            .unwrap();
        g.add_triple(bob(), FOAF_NAME, RdfTerm::literal("Bob"))
            .unwrap();
        g.add_triple(alice(), FOAF_KNOWS, bob()).unwrap();

        let name_triples = g.triples_matching(None, Some(FOAF_NAME), None);
        assert_eq!(name_triples.len(), 2);
    }

    #[test]
    fn shared_vertex_deduplication() {
        let mut g = RdfGraph::new();
        g.add_triple(alice(), FOAF_NAME, RdfTerm::literal("Alice"))
            .unwrap();
        g.add_triple(alice(), FOAF_KNOWS, bob()).unwrap();

        // "alice" vertex should be created only once
        // 3 unique terms = 3 vertex sets
        assert_eq!(g.term_to_vertex.len(), 3);
        // Each term has exactly one UUID
        for ids in g.term_to_vertex.values() {
            assert_eq!(ids.len(), 1);
        }
    }

    #[test]
    fn two_replica_sync() {
        let mut replica_a = RdfGraph::new();
        let mut replica_b = RdfGraph::new();

        // Replica A adds a triple
        let op1 = replica_a
            .add_triple(alice(), FOAF_NAME, RdfTerm::literal("Alice"))
            .unwrap();

        // Broadcast to replica B
        replica_b.apply_downstream(op1).unwrap();

        // Both should have the same triple
        assert_eq!(replica_a.len(), 1);
        assert_eq!(replica_b.len(), 1);
        assert!(replica_b.contains_triple(&alice(), FOAF_NAME, &RdfTerm::literal("Alice")));
    }

    #[test]
    fn two_replica_concurrent_add() {
        let mut replica_a = RdfGraph::new();
        let mut replica_b = RdfGraph::new();

        // Both replicas independently add triples about alice
        let op_a = replica_a
            .add_triple(alice(), FOAF_NAME, RdfTerm::literal("Alice"))
            .unwrap();
        let op_b = replica_b
            .add_triple(alice(), RDF_TYPE, RdfTerm::iri(FOAF_PERSON))
            .unwrap();

        // Exchange operations
        replica_b.apply_downstream(op_a).unwrap();
        replica_a.apply_downstream(op_b).unwrap();

        // Both should have 2 triples
        assert_eq!(replica_a.len(), 2);
        assert_eq!(replica_b.len(), 2);
    }

    #[test]
    fn remove_then_sync() {
        let mut replica_a = RdfGraph::new();
        let mut replica_b = RdfGraph::new();

        let op1 = replica_a
            .add_triple(alice(), FOAF_NAME, RdfTerm::literal("Alice"))
            .unwrap();
        replica_b.apply_downstream(op1).unwrap();

        let op2 = replica_a
            .remove_triple(&alice(), FOAF_NAME, &RdfTerm::literal("Alice"))
            .unwrap();
        replica_b.apply_downstream(op2).unwrap();

        assert_eq!(replica_a.len(), 0);
        assert_eq!(replica_b.len(), 0);
    }

    #[test]
    fn blank_node_as_subject() {
        let mut g = RdfGraph::new();
        let bnode = RdfTerm::blank_node("b0");
        g.add_triple(bnode.clone(), FOAF_NAME, RdfTerm::literal("Anonymous"))
            .unwrap();

        assert!(g.contains_triple(&bnode, FOAF_NAME, &RdfTerm::literal("Anonymous")));
    }

    #[test]
    fn triple_display_format() {
        let t = Triple::new(alice(), FOAF_NAME, RdfTerm::literal("Alice"));
        let s = t.to_string();
        assert!(s.contains("<http://example.org/alice>"));
        assert!(s.contains("<http://xmlns.com/foaf/0.1/name>"));
        assert!(s.contains("\"Alice\""));
    }

    #[test]
    fn empty_graph() {
        let g = RdfGraph::new();
        assert!(g.is_empty());
        assert_eq!(g.len(), 0);
        assert!(g.triples().is_empty());
    }
}