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>
impl<'a> Transaction<'a>
sourcepub fn query(
&self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>
) -> Result<QueryResults, EvaluationError>
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(())
})?;
sourcepub fn query_opt(
&self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
options: QueryOptions
) -> Result<QueryResults, EvaluationError>
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(())
})?;
sourcepub fn quads_for_pattern(
&self,
subject: Option<SubjectRef<'_>>,
predicate: Option<NamedNodeRef<'_>>,
object: Option<TermRef<'_>>,
graph_name: Option<GraphNameRef<'_>>
) -> QuadIter ⓘ
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(())
})?;
sourcepub fn contains<'b>(
&self,
quad: impl Into<QuadRef<'b>>
) -> Result<bool, StorageError>
pub fn contains<'b>( &self, quad: impl Into<QuadRef<'b>> ) -> Result<bool, StorageError>
Checks if this store contains a given quad.
sourcepub fn len(&self) -> Result<usize, StorageError>
pub fn len(&self) -> Result<usize, StorageError>
Returns the number of quads in the store.
Warning: this function executes a full scan.
sourcepub fn is_empty(&self) -> Result<bool, StorageError>
pub fn is_empty(&self) -> Result<bool, StorageError>
Returns if the store is empty.
sourcepub fn update(
&mut self,
update: impl TryInto<Update, Error = impl Into<EvaluationError>>
) -> Result<(), EvaluationError>
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(())
})?;
sourcepub fn update_opt(
&mut self,
update: impl TryInto<Update, Error = impl Into<EvaluationError>>,
options: impl Into<UpdateOptions>
) -> Result<(), EvaluationError>
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.
sourcepub fn load_graph<'b>(
&mut self,
reader: impl BufRead,
format: GraphFormat,
to_graph_name: impl Into<GraphNameRef<'b>>,
base_iri: Option<&str>
) -> Result<(), LoaderError>
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))?);
sourcepub fn load_dataset(
&mut self,
reader: impl BufRead,
format: DatasetFormat,
base_iri: Option<&str>
) -> Result<(), LoaderError>
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))?);
sourcepub fn insert<'b>(
&mut self,
quad: impl Into<QuadRef<'b>>
) -> Result<bool, StorageError>
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)?);
sourcepub fn extend<'b>(
&mut self,
quads: impl IntoIterator<Item = impl Into<QuadRef<'b>>>
) -> Result<(), StorageError>
pub fn extend<'b>( &mut self, quads: impl IntoIterator<Item = impl Into<QuadRef<'b>>> ) -> Result<(), StorageError>
Adds a set of quads to this store.
sourcepub fn remove<'b>(
&mut self,
quad: impl Into<QuadRef<'b>>
) -> Result<bool, StorageError>
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)?);
sourcepub fn named_graphs(&self) -> GraphNameIter ⓘ
pub fn named_graphs(&self) -> GraphNameIter ⓘ
Returns all the store named graphs.
sourcepub fn contains_named_graph<'b>(
&self,
graph_name: impl Into<NamedOrBlankNodeRef<'b>>
) -> Result<bool, StorageError>
pub fn contains_named_graph<'b>( &self, graph_name: impl Into<NamedOrBlankNodeRef<'b>> ) -> Result<bool, StorageError>
Checks if the store contains a given graph.
sourcepub fn insert_named_graph<'b>(
&mut self,
graph_name: impl Into<NamedOrBlankNodeRef<'b>>
) -> Result<bool, StorageError>
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()]);
sourcepub fn clear_graph<'b>(
&mut self,
graph_name: impl Into<GraphNameRef<'b>>
) -> Result<(), StorageError>
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());
sourcepub fn remove_named_graph<'b>(
&mut self,
graph_name: impl Into<NamedOrBlankNodeRef<'b>>
) -> Result<bool, StorageError>
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());
sourcepub fn clear(&mut self) -> Result<(), StorageError>
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()?);