Skip to main content

Transaction

Struct Transaction 

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

An object to do operations during a transaction.

See Store::start_transaction for a more detailed description.

Implementations§

Source§

impl<'a> Transaction<'a>

Source

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

👎Deprecated since 0.5.0:

Use SparqlEvaluator interface instead

Executes a SPARQL 1.1 query.

Usage example:

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

let store = Store::new()?;
let mut transaction = store.start_transaction()?;
let mut triples_to_add = Vec::new();
if let QueryResults::Solutions(solutions) = SparqlEvaluator::new()
    .parse_query("SELECT ?s WHERE { ?s ?p ?o }")?
    .on_transaction(&transaction)
    .execute()?
{
    for solution in solutions {
        if let Some(Term::NamedNode(s)) = solution?.get("s") {
            triples_to_add.push(Quad::new(
                s.clone(),
                vocab::rdf::TYPE,
                NamedNode::new_unchecked("http://example.com"),
                GraphName::DefaultGraph,
            ));
        }
    }
}
for triple in triples_to_add {
    transaction.insert(&triple);
}
transaction.commit()?;
Source

pub fn query_opt( &self, query: impl TryInto<Query, Error = impl Into<QueryEvaluationError>>, options: SparqlEvaluator, ) -> Result<QueryResults<'_>, QueryEvaluationError>

👎Deprecated since 0.5.0:

Use SparqlEvaluator interface instead

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::{QueryResults, SparqlEvaluator};
use oxigraph::store::Store;

let store = Store::new()?;
let mut transaction = store.start_transaction()?;
let mut triples_to_add = Vec::new();
if let QueryResults::Solutions(solutions) = SparqlEvaluator::new()
    .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()),
    )
    .parse_query(
        "SELECT ?s (<http://www.w3.org/ns/formats/N-Triples>(?s) AS ?nt) WHERE { ?s ?p ?o }",
    )?
    .on_transaction(&transaction)
    .execute()?
{
    for solution in solutions {
        let solution = solution?;
        if let (Some(Term::NamedNode(s)), Some(nt)) = (solution.get("s"), solution.get("nt")) {
            triples_to_add.push(Quad::new(
                s.clone(),
                NamedNode::new_unchecked("http://example.com/n-triples-representation"),
                nt.clone(),
                GraphName::DefaultGraph,
            ));
        }
    }
}
for triple in triples_to_add {
    transaction.insert(&triple);
}
transaction.commit()?;
Source

pub fn quads_for_pattern( &self, subject: Option<NamedOrBlankNodeRef<'_>>, 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::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
let mut transaction = store.start_transaction()?;
let triples = transaction
    .quads_for_pattern(Some(a.into()), None, None, None)
    .collect::<Result<Vec<_>, _>>()?;
for triple in triples {
    transaction.insert(QuadRef::new(
        b,
        &triple.predicate,
        &triple.object,
        &triple.graph_name,
    ));
}
transaction.commit()?;
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<UpdateEvaluationError>>, ) -> Result<(), UpdateEvaluationError>

Executes a SPARQL 1.1 update.

Usage example:

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

let store = Store::new()?;
let mut transaction = store.start_transaction()?;
// insertion
SparqlEvaluator::new()
    .parse_update(
        "INSERT DATA { <http://example.com> <http://example.com> <http://example.com> }",
    )?
    .on_transaction(&mut transaction)
    .execute()?;

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

transaction.commit()?;
Source

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

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 an 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::{RdfParser, RdfFormat};
use oxigraph::model::*;

let store = Store::new()?;

// insert a dataset file
let file = "<http://example.com> <http://example.com> <http://example.com> <http://example.com/g> .";
let mut transaction = store.start_transaction()?;
transaction.load_from_reader(RdfFormat::NQuads, file.as_bytes())?;
transaction.commit()?;

// insert a graph file
let file = "<> <> <> .";
let mut transaction = store.start_transaction()?;
transaction.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_bytes()
)?;
transaction.commit()?;

// 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_from_slice( &mut self, parser: impl Into<RdfParser>, slice: &(impl AsRef<[u8]> + ?Sized), ) -> Result<(), LoaderError>

Loads an 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::{RdfParser, RdfFormat};
use oxigraph::model::*;

let store = Store::new()?;

// insert a dataset file
let file = "<http://example.com> <http://example.com> <http://example.com> <http://example.com/g> .";
let mut transaction = store.start_transaction()?;
transaction.load_from_reader(RdfFormat::NQuads, file.as_bytes())?;
transaction.commit()?;

// insert a graph file
let file = "<> <> <> .";
let mut transaction = store.start_transaction()?;
transaction.load_from_slice(
    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
)?;
transaction.commit()?;

// 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 insert<'b>(&mut self, quad: impl Into<QuadRef<'b>>)

Adds a quad to this 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()?;
let mut transaction = store.start_transaction()?;
transaction.insert(quad);
transaction.commit()?;
assert!(store.contains(quad)?);
Source

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

Adds a set of quads to this 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()?;
let mut transaction = store.start_transaction()?;
transaction.extend([quad]);
transaction.commit()?;
assert!(store.contains(quad)?);
Source

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

Removes a quad from this 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()?;
let mut transaction = store.start_transaction()?;
transaction.insert(quad);
transaction.remove(quad);
transaction.commit()?;
assert!(!store.contains(quad)?);
Source

pub fn named_graphs(&self) -> GraphNameIter<'_>

Returns all the named graphs in the store.

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

Inserts a graph into this store.

Usage example:

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

let ex = NamedNodeRef::new_unchecked("http://example.com");
let store = Store::new()?;
let mut transaction = store.start_transaction()?;
transaction.insert_named_graph(ex);
transaction.commit()?;
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()?;
let mut transaction = store.start_transaction()?;
transaction.insert(quad);
transaction.clear_graph(ex)?;
transaction.commit()?;
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<(), StorageError>

Removes 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()?;
let mut transaction = store.start_transaction()?;
transaction.insert(quad);
transaction.remove_named_graph(ex)?;
transaction.commit()?;
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()?;
let mut transaction = store.start_transaction()?;
transaction.insert(QuadRef::new(ex, ex, ex, ex));
transaction.clear()?;
transaction.commit()?;
assert!(store.is_empty()?);
Source

pub fn commit(self) -> Result<(), StorageError>

Commits the transaction, i.e., apply its modifications to the underlying store.

Usage example:

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

let ex = NamedNodeRef::new_unchecked("http://example.com");
let store = Store::new()?;
let mut transaction = store.start_transaction()?;
transaction.insert(QuadRef::new(ex, ex, ex, ex));
transaction.commit()?;
assert!(store.contains(QuadRef::new(ex, ex, ex, ex))?);

Trait Implementations§

Source§

impl<'a> IntoIterator for &'a Transaction<'_>

Source§

type Item = Result<Quad, StorageError>

The type of the elements being iterated over.
Source§

type IntoIter = QuadIter<'a>

Which kind of iterator are we turning this into?
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> UnsafeUnpin 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