KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > iterator > condition > NonIndexedFilterIterator


1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.condition;
2
3 import com.daffodilwoods.daffodildb.client.*;
4 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
5 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record;
6 import com.daffodilwoods.daffodildb.server.sql99.common.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
9 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
10 import com.daffodilwoods.daffodildb.utils.comparator.*;
11 import com.daffodilwoods.database.resource.*;
12 import com.daffodilwoods.database.sqlinitiator.*;
13 import com.daffodilwoods.database.utility.*;
14 import java.util.ArrayList JavaDoc;
15
16 /**
17  *
18  * <p>Title: NonIndexedFilterIterator</p>
19  * <p>Description:
20  * This Iterator represents that the condition will be solved with the help of
21  * Table Scan.
22  * In this Iterator, all the rows in the underlying iterator are evaluated with
23  * respect to condition, if condition is satisfied, record is returned,otherwise,
24  * search is continued to find next record in same navigation direction.
25  * </p>
26  * <p>Copyright: Copyright (c) 2003</p>
27  * <p>Company: Daffodil S/W Ltd.</p>
28  * @author unascribed
29  * @version 1.0
30  */

31
32 public class NonIndexedFilterIterator extends BaseSingleIterator implements _TableOperations, _UserTableOperations, _IndexIteratorInfo {
33
34   /**
35    * instance variable representing executable condition.
36    */

37    ConditionVariableValue nonIndexConditionVariableValue;
38    /**
39     * Representing the resultset which is sorted.
40     */

41    _Iterator backUpIterator;
42    /**
43     * state is used to maintain current state of iterator.
44     */

45    int state;
46
47
48    public NonIndexedFilterIterator(_Iterator iterator0, ConditionVariableValue nonIndexConditionVariableValue0) {
49       super(iterator0);
50       nonIndexConditionVariableValue = nonIndexConditionVariableValue0;
51       backUpIterator = iterator0;
52       state = INVALIDSTATE;
53    }
54
55    /**
56     * As this Iterator represents Table Scan, First row is retrived from the
57     * underling iterator by traversing sequentially and condition is evaluated
58     * on this record, If this row violates the condition, next row is retrived
59     * from underlying iterator , untill condition is satisfied. If conditon is
60     * satified for a particular record, this is first record of this iterator.
61     * There may be the condition that record retrieved from underlying iterator
62     * has been deleted from the table. In this case, evaluation of condition will
63     * throw an exception which is ignored and search is processed. Iterator state
64     * is set to valid state if record is retrieved, otherwise state is set to after last.
65     * @return true if record found, false otherwise
66     * @throws DExceptionP.s
67     */

68
69    public boolean first() throws DException {
70       try {
71           try {
72              if (!iterator.first()) {
73                 state = AFTERLAST;
74                 return false;
75              }
76              while (nonIndexConditionVariableValue.run().hashCode() != 0) {
77                 if (!iterator.next()) {
78                    state = AFTERLAST;
79                    return false;
80                 }
81              }
82           } catch (DException ex) {
83              if (ex.getDseCode().equalsIgnoreCase("DSE2004")) {
84                 return next();
85              }
86              throw ex;
87           }
88           state = VALIDSTATE;
89           return true;
90       } catch (DException ex) {
91           throw ex;
92
93
94       }
95    }
96
97    /**
98     * As this Iterator represents Table Scan, Last row is retrived from the
99     * underling iterator by traversing sequentially and condition is evaluated
100     * on this record, If this row violates the condition, previous row is retrived
101     * from underlying iterator , untill condition is satisfied. If conditon is
102     * satified for a particular record, return true. This is last record of this
103     * iterator. If all the records of underlying iterator violates condition, return false.
104     * There may be the condition that record retrieved from underlying iterator
105     * has been deleted from the table. In this case, evaluation of condition will
106     * throw an exception which is ignored and search is processed. Iterator state
107     * is set to valid state if record is retrieved, otherwise state is set to after last.
108     * @return true if record found, false otherwise
109     * @throws DException
110     */

111
112    public boolean last() throws DException {
113       try {
114          if (!iterator.last()) {
115             state = BEFOREFIRST;
116             return false;
117          } while (nonIndexConditionVariableValue.run().hashCode() != 0) {
118             if (!iterator.previous()) {
119                state = BEFOREFIRST;
120                return false;
121             }
122          }
123       } catch (DException ex) {
124          if (ex.getDseCode().equalsIgnoreCase("DSE2004")) {
125             return previous();
126          }
127          throw ex;
128       }
129       state = VALIDSTATE;
130       return true;
131    }
132
133    /**
134     * As this Iterator represents Table Scan, Next row is retrived from the
135     * underling iterator by traversing sequentially and condition is evaluated
136     * on this record, If this row violates the condition, next row is retrived
137     * from underlying iterator , untill condition is satisfied. If conditon is
138     * satified for a particular record, return true. This is next record of this
139     * iterator. If all the records of underlying iterator violates condition, return false.
140     * There may be the condition that record retrieved from underlying iterator
141     * has been deleted from the table. In this case, evaluation of condition will
142     * throw an exception which is ignored and search is processed. Iterator state
143     * is set to valid state if record is retrieved, otherwise state is set to after last.
144     * @return true if record found, false otherwise
145     * @throws DException
146     */

147
148    public boolean next() throws DException {
149       try {
150          do {
151             if (!iterator.next()) {
152                state = AFTERLAST;
153                return false;
154             }
155          } while (nonIndexConditionVariableValue.run().hashCode() != 0);
156       } catch (DException ex) {
157          if (ex.getDseCode().equalsIgnoreCase("DSE2004")) {
158             return next();
159          }
160          throw ex;
161       }
162       state = VALIDSTATE;
163       return true;
164    }
165
166    /**
167     * As this Iterator represents Table Scan, Previous row is retrived from the
168     * underling iterator by traversing sequentially and condition is evaluated
169     * on this record, If this row violates the condition, previous row is retrived
170     * from underlying iterator , untill condition is satisfied. If conditon is
171     * satified for a particular record, return true. This is previous record of this
172     * iterator. If all the records of underlying iterator violates condition, return false.
173     * There may be the condition that record retrieved from underlying iterator
174     * has been deleted from the table. In this case, evaluation of condition will
175     * throw an exception which is ignored and search is processed. Iterator state
176     * is set to valid state if record is retrieved, otherwise state is set to after last.
177     * @return true if record found, false otherwise
178     * @throws DException
179     */

180
181    public boolean previous() throws DException {
182        int result = 0;
183        try {
184          do {
185            if (!iterator.previous()) {
186              state = BEFOREFIRST;
187              return false;
188            }
189          }
190          while (nonIndexConditionVariableValue.run().hashCode() != 0);
191        }
192        catch (DException ex) {
193          if (ex.getDseCode().equalsIgnoreCase("DSE2004")) {
194            return previous();
195          }
196          throw ex;
197        }
198        state = VALIDSTATE;
199        return true;
200    }
201
202    /**
203     * This method is used to retrieve the value of passed reference.
204     * @param leftColumnReferences references for which values is to be retrived.
205     * References may be columns or parameters.
206     * @return NonShared FieldBases denoting the value of References. Non Shared
207     * FieldBases are those for which BufferRange is not shared with some other
208     * FieldBase.
209     * @throws DException
210     */

211    public Object JavaDoc getColumnValues(_Reference[] leftColumnReferences) throws DException {
212      return iterator.getColumnValues(leftColumnReferences);
213    }
214
215    public String JavaDoc toString() {
216       String JavaDoc str = "NONINDEXEDFILTERITERATOR[";
217       str += iterator + "]";
218       str += iterator.getClass() + "]";
219       str += nonIndexConditionVariableValue + "]";
220       return str;
221    }
222
223    /**
224     * This method helps in setting the value of parameters wherever they are
225     * placed in the hierarchy of Iterators.
226     * @param references
227     * @param values
228     * @param priority
229     * @throws DException
230     */

231    public void setConditionVariableValue(_Reference[] references, Object JavaDoc[] values, int priority) throws DException {
232      if(underlyingRef!=null){
233        /*following code added for replacing underlying iterator passed from above. For details
234        see bug number 13178
235         */

236
237        ArrayList JavaDoc urefs = new ArrayList JavaDoc();
238        for (int i = 0; i < underlyingRef.length; i++) {
239            urefs.add(underlyingRef[i]);
240        }
241        if( values !=null){
242            Object JavaDoc[] newValues = new Object JavaDoc[values==null?0:values.length];
243            System.arraycopy(values, 0, newValues, 0, values.length);
244            values = newValues;
245        }
246
247        if (references != null) {
248            for (int i = 0; i < underlyingRef.length; i++) {
249                for (int j = 0; j < references.length; j++) {
250                    if (references[j] == underlyingRef[i]) {
251                        values[j] = this;
252                        urefs.remove(underlyingRef[i]);
253                    }
254                }
255            }
256        }
257
258
259        if (underlyingRef.length != urefs.size()) { // Means some urefs are removed
260
if( urefs.size() > 0 ){
261                references = GeneralPurposeStaticClass.getJointReferences(
262                        references, (_Reference[]) urefs.toArray(new _Reference[0]));
263                values = GeneralPurposeStaticClass.getJointValues(this, values,
264                        urefs.size());
265            }
266
267        } else { // No urefs are removed
268
references = GeneralPurposeStaticClass.getJointReferences(
269                    references, underlyingRef);
270            values = GeneralPurposeStaticClass.getJointValues(this, values,underlyingRef.length);
271        }
272
273      }
274      this.nonIndexConditionVariableValue.setVariableValues(references, values, priority);
275      iterator.setConditionVariableValue(references, values, priority);
276    }
277
278    /**
279     * Used to search a record in the table while traversing from bottom to top.
280     * Search is performed from previous position of current record of the underlying iterator .
281     * While seeking the records condition is evaluated for each record retrieved
282     * from underlying iterator. Records are seeked upto the top of underlying
283     * iterator by traversing sequencially. If the record is found, returns true,
284     * and maintains the current position.
285     * @param indexKey Condition on which the search is made.
286     * @return if the record is found, returns true, false otherwise.
287     * @throws DException
288     */

289    public boolean seekFromBottomRelative(Object JavaDoc indexKey) throws com.daffodilwoods.database.resource.DException {
290       if (!iterator.seekFromBottomRelative(indexKey)) {
291          return false;
292       } while (nonIndexConditionVariableValue.run().hashCode() != 0) {
293          if (!iterator.previous()) {
294             state = BEFOREFIRST;
295             return false;
296          }
297       }
298       return true;
299    }
300
301    /**
302     * used to retreive the current position of the iterator
303     * @return the current position of the iterator.
304     * @throws com.daffodilwoods.database.resource.DException
305     */

306    public Object JavaDoc getKey() throws com.daffodilwoods.database.resource.DException {
307       return iterator.getKey();
308    }
309
310    /**
311     * Used to set the pointer of the iterator to the given position.
312     * @param key the key to which the pointer is to be moved.
313     * @throws DException
314     */

315    public void move(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.DException {
316       iterator.move(parm1);
317       state = VALIDSTATE;
318    }
319
320    /**
321     * This method is used to retrieve the values of column indexed passed.
322     * @param parm1 column indexes of table for which values are to be retrieved
323     * @return NonShared FieldBases denoting the value of References. Non Shared
324     * FieldBases are those for which BufferRange is not shared with some other
325     * FieldBase.
326     * @throws DException
327     */

328    public Object JavaDoc getColumnValues(int[] parm1) throws com.daffodilwoods.database.resource.DException {
329       return iterator.getColumnValues(parm1);
330    }
331
332    /**
333     * Used to get the values of the record at the current position of the iterator.
334     * @return _Record.
335     * @throws DException
336     */

337    public _Record getRecord() throws com.daffodilwoods.database.resource.DException {
338       return iterator.getRecord();
339    }
340
341    /**
342     * This method is used to retrieve all the values of the row at current position.
343     * @return NonShared FieldBases denoting the value of References. Non Shared
344     * FieldBases are those for which BufferRange is not shared with some other
345     * FieldBase.
346     * @throws DException throws an exception if iterator is not at valid position
347     */

348    public Object JavaDoc getColumnValues() throws DException {
349       return iterator.getColumnValues();
350    }
351
352    /**
353     * This method is required to obtain the tables on which record's navigation
354     * is provided by this Iterator.
355     * @return TableDetails representing underlying tables.
356     * @throws DException
357     */

358    public TableDetails[] getTableDetails() throws com.daffodilwoods.database.resource.DException {
359       return iterator.getTableDetails();
360    }
361
362    /**
363     * This method allows a user to get the lowest level iterator of the passed
364     * column.
365     * @param column
366     * @return
367     * @throws DException
368     */

369    public _Iterator getBaseIterator(ColumnDetails column) throws com.daffodilwoods.database.resource.DException {
370       return iterator.getBaseIterator(column);
371    }
372
373    public _KeyColumnInformation[] getKeyColumnInformations() throws DException {
374       return iterator.getKeyColumnInformations();
375    }
376
377    /**
378     * Used to search a record in the table while traversing from top to bottom.
379     * Search is performed from the next position of current record of the underlying iterator .
380     * While seeking the records condition is evaluated for each record retrieved
381     * from underlying iterator. Records are seeked upto the bottom of underlying
382     * iterator by traversing sequencially. If the record is found, returns true,
383     * and maintains the current position.
384     * @param condition Condition on which the search is made.
385     * @return if the record is found, returns true, false otherwise.
386     * @throws DException
387     */

388    public boolean seekFromTopRelative(Object JavaDoc indexKey) throws com.daffodilwoods.database.resource.DException {
389       if (!iterator.seekFromTopRelative(indexKey)) {
390          return false;
391       } while (nonIndexConditionVariableValue.run().hashCode() != 0) {
392          if (!iterator.next()) {
393             state = AFTERLAST;
394             return false;
395          }
396       }
397       return true;
398    }
399
400    /**
401     * This method is used to retrieve the value of passed reference.
402     * @param leftColumnReferences reference for which value is to be retrived.
403     * Reference may be column or parameter.
404     * @return NonShared FieldBases denoting the value of References. Non Shared
405     * FieldBases are those for which BufferRange is not shared with some other
406     * FieldBase.
407     * @throws DException
408     */

409    public Object JavaDoc getColumnValues(_Reference references) throws DException {
410       return iterator.getColumnValues(references);
411    }
412
413    public _ExecutionPlan getExecutionPlan() throws DException {
414       _ExecutionPlan cplan = iterator.getExecutionPlan();
415       _ExecutionPlan cplans[] = cplan == null ? null : new _ExecutionPlan[] {cplan};
416       ExecutionPlan plan = new ExecutionPlan("NonIndexedFilterIterator", cplans, "" + nonIndexConditionVariableValue, null, null);
417       return plan;
418    }
419
420    public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException {
421       ExecutionPlanForBrowser cplan = iterator.getExecutionPlanForBrowser();
422       if ( ("" + nonIndexConditionVariableValue).trim().length() == 0) {
423          return cplan;
424       }
425       ExecutionPlanForBrowser cplans[] = cplan == null ? null : new ExecutionPlanForBrowser[] {cplan};
426       ExecutionPlanForBrowser plan = new ExecutionPlanForBrowser("Filter " + nonIndexConditionVariableValue, "NonIndexedFilterIterator", cplans, null, null, null);
427       return plan;
428    }
429
430    /**
431     * This method is required when we want to share same iterator with different
432     * values of Parameters. Removes the initialization done for the previous
433     * values of parameters.
434     * @throws DException
435     */

436    public void releaseResource() throws DException {
437       super.releaseResource();
438       nonIndexConditionVariableValue.releaseResource();
439    }
440
441
442    /**
443     * The following methods of this class are used a intermediate in the
444     * iterator hierarchy. These methods simply transfer the call to the
445     * underlying iterator with the same arguments.
446     */

447
448
449    public void insert(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.DException {
450       ( (_TableOperations) iterator).insert(parm1);
451    }
452
453    public void update(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.DException {
454       ( (_TableOperations) iterator).update(parm1);
455    }
456
457    public void update(int[] parm1, Object JavaDoc[] parm2) throws com.daffodilwoods.database.resource.DException {
458       ( (_TableOperations) iterator).update(parm1, parm2);
459    }
460
461    public void delete() throws com.daffodilwoods.database.resource.DException {
462       ( (_TableOperations) iterator).delete();
463    }
464
465    public void insert(_DatabaseUser parm1, Object JavaDoc parm2) throws com.daffodilwoods.database.resource.DException {
466       ( (_UserTableOperations) iterator).insert(parm1, parm2);
467    }
468
469    public void update(_DatabaseUser parm1, Object JavaDoc parm2) throws com.daffodilwoods.database.resource.DException {
470       ( (_UserTableOperations) iterator).update(parm1, parm2);
471    }
472
473    public void update(_DatabaseUser parm1, int[] parm2, Object JavaDoc[] parm3) throws com.daffodilwoods.database.resource.DException {
474       ( (_UserTableOperations) iterator).update(parm1, parm2, parm3);
475    }
476
477    public void delete(_DatabaseUser parm1) throws com.daffodilwoods.database.resource.DException {
478       ( (_UserTableOperations) iterator).delete(parm1);
479    }
480
481
482    public int getBtreeIndex() throws com.daffodilwoods.database.resource.DException {
483       return ( (_IndexIteratorInfo) iterator).getBtreeIndex();
484    }
485
486    public _OrderCount getOrderCounts() throws DException {
487       return iterator.getOrderCounts();
488    }
489
490    public _Order getDefaultOrder() throws DException {
491       return iterator.getDefaultOrder();
492    }
493
494    public boolean locateKey(Object JavaDoc parm1, boolean parm2) throws com.daffodilwoods.database.resource.DException {
495       if ( ( (_IndexIteratorInfo) iterator).locateKey(parm1, parm2)) {
496          return nonIndexConditionVariableValue.run().hashCode() != 0 ?
497              parm2 ? next() : previous() : true;
498       }
499       return false;
500    }
501
502    public void ensureRecordInMemory() throws com.daffodilwoods.database.resource.DException {
503       ( (_IndexIteratorInfo) iterator).ensureRecordInMemory();
504    }
505
506    public void moveOnActualKey(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.DException {
507       ( (_IndexIteratorInfo) iterator).moveOnActualKey(parm1);
508    }
509
510    public Object JavaDoc getActualKey() throws com.daffodilwoods.database.resource.DException {
511       return ( (_IndexIteratorInfo) iterator).getActualKey();
512    }
513
514    public void setKeyCount(Object JavaDoc[][] tableAndKeyCount) throws DException {
515       iterator.setKeyCount(tableAndKeyCount);
516    }
517
518    public boolean seekKeyAddress(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.DException {
519       throw new java.lang.UnsupportedOperationException JavaDoc("Method seekKeyAddress() not yet implemented.");
520    }
521
522    public boolean seekFromBottomAbsolute(_IndexPredicate parm1) throws com.daffodilwoods.database.resource.DException {
523       return iterator.seekFromBottomRelative(parm1);
524    }
525
526    public Object JavaDoc getPhysicalAddress() throws com.daffodilwoods.database.resource.DException {
527       return ( (_IndexIteratorInfo) iterator).getPhysicalAddress();
528    }
529
530    public _Iterator getForeignKeyIterator() throws DException {
531       return iterator;
532    }
533
534    public Object JavaDoc[] getUniqueColumnReference() throws DException {
535       return backUpIterator.getUniqueColumnReference();
536    }
537
538    public boolean seek(Object JavaDoc indexKey) throws DException {
539       return iterator.seek(indexKey);
540    }
541
542    public SuperComparator getComparator() {
543       return ( (_IndexIteratorInfo) iterator).getComparator();
544    }
545
546    public SuperComparator getObjectComparator() throws com.daffodilwoods.database.resource.DException {
547       return ( (_IndexIteratorInfo) iterator).getObjectComparator();
548    }
549    public byte[] getByteKey() throws DException {
550   return iterator.getByteKey();
551  }
552  public void moveByteKey(byte[] key) throws DException {
553    iterator.moveByteKey(key);
554    state = VALIDSTATE;
555  }
556  public void deleteBlobClobRecord(_DatabaseUser user) throws com.daffodilwoods.database.resource.DException {
557      ( (_TableOperations) iterator).deleteBlobClobRecord(user) ;
558   }
559
560
561 }
562
Popular Tags