Search in sources :

Example 6 with RepositoryConnection

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();
        }
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Example 7 with RepositoryConnection

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();
        }
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection)

Example 8 with RepositoryConnection

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);
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) Statement(org.eclipse.rdf4j.model.Statement) HashSet(java.util.HashSet)

Example 9 with RepositoryConnection

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);
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) Statement(org.eclipse.rdf4j.model.Statement)

Example 10 with RepositoryConnection

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());
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) Repository(org.eclipse.rdf4j.repository.Repository) Test(org.junit.Test)

Aggregations

RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)34 Test (org.junit.Test)21 Repository (org.eclipse.rdf4j.repository.Repository)18 IRI (org.eclipse.rdf4j.model.IRI)6 Resource (org.eclipse.rdf4j.model.Resource)6 Statement (org.eclipse.rdf4j.model.Statement)6 Model (org.eclipse.rdf4j.model.Model)5 QueryLanguage (org.eclipse.rdf4j.query.QueryLanguage)5 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)4 RepositoryConfig (org.eclipse.rdf4j.repository.config.RepositoryConfig)4 Value (org.eclipse.rdf4j.model.Value)3 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)3 Dataset (org.eclipse.rdf4j.query.Dataset)3 ProxyRepositoryConfig (org.eclipse.rdf4j.repository.sail.config.ProxyRepositoryConfig)3 SailRepositoryConfig (org.eclipse.rdf4j.repository.sail.config.SailRepositoryConfig)3 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)2 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)2 Update (org.eclipse.rdf4j.query.Update)2 AbstractUpdate (org.eclipse.rdf4j.query.impl.AbstractUpdate)2 InterceptingRepositoryConnectionWrapper (org.eclipse.rdf4j.repository.event.base.InterceptingRepositoryConnectionWrapper)2