http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/indexing/src/test/java/org/apache/rya/indexing/statement/metadata/MongoStatementMetadataNodeTest.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/org/apache/rya/indexing/statement/metadata/MongoStatementMetadataNodeTest.java b/extras/indexing/src/test/java/org/apache/rya/indexing/statement/metadata/MongoStatementMetadataNodeTest.java index d005ce7..aa700b1 100644 --- a/extras/indexing/src/test/java/org/apache/rya/indexing/statement/metadata/MongoStatementMetadataNodeTest.java +++ b/extras/indexing/src/test/java/org/apache/rya/indexing/statement/metadata/MongoStatementMetadataNodeTest.java @@ -27,12 +27,10 @@ import org.apache.rya.api.domain.RyaStatement; import org.apache.rya.api.domain.RyaType; import org.apache.rya.api.domain.RyaURI; import org.apache.rya.api.domain.StatementMetadata; -import org.apache.rya.api.persist.RyaDAOException; import org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode; import org.apache.rya.mongodb.MongoDBRdfConfiguration; import org.apache.rya.mongodb.MongoDBRyaDAO; import org.apache.rya.mongodb.MongoTestBase; -import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -40,7 +38,6 @@ import org.openrdf.model.impl.LiteralImpl; import org.openrdf.model.impl.URIImpl; import org.openrdf.model.vocabulary.XMLSchema; import org.openrdf.query.BindingSet; -import org.openrdf.query.MalformedQueryException; import org.openrdf.query.QueryEvaluationException; import org.openrdf.query.algebra.StatementPattern; import org.openrdf.query.algebra.evaluation.QueryBindingSet; @@ -51,307 +48,321 @@ import org.openrdf.query.parser.sparql.SPARQLParser; import info.aduna.iteration.CloseableIteration; public class MongoStatementMetadataNodeTest extends MongoTestBase { - private MongoDBRyaDAO dao; - private final String query = "prefix owl: <http://www.w3.org/2002/07/owl#> prefix ano: <http://www.w3.org/2002/07/owl#annotated> prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> select ?x ?y where {_:blankNode rdf:type owl:Annotation; ano:Source <http://Joe>; " - + "ano:Property <http://worksAt>; ano:Target ?x; <http://createdBy> ?y; <http://createdOn> \'2017-01-04\'^^xsd:date }"; - private final String query2 = "prefix owl: <http://www.w3.org/2002/07/owl#> prefix ano: <http://www.w3.org/2002/07/owl#annotated> prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> select ?x ?y where {_:blankNode rdf:type owl:Annotation; ano:Source ?x; " - + "ano:Property <http://worksAt>; ano:Target ?y; <http://createdBy> ?x; <http://createdOn> \'2017-01-04\'^^xsd:date }"; - - @Before - public void init() throws Exception { - final Set<RyaURI> propertySet = new HashSet<RyaURI>(Arrays.asList(new RyaURI("http://createdBy"), new RyaURI("http://createdOn"))); - conf.setUseStatementMetadata(true); - conf.setStatementMetadataProperties(propertySet); - - dao = new MongoDBRyaDAO(conf, super.getMongoClient()); - dao.init(); - } - - @Test - public void simpleQueryWithoutBindingSet() - throws MalformedQueryException, QueryEvaluationException, RyaDAOException { - StatementMetadata metadata = new StatementMetadata(); - metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe")); - metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04")); - - RyaStatement statement = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), - new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); - dao.add(statement); - - SPARQLParser parser = new SPARQLParser(); - ParsedQuery pq = parser.parseQuery(query, null); - List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); - - StatementMetadataNode<?> node = new StatementMetadataNode<>(spList, conf); - CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet()); - - QueryBindingSet bs = new QueryBindingSet(); - bs.addBinding("x", new LiteralImpl("CoffeeShop")); - bs.addBinding("y", new LiteralImpl("Joe")); - - List<BindingSet> bsList = new ArrayList<>(); - while (iteration.hasNext()) { - bsList.add(iteration.next()); - } - - Assert.assertEquals(1, bsList.size()); - Assert.assertEquals(bs, bsList.get(0)); - dao.delete(statement, conf); - } - - /** - * Tests if results are filtered correctly using the metadata properties. In - * this case, the date for the ingested RyaStatement differs from the date - * specified in the query. - * - * @throws MalformedQueryException - * @throws QueryEvaluationException - * @throws RyaDAOException - */ - @Test - public void simpleQueryWithoutBindingSetInvalidProperty() - throws MalformedQueryException, QueryEvaluationException, RyaDAOException { - StatementMetadata metadata = new StatementMetadata(); - metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Doug")); - metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-02-15")); - - RyaStatement statement = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), - new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); - dao.add(statement); - - SPARQLParser parser = new SPARQLParser(); - ParsedQuery pq = parser.parseQuery(query, null); - List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); - StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf); - CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet()); - - List<BindingSet> bsList = new ArrayList<>(); - while (iteration.hasNext()) { - bsList.add(iteration.next()); - } - Assert.assertEquals(0, bsList.size()); - dao.delete(statement, conf); - } - - @Test - public void simpleQueryWithBindingSet() throws MalformedQueryException, QueryEvaluationException, RyaDAOException { - - StatementMetadata metadata = new StatementMetadata(); - metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe")); - metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04")); - - RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), - new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); - RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), - new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata); - dao.add(statement1); - dao.add(statement2); - - SPARQLParser parser = new SPARQLParser(); - ParsedQuery pq = parser.parseQuery(query, null); - List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); - StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf); - - QueryBindingSet bsConstraint = new QueryBindingSet(); - bsConstraint.addBinding("x", new LiteralImpl("CoffeeShop")); - bsConstraint.addBinding("z", new LiteralImpl("Virginia")); - - CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint); - - QueryBindingSet expected = new QueryBindingSet(); - expected.addBinding("x", new LiteralImpl("CoffeeShop")); - expected.addBinding("y", new LiteralImpl("Joe")); - expected.addBinding("z", new LiteralImpl("Virginia")); - - List<BindingSet> bsList = new ArrayList<>(); - while (iteration.hasNext()) { - bsList.add(iteration.next()); - } - - Assert.assertEquals(1, bsList.size()); - Assert.assertEquals(expected, bsList.get(0)); - - dao.delete(statement1, conf); - dao.delete(statement2, conf); - } - - /** - * Tests to see if correct result is passed back when a metadata statement - * is joined with a StatementPattern statement (i.e. a common variable - * appears in a StatementPattern statement and a metadata statement). - * StatementPattern statements have either rdf:subject, rdf:predicate, or - * rdf:object as the predicate while a metadata statement is any statement - * in the reified query whose predicate is not rdf:type and not a - * StatementPattern predicate. - * - * @throws MalformedQueryException - * @throws QueryEvaluationException - * @throws RyaDAOException - */ - @Test - public void simpleQueryWithBindingSetJoinPropertyToSubject() - throws MalformedQueryException, QueryEvaluationException, RyaDAOException { - - StatementMetadata metadata = new StatementMetadata(); - metadata.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Joe")); - metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04")); - - RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), - new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); - RyaStatement statement2 = new RyaStatement(new RyaURI("http://Bob"), new RyaURI("http://worksAt"), - new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata); - dao.add(statement1); - dao.add(statement2); - - SPARQLParser parser = new SPARQLParser(); - ParsedQuery pq = parser.parseQuery(query2, null); - List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); - StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf); - - List<BindingSet> bsCollection = new ArrayList<>(); - QueryBindingSet bsConstraint1 = new QueryBindingSet(); - bsConstraint1.addBinding("y", new LiteralImpl("CoffeeShop")); - bsConstraint1.addBinding("z", new LiteralImpl("Virginia")); - - QueryBindingSet bsConstraint2 = new QueryBindingSet(); - bsConstraint2.addBinding("y", new LiteralImpl("HardwareStore")); - bsConstraint2.addBinding("z", new LiteralImpl("Maryland")); - bsCollection.add(bsConstraint1); - bsCollection.add(bsConstraint2); - - CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection); - - QueryBindingSet expected = new QueryBindingSet(); - expected.addBinding("y", new LiteralImpl("CoffeeShop")); - expected.addBinding("x", new URIImpl("http://Joe")); - expected.addBinding("z", new LiteralImpl("Virginia")); - - List<BindingSet> bsList = new ArrayList<>(); - while (iteration.hasNext()) { - bsList.add(iteration.next()); - } - - Assert.assertEquals(1, bsList.size()); - Assert.assertEquals(expected, bsList.get(0)); - - dao.delete(statement1, conf); - dao.delete(statement2, conf); - } - - /** - * Tests if the StatementMetadataNode joins BindingSet correctly for - * variables appearing in metadata statements. In this case, the metadata - * statements are (_:blankNode <http://createdOn 2017-01-04 ) and - * (_:blankNode <http://createdBy> ?y). The variable ?y appears as the - * object in the above metadata statement and its values are joined to the - * constraint BindingSets in the example below. - * - * @throws MalformedQueryException - * @throws QueryEvaluationException - * @throws RyaDAOException - */ - @Test - public void simpleQueryWithBindingSetJoinOnProperty() - throws MalformedQueryException, QueryEvaluationException, RyaDAOException { - - StatementMetadata metadata = new StatementMetadata(); - metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe")); - metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04")); - - RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), - new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); - dao.add(statement1); - - SPARQLParser parser = new SPARQLParser(); - ParsedQuery pq = parser.parseQuery(query, null); - List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); - StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf); - - QueryBindingSet bsConstraint = new QueryBindingSet(); - bsConstraint.addBinding("x", new LiteralImpl("CoffeeShop")); - bsConstraint.addBinding("y", new LiteralImpl("Doug")); - - CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint); - - List<BindingSet> bsList = new ArrayList<>(); - while (iteration.hasNext()) { - bsList.add(iteration.next()); - } - - Assert.assertEquals(0, bsList.size()); - dao.delete(statement1, conf); - } - - /** - * Tests if StatementMetadataNode joins BindingSet values correctly for - * variables appearing as the object in one of the StatementPattern - * statements (in the case ?x appears as the Object in the statement - * _:blankNode rdf:object ?x). StatementPattern statements have either - * rdf:subject, rdf:predicate, or rdf:object as the predicate. - * - * @throws MalformedQueryException - * @throws QueryEvaluationException - * @throws RyaDAOException - */ - @Test - public void simpleQueryWithBindingSetCollection() - throws MalformedQueryException, QueryEvaluationException, RyaDAOException { - - StatementMetadata metadata = new StatementMetadata(); - metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe")); - metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04")); - - RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), - new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); - RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), - new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata); - dao.add(statement1); - dao.add(statement2); - - SPARQLParser parser = new SPARQLParser(); - ParsedQuery pq = parser.parseQuery(query, null); - List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); - StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf); - - List<BindingSet> bsCollection = new ArrayList<>(); - QueryBindingSet bsConstraint1 = new QueryBindingSet(); - bsConstraint1.addBinding("x", new LiteralImpl("CoffeeShop")); - bsConstraint1.addBinding("z", new LiteralImpl("Virginia")); - - QueryBindingSet bsConstraint2 = new QueryBindingSet(); - bsConstraint2.addBinding("x", new LiteralImpl("HardwareStore")); - bsConstraint2.addBinding("z", new LiteralImpl("Maryland")); - - QueryBindingSet bsConstraint3 = new QueryBindingSet(); - bsConstraint3.addBinding("x", new LiteralImpl("BurgerShack")); - bsConstraint3.addBinding("z", new LiteralImpl("Delaware")); - bsCollection.add(bsConstraint1); - bsCollection.add(bsConstraint2); - bsCollection.add(bsConstraint3); - - CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection); - - Set<BindingSet> expected = new HashSet<>(); - QueryBindingSet expected1 = new QueryBindingSet(); - expected1.addBinding("x", new LiteralImpl("CoffeeShop")); - expected1.addBinding("y", new LiteralImpl("Joe")); - expected1.addBinding("z", new LiteralImpl("Virginia")); - - QueryBindingSet expected2 = new QueryBindingSet(); - expected2.addBinding("x", new LiteralImpl("HardwareStore")); - expected2.addBinding("y", new LiteralImpl("Joe")); - expected2.addBinding("z", new LiteralImpl("Maryland")); - expected.add(expected1); - expected.add(expected2); - - Set<BindingSet> bsSet = new HashSet<>(); - while (iteration.hasNext()) { - bsSet.add(iteration.next()); - } - - Assert.assertEquals(expected, bsSet); - - dao.delete(statement1, conf); - dao.delete(statement2, conf); - } + private final String query = "prefix owl: <http://www.w3.org/2002/07/owl#> prefix ano: <http://www.w3.org/2002/07/owl#annotated> prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> select ?x ?y where {_:blankNode rdf:type owl:Annotation; ano:Source <http://Joe>; " + + "ano:Property <http://worksAt>; ano:Target ?x; <http://createdBy> ?y; <http://createdOn> \'2017-01-04\'^^xsd:date }"; + private final String query2 = "prefix owl: <http://www.w3.org/2002/07/owl#> prefix ano: <http://www.w3.org/2002/07/owl#annotated> prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> select ?x ?y where {_:blankNode rdf:type owl:Annotation; ano:Source ?x; " + + "ano:Property <http://worksAt>; ano:Target ?y; <http://createdBy> ?x; <http://createdOn> \'2017-01-04\'^^xsd:date }"; + + @Before + public void init() throws Exception { + final Set<RyaURI> propertySet = new HashSet<RyaURI>(Arrays.asList(new RyaURI("http://createdBy"), new RyaURI("http://createdOn"))); + conf.setUseStatementMetadata(true); + conf.setStatementMetadataProperties(propertySet); + } + + @Test + public void simpleQueryWithoutBindingSet() throws Exception { + MongoDBRyaDAO dao = new MongoDBRyaDAO(); + try { + dao.setConf(conf); + dao.init(); + StatementMetadata metadata = new StatementMetadata(); + metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe")); + metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04")); + + RyaStatement statement = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), + new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); + dao.add(statement); + + SPARQLParser parser = new SPARQLParser(); + ParsedQuery pq = parser.parseQuery(query, null); + List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); + + StatementMetadataNode<?> node = new StatementMetadataNode<>(spList, conf); + CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet()); + + QueryBindingSet bs = new QueryBindingSet(); + bs.addBinding("x", new LiteralImpl("CoffeeShop")); + bs.addBinding("y", new LiteralImpl("Joe")); + + List<BindingSet> bsList = new ArrayList<>(); + while (iteration.hasNext()) { + bsList.add(iteration.next()); + } + + Assert.assertEquals(1, bsList.size()); + Assert.assertEquals(bs, bsList.get(0)); + dao.delete(statement, conf); + } finally { + dao.destroy(); + } + } + + /** + * Tests if results are filtered correctly using the metadata properties. In + * this case, the date for the ingested RyaStatement differs from the date + * specified in the query. + */ + @Test + public void simpleQueryWithoutBindingSetInvalidProperty() throws Exception { + MongoDBRyaDAO dao = new MongoDBRyaDAO(); + try { + dao.setConf(conf); + dao.init(); + + StatementMetadata metadata = new StatementMetadata(); + metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Doug")); + metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-02-15")); + + RyaStatement statement = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), + new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); + dao.add(statement); + + SPARQLParser parser = new SPARQLParser(); + ParsedQuery pq = parser.parseQuery(query, null); + List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); + StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf); + CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet()); + + List<BindingSet> bsList = new ArrayList<>(); + while (iteration.hasNext()) { + bsList.add(iteration.next()); + } + Assert.assertEquals(0, bsList.size()); + dao.delete(statement, conf); + } finally { + dao.destroy(); + } + } + + @Test + public void simpleQueryWithBindingSet() throws Exception { + MongoDBRyaDAO dao = new MongoDBRyaDAO(); + try { + dao.setConf(conf); + dao.init(); + StatementMetadata metadata = new StatementMetadata(); + metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe")); + metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04")); + + RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), + new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); + RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), + new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata); + dao.add(statement1); + dao.add(statement2); + + SPARQLParser parser = new SPARQLParser(); + ParsedQuery pq = parser.parseQuery(query, null); + List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); + StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf); + + QueryBindingSet bsConstraint = new QueryBindingSet(); + bsConstraint.addBinding("x", new LiteralImpl("CoffeeShop")); + bsConstraint.addBinding("z", new LiteralImpl("Virginia")); + + CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint); + + QueryBindingSet expected = new QueryBindingSet(); + expected.addBinding("x", new LiteralImpl("CoffeeShop")); + expected.addBinding("y", new LiteralImpl("Joe")); + expected.addBinding("z", new LiteralImpl("Virginia")); + + List<BindingSet> bsList = new ArrayList<>(); + while (iteration.hasNext()) { + bsList.add(iteration.next()); + } + + Assert.assertEquals(1, bsList.size()); + Assert.assertEquals(expected, bsList.get(0)); + + dao.delete(statement1, conf); + dao.delete(statement2, conf); + } finally { + dao.destroy(); + } + } + + /** + * Tests to see if correct result is passed back when a metadata statement + * is joined with a StatementPattern statement (i.e. a common variable + * appears in a StatementPattern statement and a metadata statement). + * StatementPattern statements have either rdf:subject, rdf:predicate, or + * rdf:object as the predicate while a metadata statement is any statement + * in the reified query whose predicate is not rdf:type and not a + * StatementPattern predicate. + */ + @Test + public void simpleQueryWithBindingSetJoinPropertyToSubject() throws Exception { + MongoDBRyaDAO dao = new MongoDBRyaDAO(); + try { + dao.setConf(conf); + dao.init(); + StatementMetadata metadata = new StatementMetadata(); + metadata.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Joe")); + metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04")); + + RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), + new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); + RyaStatement statement2 = new RyaStatement(new RyaURI("http://Bob"), new RyaURI("http://worksAt"), + new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata); + dao.add(statement1); + dao.add(statement2); + + SPARQLParser parser = new SPARQLParser(); + ParsedQuery pq = parser.parseQuery(query2, null); + List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); + StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf); + + List<BindingSet> bsCollection = new ArrayList<>(); + QueryBindingSet bsConstraint1 = new QueryBindingSet(); + bsConstraint1.addBinding("y", new LiteralImpl("CoffeeShop")); + bsConstraint1.addBinding("z", new LiteralImpl("Virginia")); + + QueryBindingSet bsConstraint2 = new QueryBindingSet(); + bsConstraint2.addBinding("y", new LiteralImpl("HardwareStore")); + bsConstraint2.addBinding("z", new LiteralImpl("Maryland")); + bsCollection.add(bsConstraint1); + bsCollection.add(bsConstraint2); + + CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection); + + QueryBindingSet expected = new QueryBindingSet(); + expected.addBinding("y", new LiteralImpl("CoffeeShop")); + expected.addBinding("x", new URIImpl("http://Joe")); + expected.addBinding("z", new LiteralImpl("Virginia")); + + List<BindingSet> bsList = new ArrayList<>(); + while (iteration.hasNext()) { + bsList.add(iteration.next()); + } + + Assert.assertEquals(1, bsList.size()); + Assert.assertEquals(expected, bsList.get(0)); + + dao.delete(statement1, conf); + dao.delete(statement2, conf); + } finally { + dao.destroy(); + } + } + + /** + * Tests if the StatementMetadataNode joins BindingSet correctly for + * variables appearing in metadata statements. In this case, the metadata + * statements are (_:blankNode <http://createdOn 2017-01-04 ) and + * (_:blankNode <http://createdBy> ?y). The variable ?y appears as the + * object in the above metadata statement and its values are joined to the + * constraint BindingSets in the example below. + */ + @Test + public void simpleQueryWithBindingSetJoinOnProperty() throws Exception { + MongoDBRyaDAO dao = new MongoDBRyaDAO(); + try { + dao.setConf(conf); + dao.init(); + StatementMetadata metadata = new StatementMetadata(); + metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe")); + metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04")); + + RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), + new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); + dao.add(statement1); + + SPARQLParser parser = new SPARQLParser(); + ParsedQuery pq = parser.parseQuery(query, null); + List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); + StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf); + + QueryBindingSet bsConstraint = new QueryBindingSet(); + bsConstraint.addBinding("x", new LiteralImpl("CoffeeShop")); + bsConstraint.addBinding("y", new LiteralImpl("Doug")); + + CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint); + + List<BindingSet> bsList = new ArrayList<>(); + while (iteration.hasNext()) { + bsList.add(iteration.next()); + } + + Assert.assertEquals(0, bsList.size()); + dao.delete(statement1, conf); + } finally { + dao.destroy(); + } + } + + /** + * Tests if StatementMetadataNode joins BindingSet values correctly for + * variables appearing as the object in one of the StatementPattern + * statements (in the case ?x appears as the Object in the statement + * _:blankNode rdf:object ?x). StatementPattern statements have either + * rdf:subject, rdf:predicate, or rdf:object as the predicate. + */ + @Test + public void simpleQueryWithBindingSetCollection() throws Exception { + MongoDBRyaDAO dao = new MongoDBRyaDAO(); + try { + dao.setConf(conf); + dao.init(); + StatementMetadata metadata = new StatementMetadata(); + metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe")); + metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04")); + + RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), + new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata); + RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), + new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata); + dao.add(statement1); + dao.add(statement2); + + SPARQLParser parser = new SPARQLParser(); + ParsedQuery pq = parser.parseQuery(query, null); + List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr()); + StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf); + + List<BindingSet> bsCollection = new ArrayList<>(); + QueryBindingSet bsConstraint1 = new QueryBindingSet(); + bsConstraint1.addBinding("x", new LiteralImpl("CoffeeShop")); + bsConstraint1.addBinding("z", new LiteralImpl("Virginia")); + + QueryBindingSet bsConstraint2 = new QueryBindingSet(); + bsConstraint2.addBinding("x", new LiteralImpl("HardwareStore")); + bsConstraint2.addBinding("z", new LiteralImpl("Maryland")); + + QueryBindingSet bsConstraint3 = new QueryBindingSet(); + bsConstraint3.addBinding("x", new LiteralImpl("BurgerShack")); + bsConstraint3.addBinding("z", new LiteralImpl("Delaware")); + bsCollection.add(bsConstraint1); + bsCollection.add(bsConstraint2); + bsCollection.add(bsConstraint3); + + CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection); + + Set<BindingSet> expected = new HashSet<>(); + QueryBindingSet expected1 = new QueryBindingSet(); + expected1.addBinding("x", new LiteralImpl("CoffeeShop")); + expected1.addBinding("y", new LiteralImpl("Joe")); + expected1.addBinding("z", new LiteralImpl("Virginia")); + + QueryBindingSet expected2 = new QueryBindingSet(); + expected2.addBinding("x", new LiteralImpl("HardwareStore")); + expected2.addBinding("y", new LiteralImpl("Joe")); + expected2.addBinding("z", new LiteralImpl("Maryland")); + expected.add(expected1); + expected.add(expected2); + + Set<BindingSet> bsSet = new HashSet<>(); + while (iteration.hasNext()) { + bsSet.add(iteration.next()); + } + + Assert.assertEquals(expected, bsSet); + + dao.delete(statement1, conf); + dao.delete(statement2, conf); + } finally { + dao.destroy(); + } + } }
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/indexingExample/src/main/java/MongoRyaDirectExample.java ---------------------------------------------------------------------- diff --git a/extras/indexingExample/src/main/java/MongoRyaDirectExample.java b/extras/indexingExample/src/main/java/MongoRyaDirectExample.java index 477e579..66d94db 100644 --- a/extras/indexingExample/src/main/java/MongoRyaDirectExample.java +++ b/extras/indexingExample/src/main/java/MongoRyaDirectExample.java @@ -58,8 +58,8 @@ import org.openrdf.repository.sail.SailRepositoryConnection; import org.openrdf.sail.Sail; import com.mongodb.MongoClient; -import com.mongodb.ServerAddress; +import de.flapdoodle.embed.mongo.config.IMongoConfig; import info.aduna.iteration.Iterations; @@ -295,12 +295,12 @@ public class MongoRyaDirectExample { .setUseMockMongo(USE_MOCK).setUseInference(USE_INFER).setAuths("U"); if (USE_MOCK) { - final MongoClient c = EmbeddedMongoFactory.newFactory().newMongoClient(); - final ServerAddress address = c.getAddress(); - final String url = address.getHost(); - final String port = Integer.toString(address.getPort()); - c.close(); - builder.setMongoHost(url).setMongoPort(port); + final EmbeddedMongoFactory factory = EmbeddedMongoFactory.newFactory(); + final MongoClient c = factory.newMongoClient(); + final IMongoConfig connectionConfig = factory.getMongoServerDetails(); + //c.close(); + builder.setMongoHost(connectionConfig.net().getServerAddress().getHostAddress()) + .setMongoPort(connectionConfig.net().getPort() + ""); } else { // User name and password must be filled in: builder = builder.setMongoUser(MONGO_USER) http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.export/export.client/src/main/java/org/apache/rya/export/client/conf/MergeConfigHadoopAdapter.java ---------------------------------------------------------------------- diff --git a/extras/rya.export/export.client/src/main/java/org/apache/rya/export/client/conf/MergeConfigHadoopAdapter.java b/extras/rya.export/export.client/src/main/java/org/apache/rya/export/client/conf/MergeConfigHadoopAdapter.java index c51d637..3b722f1 100644 --- a/extras/rya.export/export.client/src/main/java/org/apache/rya/export/client/conf/MergeConfigHadoopAdapter.java +++ b/extras/rya.export/export.client/src/main/java/org/apache/rya/export/client/conf/MergeConfigHadoopAdapter.java @@ -28,8 +28,8 @@ import org.apache.rya.mongodb.MongoDBRdfConfiguration; public class MergeConfigHadoopAdapter { public static MongoDBRdfConfiguration getMongoConfiguration(final MergeConfiguration config) { final MongoDBRdfConfiguration configuration = new MongoDBRdfConfiguration(); - configuration.setMongoInstance(config.getChildHostname()); - configuration.set(MongoDBRdfConfiguration.MONGO_INSTANCE_PORT, config.getChildPort() + ""); + configuration.setMongoHostname(config.getChildHostname()); + configuration.set(MongoDBRdfConfiguration.MONGO_PORT, config.getChildPort() + ""); configuration.set(MongoDBRdfConfiguration.MONGO_DB_NAME, config.getChildRyaInstanceName()); return configuration; } http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.export/export.client/src/main/java/org/apache/rya/export/client/merge/StatementStoreFactory.java ---------------------------------------------------------------------- diff --git a/extras/rya.export/export.client/src/main/java/org/apache/rya/export/client/merge/StatementStoreFactory.java b/extras/rya.export/export.client/src/main/java/org/apache/rya/export/client/merge/StatementStoreFactory.java index c96a2b6..2f7dca7 100644 --- a/extras/rya.export/export.client/src/main/java/org/apache/rya/export/client/merge/StatementStoreFactory.java +++ b/extras/rya.export/export.client/src/main/java/org/apache/rya/export/client/merge/StatementStoreFactory.java @@ -20,6 +20,7 @@ package org.apache.rya.export.client.merge; import static java.util.Objects.requireNonNull; +import java.util.ArrayList; import java.util.Date; import org.apache.rya.accumulo.AccumuloRyaDAO; @@ -37,8 +38,8 @@ import org.apache.rya.export.api.store.RyaStatementStore; import org.apache.rya.export.client.conf.MergeConfigHadoopAdapter; import org.apache.rya.export.mongo.MongoRyaStatementStore; import org.apache.rya.export.mongo.policy.TimestampPolicyMongoRyaStatementStore; -import org.apache.rya.mongodb.MongoDBRdfConfiguration; import org.apache.rya.mongodb.MongoDBRyaDAO; +import org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration; import com.mongodb.MongoClient; @@ -121,7 +122,9 @@ public class StatementStoreFactory { private MongoRyaStatementStore getBaseMongoStore(final String hostname, final int port, final String ryaInstanceName) throws RyaDAOException { final MongoClient client = new MongoClient(hostname, port); - final MongoDBRyaDAO dao = new MongoDBRyaDAO(new MongoDBRdfConfiguration(MergeConfigHadoopAdapter.getMongoConfiguration(configuration)), client); + final MongoDBRyaDAO dao = new MongoDBRyaDAO(); + dao.setConf(new StatefulMongoDBRdfConfiguration(MergeConfigHadoopAdapter.getMongoConfiguration(configuration), client, new ArrayList<>())); + dao.init(); return new MongoRyaStatementStore(client, ryaInstanceName, dao); } http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.export/export.integration/src/test/java/org/apache/rya/indexing/export/StoreToStoreIT.java ---------------------------------------------------------------------- diff --git a/extras/rya.export/export.integration/src/test/java/org/apache/rya/indexing/export/StoreToStoreIT.java b/extras/rya.export/export.integration/src/test/java/org/apache/rya/indexing/export/StoreToStoreIT.java index 2eef621..c982d21 100644 --- a/extras/rya.export/export.integration/src/test/java/org/apache/rya/indexing/export/StoreToStoreIT.java +++ b/extras/rya.export/export.integration/src/test/java/org/apache/rya/indexing/export/StoreToStoreIT.java @@ -41,6 +41,7 @@ import org.apache.rya.export.client.merge.VisibilityStatementMerger; import org.apache.rya.export.mongo.MongoRyaStatementStore; import org.apache.rya.export.mongo.policy.TimestampPolicyMongoRyaStatementStore; import org.apache.rya.mongodb.MongoDBRyaDAO; +import org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -65,7 +66,9 @@ public class StoreToStoreIT extends ITBase { private static TimestampPolicyMongoRyaStatementStore getParentMongo() throws Exception { final MongoClient mongo = getNewMongoResources(RYA_INSTANCE); - final MongoDBRyaDAO dao = new MongoDBRyaDAO(ITBase.getConf(mongo), mongo); + final MongoDBRyaDAO dao = new MongoDBRyaDAO(); + dao.setConf(new StatefulMongoDBRdfConfiguration(ITBase.getConf(mongo), mongo, new ArrayList<>())); + dao.init(); final MongoRyaStatementStore store = new MongoRyaStatementStore(mongo, RYA_INSTANCE, dao); final TimestampPolicyMongoRyaStatementStore timeStore = new TimestampPolicyMongoRyaStatementStore(store, currentDate, RYA_INSTANCE); clients.add(mongo); @@ -74,7 +77,9 @@ public class StoreToStoreIT extends ITBase { private static MongoRyaStatementStore getChildMongo() throws Exception { final MongoClient mongo = getNewMongoResources(RYA_INSTANCE); - final MongoDBRyaDAO dao = new MongoDBRyaDAO(ITBase.getConf(mongo), mongo); + final MongoDBRyaDAO dao = new MongoDBRyaDAO(); + dao.setConf(new StatefulMongoDBRdfConfiguration(ITBase.getConf(mongo), mongo, new ArrayList<>())); + dao.init(); final MongoRyaStatementStore store = new MongoRyaStatementStore(mongo, RYA_INSTANCE, dao); clients.add(mongo); return store; http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/GeoEnabledFilterFunctionOptimizer.java ---------------------------------------------------------------------- diff --git a/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/GeoEnabledFilterFunctionOptimizer.java b/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/GeoEnabledFilterFunctionOptimizer.java index 6ad0edc..eee7310 100644 --- a/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/GeoEnabledFilterFunctionOptimizer.java +++ b/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/GeoEnabledFilterFunctionOptimizer.java @@ -1,6 +1,4 @@ -package org.apache.rya.indexing; - -/* +/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -18,7 +16,7 @@ package org.apache.rya.indexing; * specific language governing permissions and limitations * under the License. */ - +package org.apache.rya.indexing; import java.io.IOException; import java.net.UnknownHostException; @@ -43,11 +41,10 @@ import org.apache.rya.indexing.accumulo.geo.GeoParseUtils; import org.apache.rya.indexing.accumulo.geo.GeoTupleSet; import org.apache.rya.indexing.accumulo.geo.OptionalConfigUtils; import org.apache.rya.indexing.accumulo.temporal.AccumuloTemporalIndexer; -import org.apache.rya.indexing.mongodb.freetext.MongoFreeTextIndexer; -import org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer; +import org.apache.rya.mongodb.MongoSecondaryIndex; +import org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration; import org.openrdf.model.Resource; import org.openrdf.model.URI; -import org.openrdf.model.Value; import org.openrdf.model.ValueFactory; import org.openrdf.model.impl.URIImpl; import org.openrdf.model.impl.ValueFactoryImpl; @@ -103,12 +100,16 @@ public class GeoEnabledFilterFunctionOptimizer implements QueryOptimizer, Config if (!init) { if (ConfigUtils.getUseMongo(conf)) { // create a new MongoGeoIndexer() without having it at compile time. - geoIndexer = instantiate(GeoIndexerType.MONGO_DB.getGeoIndexerClassString(), GeoIndexer.class); - geoIndexer.setConf(conf); - freeTextIndexer = new MongoFreeTextIndexer(); - freeTextIndexer.setConf(conf); - temporalIndexer = new MongoTemporalIndexer(); - temporalIndexer.setConf(conf); + StatefulMongoDBRdfConfiguration stateConf = (StatefulMongoDBRdfConfiguration) conf; + for(final MongoSecondaryIndex indexer : stateConf.getAdditionalIndexers()) { + if(indexer instanceof FreeTextIndexer) { + freeTextIndexer = (FreeTextIndexer) indexer; + } else if(indexer instanceof TemporalIndexer) { + temporalIndexer = (TemporalIndexer) indexer; + } else if(indexer instanceof GeoIndexer) { + geoIndexer = (GeoIndexer) indexer; + } + } } else { GeoIndexerType geoIndexerType = OptionalConfigUtils.getGeoIndexerType(conf); if (geoIndexerType == GeoIndexerType.UNSPECIFIED) { http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/GeoRyaSailFactory.java ---------------------------------------------------------------------- diff --git a/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/GeoRyaSailFactory.java b/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/GeoRyaSailFactory.java index 3c01bf6..ae6af10 100644 --- a/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/GeoRyaSailFactory.java +++ b/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/GeoRyaSailFactory.java @@ -20,25 +20,17 @@ package org.apache.rya.indexing; import static java.util.Objects.requireNonNull; -import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.List; import java.util.Objects; import org.apache.accumulo.core.client.AccumuloException; import org.apache.accumulo.core.client.AccumuloSecurityException; import org.apache.accumulo.core.client.Connector; -import org.apache.accumulo.core.client.Instance; -import org.apache.accumulo.core.client.security.tokens.PasswordToken; +import org.apache.commons.configuration.ConfigurationRuntimeException; import org.apache.hadoop.conf.Configuration; -import org.openrdf.sail.Sail; -import org.openrdf.sail.SailException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.mongodb.MongoClient; - import org.apache.rya.accumulo.AccumuloRdfConfiguration; import org.apache.rya.accumulo.AccumuloRyaDAO; -import org.apache.rya.accumulo.instance.AccumuloRyaInstanceDetailsRepository; import org.apache.rya.api.RdfCloudTripleStoreConfiguration; import org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException; import org.apache.rya.api.instance.RyaDetailsToConfiguration; @@ -47,14 +39,24 @@ import org.apache.rya.api.persist.RyaDAO; import org.apache.rya.api.persist.RyaDAOException; import org.apache.rya.indexing.accumulo.ConfigUtils; import org.apache.rya.indexing.accumulo.geo.OptionalConfigUtils; -import org.apache.rya.mongodb.MongoConnectorFactory; import org.apache.rya.mongodb.MongoDBRdfConfiguration; import org.apache.rya.mongodb.MongoDBRyaDAO; +import org.apache.rya.mongodb.MongoSecondaryIndex; +import org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration; import org.apache.rya.mongodb.instance.MongoRyaInstanceDetailsRepository; import org.apache.rya.rdftriplestore.RdfCloudTripleStore; import org.apache.rya.rdftriplestore.inference.InferenceEngine; import org.apache.rya.rdftriplestore.inference.InferenceEngineException; import org.apache.rya.sail.config.RyaSailFactory; +import org.openrdf.sail.Sail; +import org.openrdf.sail.SailException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.mongodb.MongoClient; +import com.mongodb.MongoCredential; +import com.mongodb.MongoException; +import com.mongodb.ServerAddress; public class GeoRyaSailFactory { private static final Logger LOG = LoggerFactory.getLogger(GeoRyaSailFactory.class); @@ -84,16 +86,36 @@ public class GeoRyaSailFactory { Objects.requireNonNull(ryaInstance, "RyaInstance or table prefix is missing from configuration."+RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX); if(ConfigUtils.getUseMongo(config)) { - final MongoDBRdfConfiguration mongoConfig = new MongoDBRdfConfiguration(config); - rdfConfig = mongoConfig; - final MongoClient client = MongoConnectorFactory.getMongoClient(config); + // Get a reference to a Mongo DB configuration object. + final MongoDBRdfConfiguration mongoConfig = (config instanceof MongoDBRdfConfiguration) ? + (MongoDBRdfConfiguration)config : new MongoDBRdfConfiguration(config); + + // Create the MongoClient that will be used by the Sail object's components. + final MongoClient client = createMongoClient(mongoConfig); + + // Add the Indexer and Optimizer names to the configuration object that are configured to be used. + OptionalConfigUtils.setIndexers(mongoConfig); + + // Initialize the indexer and optimizer objects that will be used within the Sail object. + final List<MongoSecondaryIndex> indexers = mongoConfig.getInstances(AccumuloRdfConfiguration.CONF_ADDITIONAL_INDEXERS, MongoSecondaryIndex.class); + + // Populate the configuration using previously stored Rya Details if this instance uses them. try { - final MongoRyaInstanceDetailsRepository ryaDetailsRepo = new MongoRyaInstanceDetailsRepository(client, mongoConfig.getCollectionName()); + final MongoRyaInstanceDetailsRepository ryaDetailsRepo = new MongoRyaInstanceDetailsRepository(client, mongoConfig.getRyaInstance()); RyaDetailsToConfiguration.addRyaDetailsToConfiguration(ryaDetailsRepo.getRyaInstanceDetails(), mongoConfig); } catch (final RyaDetailsRepositoryException e) { LOG.info("Instance does not have a rya details collection, skipping."); } - dao = getMongoDAO((MongoDBRdfConfiguration)rdfConfig, client); + + // Set the configuration to the stateful configuration that is used to pass the constructed objects around. + final StatefulMongoDBRdfConfiguration statefulConfig = new StatefulMongoDBRdfConfiguration(mongoConfig, client, indexers); + rdfConfig = statefulConfig; + + // Create the DAO that is able to interact with MongoDB. + final MongoDBRyaDAO mongoDao = new MongoDBRyaDAO(); + mongoDao.setConf(statefulConfig); + mongoDao.init(); + dao = mongoDao; } else { rdfConfig = new AccumuloRdfConfiguration(config); user = rdfConfig.get(ConfigUtils.CLOUDBASE_USER); @@ -120,20 +142,39 @@ public class GeoRyaSailFactory { return store; } - private static MongoDBRyaDAO getMongoDAO(final MongoDBRdfConfiguration config, final MongoClient client) throws RyaDAOException { - MongoDBRyaDAO dao = null; - OptionalConfigUtils.setIndexers(config); - if(client != null) { - dao = new MongoDBRyaDAO(config, client); + /** + * Create a {@link MongoClient} that is connected to the configured database. + * + * @param mongoConf - Configures what will be connected to. (not null) + * @throws ConfigurationRuntimeException An invalid port was provided by {@code mongoConf}. + * @throws MongoException Couldn't connect to the MongoDB database. + */ + private static MongoClient createMongoClient(final MongoDBRdfConfiguration mongoConf) throws ConfigurationRuntimeException, MongoException { + requireNonNull(mongoConf); + requireNonNull(mongoConf.getMongoHostname()); + requireNonNull(mongoConf.getMongoPort()); + requireNonNull(mongoConf.getMongoDBName()); + + // Connect to a running MongoDB server. + final int port; + try { + port = Integer.parseInt( mongoConf.getMongoPort() ); + } catch(final NumberFormatException e) { + throw new ConfigurationRuntimeException("Port '" + mongoConf.getMongoPort() + "' must be an integer."); + } + + final ServerAddress server = new ServerAddress(mongoConf.getMongoHostname(), port); + + // Connect to a specific MongoDB Database if that information is provided. + final String username = mongoConf.getMongoUser(); + final String database = mongoConf.getMongoDBName(); + final String password = mongoConf.getMongoPassword(); + if(username != null && password != null) { + final MongoCredential cred = MongoCredential.createCredential(username, database, password.toCharArray()); + return new MongoClient(server, Arrays.asList(cred)); } else { - try { - dao = new MongoDBRyaDAO(config); - } catch (NumberFormatException | UnknownHostException e) { - throw new RyaDAOException("Unable to connect to mongo at the configured location.", e); - } + return new MongoClient(server); } - dao.init(); - return dao; } private static AccumuloRyaDAO getAccumuloDAO(final AccumuloRdfConfiguration config) throws AccumuloException, AccumuloSecurityException, RyaDAOException { http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalIndexer.java ---------------------------------------------------------------------- diff --git a/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalIndexer.java b/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalIndexer.java index 106588b..cb34bd0 100644 --- a/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalIndexer.java +++ b/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalIndexer.java @@ -18,7 +18,6 @@ */ package org.apache.rya.indexing.geotemporal; -import org.apache.hadoop.conf.Configuration; import org.apache.rya.api.persist.index.RyaSecondaryIndexer; import org.apache.rya.indexing.GeoConstants; import org.apache.rya.indexing.geotemporal.storage.EventStorage; @@ -33,15 +32,15 @@ public interface GeoTemporalIndexer extends RyaSecondaryIndexer { /** * initialize after setting configuration. */ - public void init(); + @Override + public void init(); /** - * Creates the {@link Eventtorage} that will be used by the indexer. - * - * @param conf - Indicates how the {@link EventStorage} is initialized. (not null) + * Creates the {@link EventStorage} that will be used by the indexer. + * NOTE: {@link #setConf(org.apache.hadoop.conf.Configuration)} must be called before calling this. * @return The {@link EventStorage} that will be used by this indexer. */ - public abstract EventStorage getEventStorage(final Configuration conf); + public abstract EventStorage getEventStorage(); /** * Used to indicate which geo filter functions to use in a query. http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalIndexerFactory.java ---------------------------------------------------------------------- diff --git a/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalIndexerFactory.java b/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalIndexerFactory.java index f4df8bc..24e9666 100644 --- a/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalIndexerFactory.java +++ b/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalIndexerFactory.java @@ -19,13 +19,11 @@ package org.apache.rya.indexing.geotemporal; import org.apache.hadoop.conf.Configuration; -import org.apache.rya.indexing.GeoEnabledFilterFunctionOptimizer; -import org.apache.rya.indexing.GeoIndexer; -import org.apache.rya.indexing.GeoIndexerType; -import org.apache.rya.indexing.GeoTemporalIndexerType; import org.apache.rya.indexing.accumulo.ConfigUtils; -import org.apache.rya.mongodb.MongoDBRdfConfiguration; import org.apache.rya.mongodb.MongoSecondaryIndex; +import org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration; + +import com.google.common.base.Preconditions; /** * Factory for retrieving a {@link GeoTemporalIndexer} based on a provided {@link Configuration}. @@ -38,17 +36,16 @@ public class GeoTemporalIndexerFactory { */ public GeoTemporalIndexer getIndexer(final Configuration conf) { if(ConfigUtils.getUseMongo(conf)) { - final MongoDBRdfConfiguration config = new MongoDBRdfConfiguration(conf); - for(final MongoSecondaryIndex index : config.getAdditionalIndexers()) { + Preconditions.checkArgument(conf instanceof StatefulMongoDBRdfConfiguration, + "The configuration provided must be a StatefulMongoDBRdfConfiguration, found: " + conf.getClass().getSimpleName()); + final StatefulMongoDBRdfConfiguration statefulConf = (StatefulMongoDBRdfConfiguration) conf; + for(final MongoSecondaryIndex index : statefulConf.getAdditionalIndexers()) { if(index instanceof GeoTemporalIndexer) { return (GeoTemporalIndexer) index; } } - /* Created a MongoGeoTemporalIndexer */ - final GeoTemporalIndexer index = GeoEnabledFilterFunctionOptimizer.instantiate(GeoTemporalIndexerType.MONGO_GEO_TEMPORAL.getGeoTemporalIndexerClassString(), GeoTemporalIndexer.class); - index.setConf(conf); - index.init(); - return index; + + throw new IllegalStateException("Geo Temporal Indexing is not turned on. Check configuration."); } else { //TODO: add Accumulo here. return null; http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalOptimizer.java ---------------------------------------------------------------------- diff --git a/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalOptimizer.java b/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalOptimizer.java index d626adc..f6e57a7 100644 --- a/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalOptimizer.java +++ b/extras/rya.geoindexing/geo.common/src/main/java/org/apache/rya/indexing/geotemporal/GeoTemporalOptimizer.java @@ -44,7 +44,7 @@ public class GeoTemporalOptimizer extends AbstractExternalSetOptimizer<EventQuer indexer = factory.getIndexer(conf); //conf here does not matter since EventStorage has already been set in the indexer. - provider = new GeoTemporalIndexSetProvider(indexer.getEventStorage(conf)); + provider = new GeoTemporalIndexSetProvider(indexer.getEventStorage()); } @Override http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.geoindexing/geo.mongo/pom.xml ---------------------------------------------------------------------- diff --git a/extras/rya.geoindexing/geo.mongo/pom.xml b/extras/rya.geoindexing/geo.mongo/pom.xml index 2631e06..261f061 100644 --- a/extras/rya.geoindexing/geo.mongo/pom.xml +++ b/extras/rya.geoindexing/geo.mongo/pom.xml @@ -43,5 +43,11 @@ <artifactId>gt-xsd-gml3</artifactId> <version>${geotools.version}</version> </dependency> + <dependency> + <groupId>org.apache.rya</groupId> + <artifactId>mongodb.rya</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.geoindexing/geo.mongo/src/main/java/org/apache/rya/indexing/geoExamples/RyaMongoGeoDirectExample.java ---------------------------------------------------------------------- diff --git a/extras/rya.geoindexing/geo.mongo/src/main/java/org/apache/rya/indexing/geoExamples/RyaMongoGeoDirectExample.java b/extras/rya.geoindexing/geo.mongo/src/main/java/org/apache/rya/indexing/geoExamples/RyaMongoGeoDirectExample.java index ede3f98..04488bb 100644 --- a/extras/rya.geoindexing/geo.mongo/src/main/java/org/apache/rya/indexing/geoExamples/RyaMongoGeoDirectExample.java +++ b/extras/rya.geoindexing/geo.mongo/src/main/java/org/apache/rya/indexing/geoExamples/RyaMongoGeoDirectExample.java @@ -30,18 +30,14 @@ import org.apache.rya.indexing.accumulo.geo.OptionalConfigUtils; import org.apache.rya.indexing.mongodb.MongoIndexingConfiguration; import org.apache.rya.indexing.mongodb.MongoIndexingConfiguration.MongoDBIndexingConfigBuilder; import org.apache.rya.mongodb.EmbeddedMongoFactory; -import org.apache.rya.mongodb.MongoConnectorFactory; import org.openrdf.model.vocabulary.RDFS; import org.openrdf.query.BindingSet; -import org.openrdf.query.MalformedQueryException; -import org.openrdf.query.QueryEvaluationException; import org.openrdf.query.QueryLanguage; import org.openrdf.query.QueryResultHandlerException; import org.openrdf.query.TupleQuery; import org.openrdf.query.TupleQueryResultHandler; import org.openrdf.query.TupleQueryResultHandlerException; import org.openrdf.query.Update; -import org.openrdf.query.UpdateExecutionException; import org.openrdf.repository.RepositoryException; import org.openrdf.repository.sail.SailRepository; import org.openrdf.repository.sail.SailRepositoryConnection; @@ -90,7 +86,6 @@ public class RyaMongoGeoDirectExample { if (mock != null) { mock.shutdown(); } - MongoConnectorFactory.closeMongoClient(); } } /** http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.geoindexing/geo.mongo/src/main/java/org/apache/rya/indexing/geotemporal/mongo/MongoGeoTemporalIndexer.java ---------------------------------------------------------------------- diff --git a/extras/rya.geoindexing/geo.mongo/src/main/java/org/apache/rya/indexing/geotemporal/mongo/MongoGeoTemporalIndexer.java b/extras/rya.geoindexing/geo.mongo/src/main/java/org/apache/rya/indexing/geotemporal/mongo/MongoGeoTemporalIndexer.java index 2561c23..62a2181 100644 --- a/extras/rya.geoindexing/geo.mongo/src/main/java/org/apache/rya/indexing/geotemporal/mongo/MongoGeoTemporalIndexer.java +++ b/extras/rya.geoindexing/geo.mongo/src/main/java/org/apache/rya/indexing/geotemporal/mongo/MongoGeoTemporalIndexer.java @@ -18,6 +18,7 @@ */ package org.apache.rya.indexing.geotemporal.mongo; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import static java.util.Objects.requireNonNull; @@ -43,8 +44,7 @@ import org.apache.rya.indexing.geotemporal.storage.EventStorage; import org.apache.rya.indexing.mongodb.AbstractMongoIndexer; import org.apache.rya.indexing.mongodb.IndexingException; import org.apache.rya.indexing.mongodb.geo.GmlParser; -import org.apache.rya.mongodb.MongoConnectorFactory; -import org.apache.rya.mongodb.MongoDBRdfConfiguration; +import org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration; import org.joda.time.DateTime; import org.openrdf.model.Statement; import org.openrdf.model.URI; @@ -77,12 +77,13 @@ public class MongoGeoTemporalIndexer extends AbstractMongoIndexer<GeoTemporalMon private static final Logger LOG = Logger.getLogger(MongoGeoTemporalIndexer.class); public static final String GEO_TEMPORAL_COLLECTION = "geo_temporal"; - private final AtomicReference<MongoDBRdfConfiguration> configuration = new AtomicReference<>(); private final AtomicReference<EventStorage> events = new AtomicReference<>(); @Override public void init() { initCore(); + events.set(getEventStorage()); + predicates = ConfigUtils.getGeoPredicates(conf); predicates.addAll(ConfigUtils.getTemporalPredicates(conf)); storageStrategy = new GeoTemporalMongoDBStorageStrategy(); @@ -91,10 +92,10 @@ public class MongoGeoTemporalIndexer extends AbstractMongoIndexer<GeoTemporalMon @Override public void setConf(final Configuration conf) { requireNonNull(conf); - events.set(null); - events.set(getEventStorage(conf)); - super.conf = conf; - configuration.set(new MongoDBRdfConfiguration(conf)); + checkArgument(conf instanceof StatefulMongoDBRdfConfiguration, + "The configuration must be of type StatefulMongoDBRdfConfiguration but you provided: " + + conf.getClass().getSimpleName()); + super.conf = (StatefulMongoDBRdfConfiguration) conf; } @Override @@ -206,22 +207,14 @@ public class MongoGeoTemporalIndexer extends AbstractMongoIndexer<GeoTemporalMon } @Override - public EventStorage getEventStorage(final Configuration conf) { + public EventStorage getEventStorage() { requireNonNull(conf); if(events.get() != null) { return events.get(); } - - final MongoDBRdfConfiguration mongoConf = new MongoDBRdfConfiguration(conf); - mongoClient = mongoConf.getMongoClient(); - configuration.set(mongoConf); - if (mongoClient == null) { - mongoClient = MongoConnectorFactory.getMongoClient(conf); - } - final String ryaInstanceName = mongoConf.getMongoDBName(); - events.set(new MongoEventStorage(mongoClient, ryaInstanceName)); + events.set(new MongoEventStorage(conf.getMongoClient(), conf.getRyaInstance())); return events.get(); } } http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalProviderTest.java ---------------------------------------------------------------------- diff --git a/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalProviderTest.java b/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalProviderTest.java index 7151b56..fa2f52b 100644 --- a/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalProviderTest.java +++ b/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalProviderTest.java @@ -36,7 +36,7 @@ import org.openrdf.model.Value; import org.openrdf.model.ValueFactory; import org.openrdf.model.impl.ValueFactoryImpl; -public class GeoTemporalProviderTest extends GeoTemporalTestBase { +public class GeoTemporalProviderTest extends GeoTemporalTestUtils { private static final String URI_PROPERTY_AT_TIME = "Property:atTime"; private GeoTemporalIndexSetProvider provider; private EventStorage events; http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalTestBase.java ---------------------------------------------------------------------- diff --git a/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalTestBase.java b/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalTestBase.java deleted file mode 100644 index 6b6bf15..0000000 --- a/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalTestBase.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.rya.indexing.geotemporal; - -import static org.junit.Assert.assertEquals; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.rya.indexing.TemporalInstant; -import org.apache.rya.indexing.TemporalInstantRfc3339; -import org.apache.rya.indexing.external.matching.QuerySegment; -import org.apache.rya.indexing.geotemporal.model.EventQueryNode; -import org.junit.ComparisonFailure; -import org.mockito.Mockito; -import org.openrdf.query.algebra.FunctionCall; -import org.openrdf.query.algebra.QueryModelNode; -import org.openrdf.query.algebra.StatementPattern; -import org.openrdf.query.algebra.helpers.QueryModelVisitorBase; -import org.openrdf.query.algebra.helpers.StatementPatternCollector; -import org.openrdf.query.parser.sparql.SPARQLParser; - -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.GeometryFactory; -import com.vividsolutions.jts.geom.LineString; -import com.vividsolutions.jts.geom.LinearRing; -import com.vividsolutions.jts.geom.Point; -import com.vividsolutions.jts.geom.Polygon; -import com.vividsolutions.jts.geom.PrecisionModel; -import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence; - -public class GeoTemporalTestBase { - private static final GeometryFactory gf = new GeometryFactory(new PrecisionModel(), 4326); - - /** - * Make an uniform instant with given seconds. - */ - protected static TemporalInstant makeInstant(final int secondsMakeMeUnique) { - return new TemporalInstantRfc3339(2015, 12, 30, 12, 00, secondsMakeMeUnique); - } - - protected static Polygon poly(final double[] arr) { - final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(arr, 2)); - final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - return p1; - } - - protected static Point point(final double x, final double y) { - return gf.createPoint(new Coordinate(x, y)); - } - - protected static LineString line(final double x1, final double y1, final double x2, final double y2) { - return new LineString(new PackedCoordinateSequence.Double(new double[] { x1, y1, x2, y2 }, 2), gf); - } - - protected static double[] bbox(final double x1, final double y1, final double x2, final double y2) { - return new double[] { x1, y1, x1, y2, x2, y2, x2, y1, x1, y1 }; - } - - protected void assertEqualMongo(final Object expected, final Object actual) throws ComparisonFailure { - try { - assertEquals(expected, actual); - } catch(final Throwable e) { - throw new ComparisonFailure(e.getMessage(), expected.toString(), actual.toString()); - } - } - - public List<FunctionCall> getFilters(final String query) throws Exception { - final FunctionCallCollector collector = new FunctionCallCollector(); - new SPARQLParser().parseQuery(query, null).getTupleExpr().visit(collector); - return collector.getTupleExpr(); - } - - public List<StatementPattern> getSps(final String query) throws Exception { - final StatementPatternCollector collector = new StatementPatternCollector(); - new SPARQLParser().parseQuery(query, null).getTupleExpr().visit(collector); - return collector.getStatementPatterns(); - } - - public QuerySegment<EventQueryNode> getQueryNode(final String query) throws Exception { - final List<QueryModelNode> exprs = getNodes(query); - final QuerySegment<EventQueryNode> node = Mockito.mock(QuerySegment.class); - //provider only cares about Ordered nodes. - Mockito.when(node.getOrderedNodes()).thenReturn(exprs); - return node; - } - - private static List<QueryModelNode> getNodes(final String sparql) throws Exception { - final NodeCollector collector = new NodeCollector(); - new SPARQLParser().parseQuery(sparql, null).getTupleExpr().visit(collector); - return collector.getTupleExpr(); - } - - private static class NodeCollector extends QueryModelVisitorBase<RuntimeException> { - private final List<QueryModelNode> stPatterns = new ArrayList<>(); - - public List<QueryModelNode> getTupleExpr() { - return stPatterns; - } - - @Override - public void meet(final FunctionCall node) { - stPatterns.add(node); - } - - @Override - public void meet(final StatementPattern node) { - stPatterns.add(node); - } - } - - private static class FunctionCallCollector extends QueryModelVisitorBase<RuntimeException> { - private final List<FunctionCall> filters = new ArrayList<>(); - - public List<FunctionCall> getTupleExpr() { - return filters; - } - - @Override - public void meet(final FunctionCall node) { - filters.add(node); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/17cebae3/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalTestUtils.java ---------------------------------------------------------------------- diff --git a/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalTestUtils.java b/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalTestUtils.java new file mode 100644 index 0000000..23399c8 --- /dev/null +++ b/extras/rya.geoindexing/geo.mongo/src/test/java/org/apache/rya/indexing/geotemporal/GeoTemporalTestUtils.java @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.indexing.geotemporal; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.rya.indexing.TemporalInstant; +import org.apache.rya.indexing.TemporalInstantRfc3339; +import org.apache.rya.indexing.external.matching.QuerySegment; +import org.apache.rya.indexing.geotemporal.model.EventQueryNode; +import org.junit.ComparisonFailure; +import org.mockito.Mockito; +import org.openrdf.query.algebra.FunctionCall; +import org.openrdf.query.algebra.QueryModelNode; +import org.openrdf.query.algebra.StatementPattern; +import org.openrdf.query.algebra.helpers.QueryModelVisitorBase; +import org.openrdf.query.algebra.helpers.StatementPatternCollector; +import org.openrdf.query.parser.sparql.SPARQLParser; + +import com.vividsolutions.jts.geom.Coordinate; +import com.vividsolutions.jts.geom.GeometryFactory; +import com.vividsolutions.jts.geom.LineString; +import com.vividsolutions.jts.geom.LinearRing; +import com.vividsolutions.jts.geom.Point; +import com.vividsolutions.jts.geom.Polygon; +import com.vividsolutions.jts.geom.PrecisionModel; +import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence; + +public class GeoTemporalTestUtils { + private static final GeometryFactory gf = new GeometryFactory(new PrecisionModel(), 4326); + + /** + * Make an uniform instant with given seconds. + */ + public static TemporalInstant makeInstant(final int secondsMakeMeUnique) { + return new TemporalInstantRfc3339(2015, 12, 30, 12, 00, secondsMakeMeUnique); + } + + public static Polygon poly(final double[] arr) { + final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(arr, 2)); + final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); + return p1; + } + + public static Point point(final double x, final double y) { + return gf.createPoint(new Coordinate(x, y)); + } + + public static LineString line(final double x1, final double y1, final double x2, final double y2) { + return new LineString(new PackedCoordinateSequence.Double(new double[] { x1, y1, x2, y2 }, 2), gf); + } + + public static double[] bbox(final double x1, final double y1, final double x2, final double y2) { + return new double[] { x1, y1, x1, y2, x2, y2, x2, y1, x1, y1 }; + } + + public static void assertEqualMongo(final Object expected, final Object actual) throws ComparisonFailure { + try { + assertEquals(expected, actual); + } catch(final Throwable e) { + throw new ComparisonFailure(e.getMessage(), expected.toString(), actual.toString()); + } + } + + public static List<FunctionCall> getFilters(final String query) throws Exception { + final FunctionCallCollector collector = new FunctionCallCollector(); + new SPARQLParser().parseQuery(query, null).getTupleExpr().visit(collector); + return collector.getTupleExpr(); + } + + public static List<StatementPattern> getSps(final String query) throws Exception { + final StatementPatternCollector collector = new StatementPatternCollector(); + new SPARQLParser().parseQuery(query, null).getTupleExpr().visit(collector); + return collector.getStatementPatterns(); + } + + public static QuerySegment<EventQueryNode> getQueryNode(final String query) throws Exception { + final List<QueryModelNode> exprs = getNodes(query); + final QuerySegment<EventQueryNode> node = Mockito.mock(QuerySegment.class); + //provider only cares about Ordered nodes. + Mockito.when(node.getOrderedNodes()).thenReturn(exprs); + return node; + } + + private static List<QueryModelNode> getNodes(final String sparql) throws Exception { + final NodeCollector collector = new NodeCollector(); + new SPARQLParser().parseQuery(sparql, null).getTupleExpr().visit(collector); + return collector.getTupleExpr(); + } + + private static class NodeCollector extends QueryModelVisitorBase<RuntimeException> { + private final List<QueryModelNode> stPatterns = new ArrayList<>(); + + public List<QueryModelNode> getTupleExpr() { + return stPatterns; + } + + @Override + public void meet(final FunctionCall node) { + stPatterns.add(node); + } + + @Override + public void meet(final StatementPattern node) { + stPatterns.add(node); + } + } + + private static class FunctionCallCollector extends QueryModelVisitorBase<RuntimeException> { + private final List<FunctionCall> filters = new ArrayList<>(); + + public List<FunctionCall> getTupleExpr() { + return filters; + } + + @Override + public void meet(final FunctionCall node) { + filters.add(node); + } + } +}
