Search in sources :

Example 16 with SailConnection

use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.

the class SailTest method addFile.

protected void addFile(final InputStream in, final RDFFormat format) throws Exception {
    try {
        SailConnection sc = sail.getConnection();
        sc.begin();
        try {
            RDFHandler h = new SailAdder(sc);
            RDFParser p = Rio.createParser(format);
            p.setRDFHandler(h);
            p.parse(in, "http://example.org/bogusBaseURI/");
            sc.commit();
        } finally {
            sc.rollback();
            sc.close();
        }
    } finally {
        in.close();
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) RDFParser(org.openrdf.rio.RDFParser) RDFHandler(org.openrdf.rio.RDFHandler)

Example 17 with SailConnection

use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.

the class SailTest method testDuplicateStatements.

@Test
public void testDuplicateStatements() throws Exception {
    if (uniqueStatements) {
        URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
        URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
        URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
        SailConnection sc = sail.getConnection();
        try {
            sc.begin();
            sc.clear();
            assertEquals(0, countStatements(sc, uriA, uriB, uriC, false));
            sc.addStatement(uriA, uriB, uriC);
            assertEquals(1, countStatements(sc, uriA, uriB, uriC, false));
            sc.addStatement(uriA, uriB, uriC);
            assertEquals(1, countStatements(sc, uriA, uriB, uriC, false));
            sc.addStatement(uriA, uriB, uriC, uriC);
            assertEquals(2, countStatements(sc, uriA, uriB, uriC, false));
            assertEquals(1, countStatements(sc, uriA, uriB, uriC, false, uriC));
            sc.commit();
        } finally {
            sc.rollback();
            sc.close();
        }
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 18 with SailConnection

use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.

the class SailGraph method createConnection.

private synchronized SailConnection createConnection() throws SailException {
    cleanupConnections();
    final SailConnection sc = rawGraph.getConnection();
    sc.begin();
    connections.add(sc);
    return sc;
}
Also used : SailConnection(org.openrdf.sail.SailConnection)

Example 19 with SailConnection

use of org.openrdf.sail.SailConnection in project incubator-rya by apache.

the class AccumuloBatchUpdatePCJIT method batchUpdate.

@Test
public void batchUpdate() throws Exception {
    // Setup a Rya Client.
    final AccumuloConnectionDetails connectionDetails = new AccumuloConnectionDetails(super.getUsername(), super.getPassword().toCharArray(), super.getInstanceName(), super.getZookeepers());
    final RyaClient ryaClient = AccumuloRyaClientFactory.build(connectionDetails, super.getConnector());
    // Install an instance of Rya on the mini accumulo cluster.
    ryaClient.getInstall().install(RYA_INSTANCE_NAME, InstallConfiguration.builder().setEnablePcjIndex(true).build());
    Sail sail = null;
    try (final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(super.getConnector(), RYA_INSTANCE_NAME)) {
        // Get a Sail connection backed by the installed Rya instance.
        final AccumuloRdfConfiguration ryaConf = new AccumuloRdfConfiguration();
        ryaConf.setTablePrefix(RYA_INSTANCE_NAME);
        ryaConf.set(ConfigUtils.CLOUDBASE_USER, super.getUsername());
        ryaConf.set(ConfigUtils.CLOUDBASE_PASSWORD, super.getPassword());
        ryaConf.set(ConfigUtils.CLOUDBASE_ZOOKEEPERS, super.getZookeepers());
        ryaConf.set(ConfigUtils.CLOUDBASE_INSTANCE, super.getInstanceName());
        ryaConf.set(ConfigUtils.USE_PCJ, "true");
        ryaConf.set(ConfigUtils.PCJ_STORAGE_TYPE, PrecomputedJoinStorageType.ACCUMULO.toString());
        ryaConf.set(ConfigUtils.PCJ_UPDATER_TYPE, PrecomputedJoinUpdaterType.NO_UPDATE.toString());
        sail = RyaSailFactory.getInstance(ryaConf);
        // Load some statements into the Rya instance.
        final ValueFactory vf = sail.getValueFactory();
        final SailConnection sailConn = sail.getConnection();
        sailConn.begin();
        sailConn.addStatement(vf.createURI("urn:Alice"), vf.createURI("urn:likes"), vf.createURI("urn:icecream"));
        sailConn.addStatement(vf.createURI("urn:Bob"), vf.createURI("urn:likes"), vf.createURI("urn:icecream"));
        sailConn.addStatement(vf.createURI("urn:Charlie"), vf.createURI("urn:likes"), vf.createURI("urn:icecream"));
        sailConn.addStatement(vf.createURI("urn:David"), vf.createURI("urn:likes"), vf.createURI("urn:icecream"));
        sailConn.addStatement(vf.createURI("urn:Eve"), vf.createURI("urn:likes"), vf.createURI("urn:icecream"));
        sailConn.addStatement(vf.createURI("urn:Frank"), vf.createURI("urn:likes"), vf.createURI("urn:icecream"));
        sailConn.addStatement(vf.createURI("urn:George"), vf.createURI("urn:likes"), vf.createURI("urn:icecream"));
        sailConn.addStatement(vf.createURI("urn:Hillary"), vf.createURI("urn:likes"), vf.createURI("urn:icecream"));
        sailConn.addStatement(vf.createURI("urn:Alice"), vf.createURI("urn:hasEyeColor"), vf.createURI("urn:blue"));
        sailConn.addStatement(vf.createURI("urn:Bob"), vf.createURI("urn:hasEyeColor"), vf.createURI("urn:blue"));
        sailConn.addStatement(vf.createURI("urn:Charlie"), vf.createURI("urn:hasEyeColor"), vf.createURI("urn:blue"));
        sailConn.addStatement(vf.createURI("urn:David"), vf.createURI("urn:hasEyeColor"), vf.createURI("urn:blue"));
        sailConn.addStatement(vf.createURI("urn:Eve"), vf.createURI("urn:hasEyeColor"), vf.createURI("urn:blue"));
        sailConn.addStatement(vf.createURI("urn:Frank"), vf.createURI("urn:hasEyeColor"), vf.createURI("urn:blue"));
        sailConn.addStatement(vf.createURI("urn:George"), vf.createURI("urn:hasEyeColor"), vf.createURI("urn:green"));
        sailConn.addStatement(vf.createURI("urn:Hillary"), vf.createURI("urn:hasEyeColor"), vf.createURI("urn:brown"));
        sailConn.commit();
        sailConn.close();
        // Create a PCJ for a SPARQL query.
        final String sparql = "SELECT ?name WHERE { ?name <urn:likes> <urn:icecream> . ?name <urn:hasEyeColor> <urn:blue> . }";
        final String pcjId = pcjStorage.createPcj(sparql);
        // Run the test.
        ryaClient.getBatchUpdatePCJ().batchUpdate(RYA_INSTANCE_NAME, pcjId);
        // Verify the correct results were loaded into the PCJ table.
        final Set<BindingSet> expectedResults = new HashSet<>();
        MapBindingSet bs = new MapBindingSet();
        bs.addBinding("name", vf.createURI("urn:Alice"));
        expectedResults.add(bs);
        bs = new MapBindingSet();
        bs.addBinding("name", vf.createURI("urn:Bob"));
        expectedResults.add(bs);
        bs = new MapBindingSet();
        bs.addBinding("name", vf.createURI("urn:Charlie"));
        expectedResults.add(bs);
        bs = new MapBindingSet();
        bs.addBinding("name", vf.createURI("urn:David"));
        expectedResults.add(bs);
        bs = new MapBindingSet();
        bs.addBinding("name", vf.createURI("urn:Eve"));
        expectedResults.add(bs);
        bs = new MapBindingSet();
        bs.addBinding("name", vf.createURI("urn:Frank"));
        expectedResults.add(bs);
        final Set<BindingSet> results = new HashSet<>();
        try (CloseableIterator<BindingSet> resultsIt = pcjStorage.listResults(pcjId)) {
            while (resultsIt.hasNext()) {
                results.add(resultsIt.next());
            }
        }
        assertEquals(expectedResults, results);
    } finally {
        if (sail != null) {
            sail.shutDown();
        }
    }
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) AccumuloPcjStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage) RyaClient(org.apache.rya.api.client.RyaClient) ValueFactory(org.openrdf.model.ValueFactory) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) SailConnection(org.openrdf.sail.SailConnection) Sail(org.openrdf.sail.Sail) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with SailConnection

use of org.openrdf.sail.SailConnection in project incubator-rya by apache.

the class AccumuloBatchUpdatePCJ method updatePCJResults.

private void updatePCJResults(final String ryaInstanceName, final String pcjId) throws InstanceDoesNotExistException, PCJDoesNotExistException, RyaClientException {
    // Things that have to be closed before we exit.
    Sail sail = null;
    SailConnection sailConn = null;
    CloseableIteration<? extends BindingSet, QueryEvaluationException> results = null;
    try (final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(super.getConnector(), ryaInstanceName)) {
        // Create an instance of Sail backed by the Rya instance.
        sail = connectToRya(ryaInstanceName);
        // Purge the old results from the PCJ.
        try {
            pcjStorage.purge(pcjId);
        } catch (final PCJStorageException e) {
            throw new RyaClientException("Could not batch update PCJ with ID '" + pcjId + "' because the old " + "results could not be purged from it.", e);
        }
        // Parse the PCJ's SPARQL query.
        final PcjMetadata metadata = pcjStorage.getPcjMetadata(pcjId);
        final String sparql = metadata.getSparql();
        final SPARQLParser parser = new SPARQLParser();
        final ParsedQuery parsedQuery = parser.parseQuery(sparql, null);
        // Execute the query.
        sailConn = sail.getConnection();
        results = sailConn.evaluate(parsedQuery.getTupleExpr(), null, null, false);
        // Load the results into the PCJ table.
        final List<VisibilityBindingSet> batch = new ArrayList<>(1000);
        while (results.hasNext()) {
            final VisibilityBindingSet result = new VisibilityBindingSet(results.next(), "");
            batch.add(result);
            if (batch.size() == 1000) {
                pcjStorage.addResults(pcjId, batch);
                batch.clear();
            }
        }
        if (!batch.isEmpty()) {
            pcjStorage.addResults(pcjId, batch);
            batch.clear();
        }
    } catch (final MalformedQueryException | PCJStorageException | SailException | QueryEvaluationException e) {
        throw new RyaClientException("Fail to batch load new results into the PCJ with ID '" + pcjId + "'.", e);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (final QueryEvaluationException e) {
                log.warn(e.getMessage(), e);
            }
        }
        if (sailConn != null) {
            try {
                sailConn.close();
            } catch (final SailException e) {
                log.warn(e.getMessage(), e);
            }
        }
        if (sail != null) {
            try {
                sail.shutDown();
            } catch (final SailException e) {
                log.warn(e.getMessage(), e);
            }
        }
    }
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) RyaClientException(org.apache.rya.api.client.RyaClientException) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) AccumuloPcjStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) SailException(org.openrdf.sail.SailException) SailConnection(org.openrdf.sail.SailConnection) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Sail(org.openrdf.sail.Sail) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) MalformedQueryException(org.openrdf.query.MalformedQueryException) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)

Aggregations

SailConnection (org.openrdf.sail.SailConnection)55 Test (org.junit.Test)34 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)28 URI (org.openrdf.model.URI)25 SailException (org.openrdf.sail.SailException)14 Sail (org.openrdf.sail.Sail)10 ValueFactory (org.openrdf.model.ValueFactory)9 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)8 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)5 RyaClient (org.apache.rya.api.client.RyaClient)5 Literal (org.openrdf.model.Literal)5 Resource (org.openrdf.model.Resource)5 Statement (org.openrdf.model.Statement)5 ParsedQuery (org.openrdf.query.parser.ParsedQuery)5 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)5 URIImpl (org.openrdf.model.impl.URIImpl)4 BindingSet (org.openrdf.query.BindingSet)4 Vertex (com.tinkerpop.blueprints.Vertex)3 EmptyBindingSet (org.openrdf.query.impl.EmptyBindingSet)3 RDFHandlerException (org.openrdf.rio.RDFHandlerException)3