1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.graph.query.optimize;
25
26 import java.util.LinkedList;
27 import net.jcip.annotations.Immutable;
28 import org.modeshape.graph.query.QueryContext;
29 import org.modeshape.graph.query.plan.CanonicalPlanner;
30 import org.modeshape.graph.query.plan.PlanHints;
31 import org.modeshape.graph.query.plan.PlanNode;
32 import org.modeshape.graph.query.plan.PlanNode.Type;
33
34 /**
35 * An {@link OptimizerRule optimizer rule} that inserts an ACCESS above each SOURCE leaf node in a query plan. This rule is often
36 * the first rule to run against a {@link CanonicalPlanner canonical plan} (see
37 * {@link RuleBasedOptimizer#populateRuleStack(LinkedList, PlanHints)}.
38 * <p>
39 * Before:
40 *
41 * <pre>
42 * ...
43 * |
44 * SOURCE
45 * </pre>
46 *
47 * After:
48 *
49 * <pre>
50 * ...
51 * |
52 * ACCESS
53 * |
54 * SOURCE
55 * </pre>
56 *
57 * </p>
58 */
59 @Immutable
60 public class AddAccessNodes implements OptimizerRule {
61
62 public static final AddAccessNodes INSTANCE = new AddAccessNodes();
63
64 /**
65 * {@inheritDoc}
66 *
67 * @see org.modeshape.graph.query.optimize.OptimizerRule#execute(org.modeshape.graph.query.QueryContext,
68 * org.modeshape.graph.query.plan.PlanNode, java.util.LinkedList)
69 */
70 public PlanNode execute( QueryContext context,
71 PlanNode plan,
72 LinkedList<OptimizerRule> ruleStack ) {
73 // On each of the source nodes ...
74 for (PlanNode source : plan.findAllAtOrBelow(Type.SOURCE)) {
75 // The source node may have children if it is a view ...
76 if (source.getChildCount() != 0) continue;
77
78 // Create the ACCESS node, set the selectors, and insert above the source node ...
79 PlanNode access = new PlanNode(Type.ACCESS);
80 access.addSelectors(source.getSelectors());
81 source.insertAsParent(access);
82 }
83 return plan;
84 }
85
86 /**
87 * {@inheritDoc}
88 *
89 * @see java.lang.Object#toString()
90 */
91 @Override
92 public String toString() {
93 return getClass().getSimpleName();
94 }
95
96 }