KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > interfaces > UnionIterator


1 package com.daffodilwoods.daffodildb.server.datasystem.interfaces;
2
3 import java.util.*;
4
5
6 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*;
7 import com.daffodilwoods.daffodildb.server.datasystem.utility.*;
8 import com.daffodilwoods.daffodildb.server.sql99.common.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
10 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
11 import com.daffodilwoods.daffodildb.utils.comparator.*;
12 import com.daffodilwoods.daffodildb.utils.field.*;
13 import com.daffodilwoods.database.resource.*;
14 import com.daffodilwoods.database.utility.P;
15
16 /**
17  *
18  * <p>Title: UnionIterator</p>
19  * <p>Description:Objective of the union iterator is to manage the operations of both the iterators it has.
20  */

21 public class UnionIterator implements _TableIterator, _UniqueIterator {
22
23     /**
24      * Used to maintain the pointer of the UnionIterator
25      */

26
27     protected _IndexIterator[] iterators;
28     protected Object JavaDoc currentKey;
29     protected SuperComparator comparator;
30     protected int status ;
31
32     protected static final int LENGTH = 2;
33     protected boolean recordLoaded;
34
35     /**
36      * Constructs the Union iterator with an array of two index iterators and an index table.
37      * @param iterators0 is an array of two index iterators one to iterate on the memory table and the other to iterate on file table..
38      * @param table0 is an index table that maintains the operations on memory and the file table.
39      */

40
41     public UnionIterator(_IndexIterator[] iterators0) {
42         iterators = iterators0;
43         for (int i = 0; i < LENGTH ; i++) {
44             if(iterators[i] instanceof _Iterator )
45                 new Exception JavaDoc(" IT'S IMPOSSIBLE ").printStackTrace();
46         }
47         comparator = ((_IndexIteratorInfo)iterators[1]).getComparator();
48         status = BEFOREFIRST;
49     }
50
51     /**
52      * set it's pointer to the first record in the table
53      * @return true if it gets the key corresponding to the first record in the table.
54      */

55
56     public boolean first() throws DException{
57         recordLoaded = false;
58         int hasFirst = 0;
59         _Key[] keys = new _Key[LENGTH];
60         for(int i = 0 ; i < LENGTH ; i++){
61             if(iterators[i].first()){
62                 hasFirst += 1;
63                 keys[i] = (_Key)iterators[i].getKey();
64             }
65         }
66         return hasFirst == 0 ? setKeyAndStatus(BEFOREFIRST,null,false)
67                          : setKeyAndStatus(VALIDSTATE,new UnionKey(getLowestKey(keys),keys,true),true);
68     }
69
70     /**
71      * sets the pointer to the last record in the table
72      * @return true if it gets the key corresponding to the last record in the table.
73      */

74
75     public boolean last() throws DException{
76         recordLoaded = false;
77         int hasLast = 0;
78         _Key[] keys = new _Key[LENGTH];
79         for(int i = 0 ; i < LENGTH ; i++){
80             if(iterators[i].last()){
81                 hasLast += 1;
82                 keys[i] = (_Key)iterators[i].getKey();
83             }
84         }
85         return hasLast == 0 ? setKeyAndStatus(AFTERLAST,null,false)
86                         : setKeyAndStatus(VALIDSTATE,new UnionKey(getHighestKey(keys),keys,false),true);
87     }
88
89
90     /**
91      * sets the pointer to the next record in the table
92      * @return true if it gets the key corresponding to the next record in the table.
93      */

94
95     public boolean next() throws DException{
96         if(status != VALIDSTATE)
97             return status == AFTERLAST ? false : first();
98         recordLoaded = false;
99         UnionKey unionKey = (UnionKey)currentKey;
100         BitSet bitSet = unionKey.getBitSet();
101         int hasNext = 0;
102         _Key[] keys = new _Key[LENGTH];
103         boolean direction = unionKey.getDirection();
104         for(int i = 0 ; i < LENGTH ; i++){
105             if(bitSet.get(i) || !direction){
106                 if(iterators[i].next()){
107                     hasNext++;
108                     keys[i] = (_Key)iterators[i].getKey();
109                 }
110             }
111             else{
112                 hasNext += (keys[i] = (_Key)iterators[i].getKey()) == null ? 0 : 1;
113             }
114         }
115         return hasNext == 0 ? setKeyAndStatus(AFTERLAST,null,false)
116                         : setKeyAndStatus(VALIDSTATE,new UnionKey(getLowestKey(keys),keys,true),true);
117     }
118
119     /**
120      * sets the pointer to the previous record in the table.
121      * @return true if it gets the key corresponding to the previous record in the table.
122      */

123
124     public boolean previous() throws DException{
125         if(status != VALIDSTATE)
126             return status == BEFOREFIRST ? false : last();
127         recordLoaded = false;
128         UnionKey unionKey = (UnionKey)currentKey;
129         BitSet bitSet = unionKey.getBitSet();
130         int hasPrevious = 0;
131         _Key[] keys = new _Key[LENGTH];
132         boolean direction = unionKey.getDirection();
133         for(int i = 0 ; i < LENGTH ; i++){
134             if(bitSet.get(i) || direction ){
135                 if(iterators[i].previous()){
136                     hasPrevious += 1;
137                     keys[i] = (_Key)iterators[i].getKey();
138                 }
139             }
140             else{
141                 keys[i] = (_Key)iterators[i].getKey();
142                 hasPrevious += keys[i] == null ? 0 : 1;
143             }
144         }
145         return hasPrevious == 0 ? setKeyAndStatus(BEFOREFIRST,null,false) : setKeyAndStatus(VALIDSTATE,new UnionKey(getHighestKey(keys),keys,false),true);
146     }
147
148     public Object JavaDoc getKey() throws DException{
149         return currentKey;
150     }
151
152
153     /**
154      * moves the pointer to the given key in the table
155      * @param key Key to which the iterator will move its pointer
156      * @return true true if it gets the record corresponding to the given key in the table.
157      */

158
159     public void move(Object JavaDoc key) throws DException{
160         if(key == null)
161             return;
162         recordLoaded = false;
163         UnionKey unionKey = (UnionKey)key;
164         currentKey = key;
165         status = VALIDSTATE;
166         Object JavaDoc[] keys = unionKey.getKeys();
167         for(int i = 0 ; i < LENGTH ; i++)
168             iterators[i].move(keys[i]);
169     }
170
171     /**
172      * retreives the value of the record at the current pointer from the table.
173      * @param columns array of columns whose value is to be retreived.
174      * @return an array of values corresponding to those columns.
175      */

176
177     public Object JavaDoc getColumnValues(int[] columns) throws DException {
178         if(status != VALIDSTATE){
179             throw new DException("DSE2019",new Object JavaDoc[] {new Integer JavaDoc(status)});
180         }
181         BitSet bitSet = ((UnionKey)currentKey).getBitSet();
182         for(int i = 0 ; i < LENGTH ; i++)
183             if(bitSet.get(i))
184                 return iterators[i].getColumnValues(columns);
185         throw new DException("DSE2047",null);
186     }
187
188     public void show(boolean flag) throws DException{
189         ArrayList list = new ArrayList();
190         if(first()){
191             do{
192                 Object JavaDoc obj = getColumnValues();
193                 if(flag)
194          ;//// Removed By Program ** System.out.println(currentKey+" :::::::: "+Arrays.asList((Object[])obj));
195
list.add(obj);
196             }while(next());
197         }
198         else
199          ;//// Removed By Program ** System.out.println(" No Record In Table ");
200
}
201
202     public _KeyColumnInformation[] getKeyColumnInformations() throws DException {
203         try{
204             return iterators[1].getKeyColumnInformations() ;
205         }
206         catch(NullPointerException JavaDoc npe){
207             return iterators[0].getKeyColumnInformations() ;
208         }
209     }
210
211
212     public int getBtreeIndex() throws DException{
213         return ((IndexTableIterator)iterators[0]).getBtreeIndex();
214     }
215
216
217     public _Record getRecord() throws com.daffodilwoods.database.resource.DException {
218         /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._TableIterator method*/
219         throw new java.lang.UnsupportedOperationException JavaDoc("Method getRecord() not yet implemented.");
220     }
221
222     protected BitSet getLowestKey(Object JavaDoc[] values) throws DException{
223         BitSet flags = new BitSet();
224         if(values[0] == null){
225             flags.set(1);
226             return flags;
227         }
228         else if (values[1] == null){
229             flags.set(0);
230             return flags;
231         }
232         Object JavaDoc keyValues0 = ((_Key)values[0]).getKeyValue();
233         Object JavaDoc keyValues1 = ((_Key)values[1]).getKeyValue();
234         int cmp = comparator.compare(keyValues0,keyValues1);
235         if(cmp == 0){
236             flags.set(0);
237             flags.set(1);
238         }
239         else if(cmp < 0)
240             flags.set(0);
241         else
242             flags.set(1);
243         return flags;
244     }
245
246     protected BitSet getHighestKey(Object JavaDoc[] values) throws DException{
247         BitSet flags = new BitSet();
248         if(values[0] == null){
249             flags.set(1);
250             return flags;
251         }
252         else if (values[1] == null){
253             flags.set(0);
254             return flags;
255         }
256         Object JavaDoc keyValues0 = ((_Key)values[0]).getKeyValue();
257         Object JavaDoc keyValues1 = ((_Key)values[1]).getKeyValue();
258         int cmp = comparator.compare(keyValues0,keyValues1);
259         if(cmp == 0){
260             flags.set(0);
261             flags.set(1);
262         }
263         else if(cmp > 0)
264             flags.set(0);
265         else
266             flags.set(1);
267         return flags;
268     }
269
270
271
272
273
274
275     protected Object JavaDoc getKeyToLocate(Object JavaDoc key,_IndexPredicate[] condition) throws DException{
276         Object JavaDoc keyToLocate = null;
277         try {
278             Object JavaDoc[] kee = (Object JavaDoc[])key;
279             if(kee.length != condition.length){
280                 Object JavaDoc[] temp = new Object JavaDoc[condition.length];
281                 for(int i =0 ; i< temp.length; i++)
282                     temp[i] = kee[i];
283                 keyToLocate = temp;
284             }
285             else
286                 keyToLocate = kee;
287         }
288         catch (ClassCastException JavaDoc ex) {
289             keyToLocate = key;
290         }
291         return keyToLocate;
292     }
293
294     protected boolean setKeyAndStatus(int num,Object JavaDoc key,boolean flag) {
295         status = num;
296         currentKey = key;
297         return flag;
298     }
299
300     protected void setLowestKey() throws DException{
301         int length = iterators.length;
302         _Key[] keys = new _Key[length];
303         for (int i = 0; i < length; i++) {
304             keys[i] = iterators[i] != null ? (_Key)iterators[i].getKey() : null;
305         }
306         BitSet set = getLowestKey(keys);
307         currentKey = set == null ? currentKey : new UnionKey(set,keys,true);
308     }
309
310     protected void setHighestKey() throws DException{
311         int length = iterators.length;
312         _Key[] keys = new _Key[length];
313         for (int i = 0; i < length; i++) {
314             keys[i] = iterators[i] != null ? (_Key)iterators[i].getKey() : null;
315         }
316         BitSet set = getHighestKey(keys);
317         currentKey = set == null ? currentKey : new UnionKey(set,keys,false);
318     }
319 /*
320     public boolean locateKey(Object key, boolean top) throws DException {
321         recordLoaded = false;
322         _Key[] keys = new _Key[LENGTH];
323         int num = 0;
324         for (int i = 0; i < LENGTH ; i++) {
325             boolean flag = ((_IndexIteratorInfo)iterators[i]).locateKey(key,top);
326             if(flag){
327                 keys[i] = (_Key)iterators[i].getKey();
328                 num++;
329             }
330         }
331         return num == 0 ? setKeyAndStatus(top ? AFTERLAST : BEFOREFIRST,null,false)
332                     : setKeyAndStatus(VALIDSTATE,new UnionKey(getLowestKey(keys),keys,top),true);
333     }
334 */

335     protected int compareToGetLowest(Object JavaDoc indexKey, Object JavaDoc otherKey)throws DException{
336         return indexKey == null ? -1 : otherKey == null ? 1 : comparator.compare(indexKey,otherKey);
337     }
338
339     protected int compareToGetHighest(Object JavaDoc indexKey, Object JavaDoc otherKey)throws DException{
340         return indexKey == null ? 1 : otherKey == null ? -1 : comparator.compare(indexKey,otherKey);
341     }
342
343     public Object JavaDoc getColumnValues(int column) throws com.daffodilwoods.database.resource.DException {
344         if(status != VALIDSTATE)
345             throw new DException("DSE2019",new Object JavaDoc[] {new Integer JavaDoc(status)});
346         BitSet bitSet = ((UnionKey)currentKey).getBitSet();
347         for(int i = 0 ; i < LENGTH ; i++)
348             if(bitSet.get(i))
349                 return iterators[i].getColumnValues(column);
350         throw new DException("DSE2047",null);
351     }
352
353     public Object JavaDoc getColumnValues() throws com.daffodilwoods.database.resource.DException {
354         if(status != VALIDSTATE)
355             throw new DException("DSE2019",new Object JavaDoc[] {new Integer JavaDoc(status)});
356         BitSet bitSet = ((UnionKey)currentKey).getBitSet();
357         for(int i = 0 ; i < LENGTH ; i++)
358             if(bitSet.get(i))
359                 return iterators[i].getColumnValues();
360         throw new DException("DSE2047",null);
361     }
362
363
364     public void moveOnActualKey(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.DException {
365         /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIteratorInfo method*/
366         throw new java.lang.UnsupportedOperationException JavaDoc("Method moveOnActualKey() not yet implemented.");
367     }
368     public Object JavaDoc getActualKey() throws com.daffodilwoods.database.resource.DException {
369         /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIteratorInfo method*/
370         throw new java.lang.UnsupportedOperationException JavaDoc("Method getActualKey() not yet implemented.");
371     }
372
373
374
375     public _TableCharacteristics getTableCharacteristics() throws DException {
376         /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._UniqueIterator method*/
377         throw new java.lang.UnsupportedOperationException JavaDoc("Method getTableCharacteristics() not yet implemented.");
378     }
379
380     public _IndexInformation[] getUniqueInformation() throws DException {
381         /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._UniqueIterator method*/
382         throw new java.lang.UnsupportedOperationException JavaDoc("Method getUniqueInformation() not yet implemented.");
383     }
384
385
386
387
388     public Object JavaDoc getColumnValues(_Reference reference) throws com.daffodilwoods.database.resource.DException {
389         if(status != VALIDSTATE)
390             throw new DException("DSE2019",new Object JavaDoc[] {new Integer JavaDoc(status)});
391         BitSet bitSet = ((UnionKey)currentKey).getBitSet();
392         for(int i = 0 ; i < LENGTH ; i++)
393             if(bitSet.get(i))
394                 return iterators[i].getColumnValues(reference);
395         throw new DException("DSE2047",null);
396     }
397
398     public Object JavaDoc getColumnValues(_Reference[] references) throws com.daffodilwoods.database.resource.DException {
399         if(status != VALIDSTATE){
400             throw new DException("DSE2019",new Object JavaDoc[] {new Integer JavaDoc(status)});
401         }
402         BitSet bitSet = ((UnionKey)currentKey).getBitSet();
403         for(int i = 0 ; i < LENGTH ; i++)
404             if(bitSet.get(i))
405                 return iterators[i].getColumnValues(references);
406         throw new DException("DSE2047",null);
407     }
408
409     protected int getColumn(_Reference references,_TableCharacteristics tc )throws DException{
410         int column = -1;
411         try{
412             column = references.getIndex();
413         }
414         catch(DException de) {
415             if(!de.getDseCode().equalsIgnoreCase("DSE565"))
416                 throw de;
417             column = tc.getIndexForColumnName(references.getColumn());
418             references.setIndex(column);
419         }
420         return column;
421     }
422     public byte[] getByteKey() throws DException{
423       throw new java.lang.UnsupportedOperationException JavaDoc(
424         "Method getByteKey() not yet implemented.");
425  }
426
427  public void moveByteKey(byte[] key) throws DException{
428    throw new java.lang.UnsupportedOperationException JavaDoc(
429         "Method moveByteKey() not yet implemented.");
430      }
431
432 }
433
Popular Tags