http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest2.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest2.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest2.java new file mode 100644 index 0000000..fc3caf5 --- /dev/null +++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest2.java @@ -0,0 +1,783 @@ +package mvm.rya.indexing.external; + +import java.util.Arrays; +import java.util.List; + +import junit.framework.Assert; +import mvm.rya.accumulo.AccumuloRdfConfiguration; +import mvm.rya.api.persist.RyaDAOException; +import mvm.rya.indexing.RyaSailFactory; +import mvm.rya.indexing.accumulo.ConfigUtils; +import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet; + +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.MutationsRejectedException; +import org.apache.accumulo.core.client.TableExistsException; +import org.apache.accumulo.core.client.TableNotFoundException; +import org.apache.accumulo.core.client.mock.MockInstance; +import org.apache.accumulo.core.client.security.tokens.PasswordToken; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openrdf.model.URI; +import org.openrdf.model.impl.LiteralImpl; +import org.openrdf.model.impl.URIImpl; +import org.openrdf.model.vocabulary.RDF; +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.TupleQueryResultHandler; +import org.openrdf.query.TupleQueryResultHandlerException; +import org.openrdf.repository.RepositoryException; +import org.openrdf.repository.sail.SailRepository; +import org.openrdf.repository.sail.SailRepositoryConnection; +import org.openrdf.sail.Sail; +import org.openrdf.sail.SailException; + +public class AccumuloIndexSetTest2 { + + private SailRepositoryConnection conn; + private SailRepository repo; + private Connector accCon; + String tablePrefix = "table_"; + AccumuloRdfConfiguration conf; + URI sub, sub2, obj, obj2, subclass, subclass2, talksTo; + + @Before + public void init() throws RepositoryException, TupleQueryResultHandlerException, QueryEvaluationException, + MalformedQueryException, AccumuloException, AccumuloSecurityException, TableExistsException, + RyaDAOException { + + conf = new AccumuloRdfConfiguration(); + conf.set(ConfigUtils.USE_PCJ, "true"); + conf.set(ConfigUtils.USE_MOCK_INSTANCE, "true"); + conf.set(ConfigUtils.CLOUDBASE_INSTANCE, "instance"); + conf.setTablePrefix(tablePrefix); + conf.setPcjTables(Arrays.asList("table1", "table2")); + + Sail sail = RyaSailFactory.getInstance(conf); + repo = new SailRepository(sail); + repo.initialize(); + conn = repo.getConnection(); + + sub = new URIImpl("uri:entity"); + subclass = new URIImpl("uri:class"); + obj = new URIImpl("uri:obj"); + talksTo = new URIImpl("uri:talksTo"); + + conn.add(sub, RDF.TYPE, subclass); + conn.add(sub, RDFS.LABEL, new LiteralImpl("label")); + conn.add(sub, talksTo, obj); + + sub2 = new URIImpl("uri:entity2"); + subclass2 = new URIImpl("uri:class2"); + obj2 = new URIImpl("uri:obj2"); + + conn.add(sub2, RDF.TYPE, subclass2); + conn.add(sub2, RDFS.LABEL, new LiteralImpl("label2")); + conn.add(sub2, talksTo, obj2); + + accCon = new MockInstance("instance").getConnector("root", new PasswordToken("".getBytes())); + accCon.tableOperations().create("table1"); + accCon.tableOperations().create("table2"); + + } + + @After + public void close() throws RepositoryException, AccumuloException, AccumuloSecurityException, + TableNotFoundException { + + conf = null; + conn.close(); + accCon.tableOperations().delete(tablePrefix + "spo"); + accCon.tableOperations().delete(tablePrefix + "po"); + accCon.tableOperations().delete(tablePrefix + "osp"); + + if (accCon.tableOperations().exists("table1")) { + accCon.tableOperations().delete("table1"); + } + + if (accCon.tableOperations().exists("table2")) { + accCon.tableOperations().delete("table2"); + } + + } + + @Test + public void testEvaluateTwoIndexTwoVarOrder2() throws RepositoryException, MalformedQueryException, SailException, + QueryEvaluationException, MutationsRejectedException, TableNotFoundException, + TupleQueryResultHandlerException { + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + String indexSparqlString = ""// + + "SELECT ?c ?e ?l " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?e ?o ?l " // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(2, crh.getCount()); + + } + + @Test + public void testEvaluateTwoIndexTwoVarInvalidOrder() throws RepositoryException, MalformedQueryException, + SailException, QueryEvaluationException, MutationsRejectedException, TableNotFoundException, + TupleQueryResultHandlerException { + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + String indexSparqlString = ""// + + "SELECT ?e ?c ?l " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?e ?o ?l " // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(2, crh.getCount()); + + } + + @Test + public void testEvaluateTwoIndexThreeVarOrder1() throws MalformedQueryException, SailException, + QueryEvaluationException, MutationsRejectedException, TableNotFoundException, RepositoryException, + TupleQueryResultHandlerException { + + URI superclass = new URIImpl("uri:superclass"); + URI superclass2 = new URIImpl("uri:superclass2"); + + conn.add(subclass, RDF.TYPE, superclass); + conn.add(subclass2, RDF.TYPE, superclass2); + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + String indexSparqlString = ""// + + "SELECT ?c ?e ?l " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?e ?c ?l ?f ?o" // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?f ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(2, crh.getCount()); + + } + + // @Test + public void testEvaluateTwoIndexThreeVarsDiffLabel() throws RepositoryException, MalformedQueryException, + SailException, QueryEvaluationException, MutationsRejectedException, TableNotFoundException, + TupleQueryResultHandlerException { + + URI superclass = new URIImpl("uri:superclass"); + URI superclass2 = new URIImpl("uri:superclass2"); + conn.add(subclass, RDF.TYPE, superclass); + conn.add(subclass2, RDF.TYPE, superclass2); + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + // TODO Auto-generated method stub + String indexSparqlString = ""// + + "SELECT ?dog ?pig ?owl " // + + "{" // + + " ?pig a ?dog . "// + + " ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?owl "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?e ?c ?l ?f ?o" // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?f ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(2, crh.getCount()); + + } + + @Test + public void testEvaluateTwoIndexThreeVarOrder2() throws RepositoryException, MalformedQueryException, + SailException, QueryEvaluationException, MutationsRejectedException, TableNotFoundException, + TupleQueryResultHandlerException { + + URI superclass = new URIImpl("uri:superclass"); + URI superclass2 = new URIImpl("uri:superclass2"); + + conn.add(subclass, RDF.TYPE, superclass); + conn.add(subclass2, RDF.TYPE, superclass2); + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + // TODO Auto-generated method stub + String indexSparqlString = ""// + + "SELECT ?c ?e ?l " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?o ?f ?e ?c ?l " // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?f ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(2, crh.getCount()); + + } + + @Test + public void testEvaluateTwoIndexThreeVarOrder3ThreeBindingSet() throws TupleQueryResultHandlerException, + QueryEvaluationException, MalformedQueryException, RepositoryException, SailException, + MutationsRejectedException, TableNotFoundException { + + URI sub3 = new URIImpl("uri:entity3"); + URI subclass3 = new URIImpl("uri:class3"); + URI obj3 = new URIImpl("uri:obj3"); + + URI superclass = new URIImpl("uri:superclass"); + URI superclass2 = new URIImpl("uri:superclass2"); + URI superclass3 = new URIImpl("uri:superclass3"); + + conn.add(sub3, RDF.TYPE, subclass3); + conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3")); + conn.add(sub3, talksTo, obj3); + + conn.add(subclass, RDF.TYPE, superclass); + conn.add(subclass2, RDF.TYPE, superclass2); + conn.add(subclass3, RDF.TYPE, superclass3); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3")); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + String indexSparqlString = ""// + + "SELECT ?c ?e ?l " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?o ?f ?l ?e ?c " // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?f ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(3, crh.getCount()); + + } + + @Test + public void testEvaluateTwoIndexThreeVarOrder5ThreeBindingSet() throws MalformedQueryException, SailException, + QueryEvaluationException, MutationsRejectedException, TableNotFoundException, RepositoryException, + TupleQueryResultHandlerException { + + URI sub3 = new URIImpl("uri:entity3"); + URI subclass3 = new URIImpl("uri:class3"); + URI obj3 = new URIImpl("uri:obj3"); + + URI superclass = new URIImpl("uri:superclass"); + URI superclass2 = new URIImpl("uri:superclass2"); + URI superclass3 = new URIImpl("uri:superclass3"); + + conn.add(sub3, RDF.TYPE, subclass3); + conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3")); + conn.add(sub3, talksTo, obj3); + + conn.add(subclass, RDF.TYPE, superclass); + conn.add(subclass2, RDF.TYPE, superclass2); + conn.add(subclass3, RDF.TYPE, superclass3); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3")); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + // TODO Auto-generated method stub + String indexSparqlString = ""// + + "SELECT ?c ?e ?l " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?o ?f ?e ?l ?c " // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?f ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(3, crh.getCount()); + + } + + @Test + public void testEvaluateTwoIndexThreeVarOrder4ThreeBindingSet() throws MalformedQueryException, SailException, + QueryEvaluationException, MutationsRejectedException, TableNotFoundException, RepositoryException, + TupleQueryResultHandlerException { + + URI sub3 = new URIImpl("uri:entity3"); + URI subclass3 = new URIImpl("uri:class3"); + URI obj3 = new URIImpl("uri:obj3"); + + URI superclass = new URIImpl("uri:superclass"); + URI superclass2 = new URIImpl("uri:superclass2"); + URI superclass3 = new URIImpl("uri:superclass3"); + + conn.add(sub3, RDF.TYPE, subclass3); + conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3")); + conn.add(sub3, talksTo, obj3); + + conn.add(subclass, RDF.TYPE, superclass); + conn.add(subclass2, RDF.TYPE, superclass2); + conn.add(subclass3, RDF.TYPE, superclass3); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3")); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + // TODO Auto-generated method stub + String indexSparqlString = ""// + + "SELECT ?c ?e ?l " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?o ?f ?c ?e ?l " // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?f ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(3, crh.getCount()); + + } + + @Test + public void testEvaluateTwoIndexThreeVarOrder6ThreeBindingSet() throws MalformedQueryException, SailException, + QueryEvaluationException, MutationsRejectedException, TableNotFoundException, RepositoryException, + TupleQueryResultHandlerException { + + URI sub3 = new URIImpl("uri:entity3"); + URI subclass3 = new URIImpl("uri:class3"); + URI obj3 = new URIImpl("uri:obj3"); + + URI superclass = new URIImpl("uri:superclass"); + URI superclass2 = new URIImpl("uri:superclass2"); + URI superclass3 = new URIImpl("uri:superclass3"); + + conn.add(sub3, RDF.TYPE, subclass3); + conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3")); + conn.add(sub3, talksTo, obj3); + + conn.add(subclass, RDF.TYPE, superclass); + conn.add(subclass2, RDF.TYPE, superclass2); + conn.add(subclass3, RDF.TYPE, superclass3); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3")); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + String indexSparqlString = ""// + + "SELECT ?c ?e ?l " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?c ?l ?e ?o ?f " // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?f ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(3, crh.getCount()); + + } + + @Test + public void testEvaluateTwoIndexThreeVarOrder7ThreeBindingSet() throws MalformedQueryException, SailException, + QueryEvaluationException, MutationsRejectedException, TableNotFoundException, RepositoryException, + TupleQueryResultHandlerException { + + URI sub3 = new URIImpl("uri:entity3"); + URI subclass3 = new URIImpl("uri:class3"); + URI obj3 = new URIImpl("uri:obj3"); + + URI superclass = new URIImpl("uri:superclass"); + URI superclass2 = new URIImpl("uri:superclass2"); + URI superclass3 = new URIImpl("uri:superclass3"); + + conn.add(sub3, RDF.TYPE, subclass3); + conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3")); + conn.add(sub3, talksTo, obj3); + + conn.add(subclass, RDF.TYPE, superclass); + conn.add(subclass2, RDF.TYPE, superclass2); + conn.add(subclass3, RDF.TYPE, superclass3); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3")); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + String indexSparqlString = ""// + + "SELECT ?c ?e ?l " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?o ?l ?c ?e ?f " // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?f ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(3, crh.getCount()); + + } + + @Test + public void testEvaluateOneIndex() throws RepositoryException, MalformedQueryException, SailException, + QueryEvaluationException, MutationsRejectedException, TableNotFoundException, + TupleQueryResultHandlerException { + + String indexSparqlString = ""// + + "SELECT ?dog ?pig ?duck " // + + "{" // + + " ?pig a ?dog . "// + + " ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "// + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh); + + Assert.assertEquals(2, crh.getCount()); + + } + + @Test + public void testEvaluateTwoIndexThreeVarOrder3() throws RepositoryException, MalformedQueryException, + SailException, QueryEvaluationException, MutationsRejectedException, TableNotFoundException, + TupleQueryResultHandlerException { + + URI superclass = new URIImpl("uri:superclass"); + URI superclass2 = new URIImpl("uri:superclass2"); + + conn.add(subclass, RDF.TYPE, superclass); + conn.add(subclass2, RDF.TYPE, superclass2); + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + String indexSparqlString = ""// + + "SELECT ?dog ?pig ?duck " // + + "{" // + + " ?pig a ?dog . "// + + " ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?o ?f ?e ?c ?l " // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?f ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "// + + " ?c a ?f . " // + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2"); + + CountingResultHandler crh = new CountingResultHandler(); + + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(2, crh.getCount()); + + } + + public static class CountingResultHandler implements TupleQueryResultHandler { + private int count = 0; + + public int getCount() { + return count; + } + + public void resetCount() { + this.count = 0; + } + + @Override + public void startQueryResult(List<String> arg0) throws TupleQueryResultHandlerException { + } + + @Override + public void handleSolution(BindingSet arg0) throws TupleQueryResultHandlerException { + count++; + } + + @Override + public void endQueryResult() throws TupleQueryResultHandlerException { + } + + @Override + public void handleBoolean(boolean arg0) throws QueryResultHandlerException { + // TODO Auto-generated method stub + + } + + @Override + public void handleLinks(List<String> arg0) throws QueryResultHandlerException { + // TODO Auto-generated method stub + + } + } + +}
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerIntegrationTest.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerIntegrationTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerIntegrationTest.java new file mode 100644 index 0000000..2d997af --- /dev/null +++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerIntegrationTest.java @@ -0,0 +1,530 @@ +package mvm.rya.indexing.external; + +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import junit.framework.Assert; +import mvm.rya.accumulo.AccumuloRdfConfiguration; +import mvm.rya.api.persist.RyaDAOException; +import mvm.rya.indexing.RyaSailFactory; +import mvm.rya.indexing.accumulo.ConfigUtils; +import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet; + +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.Scanner; +import org.apache.accumulo.core.client.TableExistsException; +import org.apache.accumulo.core.client.TableNotFoundException; +import org.apache.accumulo.core.client.mock.MockInstance; +import org.apache.accumulo.core.client.security.tokens.PasswordToken; +import org.apache.accumulo.core.data.Key; +import org.apache.accumulo.core.data.Value; +import org.apache.accumulo.core.security.Authorizations; +import org.apache.hadoop.conf.Configuration; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openrdf.model.URI; +import org.openrdf.model.impl.LiteralImpl; +import org.openrdf.model.impl.URIImpl; +import org.openrdf.model.vocabulary.RDF; +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.TupleQueryResultHandler; +import org.openrdf.query.TupleQueryResultHandlerException; +import org.openrdf.repository.RepositoryException; +import org.openrdf.repository.sail.SailRepository; +import org.openrdf.repository.sail.SailRepositoryConnection; +import org.openrdf.sail.Sail; +import org.openrdf.sail.SailException; +import org.openrdf.sail.memory.MemoryStore; + +public class PrecompJoinOptimizerIntegrationTest { + + private SailRepositoryConnection conn; + private SailRepository repo; + private Connector accCon; + String tablePrefix = "table_"; + AccumuloRdfConfiguration conf; + URI sub, sub2, obj,obj2,subclass, subclass2, talksTo; + + + + + @Before + public void init() throws RepositoryException, TupleQueryResultHandlerException, QueryEvaluationException, MalformedQueryException, + AccumuloException, AccumuloSecurityException, TableExistsException, RyaDAOException { + + conf = new AccumuloRdfConfiguration(); + conf.set(ConfigUtils.USE_PCJ, "true"); + conf.set(ConfigUtils.USE_MOCK_INSTANCE,"true"); + conf.set(ConfigUtils.CLOUDBASE_INSTANCE, "instance"); + conf.setTablePrefix(tablePrefix); + + Sail sail = RyaSailFactory.getInstance(conf); + repo = new SailRepository(sail); + repo.initialize(); + conn = repo.getConnection(); + + sub = new URIImpl("uri:entity"); + subclass = new URIImpl("uri:class"); + obj = new URIImpl("uri:obj"); + talksTo = new URIImpl("uri:talksTo"); + + conn.add(sub, RDF.TYPE, subclass); + conn.add(sub, RDFS.LABEL, new LiteralImpl("label")); + conn.add(sub, talksTo, obj); + + sub2 = new URIImpl("uri:entity2"); + subclass2 = new URIImpl("uri:class2"); + obj2 = new URIImpl("uri:obj2"); + + conn.add(sub2, RDF.TYPE, subclass2); + conn.add(sub2, RDFS.LABEL, new LiteralImpl("label2")); + conn.add(sub2, talksTo, obj2); + + accCon = new MockInstance("instance").getConnector("root",new PasswordToken("".getBytes())); + + } + + + @After + public void close() throws RepositoryException, AccumuloException, AccumuloSecurityException, TableNotFoundException { + + conf = null; + conn.close(); + accCon.tableOperations().delete(tablePrefix + "spo"); + accCon.tableOperations().delete(tablePrefix + "po"); + accCon.tableOperations().delete(tablePrefix + "osp"); + } + + + + @Test + public void testEvaluateSingeIndex() throws TupleQueryResultHandlerException, QueryEvaluationException, + MalformedQueryException, RepositoryException, AccumuloException, + AccumuloSecurityException, TableExistsException, RyaDAOException, SailException, TableNotFoundException { + + if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) { + accCon.tableOperations().delete(tablePrefix + "INDEX1"); + } + accCon.tableOperations().create(tablePrefix + "INDEX1"); + + String indexSparqlString = ""// + + "SELECT ?e ?l ?c " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + AccumuloIndexSet ais = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablePrefix + "INDEX1"); + + + String queryString = ""// + + "SELECT ?e ?c ?l ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "// + + " ?e <uri:talksTo> ?o . "// + + "}";// + + CountingResultHandler crh = new CountingResultHandler(); + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + +// Scanner scan = accCon.createScanner(tablePrefix + "spo", new Authorizations("U")); +// +// for(Entry<Key,Value> e: scan) { +// System.out.println(e.getKey().getRow()); +// } + + Assert.assertEquals(2, crh.getCount()); + + + } + + + + + + + @Test + public void testEvaluateTwoIndexTwoVarOrder1() throws AccumuloException, AccumuloSecurityException, + TableExistsException, RepositoryException, MalformedQueryException, SailException, QueryEvaluationException, + TableNotFoundException, TupleQueryResultHandlerException, RyaDAOException { + + if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) { + accCon.tableOperations().delete(tablePrefix + "INDEX1"); + } + + if (accCon.tableOperations().exists(tablePrefix + "INDEX2")) { + accCon.tableOperations().delete(tablePrefix + "INDEX2"); + } + + accCon.tableOperations().create(tablePrefix + "INDEX1"); + accCon.tableOperations().create(tablePrefix + "INDEX2"); + + + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + + + String indexSparqlString = ""// + + "SELECT ?e ?l ?c " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?e ?o ?l " // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String queryString = ""// + + "SELECT ?e ?c ?l ?o " // + + "{" // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "// + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablePrefix + "INDEX1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, tablePrefix + "INDEX2"); + + CountingResultHandler crh = new CountingResultHandler(); + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(2, crh.getCount()); + + + + + } + + + @Test + public void testEvaluateSingeFilterIndex() throws TupleQueryResultHandlerException, QueryEvaluationException, + MalformedQueryException, RepositoryException, AccumuloException, + AccumuloSecurityException, TableExistsException, RyaDAOException, SailException, TableNotFoundException { + + if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) { + accCon.tableOperations().delete(tablePrefix + "INDEX1"); + } + accCon.tableOperations().create(tablePrefix + "INDEX1"); + + String indexSparqlString = ""// + + "SELECT ?e ?l ?c " // + + "{" // + + " Filter(?e = <uri:entity>) " // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + AccumuloIndexSet ais = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablePrefix + "INDEX1"); + + + String queryString = ""// + + "SELECT ?e ?c ?l ?o " // + + "{" // + + " Filter(?e = <uri:entity>) " // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "// + + " ?e <uri:talksTo> ?o . "// + + "}";// + + CountingResultHandler crh = new CountingResultHandler(); + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(1, crh.getCount()); + + + } + + + + + @Test + public void testEvaluateSingeFilterWithUnion() throws TupleQueryResultHandlerException, QueryEvaluationException, + MalformedQueryException, RepositoryException, AccumuloException, + AccumuloSecurityException, TableExistsException, RyaDAOException, SailException, TableNotFoundException { + + if (accCon.tableOperations().exists(tablePrefix + "INDEX2")) { + accCon.tableOperations().delete(tablePrefix + "INDEX2"); + } + accCon.tableOperations().create(tablePrefix + "INDEX2"); + + String indexSparqlString2 = ""// + + "SELECT ?e ?l ?c " // + + "{" // + + " Filter(?l = \"label2\") " // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, tablePrefix + "INDEX2"); + + + String queryString = ""// + + "SELECT ?e ?c ?o ?m ?l" // + + "{" // + + " Filter(?l = \"label2\") " // + + " ?e <uri:talksTo> ?o . "// + + " { ?e a ?c . ?e <http://www.w3.org/2000/01/rdf-schema#label> ?m }"// + + " UNION { ?e a ?c . ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l }"// + + "}";// + + CountingResultHandler crh = new CountingResultHandler(); + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(1, crh.getCount()); + + + } + + + + @Test + public void testEvaluateSingeFilterWithLeftJoin() throws TupleQueryResultHandlerException, QueryEvaluationException, + MalformedQueryException, RepositoryException, AccumuloException, + AccumuloSecurityException, TableExistsException, RyaDAOException, SailException, TableNotFoundException { + + if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) { + accCon.tableOperations().delete(tablePrefix + "INDEX1"); + } + accCon.tableOperations().create(tablePrefix + "INDEX1"); + + String indexSparqlString1 = ""// + + "SELECT ?e ?l ?c " // + + "{" // + + " Filter(?l = \"label3\") " // + + " ?e a ?c . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + + URI sub3 = new URIImpl("uri:entity3"); + URI subclass3 = new URIImpl("uri:class3"); + conn.add(sub3, RDF.TYPE, subclass3); + conn.add(sub3,RDFS.LABEL, new LiteralImpl("label3")); + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString1, conn, accCon, tablePrefix + "INDEX1"); + + String queryString = ""// + + "SELECT ?e ?c ?o ?m ?l" // + + "{" // + + " Filter(?l = \"label3\") " // + + " ?e a ?c . " // + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " // + + " OPTIONAL { ?e <uri:talksTo> ?o . ?e <http://www.w3.org/2000/01/rdf-schema#label> ?m }"// + + "}";// + + CountingResultHandler crh = new CountingResultHandler(); + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(1, crh.getCount()); + + + } + + + + + + + + @Test + public void testEvaluateTwoIndexUnionFilter() throws AccumuloException, AccumuloSecurityException, + TableExistsException, RepositoryException, MalformedQueryException, SailException, QueryEvaluationException, + TableNotFoundException, TupleQueryResultHandlerException, RyaDAOException { + + if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) { + accCon.tableOperations().delete(tablePrefix + "INDEX1"); + } + + if (accCon.tableOperations().exists(tablePrefix + "INDEX2")) { + accCon.tableOperations().delete(tablePrefix + "INDEX2"); + } + + accCon.tableOperations().create(tablePrefix + "INDEX1"); + accCon.tableOperations().create(tablePrefix + "INDEX2"); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + conn.add(sub, RDF.TYPE, obj); + conn.add(sub2, RDF.TYPE, obj2); + + + String indexSparqlString = ""// + + "SELECT ?e ?l ?o " // + + "{" // + + " Filter(?l = \"label2\") " // + + " ?e a ?o . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?e ?l ?o " // + + "{" // + + " Filter(?l = \"label2\") " // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String queryString = ""// + + "SELECT ?c ?e ?l ?o " // + + "{" // + + " Filter(?l = \"label2\") " // + + " ?e a ?c . "// + + " { ?e a ?o . ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l }"// + + " UNION { ?e <uri:talksTo> ?o . ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l }"// + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablePrefix + "INDEX1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, tablePrefix + "INDEX2"); + + CountingResultHandler crh = new CountingResultHandler(); + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + + Assert.assertEquals(6, crh.getCount()); + + + + } + + + + + + @Test + public void testEvaluateTwoIndexLeftJoinUnionFilter() throws AccumuloException, AccumuloSecurityException, + TableExistsException, RepositoryException, MalformedQueryException, SailException, QueryEvaluationException, + TableNotFoundException, TupleQueryResultHandlerException, RyaDAOException { + + if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) { + accCon.tableOperations().delete(tablePrefix + "INDEX1"); + } + + if (accCon.tableOperations().exists(tablePrefix + "INDEX2")) { + accCon.tableOperations().delete(tablePrefix + "INDEX2"); + } + + accCon.tableOperations().create(tablePrefix + "INDEX1"); + accCon.tableOperations().create(tablePrefix + "INDEX2"); + + conn.add(obj, RDFS.LABEL, new LiteralImpl("label")); + conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2")); + conn.add(sub, RDF.TYPE, obj); + conn.add(sub2, RDF.TYPE, obj2); + + URI livesIn = new URIImpl("uri:livesIn"); + URI city = new URIImpl("uri:city"); + URI city2 = new URIImpl("uri:city2"); + URI city3 = new URIImpl("uri:city3"); + conn.add(sub,livesIn,city); + conn.add(sub2,livesIn,city2); + conn.add(sub2,livesIn,city3); + conn.add(sub,livesIn,city3); + + + String indexSparqlString = ""// + + "SELECT ?e ?l ?o " // + + "{" // + + " ?e a ?o . "// + + " ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String indexSparqlString2 = ""// + + "SELECT ?e ?l ?o " // + + "{" // + + " ?e <uri:talksTo> ?o . "// + + " ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "// + + "}";// + + String queryString = ""// + + "SELECT ?c ?e ?l ?o " // + + "{" // + + " Filter(?c = <uri:city3>) " // + + " ?e <uri:livesIn> ?c . "// + + " OPTIONAL{{ ?e a ?o . ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l }"// + + " UNION { ?e <uri:talksTo> ?o . ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l }}"// + + "}";// + + AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablePrefix + "INDEX1"); + AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, tablePrefix + "INDEX2"); + + CountingResultHandler crh = new CountingResultHandler(); + conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh); + +// Scanner scan = accCon.createScanner(tablePrefix + "spo", new Authorizations("U")); +// +// for(Entry<Key,Value> e: scan) { +// System.out.println(e.getKey().getRow()); +// } + + Assert.assertEquals(6, crh.getCount()); + + + + } + + + + + public static class CountingResultHandler implements TupleQueryResultHandler { + private int count = 0; + + public int getCount() { + return count; + } + + public void resetCount() { + this.count = 0; + } + + @Override + public void startQueryResult(List<String> arg0) throws TupleQueryResultHandlerException { + } + + + @Override + public void handleSolution(BindingSet arg0) throws TupleQueryResultHandlerException { + System.out.println(arg0); + count++; + System.out.println("Count is " + count); + } + + @Override + public void endQueryResult() throws TupleQueryResultHandlerException { + } + + @Override + public void handleBoolean(boolean arg0) throws QueryResultHandlerException { + // TODO Auto-generated method stub + + } + + @Override + public void handleLinks(List<String> arg0) throws QueryResultHandlerException { + // TODO Auto-generated method stub + + } + } + + + + + +} + + + + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest.java new file mode 100644 index 0000000..9365744 --- /dev/null +++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest.java @@ -0,0 +1,501 @@ +package mvm.rya.indexing.external; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; +import mvm.rya.indexing.external.tupleSet.ExternalTupleSet; +import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet; + +import org.junit.Test; +import org.openrdf.query.algebra.Projection; +import org.openrdf.query.algebra.QueryModelNode; +import org.openrdf.query.algebra.StatementPattern; +import org.openrdf.query.algebra.TupleExpr; +import org.openrdf.query.algebra.helpers.QueryModelVisitorBase; +import org.openrdf.query.algebra.helpers.StatementPatternCollector; +import org.openrdf.query.parser.ParsedQuery; +import org.openrdf.query.parser.sparql.SPARQLParser; + +import com.beust.jcommander.internal.Lists; + +public class PrecompJoinOptimizerTest { + + private String q7 = ""// + + "SELECT ?s ?t ?u " // + + "{" // + + " ?s a ?t ."// + + " ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."// + + " ?u <uri:talksTo> ?s . "// + + "}";// + + private String q8 = ""// + + "SELECT ?e ?l ?c " // + + "{" // + + " ?e a ?l ."// + + " ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."// + + " ?c <uri:talksTo> ?e . "// + + "}";// + + private String q9 = ""// + + "SELECT ?f ?m ?d " // + + "{" // + + " ?f a ?m ."// + + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + + " ?d <uri:talksTo> ?f . "// + + "}";// + + + + + private String q15 = ""// + + "SELECT ?f ?m ?d ?e ?l ?c " // + + "{" // + + " ?f a ?m ."// + + " ?e a ?l ."// + + " ?d <uri:talksTo> ?f . "// + + " ?c <uri:talksTo> ?e . "// + + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + + " ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."// + + "}";// + + private String q16 = ""// + + "SELECT ?f ?m ?d ?e ?l " // + + "{" // + + " ?d <uri:talksTo> ?f . "// + + " ?d <uri:talksTo> ?e . "// + + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + + " ?l <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + + "}";// + + private String q17 = ""// + + "SELECT ?chicken ?dog ?cat " // + + "{" // + + " ?chicken <uri:talksTo> ?dog . "// + + " ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."// + + "}";// + + private String q18 = ""// + + "SELECT ?dog ?chicken " // + + "{" // + + " ?chicken <uri:talksTo> ?dog . "// + + "}";// + + private String q19 = ""// + + "SELECT ?cat ?chicken " // + + "{" // + + " ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."// + + "}";// + + + private String q20 = ""// + + "SELECT ?f ?m ?d ?e ?l ?c " // + + "{" // + + " ?f a ?m ."// + + " ?e a ?l ."// + + " ?d <uri:talksTo> ?f . "// + + " ?c <uri:talksTo> ?e . "// + + " ?m <uri:talksTo> ?e . "// + + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + + " ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."// + + "}";// + + + + private String q21 = ""// + + "SELECT ?u ?s ?t " // + + "{" // + + " ?s a ?t ."// + + " ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."// + + " ?u <uri:talksTo> ?s . "// + + "}";// + + + + @Test + public void testSingleIndex() throws Exception { + + SPARQLParser parser = new SPARQLParser(); + + + ParsedQuery pq1 = parser.parseQuery(q15, null); + ParsedQuery pq2 = parser.parseQuery(q7, null); + ParsedQuery pq3 = parser.parseQuery(q8, null); + ParsedQuery pq4 = parser.parseQuery(q9, null); + + SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr()); + SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr()); + SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr()); + + List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>(); + + list.add(extTup1); + + List<QueryModelNode> optTupNodes = Lists.newArrayList(); + optTupNodes.add(extTup2); + optTupNodes.add(extTup3); + + PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true); + TupleExpr te = pq1.getTupleExpr(); + pcj.optimize(te, null, null); + + NodeCollector nc = new NodeCollector(); + te.visit(nc); + + List<QueryModelNode> qNodes = nc.getNodes(); + + + Assert.assertEquals(qNodes.size(), optTupNodes.size()); + for(QueryModelNode node: qNodes) { + Assert.assertTrue(optTupNodes.contains(node)); + } + + + } + + + + + + @Test + public void testSingleIndex2() throws Exception { + + String q1 = ""// + + "SELECT ?f ?m ?d ?e ?l ?c " // + + "{" // + + " ?f a ?m ."// + + " ?c a ?l ."// + + " ?d <uri:talksTo> ?f . "// + + " ?e <uri:talksTo> ?c . "// + + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + + " ?l <http://www.w3.org/2000/01/rdf-schema#label> ?e ."// + + " ?m <uri:talksTo> ?e . "// + + "}";// + + String q2 = ""// + + "SELECT ?u ?s ?t " // + + "{" // + + " ?s a ?t ."// + + " ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."// + + " ?u <uri:talksTo> ?s . "// + + "}";// + + String q3 = ""// + + "SELECT ?e ?c ?l " // + + "{" // + + " ?c a ?l ."// + + " ?l <http://www.w3.org/2000/01/rdf-schema#label> ?e ."// + + " ?e <uri:talksTo> ?c . "// + + "}";// + + String q4 = ""// + + "SELECT ?d ?f ?m " // + + "{" // + + " ?f a ?m ."// + + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + + " ?d <uri:talksTo> ?f . "// + + "}";// + + SPARQLParser parser = new SPARQLParser(); + + ParsedQuery pq1 = parser.parseQuery(q1, null); + ParsedQuery pq2 = parser.parseQuery(q2, null); + ParsedQuery pq3 = parser.parseQuery(q3, null); + ParsedQuery pq4 = parser.parseQuery(q4, null); + + SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr()); + SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr()); + SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr()); + + List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>(); + + list.add(extTup1); + + List<StatementPattern> spList = StatementPatternCollector.process(pq1.getTupleExpr()); + List<QueryModelNode> optTupNodes = Lists.newArrayList(); + optTupNodes.add(extTup3); + optTupNodes.add(spList.get(6)); + optTupNodes.add(extTup2); + + PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true); + TupleExpr te = pq1.getTupleExpr(); + pcj.optimize(te, null, null); + + NodeCollector nc = new NodeCollector(); + te.visit(nc); + + //System.out.println("Optimal plan is " + optimalTup); + + List<QueryModelNode> qNodes = nc.getNodes(); + //System.out.println("Returned list is " + qNodes + " and comp list is " + optTupNodes); + + Assert.assertTrue(qNodes.equals(optTupNodes)); + + } + + + + + + + + + @Test + public void testTwoIndex() throws Exception { + + String q1 = ""// + + "SELECT ?f ?m ?d ?h ?i " // + + "{" // + + " ?f a ?m ."// + + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + + " ?d <uri:talksTo> ?f . "// + + " ?d <uri:hangOutWith> ?f ." // + + " ?f <uri:hangOutWith> ?h ." // + + " ?f <uri:associatesWith> ?i ." // + + " ?i <uri:associatesWith> ?h ." // + + "}";// + + String q2 = ""// + + "SELECT ?t ?s ?u " // + + "{" // + + " ?s a ?t ."// + + " ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."// + + " ?u <uri:talksTo> ?s . "// + + "}";// + + String q3 = ""// + + "SELECT ?s ?t ?u " // + + "{" // + + " ?s <uri:hangOutWith> ?t ." // + + " ?t <uri:hangOutWith> ?u ." // + + "}";// + + String q4 = ""// + + "SELECT ?s ?t ?u " // + + "{" // + + " ?s <uri:associatesWith> ?t ." // + + " ?t <uri:associatesWith> ?u ." // + + "}";// + + String q5 = ""// + + "SELECT ?m ?f ?d " // + + "{" // + + " ?f a ?m ."// + + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + + " ?d <uri:talksTo> ?f . "// + + "}";// + + String q6 = ""// + + "SELECT ?d ?f ?h " // + + "{" // + + " ?d <uri:hangOutWith> ?f ." // + + " ?f <uri:hangOutWith> ?h ." // + + "}";// + + String q7 = ""// + + "SELECT ?f ?i ?h " // + + "{" // + + " ?f <uri:associatesWith> ?i ." // + + " ?i <uri:associatesWith> ?h ." // + + "}";// + + SPARQLParser parser = new SPARQLParser(); + + ParsedQuery pq1 = parser.parseQuery(q1, null); + ParsedQuery pq2 = parser.parseQuery(q2, null); + ParsedQuery pq3 = parser.parseQuery(q3, null); + ParsedQuery pq4 = parser.parseQuery(q4, null); + ParsedQuery pq5 = parser.parseQuery(q5, null); + ParsedQuery pq6 = parser.parseQuery(q6, null); + ParsedQuery pq7 = parser.parseQuery(q7, null); + + SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr()); + SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr()); + SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr()); + SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr()); + SimpleExternalTupleSet extTup5 = new SimpleExternalTupleSet((Projection) pq6.getTupleExpr()); + SimpleExternalTupleSet extTup6 = new SimpleExternalTupleSet((Projection) pq7.getTupleExpr()); + + List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>(); + + list.add(extTup2); + list.add(extTup1); + list.add(extTup3); + + List<QueryModelNode> optTupNodes = Lists.newArrayList(); + optTupNodes.add(extTup4); + optTupNodes.add(extTup6); + optTupNodes.add(extTup5); + + PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true); + TupleExpr te = pq1.getTupleExpr(); + pcj.optimize(te, null, null); + + System.out.println(te); + + NodeCollector nc = new NodeCollector(); + te.visit(nc); + + List<QueryModelNode> qNodes = nc.getNodes(); + + Assert.assertTrue(qNodes.equals(optTupNodes)); + + } + + + + + + + @Test + public void twoIndexFilterTest() { + + + String q1 = ""// + + "SELECT ?f ?m ?d ?e ?l ?c " // + + "{" // + + " Filter(?f > \"5\")." // + + " Filter(?e > \"5\")." // + + " ?f a ?m ."// + + " ?e a ?l ."// + + " ?d <uri:talksTo> ?f . "// + + " ?c <uri:talksTo> ?e . "// + + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + + " ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."// + + "}";// + + + String q2 = ""// + + "SELECT ?s ?t ?u " // + + "{" // + + " ?s a ?t ."// + + " ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."// + + " ?u <uri:talksTo> ?s . "// + + "}";// + + + String q3 = ""// + + "SELECT ?s ?t ?u " // + + "{" // + + " Filter(?s > \"5\") ."// + + " ?s a ?t ."// + + " ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."// + + " ?u <uri:talksTo> ?s . "// + + "}";// + + + + String q4 = ""// + + "SELECT ?f ?m ?d " // + + "{" // + + " Filter(?f > \"5\") ."// + + " ?f a ?m ."// + + " ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."// + + " ?d <uri:talksTo> ?f . "// + + "}";// + + + String q5 = ""// + + "SELECT ?e ?l ?c " // + + "{" // + + " Filter(?e > \"5\") ."// + + " ?e a ?l ."// + + " ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."// + + " ?c <uri:talksTo> ?e . "// + + "}";// + + + + + SPARQLParser parser = new SPARQLParser(); + + ParsedQuery pq1 = null; + ParsedQuery pq2 = null; + ParsedQuery pq3 = null; + ParsedQuery pq4 = null; + ParsedQuery pq5 = null; + + + + try { + pq1 = parser.parseQuery(q1, null); + pq2 = parser.parseQuery(q2, null); + pq3 = parser.parseQuery(q3, null); + pq4 = parser.parseQuery(q4, null); + pq5 = parser.parseQuery(q5, null); + + + } catch (Exception e) { + e.printStackTrace(); + } + + SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr()); + SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr()); + SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr()); + SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr()); + + List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>(); + + list.add(extTup2); + list.add(extTup1); + + List<ExternalTupleSet> list2 = new ArrayList<ExternalTupleSet>(); + + list2.add(extTup3); + list2.add(extTup4); + + PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true); + TupleExpr te = pq1.getTupleExpr(); + pcj.optimize(te, null, null); + + System.out.println(te); + + NodeCollector nc = new NodeCollector(); + te.visit(nc); + + Assert.assertEquals(nc.getNodes().size(), list2.size()); + + for(QueryModelNode e: nc.getNodes()) { + Assert.assertTrue(list2.contains((ExternalTupleSet)e)); + } + + + + } + + + + + + + + + public static class NodeCollector extends QueryModelVisitorBase<RuntimeException> { + + List<QueryModelNode> qNodes = Lists.newArrayList(); + + + public List<QueryModelNode> getNodes() { + return qNodes; + } + + + + @Override + public void meetNode(QueryModelNode node) { + if(node instanceof StatementPattern || node instanceof ExternalTupleSet) { + qNodes.add(node); + } + super.meetNode(node); + + } + + + } + + + + + + +}
