crdf
A CRDT-based RDF graph implementation in Rust, built on top of crdt-graph.
Overview
crdf provides an RDF graph that can be replicated across multiple nodes using operation-based CRDTs. Each RDF term (IRI, blank node, or literal) maps to a vertex, and each triple becomes a directed edge in the underlying 2P2P-Graph CRDT.
Key properties:
- Conflict-free — Concurrent additions on different replicas converge automatically.
- Op-based replication — Operations returned by
add_triple/remove_triplecan be broadcast and applied on remote replicas viaapply_downstream. - RDF 1.1 compliant — Literals always carry a datatype IRI; language-tagged literals use
rdf:langString; language tags are normalized to lowercase.
Features
- RDF term types —
RdfTerm::Iri,RdfTerm::BlankNode,RdfTerm::Literalwith full accessor API. - Typed literals —
Fromconversions forbool,i32,i64,f32,f64,&str, andString. - XSD constants —
XSD_STRING,XSD_INTEGER,XSD_INT,XSD_DOUBLE,XSD_FLOAT,XSD_BOOLEAN. - Pattern matching —
triples_matching(subject?, predicate?, object?)with wildcard support. - Graph queries —
subjects(),predicates(),objects(),triples_for_subject(), and more. - N-Triples display —
Displayimplementations output valid N-Triples syntax. - oxrdf conversion — Convert terms, triples, and whole graphs into
oxrdftypes. - RDF file output — Export a graph to an RDF file (currently N-Triples).
- Error handling — All fallible operations return
Result<_, CrdfError>viathiserror. - UUID v7 identifiers — Time-ordered, globally unique IDs for vertices and edges.
Quick Start
use ;
#
Typed Literals
use Literal;
#
Export to RDF File
use ;
#
API
| Method | Description |
|---|---|
RdfGraph::new() |
Creates an empty RDF graph. |
add_triple(s, p, o) |
Adds a triple (atSource). Returns the operation to broadcast. |
remove_triple(s, p, o) |
Removes a triple (atSource). Returns the operation to broadcast. |
apply_downstream(op) |
Applies an operation received from a remote replica. |
contains_triple(s, p, o) |
Returns true if the triple exists. |
triples() |
Returns all active triples. |
triples_matching(s?, p?, o?) |
Pattern-based triple lookup. |
subjects() / predicates() / objects() |
Unique terms in each position. |
to_oxrdf() |
Converts the graph to oxrdf::Graph. |
write_rdf_file(path, format) |
Writes an RDF file from the graph. |
len() / is_empty() |
Triple count and emptiness check. |
Errors
All fallible operations return Result<_, CrdfError>:
| Variant | Description |
|---|---|
Graph(TwoPTwoPGraphError) |
Error from the underlying CRDT graph. |
TripleNotFound |
The specified triple was not found. |
LiteralSubject |
Literals cannot be used as subjects. |
InvalidPredicate |
Predicates must be non-empty IRIs. |
EmptyLanguageTag |
Language tags must be non-empty (BCP 47). |
LangStringDatatype |
rdf:langString cannot be set directly; use with_language(). |