use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class MongoLoadStatementsFile method loadStatements.
@Override
public void loadStatements(final String ryaInstanceName, final Path statementsFile, final RDFFormat format) throws InstanceDoesNotExistException, RyaClientException {
requireNonNull(ryaInstanceName);
requireNonNull(statementsFile);
requireNonNull(format);
// Ensure the Rya Instance exists.
if (!instanceExists.exists(ryaInstanceName)) {
throw new InstanceDoesNotExistException(String.format("There is no Rya instance named '%s'.", ryaInstanceName));
}
Sail sail = null;
SailRepositoryConnection sailRepoConn = null;
try {
// Get a Sail object that is connected to the Rya instance.
final MongoDBRdfConfiguration ryaConf = connectionDetails.build(ryaInstanceName);
sail = RyaSailFactory.getInstance(ryaConf);
final SailRepository sailRepo = new SailRepository(sail);
sailRepoConn = sailRepo.getConnection();
// Load the file.
sailRepoConn.add(statementsFile.toFile(), null, format);
} catch (SailException | RyaDAOException | InferenceEngineException | AccumuloException | AccumuloSecurityException e) {
throw new RyaClientException("Could not load statements into Rya because of a problem while creating the Sail object.", e);
} catch (RDFParseException | RepositoryException | IOException e) {
throw new RyaClientException("Could not load the statements into Rya.", e);
} finally {
// Close the resources that were opened.
if (sailRepoConn != null) {
try {
sailRepoConn.close();
} catch (final RepositoryException e) {
log.error("Couldn't close the SailRepositoryConnection object.", e);
}
}
if (sail != null) {
try {
sail.shutDown();
} catch (final SailException e) {
log.error("Couldn't close the Sail object.", e);
}
}
}
}
use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class MongoLoadStatements method loadStatements.
@Override
public void loadStatements(final String ryaInstanceName, final Iterable<? extends Statement> statements) throws InstanceDoesNotExistException, RyaClientException {
requireNonNull(ryaInstanceName);
requireNonNull(statements);
// Ensure the Rya Instance exists.
if (!instanceExists.exists(ryaInstanceName)) {
throw new InstanceDoesNotExistException(String.format("There is no Rya instance named '%s'.", ryaInstanceName));
}
Sail sail = null;
SailRepositoryConnection sailRepoConn = null;
try {
// Get a Sail object that is connected to the Rya instance.
final MongoDBRdfConfiguration ryaConf = connectionDetails.build(ryaInstanceName);
sail = RyaSailFactory.getInstance(ryaConf);
final SailRepository sailRepo = new SailRepository(sail);
sailRepoConn = sailRepo.getConnection();
// Load the statements.
sailRepoConn = sailRepo.getConnection();
sailRepoConn.add(statements);
} catch (SailException | RyaDAOException | InferenceEngineException | AccumuloException | AccumuloSecurityException e) {
throw new RyaClientException("Could not load statements into Rya because of a problem while creating the Sail object.", e);
} catch (final RepositoryException e) {
throw new RyaClientException("Could not load the statements into Rya.", e);
} finally {
// Close the resources that were opened.
if (sailRepoConn != null) {
try {
sailRepoConn.close();
} catch (final RepositoryException e) {
log.error("Couldn't close the SailRepositoryConnection object.", e);
}
}
if (sail != null) {
try {
sail.shutDown();
} catch (final SailException e) {
log.error("Couldn't close the Sail object.", e);
}
}
}
}
use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class MongoEntityIndexIT method ensureInEntityStore_Test.
@Test
public void ensureInEntityStore_Test() throws Exception {
final Sail sail = RyaSailFactory.getInstance(conf);
final SailRepositoryConnection conn = new SailRepository(sail).getConnection();
conn.begin();
try (MongoEntityIndexer indexer = new MongoEntityIndexer()) {
indexer.setConf(conf);
indexer.init();
setupTypes(indexer);
addStatements(conn);
final EntityStorage entities = indexer.getEntityStorage();
final RyaIRI subject = new RyaIRI("urn:alice");
final Optional<Entity> alice = entities.get(subject);
assertTrue(alice.isPresent());
} finally {
conn.close();
}
}
use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class MongoEntityIndexIT method sparqlQuery_Test.
@Test
public void sparqlQuery_Test() throws Exception {
final Sail sail = RyaSailFactory.getInstance(conf);
final SailRepositoryConnection conn = new SailRepository(sail).getConnection();
conn.begin();
try (MongoEntityIndexer indexer = new MongoEntityIndexer()) {
indexer.setConf(conf);
indexer.init();
setupTypes(indexer);
addStatements(conn);
final String query = "SELECT * WHERE { " + "<urn:strawberry> <" + RDF.TYPE + "> <urn:icecream> ." + "<urn:strawberry> <urn:brand> ?brand . " + "<urn:strawberry> <urn:flavor> ?flavor . " + "}";
final TupleQueryResult rez = conn.prepareTupleQuery(QueryLanguage.SPARQL, query).evaluate();
final Set<BindingSet> results = new HashSet<>();
while (rez.hasNext()) {
final BindingSet bs = rez.next();
results.add(bs);
}
final MapBindingSet expected = new MapBindingSet();
expected.addBinding("flavor", VF.createLiteral("Strawberry"));
expected.addBinding("brand", VF.createLiteral("Awesome Icecream"));
assertEquals(1, results.size());
assertEquals(expected, results.iterator().next());
} finally {
conn.close();
}
}
use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class MongoPCJIndexIT method sparqlQuery_Test.
@Test
public void sparqlQuery_Test() throws Exception {
// Setup a Rya Client.
final MongoConnectionDetails connectionDetails = getConnectionDetails();
final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, getMongoClient());
final String pcjQuery = "SELECT ?name WHERE {" + " ?name <urn:likes> <urn:icecream> ." + " ?name <urn:hasEyeColor> <urn:blue> ." + " }";
// Install an instance of Rya and load statements.
ryaClient.getInstall().install(conf.getRyaInstanceName(), InstallConfiguration.builder().setEnablePcjIndex(true).build());
ryaClient.getLoadStatements().loadStatements(conf.getRyaInstanceName(), getStatements());
final String pcjId = ryaClient.getCreatePCJ().createPCJ(conf.getRyaInstanceName(), pcjQuery);
ryaClient.getBatchUpdatePCJ().batchUpdate(conf.getRyaInstanceName(), pcjId);
// purge contents of rya triples collection
getMongoClient().getDatabase(conf.getRyaInstanceName()).getCollection(conf.getTriplesCollectionName()).drop();
// run the query. since the triples collection is gone, if the results match, they came from the PCJ index.
conf.setBoolean(ConfigUtils.USE_PCJ, true);
conf.setBoolean(ConfigUtils.USE_OPTIMAL_PCJ, true);
conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, true);
final Sail sail = RyaSailFactory.getInstance(conf);
final SailRepositoryConnection conn = new SailRepository(sail).getConnection();
conn.begin();
final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, pcjQuery);
tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERYPLAN_FLAG, RdfCloudTripleStoreConstants.VALUE_FACTORY.createLiteral(true));
final TupleQueryResult rez = tupleQuery.evaluate();
final Set<BindingSet> results = new HashSet<>();
while (rez.hasNext()) {
final BindingSet bs = rez.next();
results.add(bs);
}
// Verify the correct results were loaded into the PCJ table.
final Set<BindingSet> expectedResults = new HashSet<>();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("name", VF.createIRI("urn:Alice"));
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("name", VF.createIRI("urn:Bob"));
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("name", VF.createIRI("urn:Charlie"));
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("name", VF.createIRI("urn:David"));
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("name", VF.createIRI("urn:Eve"));
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("name", VF.createIRI("urn:Frank"));
expectedResults.add(bs);
assertEquals(6, results.size());
assertEquals(expectedResults, results);
}
Aggregations