Struct oxigraph::store::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<'a> Transaction<'a>

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::store::Store;
use oxigraph::model::*;
use oxigraph::sparql::{EvaluationError, QueryResults};

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::store::Store;
use oxigraph::model::*;
use oxigraph::sparql::{EvaluationError, QueryOptions, QueryResults};

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: Usage example:

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

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.

Warning: 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::store::Store;
use oxigraph::model::*;
use oxigraph::sparql::EvaluationError;

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_graph<'b>( &mut self, reader: impl BufRead, format: GraphFormat, to_graph_name: impl Into<GraphNameRef<'b>>, base_iri: Option<&str> ) -> Result<(), LoaderError>

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

Usage example:

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

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(), GraphFormat::NTriples, GraphNameRef::DefaultGraph, None)
})?;

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

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

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

Usage example:

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

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(), DatasetFormat::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::store::Store;
use oxigraph::model::*;

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::store::Store;
use oxigraph::model::*;

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::store::Store;
use oxigraph::model::NamedNodeRef;

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::store::Store;
use oxigraph::model::{NamedNodeRef, QuadRef};

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::store::Store;
use oxigraph::model::{NamedNodeRef, QuadRef};

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::store::Store;
use oxigraph::model::*;

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

Auto Trait Implementations§

§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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

§

type Output = T

Should always be Self
source§

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

§

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 Twhere U: TryFrom<T>,

§

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

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

§

fn vzip(self) -> V