RYA-104 Closing resources and fixing tests for some of the Mongo interactors.
Project: http://git-wip-us.apache.org/repos/asf/incubator-rya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-rya/commit/5739f78e Tree: http://git-wip-us.apache.org/repos/asf/incubator-rya/tree/5739f78e Diff: http://git-wip-us.apache.org/repos/asf/incubator-rya/diff/5739f78e Branch: refs/heads/master Commit: 5739f78e568cf0696d4773521b658bd196a0aabe Parents: 6bb7146 Author: kchilton2 <[email protected]> Authored: Thu Dec 28 18:21:44 2017 -0500 Committer: kchilton2 <[email protected]> Committed: Fri Jan 5 16:48:40 2018 -0500 ---------------------------------------------------------------------- .../client/mongo/MongoExecuteSparqlQuery.java | 75 ++++++++------- .../api/client/mongo/MongoLoadStatements.java | 43 ++++++--- .../client/mongo/MongoLoadStatementsFile.java | 39 ++++++-- .../client/mongo/MongoExecuteSparqlQueryIT.java | 50 +++++----- .../client/mongo/MongoLoadStatementsFileIT.java | 30 +++--- .../api/client/mongo/MongoLoadStatementsIT.java | 99 ++++++++++---------- .../apache/rya/shell/RyaConnectionCommands.java | 17 ++-- .../org/apache/rya/shell/SharedShellState.java | 24 +++++ 8 files changed, 224 insertions(+), 153 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5739f78e/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoExecuteSparqlQuery.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoExecuteSparqlQuery.java b/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoExecuteSparqlQuery.java index d920b30..d3b7bcb 100644 --- a/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoExecuteSparqlQuery.java +++ b/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoExecuteSparqlQuery.java @@ -47,6 +47,8 @@ import org.openrdf.repository.sail.SailRepository; import org.openrdf.repository.sail.SailRepositoryConnection; import org.openrdf.sail.Sail; import org.openrdf.sail.SailException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import edu.umd.cs.findbugs.annotations.DefaultAnnotation; import edu.umd.cs.findbugs.annotations.NonNull; @@ -56,12 +58,13 @@ import edu.umd.cs.findbugs.annotations.NonNull; */ @DefaultAnnotation(NonNull.class) public class MongoExecuteSparqlQuery implements ExecuteSparqlQuery { + private static final Logger log = LoggerFactory.getLogger(MongoExecuteSparqlQuery.class); private final MongoConnectionDetails connectionDetails; private final InstanceExists instanceExists; /** - * Constructs an instance. + * Constructs an instance of {@link MongoExecuteSparqlQuery}. * * @param connectionDetails - Details to connect to the server. (not null) * @param instanceExists - The interactor used to check if a Rya instance exists. (not null) @@ -72,32 +75,25 @@ public class MongoExecuteSparqlQuery implements ExecuteSparqlQuery { this.connectionDetails = requireNonNull(connectionDetails); this.instanceExists = requireNonNull(instanceExists); } + @Override public String executeSparqlQuery(final String ryaInstanceName, final String sparqlQuery) throws InstanceDoesNotExistException, RyaClientException { requireNonNull(ryaInstanceName); requireNonNull(sparqlQuery); - requireNonNull(ryaInstanceName); - requireNonNull(sparqlQuery); // Ensure the Rya Instance exists. if (!instanceExists.exists(ryaInstanceName)) { - throw new InstanceDoesNotExistException(String.format("There is no Rya instance named '%s'.", - ryaInstanceName)); + throw new InstanceDoesNotExistException(String.format("There is no Rya instance named '%s'.", ryaInstanceName)); } + Sail sail = null; - SailRepository sailRepo = null; SailRepositoryConnection sailRepoConn = null; - // Get a Sail object that is connected to the Rya instance. - final MongoDBRdfConfiguration ryaConf = connectionDetails.build(ryaInstanceName); try { + // Get a Sail object that is connected to the Rya instance. + final MongoDBRdfConfiguration ryaConf = connectionDetails.build(ryaInstanceName); sail = RyaSailFactory.getInstance(ryaConf); - } catch (SailException | RyaDAOException | InferenceEngineException | AccumuloException | AccumuloSecurityException e) { - throw new RyaClientException("While getting a sail instance.", e); - } - // Load the file. - sailRepo = new SailRepository(sail); - try { + final SailRepository sailRepo = new SailRepository(sail); sailRepoConn = sailRepo.getConnection(); // Execute the query. @@ -106,26 +102,37 @@ public class MongoExecuteSparqlQuery implements ExecuteSparqlQuery { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final CountingSPARQLResultsCSVWriter handler = new CountingSPARQLResultsCSVWriter(baos); tupleQuery.evaluate(handler); - final StringBuilder sb = new StringBuilder(); - - final String newline = "\n"; - sb.append("Query Result:").append(newline); - sb.append(new String(baos.toByteArray(), StandardCharsets.UTF_8)); - - final String seconds = new DecimalFormat("0.0##").format((System.currentTimeMillis() - start) / 1000.0); - sb.append("Retrieved ").append(handler.getCount()).append(" results in ").append(seconds).append(" seconds."); - - return sb.toString(); - - } catch (final MalformedQueryException e) { - throw new RyaClientException("There was a problem parsing the supplied query.", e); - } catch (final QueryEvaluationException | TupleQueryResultHandlerException e) { - throw new RyaClientException("There was a problem evaluating the supplied query.", e); - } catch (final RepositoryException e) { - throw new RyaClientException("There was a problem executing the query against the Rya instance named " - + ryaInstanceName + ".", e); - } finally { - // close anything? + final long end = System.currentTimeMillis(); + + // Format and return the result of the query. + final String queryResult = new String(baos.toByteArray(), StandardCharsets.UTF_8); + final String queryDuration = new DecimalFormat("0.0##").format((end - start) / 1000.0); + + return "Query Result:\n" + + queryResult + + "Retrieved " + handler.getCount() + " results in " + queryDuration + " seconds."; + + } catch (SailException | RyaDAOException | InferenceEngineException | AccumuloException | AccumuloSecurityException e) { + throw new RyaClientException("Could not create the Sail object used to query the RYA instance.", e); + } catch (final MalformedQueryException | QueryEvaluationException | TupleQueryResultHandlerException | RepositoryException e) { + throw new RyaClientException("Could not execute the SPARQL query.", 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); + } + } } } http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5739f78e/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoLoadStatements.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoLoadStatements.java b/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoLoadStatements.java index 37f1d95..8c9e0b5 100644 --- a/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoLoadStatements.java +++ b/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoLoadStatements.java @@ -36,6 +36,8 @@ import org.openrdf.repository.sail.SailRepository; import org.openrdf.repository.sail.SailRepositoryConnection; import org.openrdf.sail.Sail; import org.openrdf.sail.SailException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import edu.umd.cs.findbugs.annotations.DefaultAnnotation; import edu.umd.cs.findbugs.annotations.NonNull; @@ -45,12 +47,13 @@ import edu.umd.cs.findbugs.annotations.NonNull; */ @DefaultAnnotation(NonNull.class) public class MongoLoadStatements implements LoadStatements { + private static final Logger log = LoggerFactory.getLogger(MongoLoadStatements.class); private final MongoConnectionDetails connectionDetails; private final InstanceExists instanceExists; /** - * Constructs an instance. + * Constructs an instance of {@link MongoLoadStatements}. * * @param connectionDetails - Details to connect to the server. (not null) * @param instanceExists - The interactor used to check if a Rya instance exists. (not null) @@ -71,24 +74,40 @@ public class MongoLoadStatements implements LoadStatements { } Sail sail = null; - SailRepository sailRepo = null; SailRepositoryConnection sailRepoConn = null; - - // Get a Sail object that is connected to the Rya instance. - final MongoDBRdfConfiguration ryaConf = connectionDetails.build(ryaInstanceName); try { + // Get a Sail object that is connected to the Rya instance. + final MongoDBRdfConfiguration ryaConf = connectionDetails.build(ryaInstanceName); sail = RyaSailFactory.getInstance(ryaConf); - } 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); - } - // Load the file. - sailRepo = new SailRepository(sail); - try { + 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("While getting a connection and adding statements.", 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); + } + } } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5739f78e/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoLoadStatementsFile.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoLoadStatementsFile.java b/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoLoadStatementsFile.java index 00a7c61..244d02a 100644 --- a/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoLoadStatementsFile.java +++ b/extras/indexing/src/main/java/org/apache/rya/api/client/mongo/MongoLoadStatementsFile.java @@ -40,6 +40,8 @@ import org.openrdf.rio.RDFFormat; import org.openrdf.rio.RDFParseException; import org.openrdf.sail.Sail; import org.openrdf.sail.SailException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import edu.umd.cs.findbugs.annotations.DefaultAnnotation; import edu.umd.cs.findbugs.annotations.NonNull; @@ -49,6 +51,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; */ @DefaultAnnotation(NonNull.class) public class MongoLoadStatementsFile implements LoadStatementsFile { + private static final Logger log = LoggerFactory.getLogger(MongoLoadStatementsFile.class); private final MongoConnectionDetails connectionDetails; private final InstanceExists instanceExists; @@ -78,23 +81,39 @@ public class MongoLoadStatementsFile implements LoadStatementsFile { } Sail sail = null; - SailRepository sailRepo = null; SailRepositoryConnection sailRepoConn = null; - // Get a Sail object that is connected to the Rya instance. - final MongoDBRdfConfiguration ryaConf = connectionDetails.build(ryaInstanceName); try { + // Get a Sail object that is connected to the Rya instance. + final MongoDBRdfConfiguration ryaConf = connectionDetails.build(ryaInstanceName); sail = RyaSailFactory.getInstance(ryaConf); - } catch (SailException | RyaDAOException | InferenceEngineException | AccumuloException | AccumuloSecurityException e) { - throw new RyaClientException("While getting a sail instance.", e); - } - // Load the file. - sailRepo = new SailRepository(sail); - try { + 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("While getting a connection and adding statements from a file.", 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); + } + } } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5739f78e/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoExecuteSparqlQueryIT.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoExecuteSparqlQueryIT.java b/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoExecuteSparqlQueryIT.java index 0be9e8b..1d38410 100644 --- a/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoExecuteSparqlQueryIT.java +++ b/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoExecuteSparqlQueryIT.java @@ -24,7 +24,6 @@ import java.util.ArrayList; import java.util.List; import org.apache.rya.api.client.ExecuteSparqlQuery; -import org.apache.rya.api.client.Install; import org.apache.rya.api.client.Install.DuplicateInstanceNameException; import org.apache.rya.api.client.Install.InstallConfiguration; import org.apache.rya.api.client.RyaClient; @@ -41,22 +40,37 @@ import com.mongodb.MongoException; * Integration tests the methods of {@link }. */ public class MongoExecuteSparqlQueryIT extends MongoTestBase { + @Test public void ExecuteSparqlQuery_exec() throws MongoException, DuplicateInstanceNameException, RyaClientException { // Install an instance of Rya. final MongoConnectionDetails connectionDetails = getConnectionDetails(); final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, getMongoClient()); - // install rya and load some data - final List<Statement> loadMe = installAndLoad(); - // Here comes the method to test + + final InstallConfiguration installConfig = InstallConfiguration.builder() + .setEnableTableHashPrefix(false) + .setEnableEntityCentricIndex(false) + .setEnableFreeTextIndex(false) + .setEnableTemporalIndex(false) + .setEnablePcjIndex(false) + .setEnableGeoIndex(false) + .build(); + ryaClient.getInstall().install(conf.getRyaInstanceName(), installConfig); + + // Load some statements into that instance. + final List<Statement> statements = makeTestStatements(); + ryaClient.getLoadStatements().loadStatements(conf.getRyaInstanceName(), statements); + + // Execute the SPARQL against the Rya instance. final ExecuteSparqlQuery executeSparql = ryaClient.getExecuteSparqlQuery(); final String sparql = "SELECT * where { ?a ?b ?c }"; - final String results = executeSparql.executeSparqlQuery(conf.getMongoDBName(), sparql); - System.out.println(results); + final String results = executeSparql.executeSparqlQuery(conf.getRyaInstanceName(), sparql); + + // Show the result matches what is expected. assertTrue("result has header.", results.startsWith("Query Result:")); assertTrue("result has column headings.", results.contains("a,b,c")); assertTrue("result has footer.", results.contains("Retrieved 3 results in")); - for (final Statement expect : loadMe) { + for (final Statement expect : statements) { assertTrue("All results should contain expected subjects:", results.contains(expect.getSubject().stringValue())); assertTrue("All results should contain expected predicates:", @@ -66,28 +80,6 @@ public class MongoExecuteSparqlQueryIT extends MongoTestBase { } } - private List<Statement> installAndLoad() throws DuplicateInstanceNameException, RyaClientException { - // first install rya - final InstallConfiguration installConfig = InstallConfiguration.builder() - .setEnableTableHashPrefix(false) - .setEnableEntityCentricIndex(false) - .setEnableFreeTextIndex(false) - .setEnableTemporalIndex(false) - .setEnablePcjIndex(false) - .setEnableGeoIndex(false) - .build(); - final MongoConnectionDetails connectionDetails = getConnectionDetails(); - final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, getMongoClient()); - final Install install = ryaClient.getInstall(); - install.install(conf.getMongoDBName(), installConfig); - // next, load data - final List<Statement> loadMe = makeTestStatements(); - ryaClient.getLoadStatements().loadStatements( - conf.getMongoDBName(), - loadMe); - return loadMe; - } - /** * @return some data to load */ http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5739f78e/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoLoadStatementsFileIT.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoLoadStatementsFileIT.java b/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoLoadStatementsFileIT.java index 0f84fea..674a806 100644 --- a/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoLoadStatementsFileIT.java +++ b/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoLoadStatementsFileIT.java @@ -21,8 +21,8 @@ package org.apache.rya.api.client.mongo; import static org.junit.Assert.assertEquals; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import org.apache.rya.api.client.Install; import org.apache.rya.api.client.Install.InstallConfiguration; @@ -63,30 +63,36 @@ public class MongoLoadStatementsFileIT extends MongoTestBase { final MongoConnectionDetails connectionDetails = getConnectionDetails(); final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, getMongoClient()); final Install install = ryaClient.getInstall(); - install.install(conf.getMongoDBName(), installConfig); + install.install(conf.getRyaInstanceName(), installConfig); // Load the test statement file. ryaClient.getLoadStatementsFile().loadStatements( - conf.getMongoDBName(), + conf.getRyaInstanceName(), Paths.get("src/test/resources/example.ttl"), RDFFormat.TURTLE); // Verify that the statements were loaded. final ValueFactory vf = new ValueFactoryImpl(); - final List<Statement> expected = new ArrayList<>(); + final Set<Statement> expected = new HashSet<>(); expected.add(vf.createStatement(vf.createURI("http://example#alice"), vf.createURI("http://example#talksTo"), vf.createURI("http://example#bob"))); expected.add(vf.createStatement(vf.createURI("http://example#bob"), vf.createURI("http://example#talksTo"), vf.createURI("http://example#charlie"))); expected.add(vf.createStatement(vf.createURI("http://example#charlie"), vf.createURI("http://example#likes"), vf.createURI("http://example#icecream"))); - final List<Statement> statements = new ArrayList<>(); - final MongoCursor<Document> x = getRyaCollection().find().iterator(); - while (x.hasNext()) { - final Document y = x.next(); - statements.add(vf.createStatement(vf.createURI(y.getString("subject")), vf.createURI(y.getString("predicate")), vf.createURI(y.getString("object")))); + final Set<Statement> statements = new HashSet<>(); + final MongoCursor<Document> triplesIterator = getMongoClient() + .getDatabase( conf.getRyaInstanceName() ) + .getCollection( conf.getTriplesCollectionName() ) + .find().iterator(); + while (triplesIterator.hasNext()) { + final Document triple = triplesIterator.next(); + statements.add(vf.createStatement( + vf.createURI(triple.getString("subject")), + vf.createURI(triple.getString("predicate")), + vf.createURI(triple.getString("object")))); } - assertEquals("Expect all rows to be read.", 3, getRyaCollection().count()); - assertEquals("All rows in DB should match expected rows:", expected, statements); + + assertEquals(expected, statements); } private MongoConnectionDetails getConnectionDetails() { http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5739f78e/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoLoadStatementsIT.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoLoadStatementsIT.java b/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoLoadStatementsIT.java index 47b953d..b0d0e5b 100644 --- a/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoLoadStatementsIT.java +++ b/extras/indexing/src/test/java/org/apache/rya/api/client/mongo/MongoLoadStatementsIT.java @@ -20,15 +20,12 @@ package org.apache.rya.api.client.mongo; import static org.junit.Assert.assertEquals; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; -import org.apache.rya.api.client.Install; -import org.apache.rya.api.client.Install.DuplicateInstanceNameException; import org.apache.rya.api.client.Install.InstallConfiguration; import org.apache.rya.api.client.InstanceDoesNotExistException; import org.apache.rya.api.client.RyaClient; -import org.apache.rya.api.client.RyaClientException; import org.apache.rya.mongodb.MongoTestBase; import org.bson.Document; import org.junit.Test; @@ -43,9 +40,10 @@ import com.mongodb.client.MongoCursor; */ public class MongoLoadStatementsIT extends MongoTestBase { + private static final ValueFactory VF = new ValueFactoryImpl(); + @Test(expected = InstanceDoesNotExistException.class) public void instanceDoesNotExist() throws Exception { - org.apache.log4j.BasicConfigurator.configure(); final RyaClient ryaClient = MongoRyaClientFactory.build(getConnectionDetails(), getMongoClient()); // Skip the install step to create error causing situation. ryaClient.getLoadStatements().loadStatements(getConnectionDetails().getHostname(), makeTestStatements()); @@ -55,40 +53,11 @@ public class MongoLoadStatementsIT extends MongoTestBase { * Pass a list of statements to our loadStatement class. */ @Test - public void loadTurtleFile() throws Exception { + public void loadStatements() throws Exception { // Install an instance of Rya. - final List<Statement> loadMe = installAndLoad(); - final List<Statement> stmtResults = new ArrayList<>(); - final MongoCursor<Document> triplesIterator = getRyaCollection().find().iterator(); - final ValueFactory vf = new ValueFactoryImpl(); - while (triplesIterator.hasNext()) { - final Document triple = triplesIterator.next(); - stmtResults.add(vf.createStatement(vf.createURI(triple.getString("subject")), vf.createURI(triple.getString( - "predicate")), vf.createURI(triple.getString("object")))); - } - stmtResults.sort(((stmt1, stmt2) -> stmt1.getSubject().toString().compareTo(stmt2.getSubject().toString()))); - assertEquals("Expect all rows to be read.", 3, getRyaCollection().count()); - assertEquals("All rows in DB should match expected rows:", loadMe, stmtResults); - } - - /** - * @return some data to load - */ - private List<Statement> makeTestStatements() { - final List<Statement> loadMe = new ArrayList<>(); - final ValueFactory vf = new ValueFactoryImpl(); - - loadMe.add(vf.createStatement(vf.createURI("http://example#alice"), vf.createURI("http://example#talksTo"), vf - .createURI("http://example#bob"))); - loadMe.add(vf.createStatement(vf.createURI("http://example#bob"), vf.createURI("http://example#talksTo"), vf - .createURI("http://example#charlie"))); - loadMe.add(vf.createStatement(vf.createURI("http://example#charlie"), vf.createURI("http://example#likes"), vf - .createURI("http://example#icecream"))); - return loadMe; - } + final MongoConnectionDetails connectionDetails = getConnectionDetails(); + final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, getMongoClient()); - private List<Statement> installAndLoad() throws DuplicateInstanceNameException, RyaClientException { - // first install rya final InstallConfiguration installConfig = InstallConfiguration.builder() .setEnableTableHashPrefix(false) .setEnableEntityCentricIndex(false) @@ -97,16 +66,50 @@ public class MongoLoadStatementsIT extends MongoTestBase { .setEnablePcjIndex(false) .setEnableGeoIndex(false) .build(); - final MongoConnectionDetails connectionDetails = getConnectionDetails(); - final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, getMongoClient()); - final Install install = ryaClient.getInstall(); - install.install(conf.getMongoDBName(), installConfig); - // next, load data - final List<Statement> loadMe = makeTestStatements(); - ryaClient.getLoadStatements().loadStatements( - conf.getMongoDBName(), - loadMe); - return loadMe; + ryaClient.getInstall().install(conf.getRyaInstanceName(), installConfig); + + // Create the statements that will be loaded. + final Set<Statement> statements = makeTestStatements(); + + // Load them. + ryaClient.getLoadStatements().loadStatements(conf.getRyaInstanceName(), statements); + + // Fetch the statements that have been stored in Mongo DB. + final Set<Statement> stmtResults = new HashSet<>(); + final MongoCursor<Document> triplesIterator = getMongoClient() + .getDatabase( conf.getRyaInstanceName() ) + .getCollection( conf.getTriplesCollectionName() ) + .find().iterator(); + + while (triplesIterator.hasNext()) { + final Document triple = triplesIterator.next(); + stmtResults.add(VF.createStatement( + VF.createURI(triple.getString("subject")), + VF.createURI(triple.getString("predicate")), + VF.createURI(triple.getString("object")))); + } + + // Show the discovered statements match the original statements. + assertEquals(statements, stmtResults); + } + + public Set<Statement> makeTestStatements() { + final Set<Statement> statements = new HashSet<>(); + statements.add(VF.createStatement( + VF.createURI("http://example#alice"), + VF.createURI("http://example#talksTo"), + VF.createURI("http://example#bob"))); + statements.add( + VF.createStatement( + VF.createURI("http://example#bob"), + VF.createURI("http://example#talksTo"), + VF.createURI("http://example#charlie"))); + statements.add( + VF.createStatement( + VF.createURI("http://example#charlie"), + VF.createURI("http://example#likes"), + VF.createURI("http://example#icecream"))); + return statements; } private MongoConnectionDetails getConnectionDetails() { http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5739f78e/extras/shell/src/main/java/org/apache/rya/shell/RyaConnectionCommands.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/main/java/org/apache/rya/shell/RyaConnectionCommands.java b/extras/shell/src/main/java/org/apache/rya/shell/RyaConnectionCommands.java index 4cb14fa..c6bdbb3 100644 --- a/extras/shell/src/main/java/org/apache/rya/shell/RyaConnectionCommands.java +++ b/extras/shell/src/main/java/org/apache/rya/shell/RyaConnectionCommands.java @@ -121,7 +121,7 @@ public class RyaConnectionCommands implements CommandMarker { case MONGO: final MongoConnectionDetails mongoDetails = sharedState.getShellState().getMongoDetails().get(); - StringBuilder message = new StringBuilder() + final StringBuilder message = new StringBuilder() .append("The shell is connected to an instance of MongoDB using the following parameters:\\n") .append(" Hostname: " + mongoDetails.getHostname() + "\n") .append(" Port: " + mongoDetails.getPort() + "\n"); @@ -189,7 +189,7 @@ public class RyaConnectionCommands implements CommandMarker { Optional.ofNullable(password)); // Connect to a MongoDB server. TODO Figure out how to provide auth info? - MongoClient adminClient = new MongoClient(hostname, Integer.parseInt(port)); + final MongoClient adminClient = new MongoClient(hostname, Integer.parseInt(port)); // Make sure the client is closed at shutdown. Runtime.getRuntime().addShutdownHook(new Thread() { @@ -215,14 +215,8 @@ public class RyaConnectionCommands implements CommandMarker { @CliOption(key = {"instance"}, mandatory = true, help = "The name of the Rya instance the shell will interact with.") final String instance) { try { - // TODO When you are connected to mongo db, then connecting to an instance may require - // a username/password. this is because each Database has its own credentials, so - // every rya instance likewise has their own credentials. - final InstanceExists instanceExists = sharedState.getShellState().getConnectedCommands().get().getInstanceExists(); - // TODO gracefully fail if that version doen't support it. maybe the list command should go ahead - // Make sure the requested instance exists. if(!instanceExists.exists(instance)) { throw new RuntimeException(String.format("'%s' does not match an existing Rya instance.", instance)); @@ -237,6 +231,13 @@ public class RyaConnectionCommands implements CommandMarker { @CliCommand(value = DISCONNECT_COMMAND_NAME_CMD, help = "Disconnect the shell's Rya storage connection (Accumulo).") public void disconnect() { + // If connected to Mongo, there is a client that needs to be closed. + final com.google.common.base.Optional<MongoClient> mongoAdminClient = sharedState.getShellState().getMongoAdminClient(); + if(mongoAdminClient.isPresent()) { + mongoAdminClient.get().close(); + } + + // Update the shared state to disconnected. sharedState.disconnected(); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/5739f78e/extras/shell/src/main/java/org/apache/rya/shell/SharedShellState.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/main/java/org/apache/rya/shell/SharedShellState.java b/extras/shell/src/main/java/org/apache/rya/shell/SharedShellState.java index 89cbd5d..bb22fd3 100644 --- a/extras/shell/src/main/java/org/apache/rya/shell/SharedShellState.java +++ b/extras/shell/src/main/java/org/apache/rya/shell/SharedShellState.java @@ -28,6 +28,7 @@ import org.apache.rya.api.client.accumulo.AccumuloConnectionDetails; import org.apache.rya.api.client.mongo.MongoConnectionDetails; import com.google.common.base.Optional; +import com.mongodb.MongoClient; import edu.umd.cs.findbugs.annotations.DefaultAnnotation; import edu.umd.cs.findbugs.annotations.NonNull; @@ -222,6 +223,7 @@ public class SharedShellState { private final Optional<StorageType> storageType; private final Optional<AccumuloConnectionDetails> accumuloDetails; private final Optional<MongoConnectionDetails> mongoDetails; + private final Optional<MongoClient> mongoAdminClient; private final Optional<RyaClient> connectedCommands; // Instance specific values. @@ -232,12 +234,14 @@ public class SharedShellState { final Optional<StorageType> storageType, final Optional<AccumuloConnectionDetails> accumuloDetails, final Optional<MongoConnectionDetails> mongoDetails, + final Optional<MongoClient> mongoAdminClient, final Optional<RyaClient> connectedCommands, final Optional<String> instanceName) { this.connectionState = requireNonNull(connectionState); this.storageType = requireNonNull(storageType); this.accumuloDetails = requireNonNull(accumuloDetails); this.mongoDetails = requireNonNull(mongoDetails); + this.mongoAdminClient = requireNonNull(mongoAdminClient); this.connectedCommands = requireNonNull(connectedCommands); this.instanceName = requireNonNull(instanceName); } @@ -275,6 +279,13 @@ public class SharedShellState { } /** + * @return The Mongo Client that is used to perform administrative tasks while connected to Mongo DB. + */ + public Optional<MongoClient> getMongoAdminClient() { + return mongoAdminClient; + } + + /** * @return The {@link RyaClient} to use when a command on the shell is issued. * The value will not be present if the Rya Shell is not connected to a storage. */ @@ -340,6 +351,7 @@ public class SharedShellState { private StorageType storageType; private AccumuloConnectionDetails accumuloDetails; private MongoConnectionDetails mongoDetails; + private MongoClient mongoAdminClient; private RyaClient connectedCommands; // Instance specific values. @@ -361,6 +373,7 @@ public class SharedShellState { this.storageType = shellState.getStorageType().orNull(); this.accumuloDetails = shellState.getAccumuloDetails().orNull(); this.mongoDetails = shellState.getMongoDetails().orNull(); + this.mongoAdminClient = shellState.getMongoAdminClient().orNull(); this.connectedCommands = shellState.getConnectedCommands().orNull(); this.instanceName = shellState.getRyaInstanceName().orNull(); } @@ -403,6 +416,16 @@ public class SharedShellState { } /** + * @param mongoAdminClient - The Mongo Client that is used to perform administrative tasks while + * connected to Mongo DB. + * @return This {@link Builder} so that method invocations may be chained. + */ + public Builder setMongoAdminClient(@Nullable final MongoClient mongoAdminClient) { + this.mongoAdminClient = mongoAdminClient; + return this; + } + + /** * @param connectedCommands - The {@link RyaClient} to use when a command on the shell is issued. * @return This {@link Builder} so that method invocations may be chained. */ @@ -429,6 +452,7 @@ public class SharedShellState { Optional.fromNullable(storageType), Optional.fromNullable(accumuloDetails), Optional.fromNullable(mongoDetails), + Optional.fromNullable(mongoAdminClient), Optional.fromNullable(connectedCommands), Optional.fromNullable(instanceName)); }
