oxigraph::store

Struct Transaction

Source
pub struct Transaction<'a> { /* private fields */ }
Expand description

An object to do operations during a transaction.

See Store::transaction for a more detailed description.

Implementations§

Source§

impl Transaction<'_>

Source

pub fn query( &self, query: impl TryInto<Query, Error = impl Into<EvaluationError>>, ) -> Result<QueryResults, EvaluationError>

Executes a SPARQL 1.1 query.

Usage example:

use oxigraph::model::*;
use oxigraph::sparql::{EvaluationError, QueryResults};
use oxigraph::store::Store;

let store = Store::new()?;
store.transaction(|mut transaction| {
    if let QueryResults::Solutions(solutions) =
        transaction.query("SELECT ?s WHERE { ?s ?p ?o }")?
    {
        for solution in solutions {
            if let Some(Term::NamedNode(s)) = solution?.get("s") {
                transaction.insert(QuadRef::new(
                    s,
                    vocab::rdf::TYPE,
                    NamedNodeRef::new_unchecked("http://example.com"),
                    GraphNameRef::DefaultGraph,
                ))?;
            }
        }
    }
    Result::<_, EvaluationError>::Ok(())
})?;
Source

pub fn query_opt( &self, query: impl TryInto<Query, Error = impl Into<EvaluationError>>, options: QueryOptions, ) -> Result<QueryResults, EvaluationError>

Executes a SPARQL 1.1 query with some options.

Usage example with a custom function serializing terms to N-Triples:

use oxigraph::model::*;
use oxigraph::sparql::{EvaluationError, QueryOptions, QueryResults};
use oxigraph::store::Store;

let store = Store::new()?;
store.transaction(|mut transaction| {
    if let QueryResults::Solutions(solutions) = transaction.query_opt(
        "SELECT ?s (<http://www.w3.org/ns/formats/N-Triples>(?s) AS ?nt) WHERE { ?s ?p ?o }",
        QueryOptions::default().with_custom_function(
            NamedNode::new_unchecked("http://www.w3.org/ns/formats/N-Triples"),
            |args| args.get(0).map(|t| Literal::from(t.to_string()).into()),
        ),
    )? {
        for solution in solutions {
            let solution = solution?;
            if let (Some(Term::NamedNode(s)), Some(nt)) =
                (solution.get("s"), solution.get("nt"))
            {
                transaction.insert(QuadRef::new(
                    s,
                    NamedNodeRef::new_unchecked("http://example.com/n-triples-representation"),
                    nt,
                    GraphNameRef::DefaultGraph,
                ))?;
            }
        }
    }
    Result::<_, EvaluationError>::Ok(())
})?;
Source

pub fn quads_for_pattern( &self, subject: Option<SubjectRef<'_>>, predicate: Option<NamedNodeRef<'_>>, object: Option<TermRef<'_>>, graph_name: Option<GraphNameRef<'_>>, ) -> QuadIter

Retrieves quads with a filter on each quad component.

Usage example:

use oxigraph::model::*;
use oxigraph::store::{StorageError, Store};

let store = Store::new()?;
let a = NamedNodeRef::new("http://example.com/a")?;
let b = NamedNodeRef::new("http://example.com/b")?;

// Copy all triples about ex:a to triples about ex:b
store.transaction(|mut transaction| {
    for q in transaction.quads_for_pattern(Some(a.into()), None, None, None) {
        let q = q?;
        transaction.insert(QuadRef::new(b, &q.predicate, &q.object, &q.graph_name))?;
    }
    Result::<_, StorageError>::Ok(())
})?;
Source

pub fn iter(&self) -> QuadIter

Returns all the quads contained in the store.

Source

pub fn contains<'b>( &self, quad: impl Into<QuadRef<'b>>, ) -> Result<bool, StorageError>

Checks if this store contains a given quad.

Source

pub fn len(&self) -> Result<usize, StorageError>

Returns the number of quads in the store.

this function executes a full scan.
Source

pub fn is_empty(&self) -> Result<bool, StorageError>

Returns if the store is empty.

Source

pub fn update( &mut self, update: impl TryInto<Update, Error = impl Into<EvaluationError>>, ) -> Result<(), EvaluationError>

Executes a SPARQL 1.1 update.

Usage example:

use oxigraph::model::*;
use oxigraph::sparql::EvaluationError;
use oxigraph::store::Store;

let store = Store::new()?;
store.transaction(|mut transaction| {
    // insertion
    transaction.update(
        "INSERT DATA { <http://example.com> <http://example.com> <http://example.com> }",
    )?;

    // we inspect the store contents
    let ex = NamedNodeRef::new_unchecked("http://example.com");
    assert!(transaction.contains(QuadRef::new(ex, ex, ex, GraphNameRef::DefaultGraph))?);
    Result::<_, EvaluationError>::Ok(())
})?;
Source

pub fn update_opt( &mut self, update: impl TryInto<Update, Error = impl Into<EvaluationError>>, options: impl Into<UpdateOptions>, ) -> Result<(), EvaluationError>

Executes a SPARQL 1.1 update with some options.

Source

pub fn load_from_reader( &mut self, parser: impl Into<RdfParser>, reader: impl Read, ) -> Result<(), LoaderError>

Loads a RDF file into the store.

This function is atomic, quite slow and memory hungry. To get much better performances you might want to use the bulk_loader.

Usage example:

use oxigraph::store::Store;
use oxigraph::io::RdfFormat;
use oxigraph::model::*;
use oxrdfio::RdfParser;

let store = Store::new()?;

// insert a dataset file (former load_dataset method)
let file = b"<http://example.com> <http://example.com> <http://example.com> <http://example.com/g> .";
store.transaction(|mut t| t.load_from_reader(RdfFormat::NQuads, file.as_ref()))?;

// insert a graph file (former load_graph method)
let file = b"<> <> <> .";
store.transaction(|mut t|
    t.load_from_reader(
        RdfParser::from_format(RdfFormat::Turtle)
            .with_base_iri("http://example.com")
            .unwrap()
            .without_named_graphs() // No named graphs allowed in the input
            .with_default_graph(NamedNodeRef::new("http://example.com/g2").unwrap()), // we put the file default graph inside of a named graph
        file.as_ref()
    )
)?;

// we inspect the store contents
let ex = NamedNodeRef::new("http://example.com")?;
assert!(store.contains(QuadRef::new(ex, ex, ex, NamedNodeRef::new("http://example.com/g")?))?);
assert!(store.contains(QuadRef::new(ex, ex, ex, NamedNodeRef::new("http://example.com/g2")?))?);
Source

pub fn load_graph( &mut self, reader: impl Read, format: impl Into<RdfFormat>, to_graph_name: impl Into<GraphName>, base_iri: Option<&str>, ) -> Result<(), LoaderError>

👎Deprecated since 0.4.0: use Transaction.load_from_reader instead

Loads a graph file (i.e. triples) into the store.

Usage example:

use oxigraph::io::RdfFormat;
use oxigraph::model::*;
use oxigraph::store::Store;

let store = Store::new()?;

// insertion
let file = b"<http://example.com> <http://example.com> <http://example.com> .";
store.transaction(|mut transaction| {
    transaction.load_graph(
        file.as_ref(),
        RdfFormat::NTriples,
        GraphName::DefaultGraph,
        None,
    )
})?;

// we inspect the store contents
let ex = NamedNodeRef::new("http://example.com")?;
assert!(store.contains(QuadRef::new(ex, ex, ex, GraphNameRef::DefaultGraph))?);
Source

pub fn load_dataset( &mut self, reader: impl Read, format: impl Into<RdfFormat>, base_iri: Option<&str>, ) -> Result<(), LoaderError>

👎Deprecated since 0.4.0: use Transaction.load_from_reader instead

Loads a dataset file (i.e. quads) into the store.

Usage example:

use oxigraph::io::RdfFormat;
use oxigraph::model::*;
use oxigraph::store::Store;

let store = Store::new()?;

// insertion
let file =
    b"<http://example.com> <http://example.com> <http://example.com> <http://example.com> .";
store.transaction(|mut transaction| {
    transaction.load_dataset(file.as_ref(), RdfFormat::NQuads, None)
})?;

// we inspect the store contents
let ex = NamedNodeRef::new_unchecked("http://example.com");
assert!(store.contains(QuadRef::new(ex, ex, ex, ex))?);
Source

pub fn insert<'b>( &mut self, quad: impl Into<QuadRef<'b>>, ) -> Result<bool, StorageError>

Adds a quad to this store.

Returns true if the quad was not already in the store.

Usage example:

use oxigraph::model::*;
use oxigraph::store::Store;

let ex = NamedNodeRef::new_unchecked("http://example.com");
let quad = QuadRef::new(ex, ex, ex, GraphNameRef::DefaultGraph);

let store = Store::new()?;
store.transaction(|mut transaction| transaction.insert(quad))?;
assert!(store.contains(quad)?);
Source

pub fn extend<'b>( &mut self, quads: impl IntoIterator<Item = impl Into<QuadRef<'b>>>, ) -> Result<(), StorageError>

Adds a set of quads to this store.

Source

pub fn remove<'b>( &mut self, quad: impl Into<QuadRef<'b>>, ) -> Result<bool, StorageError>

Removes a quad from this store.

Returns true if the quad was in the store and has been removed.

Usage example:

use oxigraph::model::*;
use oxigraph::store::Store;

let ex = NamedNodeRef::new_unchecked("http://example.com");
let quad = QuadRef::new(ex, ex, ex, GraphNameRef::DefaultGraph);
let store = Store::new()?;
store.transaction(|mut transaction| {
    transaction.insert(quad)?;
    transaction.remove(quad)
})?;
assert!(!store.contains(quad)?);
Source

pub fn named_graphs(&self) -> GraphNameIter

Returns all the store named graphs.

Source

pub fn contains_named_graph<'b>( &self, graph_name: impl Into<NamedOrBlankNodeRef<'b>>, ) -> Result<bool, StorageError>

Checks if the store contains a given graph.

Source

pub fn insert_named_graph<'b>( &mut self, graph_name: impl Into<NamedOrBlankNodeRef<'b>>, ) -> Result<bool, StorageError>

Inserts a graph into this store.

Returns true if the graph was not already in the store.

Usage example:

use oxigraph::model::NamedNodeRef;
use oxigraph::store::Store;

let ex = NamedNodeRef::new_unchecked("http://example.com");
let store = Store::new()?;
store.transaction(|mut transaction| transaction.insert_named_graph(ex))?;
assert_eq!(
    store.named_graphs().collect::<Result<Vec<_>, _>>()?,
    vec![ex.into_owned().into()]
);
Source

pub fn clear_graph<'b>( &mut self, graph_name: impl Into<GraphNameRef<'b>>, ) -> Result<(), StorageError>

Clears a graph from this store.

Usage example:

use oxigraph::model::{NamedNodeRef, QuadRef};
use oxigraph::store::Store;

let ex = NamedNodeRef::new_unchecked("http://example.com");
let quad = QuadRef::new(ex, ex, ex, ex);
let store = Store::new()?;
store.transaction(|mut transaction| {
    transaction.insert(quad)?;
    transaction.clear_graph(ex)
})?;
assert!(store.is_empty()?);
assert_eq!(1, store.named_graphs().count());
Source

pub fn remove_named_graph<'b>( &mut self, graph_name: impl Into<NamedOrBlankNodeRef<'b>>, ) -> Result<bool, StorageError>

Removes a graph from this store.

Returns true if the graph was in the store and has been removed.

Usage example:

use oxigraph::model::{NamedNodeRef, QuadRef};
use oxigraph::store::Store;

let ex = NamedNodeRef::new_unchecked("http://example.com");
let quad = QuadRef::new(ex, ex, ex, ex);
let store = Store::new()?;
store.transaction(|mut transaction| {
    transaction.insert(quad)?;
    transaction.remove_named_graph(ex)
})?;
assert!(store.is_empty()?);
assert_eq!(0, store.named_graphs().count());
Source

pub fn clear(&mut self) -> Result<(), StorageError>

Clears the store.

Usage example:

use oxigraph::model::*;
use oxigraph::store::Store;

let ex = NamedNodeRef::new_unchecked("http://example.com");
let store = Store::new()?;
store.transaction(|mut transaction| {
    transaction.insert(QuadRef::new(ex, ex, ex, ex))?;
    transaction.clear()
})?;
assert!(store.is_empty()?);

Trait Implementations§

Source§

impl IntoIterator for &Transaction<'_>

Source§

type IntoIter = QuadIter

Which kind of iterator are we turning this into?
Source§

type Item = Result<Quad, StorageError>

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Transaction<'a>

§

impl<'a> !RefUnwindSafe for Transaction<'a>

§

impl<'a> Send for Transaction<'a>

§

impl<'a> Sync for Transaction<'a>

§

impl<'a> Unpin for Transaction<'a>

§

impl<'a> !UnwindSafe for Transaction<'a>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
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