org.modeshape.jcr.query.optimize.ChooseJoinAlgorithm Maven / Gradle / Ivy
/*
* ModeShape (http://www.modeshape.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* ModeShape is free software. Unless otherwise indicated, all code in ModeShape
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* ModeShape is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.jcr.query.optimize;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.modeshape.common.annotation.Immutable;
import org.modeshape.jcr.query.QueryContext;
import org.modeshape.jcr.query.model.ChildNodeJoinCondition;
import org.modeshape.jcr.query.model.DescendantNodeJoinCondition;
import org.modeshape.jcr.query.model.DynamicOperand;
import org.modeshape.jcr.query.model.EquiJoinCondition;
import org.modeshape.jcr.query.model.JoinCondition;
import org.modeshape.jcr.query.model.Order;
import org.modeshape.jcr.query.model.Ordering;
import org.modeshape.jcr.query.model.PropertyValue;
import org.modeshape.jcr.query.model.SameNodeJoinCondition;
import org.modeshape.jcr.query.model.SelectorName;
import org.modeshape.jcr.query.plan.JoinAlgorithm;
import org.modeshape.jcr.query.plan.PlanNode;
import org.modeshape.jcr.query.plan.PlanNode.Property;
import org.modeshape.jcr.query.plan.PlanNode.Type;
/**
* An {@link OptimizerRule optimizer rule} that choose the appropriate join algorithm and sets up any prerequisites, based upon
* the {@link JoinCondition}.
*
* There are two static instances that can be used (or the equivalent can be instantiated or subclassed using the constructor):
* one that only uses {@link JoinAlgorithm#NESTED_LOOP nested-loop}, and another that will attempt to use
* {@link JoinAlgorithm#MERGE merge} where possible. Both instances ignore any existing {@link Property#JOIN_ALGORITHM} property
* value set on the JOIN node.
*
*
* For example, the {@link #USE_ONLY_NESTED_JOIN_ALGORITHM} instance will convert this simple tree:
*
*
* ...
* |
* JOIN
* / \
* ... ...
*
*
* into this:
*
*
* ...
* |
* JOIN ({@link Property#JOIN_ALGORITHM JOIN_ALGORITHM}={@link JoinAlgorithm#NESTED_LOOP NESTED_LOOP})
* / \
* ... ...
*
*
* On the other hand, the {@link #USE_BEST_JOIN_ALGORITHM} instance will do a couple of different things, depending upon the input
* plan.
*
* - If the condition is a {@link DescendantNodeJoinCondition}, then the join algorithm will always be
* {@link JoinAlgorithm#NESTED_LOOP}.
* - Otherwise, the rule will use the {@link JoinAlgorithm#MERGE} algorithm and will change this structure:
*
*
* ...
* |
* JOIN
* / \
* ... ...
*
*
* into this:
*
*
* ...
* |
* JOIN ({@link Property#JOIN_ALGORITHM JOIN_ALGORITHM}={@link JoinAlgorithm#MERGE MERGE})
* / \
* / \
* DUP_REMOVE DUP_REMOVE
* | |
* SORT SORT
* | |
* ... ...
*
*
*
*
*
*/
@Immutable
public class ChooseJoinAlgorithm implements OptimizerRule {
public static final ChooseJoinAlgorithm USE_ONLY_NESTED_JOIN_ALGORITHM = new ChooseJoinAlgorithm(true);
public static final ChooseJoinAlgorithm USE_BEST_JOIN_ALGORITHM = new ChooseJoinAlgorithm(false);
private final boolean useOnlyNested;
protected ChooseJoinAlgorithm( boolean useOnlyNested ) {
this.useOnlyNested = useOnlyNested;
}
@Override
public PlanNode execute( QueryContext context,
PlanNode plan,
LinkedList ruleStack ) {
// For each of the JOIN nodes ...
for (PlanNode joinNode : plan.findAllAtOrBelow(Type.JOIN)) {
JoinCondition condition = joinNode.getProperty(Property.JOIN_CONDITION, JoinCondition.class);
if (useOnlyNested) {
joinNode.setProperty(Property.JOIN_ALGORITHM, JoinAlgorithm.NESTED_LOOP);
break;
}
if (condition instanceof DescendantNodeJoinCondition) {
// It has to be a nest-loop join ...
joinNode.setProperty(Property.JOIN_ALGORITHM, JoinAlgorithm.NESTED_LOOP);
} else {
joinNode.setProperty(Property.JOIN_ALGORITHM, JoinAlgorithm.MERGE);
assert joinNode.getChildCount() == 2;
// We can try to use the merge join, but we need to sort and remove remove duplicates ...
// on the left and right children of the join ...
Set leftSelectors = joinNode.getFirstChild().getSelectors();
Set rightSelectors = joinNode.getLastChild().getSelectors();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy