Graph

Struct Graph 

Source
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 Arc for 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

Source

pub fn compute_hash(&self) -> Result<String>

Compute a deterministic hash of the graph content

Source§

impl Graph

Source

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());
Source

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
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn insert_quad_object(&self, quad: &Quad) -> Result<()>

Insert a Quad directly

Adds a quad object to the graph.

Source

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.

Source

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.

Source

pub fn remove_quad_object(&self, quad: &Quad) -> Result<()>

Remove a Quad directly

Removes a quad object from the graph.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

pub fn is_empty(&self) -> bool

Check if the graph is empty

Returns true if the graph contains no triples, false otherwise.

Trait Implementations§

Source§

impl Clone for Graph

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,