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<'_>
impl Transaction<'_>
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::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(())
})?;
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::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(())
})?;
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:
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(())
})?;
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.
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::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(())
})?;
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_from_reader(
&mut self,
parser: impl Into<RdfParser>,
reader: impl Read,
) -> Result<(), LoaderError>
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")?))?);
Sourcepub 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
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>
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))?);
Sourcepub 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
pub fn load_dataset( &mut self, reader: impl Read, format: impl Into<RdfFormat>, base_iri: Option<&str>, ) -> Result<(), LoaderError>
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))?);
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::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)?);
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::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)?);
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::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()]
);
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::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());
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::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());
Sourcepub fn clear(&mut self) -> Result<(), StorageError>
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()?);