KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > plan > table > IndexPredicate


1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.table;
2
3 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
4 import com.daffodilwoods.daffodildb.server.sql99.common.*;
5 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.condition.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*;
8 import com.daffodilwoods.daffodildb.server.sql99.expression.
9     booleanvalueexpression.*;
10 import com.daffodilwoods.database.resource.*;
11 import com.daffodilwoods.database.sqlinitiator.*;
12
13 /**
14  *
15  * <p>Title: IndexPredicate</p>
16  * <p>Description:
17  * It represents the condition of single table for a particular index. For
18  * particular index, some of predicates involved in condition can be solved
19  * using index and rest are solved without using index. It is used to calculate
20  * the cost of a particular index for a particular table. And based on this cost,
21  * resultset is obtained for the table.
22  * </p>
23  * <p>Copyright: Copyright (c) 2003</p>
24  * <p>Company: Daffodil S/W Ltd.</p>
25  * @author unascribed
26  * @version 1.0
27  */

28
29 public class IndexPredicate
30     implements _IndexPredicateInterface, SimpleConstants {
31
32   /**
33    * Represents the predicates which are solvable by index.
34    */

35
36   private AllColumnPredicates indexedAllColumnPredicate;
37
38   /**
39    * Represents the predicates which can't be solved by index.
40    */

41
42   private AllColumnPredicates nonIndexedAllColumnPredicate;
43
44   /**
45    * Represents the cost of index to solve the condition of table.
46    */

47
48   private double cost;
49
50   /**
51    * Represents the estimated number of rows which results after applying
52    * condition on the table.
53    */

54
55   private double ETROW;
56
57   /**
58    * Represents the order which is not solvable by index choosen for condition
59    * evaluation.
60    */

61
62   private _Order order;
63
64   /**
65    * Represents the table.
66    */

67
68   private TableDetails tableDetails;
69
70   /**
71    * Represents the position of index.
72    */

73
74   private int indexPosition;
75
76   /**
77    * boolean flag representing whether there is any false condition present
78    * on table, which will result in zero row.
79    */

80
81   private boolean isNoRecordIterator; // for conditions like A = 2 and A = 3
82

83   public IndexPredicate(AllColumnPredicates indexedAllColumnPredicate0,
84                         AllColumnPredicates nonIndexedAllColumnPredicate0) {
85     indexedAllColumnPredicate = indexedAllColumnPredicate0;
86     nonIndexedAllColumnPredicate = nonIndexedAllColumnPredicate0;
87     indexPosition = -1;
88   }
89
90   /**
91    * This method returns the cost by adding the cost of IndexedPredicates and
92    * cost of NonIndexed Predicates.
93    * @param session
94    * @return
95    * @throws DException
96    */

97
98   public double getCost(Object JavaDoc session) throws DException {
99     long rowCount = getRowCount();
100     /*Next 3 lines are Done by vibha on 26-10-2004 for performance issue.
101        Also check getRow() method above.
102      */

103     if (rowCount == -1) {
104       int rowc = ( (_IndexTable) session).getEstimatedRowCount();
105       rowCount = rowc;
106     }
107
108     Object JavaDoc[] object = (Object JavaDoc[]) CostCalculator.getCostForIndexedPredicate(
109         indexedAllColumnPredicate, rowCount);
110     if (object != null) {
111       cost = ( (Double JavaDoc) object[0]).doubleValue();
112       ETROW = ( (Double JavaDoc) object[1]).doubleValue();
113     }
114     else {
115       ETROW = rowCount;
116     }
117     object = (Object JavaDoc[]) CostCalculator.getCostForNonIndexedPredicate(
118         nonIndexedAllColumnPredicate, ETROW);
119     if (object != null) {
120       cost = cost + ( (Double JavaDoc) object[0]).doubleValue();
121       ETROW = ( (Double JavaDoc) object[1]).doubleValue();
122     }
123     return cost;
124   }
125
126   /**
127    * Returns the cost of index in evaluation of condition.
128    * @return
129    */

130
131   public double getCost() {
132     return cost;
133   }
134
135   /**
136    * Sets the cost of this index predicate.
137    * @param cost0
138    * @throws DException
139    */

140
141   public void setCost(double cost0) throws DException {
142     cost = cost0;
143   }
144
145   /**
146    * Returns the estimated number of rows of table.
147    * @return
148    * @throws DException
149    */

150
151   private long getRowCount() throws DException {
152     long rowCount = tableDetails.getRowCount();
153     /* commented by vibha on 26-10-2004. whenever rowCount ==-1 it was returning 1000 which effects cost
154         To solve performance issue .
155      */

156
157     return rowCount;
158   }
159
160   /**
161    * It allows user to obtain the resultset of condition present on table by
162    * using index.
163    * This method returns one of the following Iterator
164    * 1. No Record Iterator. while contradictory cases arises like a > 5 and a < 5.
165    * 2. Indexed Iterator - if only indexed Predicate is available.
166    * 3. Non Indexed Iterator - if only non Indexed Predicate or non index and
167    * index predicate is available. In second case, firstlty an indexde iterator
168    * is formed, after that non indexed iterator is formed.
169    * @param indexTable
170    * @return
171    * @throws DException
172    */

173
174   public _Iterator execute(Object JavaDoc indexTable) throws DException {
175     if (isNoRecordIterator) {
176       return getNoRecordIterator();
177     }
178     _Iterator iterator = null;
179     if (indexedAllColumnPredicate != null) { // means Condition is solved by Index
180
iterator = getIndexedIterator(indexTable);
181     }
182     else {
183       iterator = getTableIterator(indexTable);
184     }
185     if (nonIndexedAllColumnPredicate != null) {
186       iterator = getNonIndexedFilterIterator(iterator, indexTable);
187     }
188     if (order != null) {
189       order.setSolvableByIndex(false);
190     }
191     return iterator;
192   }
193
194   /**
195    * Returns the resultset which solve condition sequentially.
196    * @param iterator
197    * @param indexTable
198    * @return
199    * @throws DException
200    */

201
202   private _Iterator getNonIndexedFilterIterator(_Iterator iterator,
203                                                 Object JavaDoc indexTable) throws
204       DException {
205     booleanvalueexpression nonIndexedCondition = nonIndexedAllColumnPredicate.
206         getNonIndexedCondition();
207     return GeneralPurposeStaticClass.
208         getNonIndexedFilterIteratorForConditionExecute(indexTable,
209         nonIndexedCondition, new TableDetails[] {tableDetails}
210         , iterator);
211   }
212
213   /**
214    * Returns the resultset which solve condition by using index.
215    * @param indexTable
216    * @return
217    * @throws DException
218    */

219
220   private _Iterator getIndexedIterator(Object JavaDoc indexTable) throws DException {
221     return indexedAllColumnPredicate.execute(indexTable); // IndexedFilterIterator
222
}
223
224   /**
225    * Returns the whole resultset of table. If index is present then sorted
226    * resultset is returned otherwise resultset of natural order is returned.
227    * @param indexTable
228    * @return
229    * @throws DException
230    */

231
232   private _Iterator getTableIterator(Object JavaDoc indexTable) throws DException {
233     return indexPosition != -1 ?
234         ( (_IndexTable) indexTable).getIterator(indexPosition) :
235         ( (_IndexTable) indexTable).getDefaultIterator();
236   }
237
238   /**
239    * Returns the resultset which don't have any rows in it. It is the result of
240    * false condition presentc on table, which will result in zero row.
241    * @return
242    * @throws DException
243    */

244
245   private _Iterator getNoRecordIterator() throws DException {
246     return new NoRecordIterator();
247   }
248
249   /**
250    * Returns the estimated number of rows results after applying condition on
251    * table.
252    * @return
253    * @throws DException
254    */

255
256   public double getEstimatedRow() throws DException {
257     return ETROW;
258   }
259
260   /**
261    * Sets the order which is not solvable by index.
262    * @param order1
263    * @throws DException
264    */

265
266   public void setOrder(_Order order1) throws DException {
267     order = order1;
268   }
269
270   /**
271    * Returns the order which is not solvable by index.
272    * @return
273    * @throws DException
274    */

275
276   public _Order getOrder() throws DException {
277     return order;
278   }
279
280   public String JavaDoc toString() {
281     String JavaDoc str = "";
282     str += "INDEX PREDICATE -> TABLENAME = " + tableDetails;
283     str += "[INDEXED" + indexedAllColumnPredicate + "]";
284     str += "[NONINDEXED" + nonIndexedAllColumnPredicate + "]";
285     return str;
286   }
287
288   /**
289    * Sets the table.
290    * @param tableDetails0
291    * @throws DException
292    */

293
294   public void setTableDetails(TableDetails tableDetails0) throws DException {
295     tableDetails = tableDetails0;
296     if (indexedAllColumnPredicate != null) {
297       indexedAllColumnPredicate.setTableDetails(tableDetails0);
298     }
299     if (nonIndexedAllColumnPredicate != null) {
300       nonIndexedAllColumnPredicate.setTableDetails(tableDetails0);
301     }
302   }
303
304   /**
305    * Sets the index which is used to solve order in case when condition is not
306    * present.
307    * @param indexPosition0
308    */

309   public void setIndex(int indexPosition0) {
310     indexPosition = indexPosition0;
311   }
312
313   /**
314    * Set boolean flag when any false condition is present on table.
315    * @param noRecordIterator
316    */

317
318   public void setNoRecordIteratorStatus(boolean noRecordIterator) {
319     isNoRecordIterator = noRecordIterator;
320   }
321
322 }
323
Popular Tags