KickJava   Java API By Example, From Geeks To Geeks.

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


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.database.resource.*;
8 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs;
9
10 /**
11  *
12  * <p>Title: UnionAllIterator </p>
13  * <p>This class is used for retrieval of records from a query having UNION ALL
14  * set operator. UNION ALL set operator returns all the records from both the
15  * underlying iterators. In UNION ALL Iterator, all records of left iterator is
16  * retrieved, thereafter all records of right iterator are retrieved.</p>
17  * <p>Copyright: Copyright (c) 2003</p>
18  * <p>Company: </p>
19  * @author unascribed
20  * @version 1.0
21  */

22 public class UnionAllIterator extends UnionAllOrderedIterator implements TypeConstants {
23
24    /**
25     * A public constructor, that takes four arguments, two are left and right iterators, and
26     * other two are left and right side selected column References. A state is also maintained,
27     * that specifies the current status of the current level iterator(this).
28     *
29     * The State INVALIDSTATE means iterator is not aligned properly and needs to be aligned
30     * before doing any further operations.
31     *
32     * The State VALIDSTATE means iterator is aligned properly and can be moved next()/ previous()
33     * as well as values can also be retrieved
34     *
35     * The State BEFOREFIRST means, iterator has reached before the first record or there is no record in the result set.
36     * Iterator is not aligned properly and needs to be aligned through calling first()/ last() before doing any further operations.
37     *
38     * The State AFTERLAST means, iterator has reached after the last record or there is no record in the result set.
39     * Iterator is not aligned properly and needs to be aligned through calling first()/ last() before doing any further operations.
40     *
41     * The State FIRSTISCURRENT means, left iterator is the current iterator and is ready to provide the value for the current row.
42     *
43     * The State SECONDISCURRENT means, right iterator is the current iterator and is ready to provide the value for the current row.
44     *
45     * @param leftIterator0 left iterator
46     * @param rightIterator0 right iterator
47     * @param leftColumnReferences0 selected columns from left iterator
48     * @param rightColumnReferences0 selected columns from right iterator
49     */

50    public UnionAllIterator(_Iterator leftIterator0, _Iterator rightIterator0, _Reference[] leftColumnReferences0, _Reference[] rightColumnReferences0, int[] appropriateDataTypes, int[] appropriateSizes, _Reference[] orderLeftCD0, _Reference[] orderRightCD0) throws DException {
51       super(leftIterator0, rightIterator0, leftColumnReferences0, rightColumnReferences0, appropriateDataTypes, appropriateSizes, orderLeftCD0, orderRightCD0);
52       state = INVALIDSTATE;
53    }
54
55    boolean flag = false;
56
57    /**
58     * This method is responsible for retriving first record of Union Iterator.
59     * First record is retrived from first (or second, if first iterator has no
60     * records) iterator and a flag is maintained that represents that first
61     * (or second) iterator is CURRENT iterator.
62     *
63     * DETAILED DOCUMENTATION
64     *
65     * If Left Iterator.first() returns true, state 'll be set to FIRSTISCURRENT, otherwise if Right
66     * Iterator.first() returns true, state is set to SECONDISCURRENT and returned true.
67     * Otherwise state 'll be INVALIDSTATUS and first() 'll return false.
68     *
69     * @return true if either of the iterator has record, false, if none of the
70     * iterator has records.
71     * @throws com.daffodilwoods.database.resource.DException
72     */

73    public boolean first() throws com.daffodilwoods.database.resource.DException {
74       state = leftIterator.first() ? FIRSTISCURRENT
75           : rightIterator.first() ? SECONDISCURRENT : state;
76       return state != INVALIDSTATE;
77    }
78
79
80
81
82   /**
83    * This method is responsible for retriving last record of Union Iterator.
84    * Last record is retrived from second (or first, if second iterator has no
85    * records) iterator and a flag is maintained that represents that second
86    * (or first) iterator is CURRENT iterator.
87    *
88    * DETAILED DOCUMENTATION
89    *
90    * If Right Iterator.last() returns true, state 'll be set to SECONDISCURRENT, otherwise if Left
91    * Iterator.last() returns true, state is set to FIRSTISCURRENT and returned true.
92    * Otherwise state 'll be INVALIDSTATUS and last() 'll return false.
93    *
94    * @return true if either of the iterator has record, false, if none of the
95    * iterator has records.
96    * @throws com.daffodilwoods.database.resource.DException
97    */

98   public boolean last() throws com.daffodilwoods.database.resource.DException {
99     return (state = rightIterator.last() ? SECONDISCURRENT : leftIterator.last() ? FIRSTISCURRENT : state) != INVALIDSTATE;
100   }
101
102   /**
103    * This method is responsible for retriving next record of Union Iterator.
104    * Next record is retrived from first (or second, if first iterator has no
105    * more records) iterator and a flag is maintained that represents that first
106    * (or second) iterator is CURRENT iterator.
107    *
108    * DETAILED DOCUMENTATION
109    *
110    * i. If previous state is INVALIDSTATE,
111    * through Invalid State Exception.
112    * Else If previous state is AFTERLAST,
113    * return false.
114    * Else If previous state is BEFOREFIRST,
115    * call and return its first() .
116    * ii. If left iterator is current, next() of the left iterator 'll be called.
117    * a. If returned true method 'll return true.
118    * b. Else first() of rightIterator 'll be called, If returned true method 'll
119    * return true after setting state to SECONDISCURRENT.
120    * c. else state 'll be set to AFTERLAST and returned false.
121    * iii. Otherwise If right iterator is current, next() of the right iterator 'll be called. If returned
122    * true, method 'll return true, otherwise state 'll be AFTERLAST and returned false.
123    * @return true if either of the iterator has more record in same forward
124    * navigation, false, otherwise.
125    * @throws com.daffodilwoods.database.resource.DException
126    */

127   public boolean next() throws com.daffodilwoods.database.resource.DException {
128     switch(state) {
129       case INVALIDSTATE :
130         throw new DException("DSE4116",null);
131       case BEFOREFIRST :
132         return first();
133       case AFTERLAST :
134         return false;
135     }
136     state = state == FIRSTISCURRENT ? leftIterator.next() ? state
137     : rightIterator.first() ? SECONDISCURRENT
138     : AFTERLAST
139     : state == SECONDISCURRENT
140           ? rightIterator.next() ? state
141     : AFTERLAST
142     : state;
143     return state != AFTERLAST;
144   }
145
146   /**
147    * This method is responsible for retriving previous record of Union Iterator.
148    * Previous record is retrived from second (or first, if second iterator has
149    * no records) iterator and a flag is maintained that represents that second
150    * (or first) iterator is CURRENT iterator.
151    *
152    * DETAILED DOCUMENTATION
153    *
154    * i. If previous state is INVALIDSTATE,
155    * through Invalid State Exception.
156    * Else If previous state is AFTERLAST,
157    * call and return its last() .
158    * Else If previous state is BEFOREFIRST,
159    * return false.
160    * ii. If right iterator is current, next() of the right iterator 'll be called.
161    * a. If returned true method 'll return true.
162    * b. Else first() of leftIterator 'll be called, If returned true method 'll
163    * return true after setting state to FIRSTISCURRENT.
164    * c. else state 'll be set to AFTERLAST and returned false.
165    * iii. Otherwise If left iterator is current, next() of the left iterator 'll be called. If returned
166    * true, method 'll return true, otherwise state 'll be AFTERLAST and returned false.
167    * @return true if either of the iterator has more record in same backward
168    * navigation, false, otherwise.
169    * @throws com.daffodilwoods.database.resource.DException
170    */

171   public boolean previous() throws com.daffodilwoods.database.resource.DException {
172     switch(state){
173       case INVALIDSTATE :
174         throw new DException("DSE4117",null);
175       case BEFOREFIRST :
176         return false;
177       case AFTERLAST :
178         return last();
179     }
180     if(state == SECONDISCURRENT){
181       state = rightIterator.previous() ? state
182       : leftIterator.last() ? FIRSTISCURRENT : BEFOREFIRST;
183     }
184     else if(state == FIRSTISCURRENT)
185       state = leftIterator.previous() ? state : BEFOREFIRST;
186     return state != BEFOREFIRST;
187   }
188
189   /**
190    * This method is used to get the KEY of the iterator at particular position.
191    * The Key of union iterator is SetOperatorKey ( Which stores the keys of
192    * both underlying iterators, key is null if there is no data in corresponding
193    * iterator) , state of iterator and fetching direction.
194    * @return Key of Valid record of Union Iterator
195    * @throws DException
196    */

197   public Object JavaDoc getKey() throws DException {
198         switch(state) {
199           case INVALIDSTATE :
200           case BEFOREFIRST :
201           case AFTERLAST :
202              throw new DException("DSE4116",null);
203           case FIRSTISCURRENT :
204              return new Object JavaDoc[]{new SetOperatorKey(new Object JavaDoc[] {leftIterator.getKey(),null},state,direction)};
205           case SECONDISCURRENT :
206              return new Object JavaDoc[]{new SetOperatorKey(new Object JavaDoc[] {null,rightIterator.getKey()},state,direction)};
207         }
208         new Exception JavaDoc(" CHECK THE CASE :: AKASH ").printStackTrace();
209         return null;
210   }
211
212   /**
213    * This method is used to move underlying iterators corresponding to key
214    * passed. It extracts the keys of left and right iterators from the passed
215    * key and moves both underlying iterators to corresponding non-null keys.
216    * @param keys keys on which iterator is required to align.
217    * @throws DException
218    */

219   public void move(Object JavaDoc keys) throws DException {
220         SetOperatorKey setKey = (SetOperatorKey)((Object JavaDoc[])keys)[0];
221         state = setKey.getState();
222         Object JavaDoc[] kees = setKey.getKeys();
223         Object JavaDoc leftKeys = kees[0];
224         Object JavaDoc rightKeys = kees[1];
225         if(leftKeys != null){
226           leftIterator.move(leftKeys);
227         }
228         if(rightKeys != null){
229           rightIterator.move(rightKeys);
230         }
231   }
232
233   /**
234    * This method is used to get the lowest level iterator.
235    * @param column column corresponding to which iterator is to return.
236    * @return same iterator.
237    * @throws DException
238    */

239   public _Iterator getBaseIterator(ColumnDetails column) throws com.daffodilwoods.database.resource.DException {
240     return this;
241   }
242
243   /**
244    * This method is responsible to display the executionPlan of a Select Query.
245    * @return _ExecutionPlan
246    * @throws DException
247    */

248   public _ExecutionPlan getExecutionPlan() throws DException{
249     _ExecutionPlan cplans[] = new _ExecutionPlan[2];
250     cplans[0] = leftIterator.getExecutionPlan();
251     cplans[1] = rightIterator.getExecutionPlan();
252     return new ExecutionPlan("UnionAllIterator",cplans,null,null,null);
253   }
254
255   /**
256    * This method is responsible to display the iterators hierarchy of a Select Query.
257    * @return ExecutionPlanForBrowser
258    * @throws DException
259    */

260   public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException {
261     ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[2];
262     cplans[0] = leftIterator.getExecutionPlanForBrowser();
263     cplans[1] = rightIterator.getExecutionPlanForBrowser();
264     return new ExecutionPlanForBrowser("Union All","Union All Iterator",cplans,null,null,null);
265   }
266
267   /**
268    * The following methods of this class are used as intermediate in the
269    * iterator hierarchy. These methods simply transfer the call to the
270    * underlying iterator with the same arguments.
271    */

272
273   public byte[] getByteKey() throws DException {
274     byte[] resultantKeys ;
275     switch(state) {
276            case INVALIDSTATE :
277            case BEFOREFIRST :
278            case AFTERLAST :
279               throw new DException("DSE4116",null);
280            case FIRSTISCURRENT :
281               return getKeysInUnion(true,leftIterator,state,direction);
282            case SECONDISCURRENT :
283              return getKeysInUnion(false,rightIterator,state,direction);
284          }
285          return null;
286   }
287
288   public void moveByteKey(byte[] key) throws DException {
289     state = key[0];
290     direction = key[1];
291     short leftLen = CCzufDpowfsufs.getShortValue(key,2);
292     byte[] leftKeys = new byte[leftLen];
293     short rightLen =CCzufDpowfsufs.getShortValue(key,4+leftLen);
294     byte[] rightKeys = new byte[rightLen];
295     if(leftKeys.length != 0){
296       System.arraycopy(key,4,leftKeys,0,leftKeys.length);
297       leftIterator.moveByteKey(leftKeys);
298     }
299     else if(rightKeys.length != 0){
300       System.arraycopy(key ,6+leftKeys.length,rightKeys,0,rightKeys.length);
301       rightIterator.moveByteKey(rightKeys);
302     }
303   }
304
305   private byte[] getKeysInUnion(boolean firstCurrent,_Iterator iter,int state,int direction) throws DException{
306     byte[] keys = iter.getByteKey();
307     byte[] resultantKeys = new byte[keys.length+2+2+2];
308     resultantKeys[0] =(byte) state;
309     resultantKeys[1] =(byte) direction;
310     if(firstCurrent){
311       System.arraycopy(CCzufDpowfsufs.getBytes((short)keys.length),0,resultantKeys,2,2);
312       System.arraycopy(keys,0,resultantKeys,4,keys.length);
313       System.arraycopy(CCzufDpowfsufs.getBytes((short)0),0,resultantKeys,keys.length+4,2);
314     }
315     else{
316       System.arraycopy(CCzufDpowfsufs.getBytes((short)0),0,resultantKeys,2,2);
317      System.arraycopy(CCzufDpowfsufs.getBytes((short)keys.length),0,resultantKeys,4,2);
318       System.arraycopy(keys,0,resultantKeys,6,keys.length);
319     }
320      return resultantKeys;
321   }
322
323   public String JavaDoc toString() {
324         return "UnionAllIterator [" + leftIterator + "] [" + rightIterator + "] ";
325   }
326 }
327
Popular Tags