org.modeshape.jcr.query.optimize.ChooseJoinAlgorithm Maven / Gradle / Ivy
/*
* ModeShape (http://www.modeshape.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.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.NullOrder;
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 - 2024 Weber Informatics LLC | Privacy Policy