pub struct Graph { /* private fields */ }Expand description
Thread-safe Oxigraph wrapper with SPARQL caching.
The Graph type provides a high-level interface to an in-memory RDF triple store
with intelligent query caching. It wraps Oxigraph’s Store and adds:
- Query result caching: LRU cache for SPARQL query results
- Query plan caching: Cached query plans for faster execution
- Epoch-based invalidation: Automatic cache invalidation when graph changes
- Thread safety: Cheap cloning via
Arcfor concurrent access
§Thread Safety
Graph is designed for concurrent use. Cloning a Graph is cheap (O(1)) as it
shares the underlying store via Arc. Multiple threads can safely query the same
graph concurrently.
§Cache Invalidation
The graph maintains an epoch counter that increments whenever data is inserted. This automatically invalidates cached query results, ensuring consistency.
§Examples
§Basic usage
use ggen_core::graph::Graph;
// Create a new graph
let graph = Graph::new()?;
// Load RDF data
graph.insert_turtle(r#"
@prefix ex: <http://example.org/> .
ex:alice a ex:Person ;
ex:name "Alice" .
"#)?;
// Query the graph
let results = graph.query("SELECT ?name WHERE { ?s ex:name ?name }")?;Implementations§
Source§impl Graph
impl Graph
Sourcepub fn compute_hash(&self) -> Result<String>
pub fn compute_hash(&self) -> Result<String>
Compute a deterministic hash of the graph content
Source§impl Graph
impl Graph
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new empty graph
§Example
use ggen_core::graph::Graph;
let graph = Graph::new().unwrap();
assert!(graph.is_empty());Sourcepub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self>
Load RDF data from a file into a new Graph
§Errors
Returns an error if:
- The file cannot be opened or read
- The file format is unsupported
- The RDF syntax is invalid
Sourcepub fn insert_turtle(&self, turtle: &str) -> Result<()>
pub fn insert_turtle(&self, turtle: &str) -> Result<()>
Insert RDF data in Turtle format
Loads RDF triples from a Turtle string into the graph. The graph’s epoch counter is incremented, invalidating cached query results.
Sourcepub fn insert_turtle_with_base(
&self,
turtle: &str,
base_iri: &str,
) -> Result<()>
pub fn insert_turtle_with_base( &self, turtle: &str, base_iri: &str, ) -> Result<()>
Insert RDF data in Turtle format with a base IRI
Loads RDF triples from a Turtle string with a specified base IRI. Relative IRIs in the Turtle data will be resolved against this base.
Sourcepub fn insert_turtle_in(&self, turtle: &str, graph_iri: &str) -> Result<()>
pub fn insert_turtle_in(&self, turtle: &str, graph_iri: &str) -> Result<()>
Insert RDF data in Turtle format into a named graph
Loads RDF triples from a Turtle string into a specific named graph.
Sourcepub fn insert_quad(&self, s: &str, p: &str, o: &str) -> Result<()>
pub fn insert_quad(&self, s: &str, p: &str, o: &str) -> Result<()>
Insert a single RDF quad (triple) into the graph
Adds a single triple to the graph. All components must be valid IRIs. The graph’s epoch counter is incremented, invalidating cached query results.
Sourcepub fn insert_quad_in(
&self,
s: &str,
p: &str,
o: &str,
graph_iri: &str,
) -> Result<()>
pub fn insert_quad_in( &self, s: &str, p: &str, o: &str, graph_iri: &str, ) -> Result<()>
Insert a quad with a named graph
Adds a quad to a specific named graph.
Sourcepub fn insert_quad_object(&self, quad: &Quad) -> Result<()>
pub fn insert_quad_object(&self, quad: &Quad) -> Result<()>
Insert a Quad directly
Adds a quad object to the graph.
Sourcepub fn remove_quad(&self, s: &str, p: &str, o: &str) -> Result<()>
pub fn remove_quad(&self, s: &str, p: &str, o: &str) -> Result<()>
Remove a quad from the graph
Removes a specific quad from the graph.
Sourcepub fn remove_quad_from(
&self,
s: &str,
p: &str,
o: &str,
graph_iri: &str,
) -> Result<()>
pub fn remove_quad_from( &self, s: &str, p: &str, o: &str, graph_iri: &str, ) -> Result<()>
Remove a quad from a named graph
Removes a quad from a specific named graph.
Sourcepub fn remove_quad_object(&self, quad: &Quad) -> Result<()>
pub fn remove_quad_object(&self, quad: &Quad) -> Result<()>
Remove a Quad directly
Removes a quad object from the graph.
Sourcepub fn remove_for_pattern(
&self,
s: Option<&NamedOrBlankNode>,
p: Option<&NamedNode>,
o: Option<&Term>,
g: Option<&GraphName>,
) -> Result<usize>
pub fn remove_for_pattern( &self, s: Option<&NamedOrBlankNode>, p: Option<&NamedNode>, o: Option<&Term>, g: Option<&GraphName>, ) -> Result<usize>
Remove all quads matching a pattern
Removes all quads that match the specified pattern.
Sourcepub fn quads(&self) -> impl Iterator<Item = Result<Quad>> + '_
pub fn quads(&self) -> impl Iterator<Item = Result<Quad>> + '_
Get an iterator over all quads in the graph
Returns an iterator that yields all quads in the graph.
Sourcepub fn load_path<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub fn load_path<P: AsRef<Path>>(&self, path: P) -> Result<()>
Load RDF data from a file path
Automatically detects the RDF format from the file extension.
Sourcepub fn query_cached(&self, sparql: &str) -> Result<CachedResult>
pub fn query_cached(&self, sparql: &str) -> Result<CachedResult>
Execute a SPARQL query with caching
Results are cached based on query string and graph epoch. Cache is automatically invalidated when the graph changes.
Sourcepub fn query<'a>(&'a self, sparql: &str) -> Result<QueryResults<'a>>
pub fn query<'a>(&'a self, sparql: &str) -> Result<QueryResults<'a>>
Execute a SPARQL query (returns raw QueryResults)
This method provides direct access to Oxigraph’s QueryResults.
For full caching, use query_cached instead.
Sourcepub fn query_with_prolog<'a>(
&'a self,
sparql: &str,
prefixes: &BTreeMap<String, String>,
base: Option<&str>,
) -> Result<QueryResults<'a>>
pub fn query_with_prolog<'a>( &'a self, sparql: &str, prefixes: &BTreeMap<String, String>, base: Option<&str>, ) -> Result<QueryResults<'a>>
Execute a SPARQL query with PREFIX and BASE declarations
This method automatically prepends PREFIX and BASE declarations to the SPARQL query based on the provided prefixes and base IRI.
Sourcepub fn query_prepared<'a>(&'a self, q: &str) -> Result<QueryResults<'a>>
pub fn query_prepared<'a>(&'a self, q: &str) -> Result<QueryResults<'a>>
Execute a prepared SPARQL query (low-level API)
This method provides direct access to Oxigraph’s query API.
For most use cases, prefer query() or query_cached() instead.
Sourcepub fn quads_for_pattern(
&self,
s: Option<&NamedOrBlankNode>,
p: Option<&NamedNode>,
o: Option<&Term>,
g: Option<&GraphName>,
) -> Result<Vec<Quad>>
pub fn quads_for_pattern( &self, s: Option<&NamedOrBlankNode>, p: Option<&NamedNode>, o: Option<&Term>, g: Option<&GraphName>, ) -> Result<Vec<Quad>>
Find quads matching a pattern
Searches for quads (triples) in the graph that match the specified pattern.
Any component can be None to match any value (wildcard).
Sourcepub fn clear(&self) -> Result<()>
pub fn clear(&self) -> Result<()>
Clear all data from the graph
Removes all triples from the graph and increments the epoch counter, invalidating all cached query results.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of triples in the graph
Returns the total count of triples (quads) stored in the graph.
§Note
If an error occurs while getting the length, this method returns 0.
For explicit error handling, use len_result() instead.
Sourcepub fn len_result(&self) -> Result<usize>
pub fn len_result(&self) -> Result<usize>
Get the number of triples in the graph with explicit error handling
Returns the total count of triples (quads) stored in the graph. Returns an error if the length cannot be determined.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Graph
impl !RefUnwindSafe for Graph
impl Send for Graph
impl Sync for Graph
impl Unpin for Graph
impl !UnwindSafe for Graph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request