KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > execution > SingleTableExecuter


1 package com.daffodilwoods.daffodildb.server.sql99.dql.execution;
2
3 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*;
4 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
5 import com.daffodilwoods.daffodildb.server.serversystem.*;
6 import com.daffodilwoods.daffodildb.server.sql99.common.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*;
9 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
10 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
11 import com.daffodilwoods.database.resource.*;
12 import com.daffodilwoods.database.sqlinitiator.*;
13
14 /**
15  * It allows user to obtain resultset for single table. It takes care of
16  * aggregates and order present on single table.
17  * <p>Title: </p>
18  * <p>Description: </p>
19  * <p>Copyright: Copyright (c) 2003</p>
20  * <p>Company: </p>
21  * @author unascribed
22  * @version 1.0
23  */

24
25 public class SingleTableExecuter implements _SingleTableExecuter {
26
27    /**
28     * Represents the order for single table.
29     */

30
31    protected _Order order;
32
33    /**
34     * Represents the table whose resultset is to obtain.
35     */

36
37    protected TableDetails table;
38
39    /**
40     * Represents the object of connection, through the help of which, information
41     * about table is retrieved.
42     */

43
44    protected _ServerSession serverSession;
45
46    /**
47     * Represents all the columns of a table present in query.
48     */

49
50    protected String JavaDoc[] queryColumns;
51
52    /**
53     * Represents which index is used to get the resultset of table.
54     */

55
56    private int indexPosition = -1;
57
58    /**
59     * Represents whether order present is solvable by index choosen to obtain
60     * the resultset.
61     */

62
63    private boolean orderSolvableByIndex;
64
65    /**
66     * Represents whether the orderspecification(asc/desc) of Order is satisfied
67     * by orderspecification(asc/desc) of Index choosen.
68     */

69
70    private boolean sameOrReverse;
71
72    /**
73     * Represents whether user rights need to be checked on this table or not.
74     */

75
76    protected boolean checkUserRight;
77
78    /**
79     * Represents whether Select query is with 'for update' option or not.
80     */

81
82    protected boolean forUpdate;
83
84    /**
85     * Represents the aggregate columns which belong to single table.
86     */

87
88    protected ColumnDetails[] aggregateColumns;
89
90    boolean containsFlag;
91    public boolean isInternalIteratorRequired;
92    public SingleTableExecuter(_Order order0, TableDetails table0, _ServerSession serverSession0, String JavaDoc[] queryColumns0, ColumnDetails[] aggregateColumns0) {
93       order = order0;
94       table = table0;
95       serverSession = serverSession0;
96       queryColumns = queryColumns0;
97       aggregateColumns = aggregateColumns0;
98    }
99
100    /**
101     * Sets whether user right needs to be checked or not.
102     * @param checkUserRight0 is true when table is involved in view, otherwise
103     * false.
104     */

105
106    public void setUserRight(boolean checkUserRight0) {
107       checkUserRight = checkUserRight0;
108    }
109
110    /**
111     * Used to check whether user rights needs to be checked or not.
112     * @return
113     */

114
115    public boolean checkUserRight() {
116       return checkUserRight;
117    }
118
119    /**
120     * This method is required to obtain the cost of solving this table. Cost
121     * is calculated on the basis of aggregate columns,order and columns present
122     * on the table. Each index present on the table is checked to find the best
123     * possible index, which can solve this table optimally. Firstly aggregate
124     * columns are checked whether they can be satisfied by index or not.
125     * If aggregates are present, then no order will be present on table. After
126     * that order is checked whether it can be solved with the index or not, if
127     * order is satisfied then it is checked whether orderspecification of order
128     * and orderspecification of index is equal or not. Based on these criteria,
129     * cost of each index is calculated. And that index is choosen whose cost
130     * is lowest among all indexes.
131     * @param indexTable
132     * @return
133     * @throws DException
134     */

135
136    public double getCost(_IndexTable indexTable) throws DException {
137       long estimatedRows = table.getTableType() == TypeConstants.VIEW ? 1000 : ( (_ServerSession) serverSession).getEstimatedRowCount(table.getQualifiedIdentifier());
138       _IndexInformation[] indexInformations = indexTable.getIndexInformations();
139       ColumnDetails aggColumn = aggregateColumns == null ? null : aggregateColumns[0].getExistingColumnDetails()[0];
140       double cost = Double.MAX_VALUE;
141       if (TableReferenceMerger.checkForAggregateColumn(aggColumn, indexInformations[0])) {
142          int num = TableReferenceMerger.checkForOrderColumns(order, indexInformations[0]);
143          sameOrReverse = num == 0;
144          orderSolvableByIndex = num != -1;
145          cost = computeCostForOrderAndQueryColumns(indexInformations[0], estimatedRows, orderSolvableByIndex);
146          indexPosition = 0;
147       }
148       for (int i = 1, length = indexInformations.length; i < length; i++) {
149          if (TableReferenceMerger.checkForAggregateColumn(aggColumn, indexInformations[i])) {
150             int num = TableReferenceMerger.checkForOrderColumns(order, indexInformations[i]);
151             boolean checkForOrderSolvableByIndex1 = num != -1;
152             boolean sameOrReverse1 = num == 0;
153             double cost1 = computeCostForOrderAndQueryColumns(indexInformations[i], estimatedRows, checkForOrderSolvableByIndex1);
154             if (cost1 < cost) {
155                indexPosition = i;
156                cost = cost1;
157                orderSolvableByIndex = checkForOrderSolvableByIndex1;
158                sameOrReverse = sameOrReverse1;
159             }
160          }
161       }
162       return cost;
163    }
164
165    /**
166     * It helps in computing the cost of the table based on the number of rows,
167     * order and columns of table present in query. If order is not solvable
168     * by index, then cost of temporary order is added to resultant cost and also
169     * if all the columns present on the table is not satisfied by the index
170     * passed, then cost of one more disc scan is added to resultant cost.
171     * @param indexInformation
172     * @param estimatedRows
173     * @param checkForOrderSolvableByIndex
174     * @return
175     * @throws DException
176     */

177
178    private double computeCostForOrderAndQueryColumns(_IndexInformation indexInformation, long estimatedRows, boolean checkForOrderSolvableByIndex) throws DException {
179       double cost = estimatedRows;
180       if (!checkForOrderSolvableByIndex) { // true means, yes solvable by index.
181
cost = cost + CostCalculator.getCostForTemporaryOrder(estimatedRows);
182       }
183       boolean checkForQueryColumnsSolvableByIndex = TableReferenceMerger.checkForQueryColumns(indexInformation.getColumns(), queryColumns);
184       if (!checkForQueryColumnsSolvableByIndex) { // true means, yes solvable by index.
185
cost = cost + CostCalculator.getCostForQueryColumns(estimatedRows);
186       }
187       return cost;
188    }
189
190    /**
191     * It is required to obtain the resultset of single table. Resultset is
192     * formed on the basis of cost computed for each index present on table.
193     * if order is solvable by index, then sorted resultset is formed. Also in
194     * that case, it is set in order whether orderspecification of order and
195     * orderspecification of index is equal or not.S
196     * @param indexTable
197     * @return
198     * @throws DException
199     */

200
201    public _Iterator execute(_IndexTable indexTable) throws DException {
202       getCost(indexTable);
203       if (order != null) {
204          order.setSolvableByIndex(orderSolvableByIndex);
205          order.setIndexIsSameOrReverse(sameOrReverse);
206       }
207       return orderSolvableByIndex
208           ? indexTable.getIterator(indexPosition)
209           : indexTable.getDefaultIterator();
210    }
211
212    /**
213     * Used to set whether forupdate option is present with select query or not.
214     */

215
216    public void setForUpdate() {
217       forUpdate = true;
218    }
219
220    /**
221     * Used to get whether forupdate option is present with select query or not.
222     * @return
223     */

224
225    public boolean hasForUpdate() {
226       return forUpdate;
227    }
228
229    /**
230     * Required to get the condition present on the table.
231     * @return
232     * @throws DException
233     */

234
235    public booleanvalueexpression getCondition() throws DException {
236       return null;
237    }
238
239    /**
240     * Following methods are used by sessiontable. For the documentation please
241     * refer to documentation refer to sessiontable.
242     */

243
244    public _Iterator executeForDirtyLock(_IndexTable indexTable) throws DException {
245       throw new UnsupportedOperationException JavaDoc("Method executeForDirtyLock() yet not implemented");
246    }
247
248    public void addCondition(booleanvalueexpression condition) throws DException {
249       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression._SingleTableExecuter method*/
250       throw new java.lang.UnsupportedOperationException JavaDoc("Method addCondition() not yet implemented.");
251    }
252
253    public _Reference[] getReferences() throws DException {
254       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression._SingleTableExecuter method*/
255       throw new java.lang.UnsupportedOperationException JavaDoc("Method getReferences() not yet implemented.");
256    }
257
258    public boolean isContainsPresent() {
259       return containsFlag;
260    }
261
262    public void setContainsClauseFlag(boolean containsFlag) throws DException {
263       this.containsFlag = containsFlag;
264    }
265
266    public int[] getColumns() throws DException {
267      if(queryColumns == null || queryColumns.length == 0)
268        return null;
269      else
270       return table.cc.getColumnIndexes(queryColumns);
271    }
272
273    public boolean isParameterisedQuery() throws DException {
274      /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression._SingleTableExecuter method*/
275      throw new java.lang.UnsupportedOperationException JavaDoc("Method isParameterisedQuery() not yet implemented.");
276    }
277    public void isInterIteratorRequired( boolean a){
278     isInternalIteratorRequired = a;
279  }
280  public boolean getInternalIteratorRequired(){
281    return isInternalIteratorRequired;
282 }
283 }
284
Popular Tags