KickJava   Java API By Example, From Geeks To Geeks.

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


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.iterator.*;
8 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
9 import com.daffodilwoods.daffodildb.utils.comparator.*;
10 import com.daffodilwoods.database.resource.*;
11 import com.daffodilwoods.database.sqlinitiator.*;
12 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
13 import com.daffodilwoods.database.utility.P;
14
15 /**
16  * <p>Title: IndexedFilterIterator</p>
17  * <p>Description: This Iterator represents that the condition will be solved
18  * with the help of index seek instead of table scan.
19  * It provides those records which are satisfied by the condition present in it
20  * and also binary search is used to check the records.
21  * </p>
22  * <p>Copyright: Copyright (c) 2003</p>
23  * <p>Company: Daffodil S/W Ltd.</p>
24  * @author unascribed
25  * @version 1.0
26  */

27
28 public class IndexedFilterIterator
29     extends BaseSingleIterator
30     implements _TableOperations, _UserTableOperations, _IndexIteratorInfo {
31
32   /**
33    * instance variable representing executable condition.
34    */

35   ConditionVariableValue conditionVariableValue;
36   /**
37    * Representing the array of executable condition where each element is
38    * executable condition for correspong column of the index.
39    */

40   _IndexPredicate[] indexPredicate;
41   /**
42    * Representing the resultset which is sorted.
43    */

44   private _Iterator backUpIterator;
45   /**
46    * state is used to maintain current state of iterator.
47    */

48
49   private int state;
50
51
52   public IndexedFilterIterator(_Iterator indexedIterator,
53                                ConditionVariableValue conditionVariableValue,
54                                _IndexPredicate[] indexArray) {
55     super(indexedIterator);
56     this.conditionVariableValue = conditionVariableValue;
57     this.indexPredicate = indexArray;
58     backUpIterator = indexedIterator;
59     state = INVALIDSTATE;
60   }
61
62   /**
63    * As this Iterator represents Index Seek, First record is retrieved from the
64    * underlying iterator using seeking. The underlying Iterator represents a
65    * sorted resultset. Condition is used internally to verify the record validation.
66    * Iterator state is set to valid state if record is retrieved, otherwise,
67    * state is set to after last.
68    * @return true if record found, false otherwise
69    * @throws DException
70    */

71
72   public boolean first() throws DException {
73     boolean flag = iterator.seekFromTop(indexPredicate);
74     state = flag ? VALIDSTATE : AFTERLAST;
75
76     return flag;
77   }
78
79   /**
80    * As this Iterator represents Index Seek, Last record is retrieved from the
81    * underlying iterator using seeking. The underlying Iterator represents a
82    * sorted resultset. Condition is used internally to verify the record validation.
83    * Iterator state is set to valid state if record is retrieved, otherwise,
84    * state is set to before first.
85    * @return true if record found, false otherwise
86    * @throws DException
87    */

88
89   public boolean last() throws DException {
90     boolean flag = iterator.seekFromBottom(indexPredicate);
91     state = flag ? VALIDSTATE : BEFOREFIRST;
92     return flag;
93   }
94
95   /**
96    * Next method evaluates condition for next record . If condition evaluates
97    * to false, though underlying iterator may has more records, once condition
98    * is dissatisfed, thereafter, no more row will satisfy condition (since the
99    * underlying Iterator represents a sorted resultset).
100    * There may be the condition that the record retrieved from underlying iterator
101    * has been deleted from the table. In this case, evaluation of condition will
102    * throw an exception which is ignored and search is processed. Iterator state
103    * is set to valid state if record is retrieved, otherwise state is set to after last.
104    * @return true if record found, false otherwise
105    * @throws DException
106    */

107
108   public boolean next() throws DException {
109     switch (state) {
110       case INVALIDSTATE:
111         throw new DException("DSE4116", null);
112       case BEFOREFIRST:
113         return first();
114       case AFTERLAST:
115         return false;
116     }
117     while (true) {
118       boolean next = iterator.next();
119       try {
120         next = next ? conditionVariableValue.run().hashCode() == 0 : next;
121         state = next ? VALIDSTATE : AFTERLAST;
122       }
123       catch (DException de) {
124         if (de.getDseCode().equalsIgnoreCase("DSE2004"))
125           continue;
126         else {
127          ;//// Removed By Program ** System.out.println(" Inside IndexedFilterIterator condition " +
128
throw de;
129         }
130       }
131       return next;
132     }
133   }
134
135   /**
136    * Previous method evaluates condition for previous record. If condition evaluates
137    * to false, through underlying iterator may has more records, once condition
138    * is dissatisfed, thereafter, no more row will satisfy condition (since the
139    * underlying Iterator represents a sorted resultset).
140    * There may be the condition that the 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 before first.
144    * @return true if record found, false otherwise
145    * @throws DException
146    */

147
148   public boolean previous() throws DException {
149     switch (state) {
150       case INVALIDSTATE:
151         throw new DException("DSE4117", null);
152       case BEFOREFIRST:
153         return false;
154       case AFTERLAST:
155         return last();
156     }
157     while (true) {
158       boolean prev = iterator.previous();
159       try {
160         prev = prev ? conditionVariableValue.run().hashCode() == 0 : prev;
161         state = prev ? VALIDSTATE : BEFOREFIRST;
162       }
163       catch (DException de) {
164         if (de.getDseCode().equalsIgnoreCase("DSE2004"))
165           continue;
166         else
167           throw de;
168       }
169       return prev;
170     }
171   }
172
173   /**
174    * This method is used to retrieve the value of passed reference.
175    * @param leftColumnReferences references for which values is to be retrived.
176    * References may be columns or parameters.
177    * @return NonShared FieldBases denoting the value of References. Non Shared
178    * FieldBases are those for which BufferRange is not shared with some other
179    * FieldBase.
180    * @throws DException
181    */

182
183   public Object JavaDoc getColumnValues(_Reference[] leftColumnReferences) throws
184       DException {
185     if (state != VALIDSTATE)
186       throw new DException("DSE2019", new Object JavaDoc[] {new Integer JavaDoc(state)});
187     try {
188       return iterator.getColumnValues(leftColumnReferences);
189     }
190     catch (ArrayIndexOutOfBoundsException JavaDoc E) {
191       throw E;
192     }
193   }
194
195   public String JavaDoc toString() {
196     String JavaDoc str = "INDEXEDFILTERITERATOR";
197     str += "[" + iterator + "][ConditionVariableValues" +
198         conditionVariableValue + "]";
199     return str;
200   }
201
202   /**
203    * This method helps in setting the value of parameters wherever they are
204    * placed in the hierarchy of Iterators.
205    * @param references
206    * @param values
207    * @param priority
208    * @throws DException
209    */

210   public void setConditionVariableValue(_Reference[] references,
211                                         Object JavaDoc[] values, int priority) throws
212       DException {
213     if(underlyingRef!=null){
214       references = GeneralPurposeStaticClass.getJointReferences(references,underlyingRef);
215       values = GeneralPurposeStaticClass.getJointValues(this, values,underlyingRef.length);
216     }
217
218     super.setConditionVariableValue(references, values, priority);
219     conditionVariableValue.setVariableValues(references, values, priority);
220     for (int i = 0; i < indexPredicate.length; i++) {
221       ( (ConditionVariableValue) indexPredicate[i]).setVariableValues(references, values, priority);
222     }
223   }
224
225   /**
226    * Used to search the particular record from the table while traversing from
227    * top to bottom. Search is performed from next position of current record
228    * of the underlying iterator .
229    * While seeking the records condition is evaluated to check
230    * validity of the record. Once the condition is violated seek method returns
231    * false since the iterater is indexed on value to be seeked.
232    * @param indexKey Condition on which the search is made.
233    * @return true if the record or a record close to that record is found.
234    * @throws com.daffodilwoods.database.resource.DException
235    */

236   public boolean seekFromTopRelative(Object JavaDoc indexKey) throws com.daffodilwoods.
237
      database.resource.DException {
238     while (true) {
239       boolean next = iterator.seekFromTopRelative(indexKey);
240
241       try {
242         next = next ? conditionVariableValue.run().hashCode() == 0 : next;
243         state = next ? VALIDSTATE : AFTERLAST;
244       }
245       catch (DException de) {
246         if (de.getDseCode().equalsIgnoreCase("DSE2004"))
247           continue;
248         else
249           throw de;
250       }
251       return next;
252     }
253   }
254
255   /**
256    * Used to search a record in the table while traversing from bottom to top.
257    * Search is performed from previous position of current record of the underlying iterator .
258    * While seeking the records condition is evaluated to check validity of the
259    * record. Once the condition is violated seek method returns false since the
260    * iterater is indexed on value to be seeked.
261    * @param condition Condition on which the search is made.
262    * @return true if the record is found in the table
263    * @throws DException
264    */

265   public boolean seekFromBottomRelative(Object JavaDoc indexKey) throws com.
266
      daffodilwoods.database.resource.DException {
267     while (true) {
268       boolean prev = iterator.seekFromBottomRelative(indexKey);
269       try {
270         prev = prev ? conditionVariableValue.run().hashCode() == 0 : prev;
271         state = prev ? VALIDSTATE : BEFOREFIRST;
272       }
273       catch (DException de) {
274         if (de.getDseCode().equalsIgnoreCase("DSE2004"))
275           continue;
276         else
277           throw de;
278       }
279       return prev;
280     }
281   }
282
283   /**
284    * used to retreive the current position of the iterator
285    * @return the current position of the iterator.
286    * @throws com.daffodilwoods.database.resource.DException
287    */

288   public Object JavaDoc getKey() throws com.daffodilwoods.database.resource.DException {
289     return iterator.getKey();
290   }
291
292   /**
293    * Used to set the pointer of the iterator to the given position.
294    * @param key the key to which the pointer is to be moved.
295    * @throws DException
296    */

297   public void move(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.
298
      DException {
299     iterator.move(parm1);
300     state = VALIDSTATE;
301   }
302
303   /**
304    * This method is used to retrieve the values of column indexed passed.
305    * @param parm1 column indexes of table for which values are to be retrieved
306    * @return NonShared FieldBases denoting the value of References. Non Shared
307    * FieldBases are those for which BufferRange is not shared with some other
308    * FieldBase.
309    * @throws DException
310    */

311   public Object JavaDoc getColumnValues(int[] parm1) throws com.daffodilwoods.database.
312
      resource.DException {
313     return iterator.getColumnValues(parm1);
314   }
315
316   /**
317    * This method is used to retrieve all the values of the row at current position.
318    * @return NonShared FieldBases denoting the value of References. Non Shared
319    * FieldBases are those for which BufferRange is not shared with some other
320    * FieldBase.
321    * @throws DException throws an exception if iterator is not at valid position
322    */

323   public Object JavaDoc getColumnValues() throws DException {
324     if (state != VALIDSTATE)
325       throw new DException("DSE2019", new Object JavaDoc[] {new Integer JavaDoc(state)});
326     return iterator.getColumnValues();
327   }
328
329   /**
330    * This method is required to obtain the tables on which record's navigation
331    * is provided by this Iterator.
332    * @return TableDetails representing underlying tables.
333    * @throws DException
334    */

335   public TableDetails[] getTableDetails() throws com.daffodilwoods.database.
336
      resource.DException {
337     return iterator.getTableDetails();
338   }
339
340   /**
341    * This method allows a user to get the lowest level iterator of the passed
342    * column.
343    * @param column
344    * @return
345    * @throws DException
346    */

347   public _Iterator getBaseIterator(ColumnDetails column) throws com.
348
      daffodilwoods.database.resource.DException {
349     return iterator.getBaseIterator(column);
350   }
351
352   public _KeyColumnInformation[] getKeyColumnInformations() throws DException {
353     _KeyColumnInformation[] key = iterator.getKeyColumnInformations();
354     return key;
355   }
356
357   /**
358    * This method is used to retrieve the value of passed reference.
359    * @param leftColumnReferences reference for which value is to be retrived.
360    * Reference may be column or parameter.
361    * @return NonShared FieldBases denoting the value of References. Non Shared
362    * FieldBases are those for which BufferRange is not shared with some other
363    * FieldBase.
364    * @throws DException
365    */

366   public Object JavaDoc getColumnValues(_Reference references) throws DException {
367     if (state != VALIDSTATE)
368       throw new DException("DSE2019", new Object JavaDoc[] {new Integer JavaDoc(state)});
369     try {
370       return iterator.getColumnValues(references);
371     }
372     catch (ArrayIndexOutOfBoundsException JavaDoc E) {
373       throw E;
374     }
375   }
376
377   public _ExecutionPlan getExecutionPlan() throws DException {
378     _ExecutionPlan cplan = iterator.getExecutionPlan();
379     _ExecutionPlan cplans[] = cplan == null ? null : new _ExecutionPlan[] {
380         cplan};
381     int length = indexPredicate == null ? 0 : indexPredicate.length;
382     String JavaDoc condition = "Index Condition : ";
383     for (int i = 0; i < length; i++) {
384       condition += indexPredicate[i] + " , ";
385     }
386
387     condition += " And Non Index Condition : ";
388     ExecutionPlan plan = new ExecutionPlan("IndexedFilterIterator", cplans,
389                                            condition + conditionVariableValue, null, null);
390     return plan;
391   }
392
393   public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException {
394     ExecutionPlanForBrowser cplan = iterator.getExecutionPlanForBrowser();
395     ExecutionPlanForBrowser cplans[] = cplan == null ? null :
396         new ExecutionPlanForBrowser[] {
397         cplan};
398     int length = indexPredicate == null ? 0 : indexPredicate.length;
399     String JavaDoc condition = "";
400     for (int i = 0; i < length; i++) {
401       condition += indexPredicate[i] + " , ";
402     }
403     ExecutionPlanForBrowser plan = new ExecutionPlanForBrowser("Index Seek " +
404         condition, "IndexedFilterIterator", cplans, "" + conditionVariableValue, null, null);
405     return plan;
406   }
407
408   /**
409    * This method is required when we want to share same iterator with different
410    * values of Parameters. Removes the initialization done for the previous
411    * values of parameters.
412    * @throws DException
413    */

414   public void releaseResource() throws DException {
415     super.releaseResource();
416     conditionVariableValue.releaseResource();
417     for (int i = 0; i < indexPredicate.length; i++) {
418       ( (ConditionVariableValue) indexPredicate[i]).releaseResource();
419     }
420   }
421
422   /**
423    * Used to get the values of the record at the current position of the iterator.
424    * @return _Record.
425    * @throws DException
426    */

427   public _Record getRecord() throws com.daffodilwoods.database.resource.
428
      DException {
429     return iterator.getRecord();
430   }
431
432   /**
433    * The following methods of this class are used a intermediate in the
434    * iterator hierarchy. These methods simply transfer the call to the
435    * underlying iterator with the same arguments.
436    */

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