KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > OptimizerFactoryImpl


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.OptimizerFactoryImpl
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.sql.compile.CostEstimate;
25 import org.apache.derby.iapi.sql.compile.JoinStrategy;
26 import org.apache.derby.iapi.sql.compile.OptimizableList;
27 import org.apache.derby.iapi.sql.compile.OptimizablePredicateList;
28 import org.apache.derby.iapi.sql.compile.Optimizer;
29 import org.apache.derby.iapi.sql.compile.OptimizerFactory;
30 import org.apache.derby.iapi.sql.compile.RequiredRowOrdering;
31
32 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
33
34 import org.apache.derby.iapi.store.access.TransactionController;
35
36 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
37
38 import org.apache.derby.iapi.services.monitor.ModuleControl;
39 import org.apache.derby.iapi.services.context.ContextManager;
40 import org.apache.derby.iapi.services.property.PropertyUtil;
41
42 import org.apache.derby.iapi.services.sanity.SanityManager;
43
44 import org.apache.derby.iapi.error.StandardException;
45
46 import org.apache.derby.iapi.reference.Property;
47
48 import java.util.Properties JavaDoc;
49
50 /**
51     This is simply the factory for creating an optimizer.
52  */

53
54 public class OptimizerFactoryImpl
55     implements ModuleControl, OptimizerFactory {
56
57     protected String JavaDoc optimizerId = null;
58     protected boolean ruleBasedOptimization = false;
59     protected boolean noTimeout = false;
60     protected boolean useStatistics = true;
61     protected int maxMemoryPerTable = 1048576;
62
63     /*
64     ** The fact that we have one set of join strategies for use by all
65     ** optimizers means that the JoinStrategy[] must be immutable, and
66     ** also each JoinStrategy must be immutable.
67     */

68     protected JoinStrategy[] joinStrategySet;
69
70     //
71
// ModuleControl interface
72
//
73

74     public void boot(boolean create, Properties JavaDoc startParams)
75             throws StandardException {
76
77         /*
78         ** This property determines whether to use rule-based or cost-based
79         ** optimization. It is used mainly for testing - there are many tests
80         ** that assume rule-based optimization. The default is cost-based
81         ** optimization.
82         */

83         ruleBasedOptimization =
84                 Boolean.valueOf(
85                     PropertyUtil.getSystemProperty(Optimizer.RULE_BASED_OPTIMIZATION)
86                                 ).booleanValue();
87
88         /*
89         ** This property determines whether the optimizer should ever stop
90         ** optimizing a query because it has spent too long in optimization.
91         ** The default is that it will.
92         */

93         noTimeout =
94                 Boolean.valueOf(
95                     PropertyUtil.getSystemProperty(Optimizer.NO_TIMEOUT)
96                                 ).booleanValue();
97
98         /*
99         ** This property determines the maximum size of memory (in KB)
100         ** the optimizer can use for each table. If an access path takes
101         ** memory larger than that size for a table, the access path is skipped.
102         ** Default is 1024 (KB).
103         */

104         String JavaDoc maxMemValue = PropertyUtil.getSystemProperty(Optimizer.MAX_MEMORY_PER_TABLE);
105         if (maxMemValue != null)
106         {
107             int intValue = Integer.parseInt(maxMemValue);
108             if (intValue >= 0)
109                 maxMemoryPerTable = intValue * 1024;
110         }
111
112         String JavaDoc us = PropertyUtil.getSystemProperty(Optimizer.USE_STATISTICS);
113         if (us != null)
114             useStatistics = (Boolean.valueOf(us)).booleanValue();
115
116         /* Allocation of joinStrategySet deferred til
117          * getOptimizer(), even though we only need 1
118          * array for this factory. We defer allocation
119          * to improve boot time on small devices.
120          */

121     }
122
123     public void stop() {
124     }
125
126     //
127
// OptimizerFactory interface
128
//
129

130     /**
131      * @see OptimizerFactory#getOptimizer
132      *
133      * @exception StandardException Thrown on error
134      */

135     public Optimizer getOptimizer(OptimizableList optimizableList,
136                                   OptimizablePredicateList predList,
137                                   DataDictionary dDictionary,
138                                   RequiredRowOrdering requiredRowOrdering,
139                                   int numTablesInQuery,
140                                   LanguageConnectionContext lcc)
141                 throws StandardException
142     {
143         /* Get/set up the array of join strategies.
144          * See comment in boot(). If joinStrategySet
145          * is null, then we may do needless allocations
146          * in a multi-user environment if multiple
147          * users find it null on entry. However,
148          * assignment of array is atomic, so system
149          * will be consistent even in rare case
150          * where users get different arrays.
151          */

152         if (joinStrategySet == null)
153         {
154             JoinStrategy[] jss = new JoinStrategy[2];
155             jss[0] = new NestedLoopJoinStrategy();
156             jss[1] = new HashJoinStrategy();
157             joinStrategySet = jss;
158         }
159
160         return getOptimizerImpl(optimizableList,
161                             predList,
162                             dDictionary,
163                             requiredRowOrdering,
164                             numTablesInQuery,
165                             lcc);
166     }
167
168     /**
169      * @see OptimizerFactory#getCostEstimate
170      *
171      * @exception StandardException Thrown on error
172      */

173     public CostEstimate getCostEstimate()
174         throws StandardException
175     {
176         return new CostEstimateImpl();
177     }
178
179     /**
180      * @see OptimizerFactory#supportsOptimizerTrace
181      */

182     public boolean supportsOptimizerTrace()
183     {
184         return false;
185     }
186
187     //
188
// class interface
189
//
190
public OptimizerFactoryImpl() {
191     }
192
193     protected Optimizer getOptimizerImpl(OptimizableList optimizableList,
194                                   OptimizablePredicateList predList,
195                                   DataDictionary dDictionary,
196                                   RequiredRowOrdering requiredRowOrdering,
197                                   int numTablesInQuery,
198                                   LanguageConnectionContext lcc)
199                 throws StandardException
200     {
201
202         return new OptimizerImpl(
203                             optimizableList,
204                             predList,
205                             dDictionary,
206                             ruleBasedOptimization,
207                             noTimeout,
208                             useStatistics,
209                             maxMemoryPerTable,
210                             joinStrategySet,
211                             lcc.getLockEscalationThreshold(),
212                             requiredRowOrdering,
213                             numTablesInQuery);
214     }
215
216     /**
217      * @see OptimizerFactory#getMaxMemoryPerTable
218      */

219     public int getMaxMemoryPerTable()
220     {
221         return maxMemoryPerTable;
222     }
223 }
224
225
Popular Tags