KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > iterator > table > TopIterator


1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table;
2
3 import com.daffodilwoods.daffodildb.client.*;
4 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record;
5 import com.daffodilwoods.daffodildb.server.sql99.common.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
8 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
9 import com.daffodilwoods.daffodildb.utils.field.*;
10 import com.daffodilwoods.database.resource.*;
11 import com.daffodilwoods.database.sqlinitiator.*;
12 import com.daffodilwoods.database.utility.P;
13
14 /**
15  * This Class is responsible for evaluating the TOP function. Top
16  * function requires the argument as the number of the records to
17  * be returned from the top.
18  * <p>Title: </p>
19  * <p>Description: </p>
20  * <p>Copyright: Copyright (c) 2004</p>
21  * <p>Company: </p>
22  * @author not attributable
23  * @version 1.0
24  */

25 public class TopIterator extends BaseSingleIterator {
26
27   /**
28    * Variable representing the argument passed to TOP function.
29    */

30   int countLimit;
31   /**
32    * Variable representing the current record number.
33    */

34   long position;
35
36   /**
37    * Initailizes the variables required for executing the TOP function.
38    * @param iterator0 underlying iterator
39    * @param countLimit0 argument passed to the TOP function
40    * @throws DException throws an exception if TOP function argument is less
41    * than equal to zero.
42    */

43   public TopIterator(_Iterator iterator0, FieldBase countLimit0) throws DException {
44       super(iterator0);
45       countLimit = countLimit0.getObject().hashCode();
46       if (countLimit <= 0) {
47          throw new DException("DSE3552", null);
48       }
49       position = 0;
50    }
51
52    /**
53     * This method is used to retrieve the first record from the iterator.
54     * If underlying iterator has record, it set the local variable i.e position
55     * to 1, 0 otherwise.
56     * @return true if reord found, false otherwise.
57     * @throws DException
58     */

59    public boolean first() throws DException {
60       boolean flag = iterator.first();
61       position = flag ? 1 : 0;
62       return flag;
63    }
64
65    /**
66     * This method is used to retrieve the countLimit th Row from the iterator.
67     * It navigates the underlying iterator in the forward direction to get the
68     * from the underlying iterator. If given countLimit is more than the total
69     * number of records in the underlying iterator, last record is returned from
70     * the iterator. Otherwise, countLimit th row is returned.
71     *
72     * If the countLimit is more than total number of records, the underlying
73     * iterator's state is AFTERLAST after navigation. Hence, previous of base
74     * iterator is called to align the underlying iterator.
75     * @return true if reord found, false otherwise.
76     * @throws DException
77     */

78    public boolean last() throws DException {
79       boolean flag = true;
80       for (long i = position; i < countLimit && flag; i++) {
81          flag = iterator.next();
82       }
83
84       /** @todo Following Lines (check) added By Sachin , subject to be confirmed. */
85       if(!flag)
86         iterator.previous();
87
88       position = countLimit;
89       return flag;
90    }
91
92    /**
93     * This method is used to retrieve the next record from the iterator.
94     * This method checkes the number of the current record.
95     * If given count limit is less than current record number, it means, more
96     * records can be returned from the underlying iterator, hence, call is
97     * tranfered to underlying iterator to retrieve the next record and rowcount
98     * is updated. (increments the rowcount by 1).
99     * Else, all the required records had been returned, return false.
100     * @return true if record found, false otherwise.
101     * @throws DException
102     */

103    public boolean next() throws DException {
104       return position < countLimit
105           ? iterator.next() ? position++ > -1 : false
106           : false;
107    }
108
109    /**
110     * This method is used to retrieve the previous record from the iterator.
111     * If record is found, Variable rowcount is decremented by 1.
112     * @return true if record found, false otherwise.
113     * @throws DException
114     */

115    public boolean previous() throws DException {
116       if( position == countLimit )
117        return true;
118
119       boolean flag = iterator.previous();
120       position = flag ? (position - 1) : 0;
121       return flag;
122    }
123
124    /**
125     * This method is used to move the iterator at the passed key.
126     * It navigates the underlying iterator from the first record till the
127     * key matches from the passed key. If key is not found, underlying iterator
128     * is moved to the passed key.
129     * @param key key where to move the iterator
130     * @throws DException
131     */

132    public void move(Object JavaDoc key) throws DException {
133       Object JavaDoc oldKey = iterator.getKey();
134       int tempPosition = 0;
135       boolean match = false;
136       if (iterator.first()) {
137          do {
138             if (iterator.getKey().equals(key)) {
139                match = true;
140             }
141             tempPosition++;
142          } while (!match && tempPosition < countLimit && iterator.next());
143       }
144       if (match) {
145          position = tempPosition;
146       } else {
147          iterator.move(oldKey);
148       }
149    }
150
151    /**
152     * The following methods are used for getting the values from the current
153     * record. These methods check the current erecord number vs given number of
154     * records to be returned from the top. If current record number is within
155     * the valid range (first record to given top argument), call is transfered
156     * to underlying iterator to get the values. Otherwise, throws an exception.
157     */

158
159    public Object JavaDoc getColumnValues(int[] columns) throws DException {
160       if (position < 1 && position > countLimit) {
161          throw new DException("DSE3553", null);
162       }
163       return iterator.getColumnValues(columns);
164    }
165
166    public Object JavaDoc getColumnValues() throws DException {
167       if (position < 1 && position > countLimit) {
168          throw new DException("DSE3553", null);
169       }
170       return iterator.getColumnValues();
171    }
172
173    public Object JavaDoc getColumnValues(_Reference[] reference) throws DException {
174      if (position < 1 && position > countLimit) {
175          throw new DException("DSE3553", null);
176       }
177       return iterator.getColumnValues(reference);
178    }
179
180    public Object JavaDoc getColumnValues(_Reference reference) throws DException {
181       if (position < 1 && position > countLimit) {
182          throw new DException("DSE3553", null);
183       }
184       return iterator.getColumnValues(reference);
185    }
186
187    public _Record getRecord() throws DException {
188       if (position < 1 && position > countLimit) {
189          throw new DException("DSE3553", null);
190       }
191       return iterator.getRecord();
192    }
193
194    /**
195     * This method is responsible to display the executionPlan of a Select Query.
196     * @return _ExecutionPlan
197     * @throws DException
198     */

199    public _ExecutionPlan getExecutionPlan() throws DException {
200       _ExecutionPlan plan = iterator.getExecutionPlan();
201       _ExecutionPlan cplans[] = plan == null ? null : new _ExecutionPlan[] {plan};
202       return new ExecutionPlan("TopIterator", cplans, null, null, null);
203    }
204
205    /**
206     * This method is responsible to display the iterators hierarchy of a Select Query.
207     * @return ExecutionPlanForBrowser
208     * @throws DException
209     */

210    public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException {
211       int length = 1;
212       ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[length];
213       cplans[0] = iterator.getExecutionPlanForBrowser();
214       return new ExecutionPlanForBrowser("Top " + countLimit, "Top Iterator", cplans, null, null, null);
215    }
216
217    /**
218     * The following methods do not have the special fuctioning for executing
219     * TOP function, they transfer the call to the underlying iterator.
220     */

221
222    public _OrderCount getOrderCounts() throws DException {
223       return iterator.getOrderCounts();
224    }
225
226    public TableDetails[] getTableDetails() throws DException {
227       return iterator.getTableDetails();
228    }
229
230    public _Iterator getBaseIterator(ColumnDetails column) throws DException {
231    /*Done by vibha on 20-08-2004 to solve bug no 11696*/
232      return this;
233    }
234
235
236    public void setKeyCount(Object JavaDoc[][] tableAndKeyCount) throws DException {
237       iterator.setKeyCount(tableAndKeyCount);
238    }
239
240    public void setConditionVariableValue(_Reference[] references, Object JavaDoc[] values, int priority) throws DException {
241      if(underlyingRef!=null){
242            references = GeneralPurposeStaticClass.getJointReferences(references,underlyingRef);
243            values = GeneralPurposeStaticClass.getJointValues(this, values,underlyingRef.length);
244          }
245
246      iterator.setConditionVariableValue(references, values, priority);
247    }
248
249    public _KeyColumnInformation[] getKeyColumnInformations() throws DException {
250       return iterator.getKeyColumnInformations();
251    }
252
253    public Object JavaDoc[] getUniqueColumnReference() throws DException {
254       return iterator.getUniqueColumnReference();
255    }
256
257    public _Order getDefaultOrder() throws DException {
258       return iterator.getDefaultOrder();
259    }
260
261    public boolean seekFromTop(_IndexPredicate[] condition) throws DException {
262       return iterator.seekFromTop(condition);
263    }
264
265    public boolean seekFromTopRelative(Object JavaDoc indexKey) throws DException {
266       return iterator.seekFromTopRelative(indexKey);
267    }
268
269    public boolean seekFromBottom(_IndexPredicate[] condition) throws DException {
270       return iterator.seekFromBottom(condition);
271    }
272
273    public boolean seekFromBottomRelative(Object JavaDoc indexKey) throws DException {
274       return iterator.seekFromBottomRelative(indexKey);
275    }
276
277    public boolean seek(Object JavaDoc indexKey) throws DException {
278       return iterator.seek(indexKey);
279    }
280
281
282    public Object JavaDoc getKey() throws DException {
283       return iterator.getKey();
284    }
285 }
286
Popular Tags