http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/ParsedQueryUtil.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/ParsedQueryUtil.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/ParsedQueryUtil.java deleted file mode 100644 index b41c9c9..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/ParsedQueryUtil.java +++ /dev/null @@ -1,60 +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 mvm.rya.indexing.external.tupleSet; - -import static com.google.common.base.Preconditions.checkNotNull; - -import java.util.concurrent.atomic.AtomicReference; - -import javax.annotation.ParametersAreNonnullByDefault; - -import org.openrdf.query.algebra.Projection; -import org.openrdf.query.algebra.helpers.QueryModelVisitorBase; -import org.openrdf.query.parser.ParsedQuery; - -import com.google.common.base.Optional; - -/** - * Utilities that help applications inspect {@link ParsedQuery} objects. - */ -@ParametersAreNonnullByDefault -public class ParsedQueryUtil { - - /** - * Finds the first {@link Projection} node within a {@link ParsedQuery}. - * - * @param query - The query that will be searched. (not null) - * @return The first projection encountered if the query has one; otherwise absent. - */ - public Optional<Projection> findProjection(final ParsedQuery query) { - checkNotNull(query); - - // When a projection is encountered for the requested index, store it in atomic reference and quit searching. - final AtomicReference<Projection> projectionRef = new AtomicReference<>(); - - query.getTupleExpr().visit(new QueryModelVisitorBase<RuntimeException>() { - @Override - public void meet(Projection projection) { - projectionRef.set(projection); - } - }); - - return Optional.fromNullable( projectionRef.get() ); - } -} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/SimpleExternalTupleSet.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/SimpleExternalTupleSet.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/SimpleExternalTupleSet.java deleted file mode 100644 index ccdb7a8..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/SimpleExternalTupleSet.java +++ /dev/null @@ -1,81 +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 mvm.rya.indexing.external.tupleSet; - -import java.util.ArrayList; -import java.util.List; - -import org.openrdf.query.BindingSet; -import org.openrdf.query.QueryEvaluationException; -import org.openrdf.query.algebra.Projection; -import org.openrdf.query.algebra.QueryModelVisitor; - -import com.google.common.base.Joiner; - -import info.aduna.iteration.CloseableIteration; - -/** - * This a testing class to create mock pre-computed join nodes in order to - * test the {@link PrecompJoinOptimizer} for query planning. - */ -public class SimpleExternalTupleSet extends ExternalTupleSet { - - /** - * Constructs an instance of {@link SimpleExternalTupleSet}. - * - * @param tuple - An expression that represents the PCJ. (not null) - */ - public SimpleExternalTupleSet(final Projection tuple) { - this.setProjectionExpr(tuple); - setSupportedVarOrders(); - } - - private void setSupportedVarOrders() { - final List<String> varOrders = new ArrayList<>(); - - String varOrder = ""; - for(final String var : this.getTupleExpr().getAssuredBindingNames()) { - varOrder = varOrder.isEmpty() ? var : varOrder + VAR_ORDER_DELIM + var; - varOrders.add( varOrder ); - } - - this.setSupportedVariableOrderMap(varOrders); - } - - @Override - public <X extends Exception> void visit(final QueryModelVisitor<X> visitor) - throws X { - visitor.meetOther(this); - } - - @Override - public CloseableIteration<BindingSet, QueryEvaluationException> evaluate( final BindingSet bindings) throws QueryEvaluationException { - // Intentionally does nothing. - return null; - } - - @Override - public String getSignature() { - return "(SimpleExternalTupleSet) " - + Joiner.on(", ") - .join(this.getTupleExpr().getProjectionElemList() - .getElements()).replaceAll("\\s+", " "); - - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/AbstractMongoIndexer.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/AbstractMongoIndexer.java b/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/AbstractMongoIndexer.java deleted file mode 100644 index daa78e4..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/AbstractMongoIndexer.java +++ /dev/null @@ -1,192 +0,0 @@ -package mvm.rya.indexing.mongodb; - -/* - * 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. - */ - -import java.io.IOException; -import java.util.Collection; -import java.util.Set; - -import org.apache.hadoop.conf.Configuration; -import org.apache.log4j.Logger; -import org.openrdf.model.Literal; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.query.QueryEvaluationException; - -import com.mongodb.DB; -import com.mongodb.DBCollection; -import com.mongodb.DBCursor; -import com.mongodb.DBObject; -import com.mongodb.MongoClient; -import com.mongodb.QueryBuilder; - -import info.aduna.iteration.CloseableIteration; -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.api.domain.RyaURI; -import mvm.rya.api.resolver.RyaToRdfConversions; -import mvm.rya.indexing.StatementConstraints; -import mvm.rya.mongodb.MongoConnectorFactory; -import mvm.rya.mongodb.MongoDBRdfConfiguration; -import mvm.rya.mongodb.MongoDBRyaDAO; -import mvm.rya.mongodb.MongoSecondaryIndex; - -/** - * Secondary Indexer using MondoDB - * @param <T> - The {@link AbstractMongoIndexingStorageStrategy} this indexer uses. - */ -public abstract class AbstractMongoIndexer<T extends IndexingMongoDBStorageStrategy> implements MongoSecondaryIndex { - private static final Logger LOG = Logger.getLogger(AbstractMongoIndexer.class); - - private boolean isInit = false; - protected Configuration conf; - protected MongoDBRyaDAO dao; - protected MongoClient mongoClient; - protected String dbName; - protected DB db; - protected DBCollection collection; - protected Set<URI> predicates; - - protected T storageStrategy; - - protected void initCore() { - dbName = conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME); - db = this.mongoClient.getDB(dbName); - collection = db.getCollection(conf.get(MongoDBRdfConfiguration.MONGO_COLLECTION_PREFIX, "rya") + getCollectionName()); - } - - public void setClient(MongoClient client){ - this.mongoClient = client; - } - - // TODO this method is only intended to be used in testing - public void initIndexer(final Configuration conf, final MongoClient client) { - setConf(conf); - setClient(client); - if (!isInit) { - init(); - isInit = true; - } - } - - @Override - public void setConf(final Configuration conf) { - this.conf = conf; - if (!isInit){ - setClient(MongoConnectorFactory.getMongoClient(conf)); - init(); - } - } - - @Override - public void close() throws IOException { - mongoClient.close(); - } - - @Override - public void flush() throws IOException { - } - - @Override - public Configuration getConf() { - return conf; - } - - @Override - public String getTableName() { - return dbName; - } - - @Override - public Set<URI> getIndexablePredicates() { - return predicates; - } - - @Override - public void deleteStatement(final RyaStatement stmt) throws IOException { - final DBObject obj = storageStrategy.getQuery(stmt); - collection.remove(obj); - } - - @Override - public void storeStatements(final Collection<RyaStatement> ryaStatements) - throws IOException { - for (final RyaStatement ryaStatement : ryaStatements){ - storeStatement(ryaStatement); - } - } - - @Override - public void storeStatement(final RyaStatement ryaStatement) throws IOException { - try { - final Statement statement = RyaToRdfConversions.convertStatement(ryaStatement); - final boolean isValidPredicate = predicates.isEmpty() || predicates.contains(statement.getPredicate()); - if (isValidPredicate && (statement.getObject() instanceof Literal)) { - final DBObject obj = storageStrategy.serialize(ryaStatement); - if (obj != null) { - final DBObject query = storageStrategy.serialize(ryaStatement); - collection.update(query, obj, true, false); - } - } - } catch (final IllegalArgumentException e) { - LOG.error("Unable to parse the statement: " + ryaStatement.toString()); - } - } - - @Override - public void dropGraph(final RyaURI... graphs) { - throw new UnsupportedOperationException(); - } - - protected CloseableIteration<Statement, QueryEvaluationException> withConstraints(final StatementConstraints constraints, final DBObject preConstraints) { - final DBObject dbo = QueryBuilder.start().and(preConstraints).and(storageStrategy.getQuery(constraints)).get(); - return closableIterationFromCursor(dbo); - } - - private CloseableIteration<Statement, QueryEvaluationException> closableIterationFromCursor(final DBObject dbo) { - final DBCursor cursor = collection.find(dbo); - return new CloseableIteration<Statement, QueryEvaluationException>() { - @Override - public boolean hasNext() { - return cursor.hasNext(); - } - - @Override - public Statement next() throws QueryEvaluationException { - final DBObject dbo = cursor.next(); - return RyaToRdfConversions.convertStatement(storageStrategy.deserializeDBObject(dbo)); - } - - @Override - public void remove() { - throw new UnsupportedOperationException("Remove not implemented"); - } - - @Override - public void close() throws QueryEvaluationException { - cursor.close(); - } - }; - } - - /** - * @return The name of the {@link DBCollection} to use with the storage strategy. - */ - public abstract String getCollectionName(); -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/IndexingMongoDBStorageStrategy.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/IndexingMongoDBStorageStrategy.java b/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/IndexingMongoDBStorageStrategy.java deleted file mode 100644 index 2b73e6e..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/IndexingMongoDBStorageStrategy.java +++ /dev/null @@ -1,57 +0,0 @@ -package mvm.rya.indexing.mongodb; - -/* - * 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. - */ - -import java.util.Set; - -import org.openrdf.model.URI; - -import com.mongodb.BasicDBObject; -import com.mongodb.DBObject; -import com.mongodb.QueryBuilder; - -import mvm.rya.indexing.StatementConstraints; -import mvm.rya.mongodb.dao.SimpleMongoDBStorageStrategy; - -public class IndexingMongoDBStorageStrategy extends SimpleMongoDBStorageStrategy { - public DBObject getQuery(final StatementConstraints contraints) { - final QueryBuilder queryBuilder = QueryBuilder.start(); - if (contraints.hasSubject()){ - queryBuilder.and(new BasicDBObject(SUBJECT, contraints.getSubject().toString())); - } - - if (contraints.hasPredicates()){ - final Set<URI> predicates = contraints.getPredicates(); - if (predicates.size() > 1){ - for (final URI pred : predicates){ - final DBObject currentPred = new BasicDBObject(PREDICATE, pred.toString()); - queryBuilder.or(currentPred); - } - } - else if (!predicates.isEmpty()){ - queryBuilder.and(new BasicDBObject(PREDICATE, predicates.iterator().next().toString())); - } - } - if (contraints.hasContext()){ - queryBuilder.and(new BasicDBObject(CONTEXT, contraints.getContext().toString())); - } - return queryBuilder.get(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/freetext/MongoFreeTextIndexer.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/freetext/MongoFreeTextIndexer.java b/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/freetext/MongoFreeTextIndexer.java deleted file mode 100644 index 0689feb..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/freetext/MongoFreeTextIndexer.java +++ /dev/null @@ -1,58 +0,0 @@ -package mvm.rya.indexing.mongodb.freetext; -/* - * 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. - */ - -import java.io.IOException; - -import org.apache.log4j.Logger; -import org.openrdf.model.Statement; -import org.openrdf.query.QueryEvaluationException; - -import com.mongodb.QueryBuilder; - -import info.aduna.iteration.CloseableIteration; -import mvm.rya.indexing.FreeTextIndexer; -import mvm.rya.indexing.StatementConstraints; -import mvm.rya.indexing.accumulo.ConfigUtils; -import mvm.rya.indexing.mongodb.AbstractMongoIndexer; - -public class MongoFreeTextIndexer extends AbstractMongoIndexer<TextMongoDBStorageStrategy> implements FreeTextIndexer { - private static final String COLLECTION_SUFFIX = "freetext"; - private static final Logger logger = Logger.getLogger(MongoFreeTextIndexer.class); - - @Override - public void init() { - initCore(); - predicates = ConfigUtils.getFreeTextPredicates(conf); - storageStrategy = new TextMongoDBStorageStrategy(); - storageStrategy.createIndices(collection); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryText( - final String query, final StatementConstraints constraints) throws IOException { - final QueryBuilder qb = QueryBuilder.start().text(query); - return withConstraints(constraints, qb.get()); - } - - @Override - public String getCollectionName() { - return ConfigUtils.getTablePrefix(conf) + COLLECTION_SUFFIX; - } -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/freetext/TextMongoDBStorageStrategy.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/freetext/TextMongoDBStorageStrategy.java b/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/freetext/TextMongoDBStorageStrategy.java deleted file mode 100644 index cc5029c..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/freetext/TextMongoDBStorageStrategy.java +++ /dev/null @@ -1,45 +0,0 @@ -package mvm.rya.indexing.mongodb.freetext; - -/* - * 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. - */ - -import com.mongodb.BasicDBObject; -import com.mongodb.DBCollection; -import com.mongodb.DBObject; - -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.indexing.mongodb.IndexingMongoDBStorageStrategy; - -public class TextMongoDBStorageStrategy extends IndexingMongoDBStorageStrategy { - private static final String text = "text"; - - @Override - public void createIndices(final DBCollection coll){ - final BasicDBObject basicDBObject = new BasicDBObject(); - basicDBObject.append(text, "text"); - coll.createIndex(basicDBObject); - } - - @Override - public DBObject serialize(final RyaStatement ryaStatement) { - final BasicDBObject base = (BasicDBObject) super.serialize(ryaStatement); - base.append(text, ryaStatement.getObject().getData()); - return base; - } -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/temporal/MongoTemporalIndexer.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/temporal/MongoTemporalIndexer.java b/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/temporal/MongoTemporalIndexer.java deleted file mode 100644 index be991c8..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/temporal/MongoTemporalIndexer.java +++ /dev/null @@ -1,150 +0,0 @@ -package mvm.rya.indexing.mongodb.temporal; -/* - * 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. - */ - -import static mvm.rya.indexing.mongodb.temporal.TemporalMongoDBStorageStrategy.INSTANT; -import static mvm.rya.indexing.mongodb.temporal.TemporalMongoDBStorageStrategy.INTERVAL_END; -import static mvm.rya.indexing.mongodb.temporal.TemporalMongoDBStorageStrategy.INTERVAL_START; - -import org.apache.log4j.Logger; -import org.openrdf.model.Statement; -import org.openrdf.query.QueryEvaluationException; - -import com.google.common.annotations.VisibleForTesting; -import com.mongodb.DBCollection; -import com.mongodb.QueryBuilder; - -import info.aduna.iteration.CloseableIteration; -import mvm.rya.indexing.StatementConstraints; -import mvm.rya.indexing.TemporalIndexer; -import mvm.rya.indexing.TemporalInstant; -import mvm.rya.indexing.TemporalInterval; -import mvm.rya.indexing.accumulo.ConfigUtils; -import mvm.rya.indexing.mongodb.AbstractMongoIndexer; - -/** - * Indexes MongoDB based on time instants or intervals. - */ -public class MongoTemporalIndexer extends AbstractMongoIndexer<TemporalMongoDBStorageStrategy> implements TemporalIndexer { - private static final String COLLECTION_SUFFIX = "temporal"; - private static final Logger LOG = Logger.getLogger(MongoTemporalIndexer.class); - - @Override - public void init() { - initCore(); - predicates = ConfigUtils.getTemporalPredicates(conf); - storageStrategy = new TemporalMongoDBStorageStrategy(); - storageStrategy.createIndices(collection); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryInstantEqualsInstant( - final TemporalInstant queryInstant, final StatementConstraints constraints) throws QueryEvaluationException { - final QueryBuilder qb = QueryBuilder.start(INSTANT) - .is(queryInstant.getAsDateTime().toDate()); - return withConstraints(constraints, qb.get()); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryInstantBeforeInstant( - final TemporalInstant queryInstant, final StatementConstraints constraints) throws QueryEvaluationException { - final QueryBuilder qb = QueryBuilder.start(INSTANT) - .lessThan(queryInstant.getAsDateTime().toDate()); - return withConstraints(constraints, qb.get()); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryInstantAfterInstant( - final TemporalInstant queryInstant, final StatementConstraints constraints) throws QueryEvaluationException { - final QueryBuilder qb = QueryBuilder.start(INSTANT) - .greaterThan(queryInstant.getAsDateTime().toDate()); - return withConstraints(constraints, qb.get()); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryInstantBeforeInterval( - final TemporalInterval givenInterval, final StatementConstraints constraints) throws QueryEvaluationException { - final QueryBuilder qb = QueryBuilder.start(INSTANT) - .lessThan(givenInterval.getHasBeginning().getAsDateTime().toDate()); - return withConstraints(constraints, qb.get()); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryInstantAfterInterval( - final TemporalInterval givenInterval, final StatementConstraints constraints) throws QueryEvaluationException { - return queryInstantAfterInstant(givenInterval.getHasEnd(), constraints); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryInstantInsideInterval( - final TemporalInterval givenInterval, final StatementConstraints constraints) throws QueryEvaluationException { - final QueryBuilder qb = QueryBuilder.start(INSTANT) - .greaterThan(givenInterval.getHasBeginning().getAsDateTime().toDate()) - .lessThan(givenInterval.getHasEnd().getAsDateTime().toDate()); - return withConstraints(constraints, qb.get()); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryInstantHasBeginningInterval( - final TemporalInterval queryInterval, final StatementConstraints constraints) throws QueryEvaluationException { - return queryInstantEqualsInstant(queryInterval.getHasBeginning(), constraints); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryInstantHasEndInterval( - final TemporalInterval queryInterval, final StatementConstraints constraints) throws QueryEvaluationException { - return queryInstantEqualsInstant(queryInterval.getHasEnd(), constraints); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryIntervalEquals(final TemporalInterval query, - final StatementConstraints constraints) throws QueryEvaluationException { - final QueryBuilder qb = QueryBuilder.start(INTERVAL_START) - .is(query.getHasBeginning().getAsDateTime().toDate()) - .and(INTERVAL_END) - .is(query.getHasEnd().getAsDateTime().toDate()); - return withConstraints(constraints, qb.get()); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryIntervalBefore(final TemporalInterval query, - final StatementConstraints constraints) throws QueryEvaluationException { - final QueryBuilder qb = QueryBuilder.start(INTERVAL_END) - .lessThan(query.getHasBeginning().getAsDateTime().toDate()); - return withConstraints(constraints, qb.get()); - } - - @Override - public CloseableIteration<Statement, QueryEvaluationException> queryIntervalAfter(final TemporalInterval query, - final StatementConstraints constraints) throws QueryEvaluationException { - final QueryBuilder qb = QueryBuilder.start(INTERVAL_START) - .greaterThan(query.getHasEnd().getAsDateTime().toDate()); - return withConstraints(constraints, qb.get()); - } - - @Override - public String getCollectionName() { - return ConfigUtils.getTablePrefix(conf) + COLLECTION_SUFFIX; - } - - @VisibleForTesting - public DBCollection getCollection() { - return collection; - } -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/temporal/TemporalMongoDBStorageStrategy.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/temporal/TemporalMongoDBStorageStrategy.java b/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/temporal/TemporalMongoDBStorageStrategy.java deleted file mode 100644 index 3292685..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/temporal/TemporalMongoDBStorageStrategy.java +++ /dev/null @@ -1,68 +0,0 @@ -package mvm.rya.indexing.mongodb.temporal; - -/* - * 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. - */ - -import java.util.regex.Matcher; - -import com.mongodb.BasicDBObject; -import com.mongodb.DBCollection; -import com.mongodb.DBObject; - -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.indexing.TemporalInstantRfc3339; -import mvm.rya.indexing.TemporalInterval; -import mvm.rya.indexing.mongodb.IndexingMongoDBStorageStrategy; - -/** - * Defines how time based intervals/instants are stored in MongoDB. - * <p> - * Time can be stored as the following: - * <p> - * <li><b>instant</b> {[statement], instant: TIME}</li> - * <li><b>interval</b> {[statement], start: TIME, end: TIME}</li> - * @see {@link TemporalInstantRfc3339} for how the dates are formatted. - */ -public class TemporalMongoDBStorageStrategy extends IndexingMongoDBStorageStrategy { - public static final String INTERVAL_START = "start"; - public static final String INTERVAL_END = "end"; - public static final String INSTANT = "instant"; - - @Override - public void createIndices(final DBCollection coll){ - coll.createIndex(INTERVAL_START); - coll.createIndex(INTERVAL_END); - coll.createIndex(INSTANT); - } - - @Override - public DBObject serialize(final RyaStatement ryaStatement) { - final BasicDBObject base = (BasicDBObject) super.serialize(ryaStatement); - final String objString = ryaStatement.getObject().getData(); - final Matcher match = TemporalInstantRfc3339.PATTERN.matcher(objString); - if(match.find()) { - final TemporalInterval date = TemporalInstantRfc3339.parseInterval(ryaStatement.getObject().getData()); - base.append(INTERVAL_START, date.getHasBeginning().getAsDateTime().toDate()); - base.append(INTERVAL_END, date.getHasEnd().getAsDateTime().toDate()); - } else { - base.append(INSTANT, TemporalInstantRfc3339.FORMATTER.parseDateTime(objString).toDate()); - } - return base; - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/AbstractPCJMatcher.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/AbstractPCJMatcher.java b/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/AbstractPCJMatcher.java deleted file mode 100644 index fdd9ccb..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/AbstractPCJMatcher.java +++ /dev/null @@ -1,126 +0,0 @@ -package mvm.rya.indexing.pcj.matching; - -/* - * 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. - */ - -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import mvm.rya.indexing.external.tupleSet.ExternalTupleSet; -import mvm.rya.indexing.pcj.matching.QueryNodesToTupleExpr.TupleExprAndNodes; - -import org.openrdf.query.algebra.BinaryTupleOperator; -import org.openrdf.query.algebra.Filter; -import org.openrdf.query.algebra.QueryModelNode; -import org.openrdf.query.algebra.TupleExpr; -import org.openrdf.query.algebra.UnaryTupleOperator; - -/** - * This class provides implementations of methods common to all implementations of - * the {@link PCJMatcher} interface. - * - */ -public abstract class AbstractPCJMatcher implements PCJMatcher { - - protected QuerySegment segment; - protected List<QueryModelNode> segmentNodeList; - protected boolean tupleAndNodesUpToDate = false; - protected TupleExpr tuple; - protected Set<TupleExpr> unmatched; - protected PCJToSegment pcjToSegment; - protected Set<Filter> filters; - - /** - * @param - pcj - PremomputedJoin to be matched to a subset of segment - * @return - true if match occurs and false otherwise - */ - @Override - public boolean matchPCJ(ExternalTupleSet pcj) { - QuerySegment sgmnt = pcjToSegment.getSegment(pcj); - if(sgmnt == null) { - throw new IllegalArgumentException("PCJ must contain at east one Join or Left Join"); - } - return matchPCJ(sgmnt, pcj); - } - - /** - * In following method, order is determined by the order in which the - * node appear in the query. - * @return - an ordered view of the QueryModelNodes appearing tuple - * - */ - @Override - public List<QueryModelNode> getOrderedNodes() { - return Collections.unmodifiableList(segmentNodeList); - } - - - @Override - public Set<Filter> getFilters() { - if (!tupleAndNodesUpToDate) { - updateTupleAndNodes(); - } - return filters; - } - - @Override - public TupleExpr getQuery() { - if (!tupleAndNodesUpToDate) { - updateTupleAndNodes(); - } - return tuple; - } - - @Override - public Set<TupleExpr> getUnmatchedArgs() { - if (!tupleAndNodesUpToDate) { - updateTupleAndNodes(); - } - return unmatched; - } - - - private void updateTupleAndNodes() { - TupleExprAndNodes tupAndNodes = segment.getQuery(); - tuple = tupAndNodes.getTupleExpr(); - filters = tupAndNodes.getFilters(); - unmatched = new HashSet<>(); - List<QueryModelNode> nodes = tupAndNodes.getNodes(); - for (QueryModelNode q : nodes) { - if (q instanceof UnaryTupleOperator - || q instanceof BinaryTupleOperator) { - unmatched.add((TupleExpr) q); - } - } - tupleAndNodesUpToDate = true; - } - - - /** - * Interface for converting an {@link ExternalTupleSet} (PCJ) into a - * {@link QuerySegment}. - * - */ - interface PCJToSegment { - public QuerySegment getSegment(ExternalTupleSet pcj); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/AbstractQuerySegment.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/AbstractQuerySegment.java b/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/AbstractQuerySegment.java deleted file mode 100644 index 2f1e749..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/AbstractQuerySegment.java +++ /dev/null @@ -1,122 +0,0 @@ -package mvm.rya.indexing.pcj.matching; - -/* - * 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. - */ - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import mvm.rya.indexing.pcj.matching.QueryNodesToTupleExpr.TupleExprAndNodes; - -import org.openrdf.query.algebra.Filter; -import org.openrdf.query.algebra.QueryModelNode; -import org.openrdf.query.algebra.ValueExpr; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; - -/** - * This class provides implementations of methods common to implementations - * of the {@link QuerySegment} interface. - * - */ -public abstract class AbstractQuerySegment implements QuerySegment { - - - protected List<QueryModelNode> orderedNodes = new ArrayList<>(); - protected Set<QueryModelNode> unorderedNodes; - protected Map<ValueExpr, Filter> conditionMap = Maps.newHashMap(); - - /** - * Returns set view of nodes contained in the segment - */ - @Override - public Set<QueryModelNode> getUnOrderedNodes() { - return Collections.unmodifiableSet(unorderedNodes); - } - - /** - * Returns a list view of nodes contained in this segment, where order is - * determined by the getJoinArgs method - * - * @param TupleExpr - * from top to bottom. - */ - @Override - public List<QueryModelNode> getOrderedNodes() { - return Collections.unmodifiableList(orderedNodes); - } - - /** - * Allows nodes to be reordered using {@link PCJMatcher} and set - * @param nodes - reordering of orderedNodes - */ - @Override - public void setNodes(List<QueryModelNode> nodes) { - Set<QueryModelNode> nodeSet = Sets.newHashSet(nodes); - Preconditions.checkArgument(nodeSet.equals(unorderedNodes)); - orderedNodes = nodes; - unorderedNodes = nodeSet; - } - - /** - * @param query - * - QuerySegment that this method checks for in this - * JoinSegment - */ - @Override - public boolean containsQuerySegment(QuerySegment query) { - return unorderedNodes.containsAll(query.getUnOrderedNodes()); - } - - /** - * @return - a TupleExpr representing this JoinSegment - */ - @Override - public TupleExprAndNodes getQuery() { - List<QueryModelNode> nodeCopy = new ArrayList<>(); - for (QueryModelNode q : orderedNodes) { - if (!(q instanceof ValueExpr)) { - nodeCopy.add(q.clone()); - } - } - QueryNodesToTupleExpr qnt = new QueryNodesToTupleExpr(nodeCopy, getFilters()); - return qnt.getTupleAndNodes(); - } - - @Override - public Set<Filter> getFilters() { - Collection<Filter> filters = conditionMap.values(); - Set<Filter> filterSet = new HashSet<>(); - for (Filter filter : filters) { - filterSet.add(filter.clone()); - } - - return filterSet; - - } - - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/FlattenedOptional.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/FlattenedOptional.java b/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/FlattenedOptional.java deleted file mode 100644 index b4f8f28..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/FlattenedOptional.java +++ /dev/null @@ -1,331 +0,0 @@ -package mvm.rya.indexing.pcj.matching; - -/* - * 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. - */ - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import mvm.rya.rdftriplestore.inference.DoNotExpandSP; -import mvm.rya.rdftriplestore.utils.FixedStatementPattern; - -import org.openrdf.query.algebra.Filter; -import org.openrdf.query.algebra.Join; -import org.openrdf.query.algebra.LeftJoin; -import org.openrdf.query.algebra.QueryModelNodeBase; -import org.openrdf.query.algebra.QueryModelVisitor; -import org.openrdf.query.algebra.TupleExpr; -import org.openrdf.query.algebra.ValueExpr; -import org.openrdf.query.algebra.Var; - -import com.google.common.collect.Sets; - -/** - * This class is essentially a wrapper for {@link LeftJoin}. It provides a - * flattened view of a LeftJoin that is useful for matching {@AccumuloIndexSet - * } nodes to sub-queries to use for Precomputed Joins. - * Because LeftJoins cannot automatically be interchanged with {@link Join}s and - * other LeftJoins in the query plan, this class has utility methods to check - * when nodes can be interchanged in the query plan. These methods track which - * variables returned by {@link LeftJoin#getRightArg()} are bound. A variable is - * bound if it also contained in the set returned by - * {@link LeftJoin#getLeftArg()}. Nodes can be interchanged with a LeftJoin (and - * hence a FlattenedOptional) so long as the bound and unbound variables do not - * change. - * - */ -public class FlattenedOptional extends QueryModelNodeBase implements TupleExpr { - - private Set<TupleExpr> rightArgs; - private Set<String> boundVars; - private Set<String> unboundVars; - private Map<String, Integer> leftArgVarCounts = new HashMap<String, Integer>(); - private ValueExpr condition; - private TupleExpr rightArg; - private Set<String> bindingNames; - private Set<String> assuredBindingNames; - - public FlattenedOptional(LeftJoin node) { - rightArgs = getJoinArgs(node.getRightArg(), new HashSet<TupleExpr>()); - boundVars = setWithOutConstants(Sets - .intersection(node.getLeftArg().getAssuredBindingNames(), node - .getRightArg().getBindingNames())); - unboundVars = setWithOutConstants(Sets.difference(node.getRightArg() - .getBindingNames(), boundVars)); - condition = node.getCondition(); - rightArg = node.getRightArg(); - getVarCounts(node); - assuredBindingNames = new HashSet<>(leftArgVarCounts.keySet()); - bindingNames = new HashSet<>(Sets.union(assuredBindingNames, - unboundVars)); - } - - public FlattenedOptional(FlattenedOptional optional) { - this.rightArgs = optional.rightArgs; - this.boundVars = optional.boundVars; - this.unboundVars = optional.unboundVars; - this.condition = optional.condition; - this.rightArg = optional.rightArg; - this.leftArgVarCounts = optional.leftArgVarCounts; - this.bindingNames = optional.bindingNames; - this.assuredBindingNames = optional.assuredBindingNames; - } - - public Set<TupleExpr> getRightArgs() { - return rightArgs; - } - - public TupleExpr getRightArg() { - return rightArg; - } - - /** - * - * @param te - * - TupleExpr to be added to leftarg of {@link LeftJoin} - */ - public void addArg(TupleExpr te) { - if (te instanceof FlattenedOptional) { - return; - } - incrementVarCounts(te.getBindingNames()); - } - - public void removeArg(TupleExpr te) { - if (te instanceof FlattenedOptional) { - return; - } - decrementVarCounts(te.getBindingNames()); - } - - /** - * - * @param te - * - {@link TupleExpr} to be added to leftArg of LeftJoin - * @return - true if adding TupleExpr does not affect unbound variables and - * returns false otherwise - */ - public boolean canAddTuple(TupleExpr te) { - // can only add LeftJoin if rightArg varNames do not intersect - // unbound vars - if (te instanceof FlattenedOptional) { - FlattenedOptional lj = (FlattenedOptional) te; - if (Sets.intersection(lj.rightArg.getBindingNames(), unboundVars) - .size() > 0) { - return false; - } else { - return true; - } - } - - return Sets.intersection(te.getBindingNames(), unboundVars).size() == 0; - } - - /** - * - * @param te - * - {@link TupleExpr} to be removed from leftArg of LeftJoin - * @return - true if removing TupleExpr does not affect bound variables and - * returns false otherwise - */ - public boolean canRemoveTuple(TupleExpr te) { - return canRemove(te); - } - - @Override - public Set<String> getBindingNames() { - return bindingNames; - } - - @Override - public Set<String> getAssuredBindingNames() { - return assuredBindingNames; - } - - public ValueExpr getCondition() { - return condition; - } - - @Override - public boolean equals(Object other) { - if (other instanceof FlattenedOptional) { - FlattenedOptional ljDec = (FlattenedOptional) other; - ValueExpr oCond = ljDec.getCondition(); - return nullEquals(condition, oCond) - && ljDec.getRightArgs().equals(rightArgs); - } - return false; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = prime + (rightArgs == null ? 0 : rightArgs.hashCode()); - result = prime * result - + (condition == null ? 0 : condition.hashCode()); - return result; - } - - /** - * This method is used to retrieve a set view of all descendants of the - * rightArg of the LeftJoin (the optional part) - * - * @param tupleExpr - * - tupleExpr whose args are being retrieved - * @param joinArgs - * - set view of all non-join args that are descendants of - * tupleExpr - * @return joinArgs - */ - private Set<TupleExpr> getJoinArgs(TupleExpr tupleExpr, - Set<TupleExpr> joinArgs) { - if (tupleExpr instanceof Join) { - if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern) - && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) { - Join join = (Join) tupleExpr; - getJoinArgs(join.getLeftArg(), joinArgs); - getJoinArgs(join.getRightArg(), joinArgs); - } - } else if (tupleExpr instanceof LeftJoin) { // TODO probably not - // necessary if not - // including leftarg - LeftJoin lj = (LeftJoin) tupleExpr; - joinArgs.add(new FlattenedOptional(lj)); - getJoinArgs(lj.getLeftArg(), joinArgs); - } else if (tupleExpr instanceof Filter) { - getJoinArgs(((Filter) tupleExpr).getArg(), joinArgs); - } else { - joinArgs.add(tupleExpr); - } - - return joinArgs; - } - - /** - * This method counts the number of times each variable appears in the - * leftArg of the LeftJoin defining this FlattenedOptional. This information - * is used to whether nodes can be moved out of the leftarg above the - * LeftJoin in the query. - * - * @param tupleExpr - */ - private void getVarCounts(TupleExpr tupleExpr) { - if (tupleExpr instanceof Join) { - Join join = (Join) tupleExpr; - getVarCounts(join.getLeftArg()); - getVarCounts(join.getRightArg()); - } else if (tupleExpr instanceof LeftJoin) { - LeftJoin lj = (LeftJoin) tupleExpr; - getVarCounts(lj.getLeftArg()); - } else if (tupleExpr instanceof Filter) { - getVarCounts(((Filter) tupleExpr).getArg()); - } else { - incrementVarCounts(tupleExpr.getBindingNames()); - } - } - - /** - * - * @param te - * - {@link TupleExpr} to be removed from leftArg of LeftJoin - * @return - true if removing te doesn't affect bounded variables of - * LeftJoin and false otherwise - */ - private boolean canRemove(TupleExpr te) { - // can only remove LeftJoin if right varNames do not intersect - // unbound vars - if (te instanceof FlattenedOptional) { - FlattenedOptional lj = (FlattenedOptional) te; - if (Sets.intersection(lj.getRightArg().getBindingNames(), - unboundVars).size() > 0) { - return false; - } else { - return true; - } - } - Set<String> vars = te.getBindingNames(); - Set<String> intersection = Sets.intersection(vars, boundVars); - if (intersection.size() == 0) { - return true; - } - for (String s : intersection) { - if (leftArgVarCounts.containsKey(s) && leftArgVarCounts.get(s) == 1) { - return false; - } - } - return true; - } - - private void incrementVarCounts(Set<String> vars) { - for (String s : vars) { - if (!s.startsWith("-const-") && leftArgVarCounts.containsKey(s)) { - leftArgVarCounts.put(s, leftArgVarCounts.get(s) + 1); - } else if (!s.startsWith("-const-")) { - leftArgVarCounts.put(s, 1); - } - } - } - - private void decrementVarCounts(Set<String> vars) { - for (String s : vars) { - if (leftArgVarCounts.containsKey(s) && leftArgVarCounts.get(s) > 1) { - leftArgVarCounts.put(s, leftArgVarCounts.get(s) - 1); - } else { - leftArgVarCounts.remove(s); - bindingNames.remove(s); - assuredBindingNames.remove(s); - } - } - } - - /** - * - * @param vars - * - set of {@link Var} names, possibly contained constants - */ - private Set<String> setWithOutConstants(Set<String> vars) { - Set<String> copy = new HashSet<>(); - for (String s : vars) { - if (!s.startsWith("-const-")) { - copy.add(s); - } - } - - return copy; - } - - @Override - public <X extends Exception> void visit(QueryModelVisitor<X> visitor) - throws X { - throw new UnsupportedOperationException(); - } - - @Override - public String toString() { - return "FlattenedOptional: " + rightArgs; - } - - @Override - public FlattenedOptional clone() { - return new FlattenedOptional(this); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/JoinSegment.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/JoinSegment.java b/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/JoinSegment.java deleted file mode 100644 index 30f36cf..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/JoinSegment.java +++ /dev/null @@ -1,130 +0,0 @@ -package mvm.rya.indexing.pcj.matching; - -/* - * 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. - */ - -import java.util.List; -import java.util.Set; - -import mvm.rya.indexing.external.tupleSet.ExternalTupleSet; -import mvm.rya.rdftriplestore.inference.DoNotExpandSP; -import mvm.rya.rdftriplestore.utils.FixedStatementPattern; - -import org.openrdf.query.algebra.Filter; -import org.openrdf.query.algebra.Join; -import org.openrdf.query.algebra.QueryModelNode; -import org.openrdf.query.algebra.TupleExpr; -import org.openrdf.query.algebra.ValueExpr; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Sets; - -/** - * This class represents a portion of a {@link TupleExpr} query that PCJ queries - * are compared to. A JoinSegment is comprised of a collection of - * {@link QueryModelNode}s that are connected by {@link Join}s. In the case, the - * QueryModelNodes can commute within the JoinSegment, which makes JoinSegments - * a natural way to partition a query for PCJ matching. A query is decomposed - * into JoinSegments and PCJ queries can easily be compared to the {@link QueryModelNode}s - * contained in the segment using set operations. - * - */ -public class JoinSegment extends AbstractQuerySegment { - - public JoinSegment(Join join) { - Preconditions.checkNotNull(join); - createJoinSegment(join); - } - - public JoinSegment(Filter filter) { - Preconditions.checkNotNull(filter); - createJoinSegment(filter); - } - - private void createJoinSegment(TupleExpr te) { - orderedNodes = getJoinArgs(te, orderedNodes); - unorderedNodes = Sets.newHashSet(orderedNodes); - } - - /** - * This method matches the ordered nodes returned by - * {@link JoinSegment#getOrderedNodes()} for nodeToReplace with a subset of - * the ordered nodes for this JoinSegment. The order of the nodes for - * nodeToReplace must match the order of the nodes as a subset of - * orderedNodes - * - * @param nodeToReplace - * - nodes to be replaced by pcj - * @param pcj - * - pcj node that will replace specified query nodes - */ - @Override - public boolean replaceWithPcj(QuerySegment nodeToReplace, - ExternalTupleSet pcj) { - Preconditions.checkNotNull(nodeToReplace != null); - Preconditions.checkNotNull(pcj); - if (!containsQuerySegment(nodeToReplace)) { - return false; - } - Set<QueryModelNode> nodeSet = nodeToReplace.getUnOrderedNodes(); - orderedNodes.removeAll(nodeSet); - orderedNodes.add(pcj); - unorderedNodes.removeAll(nodeSet); - unorderedNodes.add(pcj); - for (QueryModelNode q : nodeSet) { - if (q instanceof ValueExpr) { - conditionMap.remove(q); - } - } - return true; - } - - /** - * - * @param tupleExpr - * - the query object that will be traversed by this method - * @param joinArgs - * - all nodes connected by Joins and Filters - * @return - List containing all nodes connected by Joins, LeftJoins, and - * Filters. This List contains the - * @param ValueExpr - * in place of the Filter - */ - private List<QueryModelNode> getJoinArgs(TupleExpr tupleExpr, - List<QueryModelNode> joinArgs) { - - if (tupleExpr instanceof Join) { - if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern) - && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) { - Join join = (Join) tupleExpr; - getJoinArgs(join.getRightArg(), joinArgs); - getJoinArgs(join.getLeftArg(), joinArgs); - } - } else if (tupleExpr instanceof Filter) { - Filter filter = (Filter) tupleExpr; - joinArgs.add(filter.getCondition()); - conditionMap.put(filter.getCondition(), filter); - getJoinArgs(filter.getArg(), joinArgs); - } else { - joinArgs.add(tupleExpr); - } - return joinArgs; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/JoinSegmentPCJMatcher.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/JoinSegmentPCJMatcher.java b/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/JoinSegmentPCJMatcher.java deleted file mode 100644 index a74abec..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/JoinSegmentPCJMatcher.java +++ /dev/null @@ -1,106 +0,0 @@ -package mvm.rya.indexing.pcj.matching; - -/* - * 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. - */ - -import java.util.ArrayList; - -import mvm.rya.indexing.external.tupleSet.ExternalTupleSet; - -import org.openrdf.query.algebra.Filter; -import org.openrdf.query.algebra.Join; -import org.openrdf.query.algebra.QueryModelNode; -import org.openrdf.query.algebra.TupleExpr; -import org.openrdf.query.algebra.helpers.QueryModelVisitorBase; - -/** - * This class is responsible for matching PCJ nodes with subsets of the - * {@link QueryModelNode}s found in {@link JoinSegment}s. Each PCJ is reduced to - * a bag of QueryModelNodes and set operations can be used to determine if the - * PCJ is a subset of the JoinSegment. If it is a subset, the PCJ node replaces - * the QueryModelNodes in the JoinSegment. - * - */ - -public class JoinSegmentPCJMatcher extends AbstractPCJMatcher { - - public JoinSegmentPCJMatcher(Join join) { - segment = new JoinSegment(join); - segmentNodeList = new ArrayList<>(segment.getOrderedNodes()); - pcjToSegment = new PCJToJoinSegment(); - } - - public JoinSegmentPCJMatcher(Filter filter) { - segment = new JoinSegment(filter); - segmentNodeList = new ArrayList<>(segment.getOrderedNodes()); - pcjToSegment = new PCJToJoinSegment(); - } - - /** - * @param pcjNodes - * - {@link QueryModelNode}s to be replaced - * @param pcj - * - the PCJ node to be compared to pcjNodes - */ - @Override - public boolean matchPCJ(QuerySegment pcjNodes, ExternalTupleSet pcj) { - - if(PCJOptimizerUtilities.pcjContainsLeftJoins(pcj)) { - return false; - } - - boolean nodesReplaced = segment.replaceWithPcj(pcjNodes, pcj); - if (nodesReplaced) { - tupleAndNodesUpToDate = false; - segmentNodeList = segment.getOrderedNodes(); - } - - return nodesReplaced; - } - - /** - * This class extracts the {@link JoinSegment} from the {@link TupleExpr} of - * specified PCJ. - * - */ - static class PCJToJoinSegment extends - QueryModelVisitorBase<RuntimeException> implements PCJToSegment { - - private JoinSegment segment; - - @Override - public QuerySegment getSegment(ExternalTupleSet pcj) { - segment = null; - pcj.getTupleExpr().visit(this); - return segment; - } - - @Override - public void meet(Join join) { - segment = new JoinSegment(join); - } - - @Override - public void meet(Filter filter) { - segment = new JoinSegment(filter); - } - - } - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/OptionalJoinSegment.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/OptionalJoinSegment.java b/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/OptionalJoinSegment.java deleted file mode 100644 index ebe5243..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/OptionalJoinSegment.java +++ /dev/null @@ -1,146 +0,0 @@ -package mvm.rya.indexing.pcj.matching; - -/* - * 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. - */ - -import java.util.List; - -import mvm.rya.indexing.external.tupleSet.ExternalTupleSet; -import mvm.rya.rdftriplestore.inference.DoNotExpandSP; -import mvm.rya.rdftriplestore.utils.FixedStatementPattern; - -import org.openrdf.query.algebra.Filter; -import org.openrdf.query.algebra.Join; -import org.openrdf.query.algebra.LeftJoin; -import org.openrdf.query.algebra.QueryModelNode; -import org.openrdf.query.algebra.TupleExpr; -import org.openrdf.query.algebra.ValueExpr; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Sets; - -/** - * An OptionalJoinSegment represents the portion of a {@link TupleExpr} that is - * connected by Filters, LeftJoins, and Joins. All nodes in the portion of the - * TupleExpr that are connected via these node types are gathered into an - * ordered and an unordered list that can easily be compared with - * {@link ExternalTupleSet} nodes for sub-query matching to use with Precomputed - * Joins. - * - */ -public class OptionalJoinSegment extends AbstractQuerySegment { - - public OptionalJoinSegment(Join join) { - Preconditions.checkNotNull(join); - createJoinSegment(join); - } - - public OptionalJoinSegment(LeftJoin join) { - Preconditions.checkNotNull(join); - createJoinSegment(join); - } - - public OptionalJoinSegment(Filter filter) { - Preconditions.checkNotNull(filter); - createJoinSegment(filter); - } - - private void createJoinSegment(TupleExpr te) { - orderedNodes = getJoinArgs(te, orderedNodes); - unorderedNodes = Sets.newHashSet(orderedNodes); - } - - /** - * This method matches the ordered nodes returned by - * {@link JoinSegment #getOrderedNodes()} for nodeToReplace with a subset of - * the ordered nodes for this JoinSegment. The order of the nodes for - * nodeToReplace must match the order of the nodes as a subset of - * orderedNodes - * - * @param nodeToReplace - * - nodes to be replaced by pcj - * @param pcj - * - pcj node that will replace specified query nodes - */ - @Override - public boolean replaceWithPcj(QuerySegment nodeToReplace, - ExternalTupleSet pcj) { - Preconditions.checkNotNull(nodeToReplace != null); - Preconditions.checkNotNull(pcj); - if (!containsQuerySegment(nodeToReplace)) { - return false; - } - List<QueryModelNode> nodeList = nodeToReplace.getOrderedNodes(); - int begin = orderedNodes.indexOf(nodeList.get(0)); - // TODO this assumes no duplicate nodes - if (begin < 0 - || begin + nodeList.size() > orderedNodes.size() - || !nodeList.equals(orderedNodes.subList(begin, begin - + nodeList.size()))) { - return false; - } - orderedNodes.removeAll(nodeList); - orderedNodes.add(begin, pcj); - unorderedNodes.removeAll(nodeList); - unorderedNodes.add(pcj); - for (QueryModelNode q : nodeList) { - if (q instanceof ValueExpr) { - conditionMap.remove(q); - } - } - return true; - } - - /** - * - * @param tupleExpr - * - the query object that will be traversed by this method - * @param joinArgs - * - all nodes connected by Joins, LeftJoins, and Filters - * @return - List containing all nodes connected by Joins, LeftJoins, and - * Filters. This List contains the {@link ValueExpr} in place of the - * Filter and a {@link FlattenedOptional} in place of the LeftJoin - * for ease of comparison with PCJ nodes. - */ - private List<QueryModelNode> getJoinArgs(TupleExpr tupleExpr, - List<QueryModelNode> joinArgs) { - - if (tupleExpr instanceof Join) { - if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern) - && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) { - Join join = (Join) tupleExpr; - getJoinArgs(join.getRightArg(), joinArgs); - getJoinArgs(join.getLeftArg(), joinArgs); - } - } else if (tupleExpr instanceof LeftJoin) { - LeftJoin lj = (LeftJoin) tupleExpr; - joinArgs.add(new FlattenedOptional(lj)); - getJoinArgs(lj.getLeftArg(), joinArgs); - } else if (tupleExpr instanceof Filter) { - Filter filter = (Filter) tupleExpr; - joinArgs.add(filter.getCondition()); - conditionMap.put(filter.getCondition(), filter); - getJoinArgs(filter.getArg(), joinArgs); - } else { - joinArgs.add(tupleExpr); - } - return joinArgs; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/OptionalJoinSegmentPCJMatcher.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/OptionalJoinSegmentPCJMatcher.java b/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/OptionalJoinSegmentPCJMatcher.java deleted file mode 100644 index 37f6867..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/OptionalJoinSegmentPCJMatcher.java +++ /dev/null @@ -1,142 +0,0 @@ -package mvm.rya.indexing.pcj.matching; - -/* - * 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. - */ - -import java.util.ArrayList; -import java.util.List; - -import mvm.rya.indexing.external.tupleSet.ExternalTupleSet; - -import org.openrdf.query.algebra.Filter; -import org.openrdf.query.algebra.Join; -import org.openrdf.query.algebra.LeftJoin; -import org.openrdf.query.algebra.QueryModelNode; -import org.openrdf.query.algebra.helpers.QueryModelVisitorBase; - -/** - * This class matches PCJ queries to sub-queries of a given - * {@link OptionalJoinSegment}. A match will occur when the - * {@link QueryModelNode}s of the PCJ can be grouped together - * in the OptionalJoinSegment and ordered to match the PCJ query. - * - */ - -public class OptionalJoinSegmentPCJMatcher extends AbstractPCJMatcher { - - public OptionalJoinSegmentPCJMatcher(Join join) { - segment = new OptionalJoinSegment(join); - segmentNodeList = new ArrayList<>(segment.getOrderedNodes()); - pcjToSegment = new PCJToOptionalJoinSegment(); - } - - public OptionalJoinSegmentPCJMatcher(LeftJoin join) { - segment = new OptionalJoinSegment(join); - segmentNodeList = new ArrayList<>(segment.getOrderedNodes()); - pcjToSegment = new PCJToOptionalJoinSegment(); - } - - public OptionalJoinSegmentPCJMatcher(Filter filter) { - segment = new OptionalJoinSegment(filter); - segmentNodeList = new ArrayList<>(segment.getOrderedNodes()); - pcjToSegment = new PCJToOptionalJoinSegment(); - } - - /** - * @param pcjNodes - {@link QuerySegment} to be replaced by PCJ - * @param pcj - PCJ to replace matchin QuerySegment - */ - @Override - public boolean matchPCJ(QuerySegment pcjNodes, ExternalTupleSet pcj) { - - if(!segment.containsQuerySegment(pcjNodes)) { - return false; - } - List<QueryModelNode> consolidatedNodes = groupNodesToMatchPCJ(getOrderedNodes(), pcjNodes.getOrderedNodes()); - if(consolidatedNodes.size() == 0) { - return false; - } - - //set segment nodes to the consolidated nodes to match pcj - segment.setNodes(consolidatedNodes); - boolean nodesReplaced = segment.replaceWithPcj(pcjNodes, pcj); - - //if pcj nodes replaced queryNodes, update segmentNodeList - //otherwise restore segment nodes back to original pre-consolidated state - if(nodesReplaced) { - segmentNodeList = segment.getOrderedNodes(); - tupleAndNodesUpToDate = false; - } else { - segment.setNodes(segmentNodeList); - } - - return nodesReplaced; - } - - /** - * - * @param queryNodes - query nodes to be compared to pcj for matching - * @param pcjNodes - pcj nodes to match to query - * @return - query nodes with pcj nodes grouped together (if possible), otherwise return - * an empty list. - */ - private List<QueryModelNode> groupNodesToMatchPCJ(List<QueryModelNode> queryNodes, List<QueryModelNode> pcjNodes) { - PCJNodeConsolidator pnc = new PCJNodeConsolidator(queryNodes, pcjNodes); - boolean canConsolidate = pnc.consolidateNodes(); - if(canConsolidate) { - return pnc.getQueryNodes(); - } - return new ArrayList<QueryModelNode>(); - } - - - /** - * This class extracts the {@link OptionalJoinSegment} of PCJ query. - * - */ - static class PCJToOptionalJoinSegment extends QueryModelVisitorBase<RuntimeException> implements PCJToSegment { - - private OptionalJoinSegment segment; - - @Override - public QuerySegment getSegment(ExternalTupleSet pcj) { - segment = null; - pcj.getTupleExpr().visit(this); - return segment; - } - - @Override - public void meet(Join join) { - segment = new OptionalJoinSegment(join); - } - - @Override - public void meet(Filter filter) { - segment = new OptionalJoinSegment(filter); - } - - @Override - public void meet(LeftJoin node) { - segment = new OptionalJoinSegment(node); - } - - } - - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/PCJMatcher.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/PCJMatcher.java b/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/PCJMatcher.java deleted file mode 100644 index d98d367..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/PCJMatcher.java +++ /dev/null @@ -1,76 +0,0 @@ -package mvm.rya.indexing.pcj.matching; - -/* - * 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. - */ - -import java.util.List; -import java.util.Set; - -import mvm.rya.indexing.external.tupleSet.ExternalTupleSet; - -import org.openrdf.query.algebra.Filter; -import org.openrdf.query.algebra.QueryModelNode; -import org.openrdf.query.algebra.TupleExpr; - -/** - * This interface provides a framework for matching PCJ {@link ExternalTupleSet}s - * to subsets of a given {@link QuerySegment}. - * - */ -public interface PCJMatcher { - - /** - * - * @param pcjNodes - QuerySegment representation of PCJ to be used for matching - * @param pcj - {@link ExternalTupleSet} used to replace matching PCJ nodes when match occurs - * @return - true is match and replace occurs and false otherwise - */ - public boolean matchPCJ(QuerySegment pcjNodes, ExternalTupleSet pcj); - - /** - * - * @param pcj - {@link ExternalTupleSet} used to replace matching PCJ nodes when match occurs - * @return - true is match and replace occurs and false otherwise - */ - public boolean matchPCJ(ExternalTupleSet pcj); - - /** - * @return - TupleExpr constructed from {@link QuerySegment} with matched nodes - */ - public TupleExpr getQuery(); - - /** - * - * @return - all {@link TupleExpr} that haven't been matched to a PCJ - */ - public Set<TupleExpr> getUnmatchedArgs(); - - /** - * - * @return - provided ordered view of QuerySegment nodes - */ - public List<QueryModelNode> getOrderedNodes(); - - /** - * - * @return - Set of {@link Filter}s of given QuerySegment - */ - public Set<Filter> getFilters(); - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/PCJMatcherFactory.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/PCJMatcherFactory.java b/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/PCJMatcherFactory.java deleted file mode 100644 index 3a42a68..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/pcj/matching/PCJMatcherFactory.java +++ /dev/null @@ -1,73 +0,0 @@ -package mvm.rya.indexing.pcj.matching; - -/* - * 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. - */ - -import org.openrdf.query.algebra.Filter; -import org.openrdf.query.algebra.Join; -import org.openrdf.query.algebra.LeftJoin; -import org.openrdf.query.algebra.Projection; -import org.openrdf.query.algebra.TupleExpr; - -/** - * This class takes in a given {@link Join}, {@Filter}, or {@link LeftJoin} - * and provides the appropriate {@link PCJMatcher} to match PCJs to the - * given query. - * - */ - -public class PCJMatcherFactory { - - public static PCJMatcher getPCJMatcher(Join join) { - if (segmentContainsLeftJoins(join)) { - return new OptionalJoinSegmentPCJMatcher(join); - } else { - return new JoinSegmentPCJMatcher(join); - } - } - - public static PCJMatcher getPCJMatcher(LeftJoin join) { - return new OptionalJoinSegmentPCJMatcher(join); - } - - public static PCJMatcher getPCJMatcher(Filter filter) { - if (segmentContainsLeftJoins(filter)) { - return new OptionalJoinSegmentPCJMatcher(filter); - } else { - return new JoinSegmentPCJMatcher(filter); - } - } - - private static boolean segmentContainsLeftJoins(TupleExpr tupleExpr) { - - if (tupleExpr instanceof Projection) { - return segmentContainsLeftJoins(((Projection) tupleExpr).getArg()); - } else if (tupleExpr instanceof Join) { - Join join = (Join) tupleExpr; - return segmentContainsLeftJoins(join.getRightArg()) - || segmentContainsLeftJoins(join.getLeftArg()); - } else if (tupleExpr instanceof LeftJoin) { - return true; - } else if (tupleExpr instanceof Filter) { - return segmentContainsLeftJoins(((Filter) tupleExpr).getArg()); - } else { - return false; - } - } -}
