KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > iterator > set > IntersectDistinctIterator


1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.set;
2
3 import com.daffodilwoods.daffodildb.client.*;
4 import com.daffodilwoods.daffodildb.server.sql99.common.*;
5 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
6 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
7 import com.daffodilwoods.daffodildb.utils.comparator.*;
8 import com.daffodilwoods.database.resource.*;
9
10 /**
11  * <p>Title: IntersectDistinctIterator </p>
12  * <p>This class is required to retrive the result from a select query having
13  * INTERSECT DISTINCT operator. INTERSECT operator displays the records which have
14  * same values of selected columns in corresonding iterator. DISTINCT is implied
15  * to eliminate duplicate rows from the resultset.
16  *
17  * This class is required to support the <INTERSECT Clause> set operator. The INTERSECT works opposite
18  * to the UNION operator. Unlike the UNION operator which outputs the distinct records, the INTERSECT operator
19  * displays the records which are common for both the queries. By default DISTINCT is implied and is used with INTERSECT (that is, INTERSECT DISTINCT), so that duplicate
20  * rows are eliminated. In either case, each row of the derived table is a row from either TABLE1 or TABLE2.
21  * </p>
22  * <p>Copyright: Copyright (c) 2003</p>
23  * <p>Company: </p>
24  * @author unascribed
25  * @version 1.0
26  */

27 public class IntersectDistinctIterator extends IntersectAllIterator {
28
29   /**
30    *
31    * A state is also maintained, that specifies the current status of the current level iterator(this).
32    *
33    * The State INVALIDSTATE means iterator is not aligned properly and needs to be aligned
34    * before doing any further operations.
35    *
36    * The State VALIDSTATE means iterator is aligned properly and can be moved next()/ previous()
37    * as well as values can also be retrieved
38    *
39    * The State BEFOREFIRST means, iterator has reached before the first record or there is no record in the result set.
40    * Iterator is not aligned properly and needs to be aligned through calling first()/ last() before doing any further operations.
41    *
42    * The State AFTERLAST means, iterator has reached after the last record or there is no record in the result set.
43    * Iterator is not aligned properly and needs to be aligned through calling first()/ last() before doing any further operations.
44    *
45    * @param leftIterator
46    * @param rightIterator
47    * @param leftColumnReferences0
48    * @param rightColumnReferences0
49    * @param comparator0
50    * @throws DException
51    */

52     public IntersectDistinctIterator(_Iterator leftIterator, _Iterator rightIterator,_Reference[] leftColumnReferences,_Reference[] rightColumnReferences,SuperComparator comparator,_Reference []orderLeftCD0,_Reference []orderRightCD0) throws DException{
53         super(leftIterator,rightIterator,leftColumnReferences,rightColumnReferences,comparator,orderLeftCD0,orderRightCD0);
54     }
55
56     /**
57      * This method is responsible to retrieve the first record from the iterator.
58      * 1. First Record is retrieved from both underlying iterator. If any of the
59      * underlying iterator has no records, return false.
60      * 2. Get selected column values from both iterator. Compare the values.
61      * a. If values are same, it means both iterator are pointed to records
62      * with common selected column values, return true.
63      * b. Next DISTINCT record is retrieved from iterator that has smaller
64      * set of values as compared to that of another iterator. If iterator
65      * has no more records, return false. Else, goto step 2.
66      *
67      * Both iterators are aligned to their valid first record. If true then Using the indexing feature. smaller value from
68      * among the iterator is relatively seeked from current location for the bigger record. The process continues till there does not appears any common record.
69      * @return true if first common record is found else return false.
70      * @throws DException
71      */

72     public boolean first() throws DException {
73         boolean b1 = leftIterator.first();
74         boolean b2 = rightIterator.first();
75         if(b1 && b2)
76             return (state = alignForward() ? VALIDSTATE : AFTERLAST ) != AFTERLAST;
77         return (state = AFTERLAST ) != AFTERLAST ;
78     }
79
80     /**
81      * This method is responsible to retrieve the last record from the iterator.
82      * 1. Last Record is retrieved from both underlying iterator. If any of the
83      * underlying iterator has no records, return false.
84      * 2. Get selected column values from both iterator. Compare the values.
85      * a. If values are same, it means both iterator are pointed to records
86      * with common selected column values, return true.
87      * b. Previous DISTINCT record is retrieved from iterator that has larger
88      * set of values as compared to that of another iterator. If iterator
89      * has no more records, return false. Else, goto step 2.
90      *
91      * DETAILED DOCUENTATION
92      *
93      * Both iterators are aligned to their valid last record. If true then Using the indexing feature. smaller value from
94      * among the iterator is relatively seeked from current location for the smaller record. The process continues till there does not appears any common record.
95      * @return true if last common record is found else return false.
96      * @throws DException
97      */

98     public boolean last() throws DException {
99       if (leftIterator.last() && rightIterator.last()) {
100          return (state = alignBackward() ? VALIDSTATE : BEFOREFIRST) != BEFOREFIRST;
101       }
102       return (state = BEFOREFIRST) != BEFOREFIRST;
103    }
104
105    /**
106     * This method is responsible to retrieve the next record from the iterator.
107     * 1. Next Record is retrieved from both underlying iterator. If any of the
108     * underlying iterator has no more records, return false.
109     * 2. Get selected column values from both iterator. Compare the values.
110     * a. If values are same, it means both iterator are pointed to records
111     * with common selected column values, return true.
112     * b. Next DISTINCT record is retrieved from iterator that has smaller
113     * set of values as compared to that of another iterator. If iterator
114     * has no more records, return false. Else, goto step 2.
115     *
116     *
117     * DETAILED DOCUMENTATION
118     *
119     * i. If previous state is INVALIDSTATE,
120     * through Invalid State Exception.
121     * ii. Else If previous state is AFTERLAST,
122     * return false.
123     * iii. Else If previous state is BEFOREFIRST,
124     * call and return its first() .
125     * iv. Else If any of the iterator could not iterate to the valid next Distinct position then method 'll return false.
126     * v. Otherwise they 'll be aligned to the valid common record using forward seek from its current position
127     * and its result 'll be returned .(through seeking of valid distinct record only Distinct record 'll be the
128     * part of the final resultset.)
129     *
130     * @return
131     * @throws DException
132     */

133    public boolean next() throws DException {
134       if (state == INVALIDSTATE) {
135          throw new DException("DSE4116", null);
136       }
137       if (state == BEFOREFIRST) {
138          return first();
139       }
140       if (state == AFTERLAST) {
141          return false;
142       }
143       Object JavaDoc left = leftIterator.getColumnValues(orderLeftCD);
144       if (!moveForward(leftIterator, rightIterator.getColumnValues(orderRightCD), orderLeftCD, true)
145           || !moveForward(rightIterator, left, orderRightCD, false)) {
146          state = AFTERLAST;
147          return false;
148       }
149       return (state = alignForward() ? state : AFTERLAST) != AFTERLAST;
150    }
151
152    /**
153     * This method is responsible to retrieve the previous record from the iterator.
154     * 1. Previous Record is retrieved from both underlying iterator. If any of
155     * the underlying iterator has no more records, return false.
156     * 2. Get selected column values from both iterator. Compare the values.
157     * a. If values are same, it means both iterator are pointed to records
158     * with common selected column values, return true.
159     * b. Previous DISTINCT record is retrieved from iterator that has larger
160     * set of values as compared to that of another iterator. If iterator
161     * has no more records, return false. Else, goto step 2.
162     *
163     * DETAILED DOCUMENTATION
164     *
165     * i. If previous state is INVALIDSTATE,
166     * through Invalid State Exception.
167     * ii. Else If previous state is BEFOREFIRST,
168     * return false.
169     * iii.Else If previous state is AFTERLAST,
170     * call and return its last() .
171     * iv. Else If any of the iterator could not iterate to the valid previous distinct position then method 'll return false.
172     * v. Otherwise they 'll be aligned to the valid common record using backward seek from its current position
173     * and its result 'll be returned (through seeking of valid distinct record only Distinct record 'll be the
174     * part of the final resultset.).
175     *
176     * @return
177     * @throws DException
178     */

179    public boolean previous() throws DException {
180       if (state == INVALIDSTATE) {
181          throw new DException("DSE4117", null);
182       }
183       if (state == AFTERLAST) {
184          return last();
185       }
186       if (state == BEFOREFIRST) {
187          return false;
188       }
189       Object JavaDoc left = leftIterator.getColumnValues(orderLeftCD);
190       if (!moveBackward(leftIterator, rightIterator.getColumnValues(orderRightCD), orderLeftCD, true)
191           || !moveBackward(rightIterator, left, orderRightCD, false)) {
192          state = BEFOREFIRST;
193          return false;
194       }
195       return (state = alignBackward() ? state : BEFOREFIRST) != BEFOREFIRST;
196    }
197
198
199    /**
200     * This method is used to navigate the passed iterator in the forward
201     * direction till the iterator is pointed to the record that has the
202     * distinct values of selected columns as compared to passed values.
203     * Move the single iterator on just next distinct record.
204     * @param iterator underlying iterator to navigate
205     * @param value target value
206     * @param references selected column given in query corr. to iterator
207     * @param leftRight true if passed iterator is leftiterator, false, otherwise.
208     * @return true if common record found from passed iterator, false otherwise.
209     * @throws DException
210     */

211    boolean moveForward(_Iterator iterator,Object JavaDoc value,_Reference[] references,boolean leftRight) throws DException{
212        while(iterator.next()){
213           if(leftRight){
214              if(compare(iterator.getColumnValues(references),value) != 0){
215               return true;
216            }
217         } else {
218            if (compare(value, iterator.getColumnValues(references)) != 0) {
219               return true;
220            }
221         }
222      }
223      return false;
224   }
225
226   /**
227    * This method is used to navigate the passed iterator in the backward
228    * direction till the iterator is pointed to the record that has the
229    * distinct values of selected columns as compared to passed values.
230    * Move the single iterator on just previous distinct record.
231    * @param iterator underlying iterator to navigate
232    * @param value target value
233    * @param references selected column given in query corr. to iterator
234    * @param leftRight true if passed iterator is leftiterator, false, otherwise.
235    * @return true if common record found from passed iterator, false otherwise.
236    * @throws DException
237    */

238   boolean moveBackward(_Iterator iterator, Object JavaDoc value, _Reference[] references, boolean leftRight) throws DException {
239      while (iterator.previous()) {
240         if (leftRight) {
241            if (compare(iterator.getColumnValues(references), value) != 0) {
242               return true;
243            }
244         } else {
245            if (compare(value, iterator.getColumnValues(references)) != 0) {
246               return true;
247            }
248         }
249      }
250      return false;
251   }
252
253    /**
254     * This method is responsible to display the executionPlan of a Select Query.
255     * @return _ExecutionPlan
256     * @throws DException
257     */

258    public _ExecutionPlan getExecutionPlan() throws DException{
259        _ExecutionPlan cplans[] = new _ExecutionPlan[2];
260        cplans[0] = leftIterator.getExecutionPlan();
261        cplans[1] = rightIterator.getExecutionPlan();
262        return new ExecutionPlan("IntersectDistinctIterator",cplans,null,null,null);
263    }
264
265    /**
266     * This method is responsible to display the iterators hierarchy of a Select Query.
267     * @return ExecutionPlanForBrowser
268     * @throws DException
269     */

270    public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException {
271        ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[2];
272        cplans[0] = leftIterator.getExecutionPlanForBrowser();
273        cplans[1] = rightIterator.getExecutionPlanForBrowser();
274        return new ExecutionPlanForBrowser("Intersect Distinct","Intersect Distinct Iterator",cplans,null,null,null);
275    }
276
277    /**
278     * The following methods of this class are used a intermediate in the
279     * iterator hierarchy. These methods simply transfer the call to the
280     * underlying iterator with the same arguments.
281     */

282
283     public _KeyColumnInformation[] getKeyColumnInformations() throws DException {
284         return GeneralPurposeStaticClass.getKeyColumnInformations(leftIterator,rightIterator);
285     }
286
287     public Object JavaDoc[] getUniqueColumnReference() throws DException {
288         return GeneralPurposeStaticClass.getUniqueColumnReference(leftIterator,rightIterator);
289     }
290
291     public String JavaDoc toString() {
292         return "IntersectDistinctIterator [" + leftIterator + "] [" + rightIterator + "]";
293     }
294 }
295
Popular Tags