use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.
the class Repositories method get.
/**
* Opens a {@link RepositoryConnection} to the given Repository within a transaction, sends the connection
* to the given {@link Function}, before either rolling back the transaction if it failed, or committing
* the transaction if it was successful.
*
* @param <T>
* The type of the return value.
* @param repository
* The {@link Repository} to open a connection to.
* @param processFunction
* A {@link Function} that performs an action on the connection and returns a result.
* @return The result of applying the function.
* @throws RepositoryException
* If there was an exception dealing with the Repository.
* @throws UnknownTransactionStateException
* If the transaction state was not properly recognised. (Optional specific exception)
*/
public static <T> T get(Repository repository, Function<RepositoryConnection, T> processFunction) throws RepositoryException, UnknownTransactionStateException {
RepositoryConnection conn = null;
try {
conn = repository.getConnection();
conn.begin();
T result = processFunction.apply(conn);
conn.commit();
return result;
} catch (RepositoryException e) {
if (conn != null && conn.isActive()) {
conn.rollback();
}
throw e;
} finally {
if (conn != null && conn.isOpen()) {
conn.close();
}
}
}
use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.
the class Repositories method getNoTransaction.
/**
* Opens a {@link RepositoryConnection} to the given Repository without opening a transaction, sends the
* connection to the given {@link Function}.
*
* @param <T>
* The type of the return value.
* @param repository
* The {@link Repository} to open a connection to.
* @param processFunction
* A {@link Function} that performs an action on the connection and returns a result.
* @return The result of applying the function.
* @throws RepositoryException
* If there was an exception dealing with the Repository.
* @throws UnknownTransactionStateException
* If the transaction state was not properly recognised. (Optional specific exception)
*/
public static <T> T getNoTransaction(Repository repository, Function<RepositoryConnection, T> processFunction) throws RepositoryException, UnknownTransactionStateException {
RepositoryConnection conn = null;
try {
conn = repository.getConnection();
T result = processFunction.apply(conn);
return result;
} finally {
if (conn != null && conn.isOpen()) {
conn.close();
}
}
}
use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.
the class RepositoryUtil method difference.
/**
* Compares two models defined by the default context of two repositories and returns the difference
* between the first and the second model (that is, all statements that are present in rep1 but not in
* rep2). Blank node IDs are not relevant for model equality, they are mapped from one model to the other
* by using the attached properties. Note that the method pulls the entire default context of both
* repositories into main memory. Use with caution.
* <p>
* <b>NOTE: this algorithm is currently broken; it doesn't actually map blank nodes between the two
* models.</b>
*
* @return The collection of statements that is the difference between rep1 and rep2.
*/
public static Collection<? extends Statement> difference(Repository rep1, Repository rep2) throws RepositoryException {
Collection<Statement> model1 = new HashSet<Statement>();
Collection<Statement> model2 = new HashSet<Statement>();
RepositoryConnection con1 = rep1.getConnection();
try {
Iterations.addAll(con1.getStatements(null, null, null, false), model1);
} finally {
con1.close();
}
RepositoryConnection con2 = rep2.getConnection();
try {
Iterations.addAll(con2.getStatements(null, null, null, false), model2);
} finally {
con2.close();
}
return difference(model1, model2);
}
use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.
the class RepositoryUtil method isSubset.
/**
* Compares the models of the default context of two repositories and returns true if rep1 is a subset of
* rep2. Note that the method pulls the entire default context of both repositories into main memory. Use
* with caution.
*/
public static boolean isSubset(Repository rep1, Repository rep2) throws RepositoryException {
Set<Statement> model1, model2;
RepositoryConnection con1 = rep1.getConnection();
try {
model1 = Iterations.asSet(con1.getStatements(null, null, null, true));
} finally {
con1.close();
}
RepositoryConnection con2 = rep2.getConnection();
try {
model2 = Iterations.asSet(con2.getStatements(null, null, null, true));
} finally {
con2.close();
}
return Models.isSubset(model1, model2);
}
use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.
the class ContextAwareConnectionTest method testQueryLanguage.
@Test
public void testQueryLanguage() throws Exception {
RepositoryConnection stub = new RepositoryConnectionStub();
Repository repo = stub.getRepository();
ContextAwareConnection a = new ContextAwareConnection(repo, stub);
ContextAwareConnection b = new ContextAwareConnection(repo, a);
b.setQueryLanguage(QueryLanguage.SERQL);
assertEquals(QueryLanguage.SERQL, b.getQueryLanguage());
assertEquals(QueryLanguage.SERQL, a.getQueryLanguage());
}
Aggregations