KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > iterator > order > MemoryIndexIterator


1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.order;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.information.
6
    _IndexInformation;
7 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*;
8 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
9 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record;
10 import com.daffodilwoods.daffodildb.server.serversystem.*;
11 import com.daffodilwoods.daffodildb.server.sql99.common.*;
12 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
13 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
14 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.
15     orderbyclause.*;
16 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
17 import com.daffodilwoods.daffodildb.utils.*;
18 import com.daffodilwoods.daffodildb.utils.byteconverter.*;
19 import com.daffodilwoods.daffodildb.utils.comparator.*;
20 import com.daffodilwoods.daffodildb.utils.field.*;
21 import com.daffodilwoods.database.general.*;
22 import com.daffodilwoods.database.resource.*;
23 import com.daffodilwoods.database.sqlinitiator.*;
24 import com.daffodilwoods.database.utility.*;
25 import com.daffodilwoods.daffodildb.server.sessionsystem.*;
26 import com.daffodilwoods.daffodildb.server.sql99.dql.resultsetmetadata.*;
27 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.QuerySpecificationPlan;
28
29
30 public class MemoryIndexIterator
31     extends BaseSingleIterator
32     implements _TableOperations, _UserTableOperations, _IndexIteratorInfo {
33
34   /**Represent the keyColunmnInformation of this iterator*/
35   private _KeyColumnInformation keyColumnInformation[];
36
37   /**This represent the order in which this iterator returns the values */
38   private _ExpressionOrderValues order;
39
40   /**Represent the comparator for Order .It is initialized to Sortcomparator in the
41            constructor */

42   private Comparator comparator;
43
44   /*Maintains hte mapping of Tables and their corrosponding columns .It will be used in returning the values
45         of passed reference(s)
46        mappingOfTableAndColumns[i][0]-- represents the TableDetails
47        mappingOfTableAndColumns[i][1]-- represent the columnNames of tables
48    */

49   private Object JavaDoc mappingOfTableAndColumns[][];
50
51   /**This represent the values in the required order*/
52   private SortKeyEntry sortedList[];
53
54   /**Represents the status of the iterator
55    * -1 ----- Before first
56    * 0 -----Vaild State
57    * 1 ----- After Last
58    * */

59   private int status;
60
61   /**Maintain the current Position of the sortKeyList .*/
62   private int currentPosition;
63
64   private String JavaDoc state;
65
66   private Object JavaDoc[] objectToMove;
67
68
69   private ColumnDetails[] keyColumnDetails;
70   /***/
71   public MemoryIndexIterator(_Iterator iterator0, _ExpressionOrderValues order0) throws
72       DException {
73     super(iterator0);
74     order = order0;
75     currentPosition = -1;
76     sortedList = null;
77     comparator = new SortComparator(order.getComparator());
78     initTableAndColumnMapping();
79      objectToMove=getObjectArray();
80   }
81
82   /**This method align the underLying iterator.It is required in case of delete/Update
83      operation */

84
85   protected void allignUnderLineIterator() throws DException {
86
87     if (status != 0)
88       throw new DException("DSE2019", new Object JavaDoc[] {new Integer JavaDoc(currentPosition)});
89     iterator.move( ( (SortKeyEntry) sortedList[currentPosition]).getValue());
90
91   }
92
93   /*This method initilaize the sortedKeyList to the values from the underLying iterator
94    It take the OrderValues, Key of underLyingIterator and all columnValues of underLying
95    iterator and wrap theses values into 'SortKeyEntry' object.
96    and then we Sort the sortedKeyList using the Arrays.sort() method .In last we initialize the status and currentPosition
97    to -1.
98    */

99   private void constructSortedList() throws DException {
100     ArrayList list = new ArrayList(200);
101     if (iterator.first())
102       do {
103         list.add(new SortKeyEntry(order.getOrderValues(null), iterator.getKey(),
104                                   iterator.getColumnValues()));
105       }
106       while (iterator.next());
107     sortedList = (SortKeyEntry[]) list.toArray(new SortKeyEntry[0]);
108     Arrays.sort(sortedList, comparator);
109     currentPosition = -1;
110     status = -1;
111   }
112
113   /*This method initialize the TableAndColumnMapping .
114     First it get ALL the tables from the underLying iterator and
115    And then get the columnNames from the each table.
116    */

117   private void initTableAndColumnMapping() throws DException {
118     TableDetails[] tables = iterator.getTableDetails();
119     mappingOfTableAndColumns = new Object JavaDoc[tables.length][2];
120     for (int i = 0; i < tables.length; i++) {
121       mappingOfTableAndColumns[i][0] = tables[i];
122       mappingOfTableAndColumns[i][1] = tables[i].getColumnCharacteristics().
123           getColumnNames();
124
125     }
126   }
127
128   /*It maintains the currentPosition and status of the iterator*/
129   private boolean setStatusAndPosition(int status, boolean flag, int position,
130                                        String JavaDoc state) throws DException {
131     this.status = status;
132     this.currentPosition = position;
133     this.state = state;
134     return flag;
135
136   }
137
138   ArrayList alist = new ArrayList();
139   boolean f;
140
141   /**This method get the index of the passed reference .If the index of references is set,It return this in dex ,Otherwise
142    it catch the Exception and get the index from thwe mappingOfTableAndColumns.
143    Then set the index to reference and return the new Index. */

144   private int getIndex(_Reference ref) throws DException {
145     int index = 0;
146     try {
147       index = ref.getIndex();
148       TableDetails td = ( (ColumnDetails) ref).getTable();
149       for (int i = 0; i < mappingOfTableAndColumns.length; i++) {
150         if (td == (TableDetails) mappingOfTableAndColumns[i][0] || ( ( (ColumnDetails) ref).getNameOfTable().equalsIgnoreCase(((TableDetails) mappingOfTableAndColumns[i][0]).getQualifiedTableName())))
151           return index;
152         else
153           index += ( (String JavaDoc[]) mappingOfTableAndColumns[i][1]).length;
154       }
155       for (int j = 0; j < mappingOfTableAndColumns.length; j++) {
156          ;//// Removed By Program ** System.out.println(" Table Mapping :" ); P.p(mappingOfTableAndColumns[j][0]);
157
}
158
159       throw new DException("DSE2032", new Object JavaDoc[] {ref.getColumn()}); //column not found
160
}
161     catch (DException ex) {
162       if (!ex.getDseCode().equalsIgnoreCase("DSE565"))
163         throw ex;
164
165       TableDetails td = ( (ColumnDetails) ref).getTable();
166       for (int i = 0; i < mappingOfTableAndColumns.length; i++) {
167         if (td == (TableDetails) mappingOfTableAndColumns[i][0]) {
168           String JavaDoc refName = ref.getColumn();
169           String JavaDoc[] columnNames = ( (String JavaDoc[]) mappingOfTableAndColumns[i][1]);
170           for (int j = 0; j < columnNames.length; j++) {
171             if (refName.equalsIgnoreCase(columnNames[j])) {
172               index += j;
173               ref.setIndex(j);
174               return index;
175             }
176           }
177           for (int j = 0; j < mappingOfTableAndColumns.length; j++) {
178          ;//// Removed By Program ** System.out.println(" Table Mapping :" ); P.p(mappingOfTableAndColumns[j][0]);
179
}
180
181           throw new DException("DSE2032", new Object JavaDoc[] {ref.getColumn()});
182         }
183         index += ( (String JavaDoc[]) mappingOfTableAndColumns[i][1]).length;
184       }
185
186       for (int i = 0; i < mappingOfTableAndColumns.length; i++) {
187          ;//// Removed By Program ** System.out.println(" Table Mapping :" ); P.p(mappingOfTableAndColumns[i][0]);
188
}
189   throw new DException("DSE2032", new Object JavaDoc[] {ref.getColumn()}); //column not found
190
}
191   }
192
193   /*Return the Array of indexex for the passed references */
194   private int[] getIndexes(_Reference[] refs) throws DException {
195     int[] indexes = new int[refs.length];
196     for (int i = 0; i < refs.length; i++) {
197       indexes[i] = getIndex(refs[i]);
198     }
199     return indexes;
200   }
201
202   private Object JavaDoc getColumnValues1(int index) throws DException {
203     try {
204       return ( (Object JavaDoc[]) sortedList[currentPosition].getColumnValues())[index];
205     }
206     catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
207       if (status != 0)
208         throw new DException("DSE2019",
209                              new Object JavaDoc[] {new Integer JavaDoc(currentPosition)});
210       else {
211         throw new DException("DSE0",
212                              new Object JavaDoc[] {"Invalid column Index " + index});
213       }
214     }
215   }
216
217   private Object JavaDoc getColumnValues1(int columns[]) throws DException {
218     Object JavaDoc[] values = new FieldBase[columns.length];
219     for (int i = 0; i < columns.length; i++) {
220       values[i] = getColumnValues1(columns[i]);
221     }
222     return values;
223
224   }
225
226   public void delete(_DatabaseUser parm1) throws DException {
227     allignUnderLineIterator();
228     ( (_UserTableOperations) iterator).delete(parm1);
229     deleteFromSortedList();
230   }
231
232   public void delete() throws DException {
233     allignUnderLineIterator();
234     ( (_TableOperations) iterator).delete();
235     deleteFromSortedList();
236   }
237
238   public void deleteBlobClobRecord(_DatabaseUser user) throws DException {
239     allignUnderLineIterator();
240     ( (_TableOperations) iterator).deleteBlobClobRecord(user);
241     deleteFromSortedList();
242   }
243
244   public void ensureRecordInMemory() throws DException {
245     allignUnderLineIterator();
246     ( (_IndexIteratorInfo) iterator).ensureRecordInMemory();
247   }
248
249   public FieldBase field(_Reference reference) throws DException {
250     if (status != 0)
251       throw new DException("DSE2019", new Object JavaDoc[] {new Integer JavaDoc(status)});
252     return (FieldBase) getColumnValues1(getIndex(reference));
253   }
254
255   public FieldBase[] fields(_Reference references[]) throws DException {
256     if (status != 0)
257       throw new DException("DSE2019", new Object JavaDoc[] {new Integer JavaDoc(status)});
258
259    return (FieldBase[]) getColumnValues1(getIndexes(references));
260
261   }
262
263   public FieldBase field(int column) throws DException {
264     throw new UnsupportedOperationException JavaDoc(
265         "Method field() not yet implemented.");
266   }
267
268   public FieldBase[] fields(int columns[]) throws DException {
269     throw new UnsupportedOperationException JavaDoc(
270         "Method field() not yet implemented.");
271   }
272
273   public Object JavaDoc getActualKey() throws DException { //need TO ASK
274
return ( (_IndexIteratorInfo) iterator).getActualKey();
275   }
276
277   public void moveOnActualKey(Object JavaDoc parm1) throws DException { //need to ask
278
( (_IndexIteratorInfo) iterator).moveOnActualKey(parm1);
279     move(iterator.getKey());
280   }
281
282   public int getBtreeIndex() throws DException {
283     return ( (_IndexIteratorInfo) iterator).getBtreeIndex();
284   }
285
286   public byte[] getByteKey() throws DException {
287     int resultantLength=0;
288     ArrayList KeyInfo = new ArrayList(5);
289     for(int i=0;i<keyColumnDetails.length;i++){
290       Object JavaDoc key = getColumnValues1(getIndex(keyColumnDetails[i]));
291      FieldBase fbKey1 = (FieldBase) key;
292      if (fbKey1.isNull()) {
293        KeyInfo.add(null);
294      }
295      else {
296        byte[] columnValues = fbKey1.getBufferRange().getBytes();
297        KeyInfo.add(columnValues);
298        resultantLength += columnValues.length;
299      }
300    }
301    return getResultantKeyInfo(resultantLength,KeyInfo);
302    }
303    private byte[] getResultantKeyInfo(int resultantLen,ArrayList KeyInfo){
304       byte[] resultantKeyInfo = new byte[resultantLen+KeyInfo.size()];
305     for(int i=0,j=0;i<KeyInfo.size();i++){
306       byte[] columnValues = (byte[]) KeyInfo.get(i);
307       if (columnValues == null) {
308         resultantKeyInfo[j] = 0;
309         j += 1;
310       }
311       else {
312         resultantKeyInfo[j] = (byte) columnValues.length;
313         System.arraycopy(columnValues, 0, resultantKeyInfo, j + 1,
314                          columnValues.length);
315         j += columnValues.length + 1;
316       }
317     }
318     return resultantKeyInfo;
319
320     }
321
322   public Object JavaDoc getColumnValues(int index) throws DException {
323     throw new UnsupportedOperationException JavaDoc(
324         "Method getColumnValues(index) not yet been implemented.");
325   }
326
327   public Object JavaDoc getColumnValues(int columns[]) throws DException {
328     throw new UnsupportedOperationException JavaDoc(
329         "Method getColumnValues(index) not yet been implemented.");
330
331   }
332
333   public Object JavaDoc getColumnValues() throws DException {
334     if (status != 0)
335       throw new DException("DSE2019", new Object JavaDoc[] {new Integer JavaDoc(status)});
336     return sortedList[currentPosition].getColumnValues();
337   }
338
339   public Object JavaDoc getColumnValues(_Reference reference) throws DException {
340     return getColumnValues1(getIndex(reference));
341
342   }
343
344   public Object JavaDoc getColumnValues(_Reference[] references) throws DException {
345     return getColumnValues1(getIndexes(references));
346   }
347
348   public SuperComparator getComparator() {
349     try {
350       return order.getComparator();
351     }
352     catch (DException ex) {
353       return null;
354     }
355   }
356
357   public _Order getDefaultOrder() throws DException {
358     return iterator.getDefaultOrder();
359   }
360
361   public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException {
362     ExecutionPlanForBrowser cplan = iterator.getExecutionPlanForBrowser();
363     ExecutionPlanForBrowser cplans[] = cplan != null ?
364         (new ExecutionPlanForBrowser[] {
365          cplan
366     }) : null;
367     ExecutionPlanForBrowser plan = new ExecutionPlanForBrowser("Sorting",
368         "MemoryIndexIterator", cplans, null, null, getIndexName());
369     return plan;
370   }
371
372   public Object JavaDoc[][] getFunctionalColumnMapping() throws DException {
373     return iterator.getFunctionalColumnMapping();
374   }
375
376   private String JavaDoc getIndexName() throws DException {
377     String JavaDoc indexName = "";
378     String JavaDoc columns[] = order.getColumns();
379     boolean orders[] = order.getOrderOfColumns();
380     int i = 0;
381     for (int length = columns.length; i < length; i++)
382       indexName = indexName + columns[i] + " " +
383           (orders[i] ? " ASC ," : " DESC ,");
384
385     return indexName;
386   }
387
388   /*Return the key for the Iterator */
389
390   public Object JavaDoc getKey() throws DException {
391     if (status != 0)
392       throw new DException("DSE2019", new Object JavaDoc[] {new Integer JavaDoc(status)});
393     return sortedList[currentPosition].getKey();
394   }
395
396   public _KeyColumnInformation[] getKeyColumnInformations() throws DException {
397     if (keyColumnInformation == null) {
398       boolean orders[] = order.getOrderOfColumns();
399       ColumnDetails columnDetails[] = order.getKeyColumnDetails();
400       int len = columnDetails.length;
401       keyColumnInformation = new _KeyColumnInformation[len];
402       for (int i = 0; i < len; i++) {
403         keyColumnInformation[i] = new TableKeyColumnInformation(columnDetails[i],
404             orders[i]);
405         com.daffodilwoods.daffodildb.server.sql99.common.TableDetails table =
406             columnDetails[i].getTable();
407         keyColumnInformation[i].setTableDetails(table);
408       }
409
410     }
411     return keyColumnInformation;
412   }
413
414   public SuperComparator getObjectComparator() {
415     throw new UnsupportedOperationException JavaDoc("Method not implemented");
416   }
417
418   public _ExpressionOrderValues getOrder() throws DException {
419     return order;
420   }
421
422   public _OrderCount getOrderCounts() throws DException {
423     _Order tempOrder = order.getOrder();
424     ColumnDetails cd[] = tempOrder.getKeyColumnDetails();
425     boolean b[] = tempOrder.getOrderOfColumns();
426     ColumnDetails result[] = {
427         cd[0]};
428     boolean orderType[] = {
429         b[0]};
430     int i = 1;
431     for (int length = cd.length; i < length; i++) {
432       if (cd[i].getColumn().equalsIgnoreCase(SystemFields.systemFields[0]))
433         break;
434       ColumnDetails temp[] = new ColumnDetails[i + 1];
435       boolean tem[] = new boolean[i + 1];
436       System.arraycopy(result, 0, temp, 0, i);
437       System.arraycopy(orderType, 0, tem, 0, i);
438       temp[i] = cd[i];
439       tem[i] = b[i];
440       result = temp;
441       orderType = tem;
442     }
443
444     return new OrderCount(i, new SelectOrder(result, orderType));
445   }
446
447   public Object JavaDoc getPhysicalAddress() throws DException {
448     throw new UnsupportedOperationException JavaDoc(
449         "Method getPhysicalAddress() not yet implemented.");
450   }
451
452   public _Record getRecord() throws DException { //need to ask
453
allignUnderLineIterator();
454     return iterator.getRecord();
455   }
456
457   public _TableCharacteristics getTableCharacteristics() throws DException {
458     throw new UnsupportedOperationException JavaDoc("getTableCharacteristics");
459   }
460
461   public TableDetails[] getTableDetails() throws DException {
462     return iterator.getTableDetails();
463   }
464
465   public _Iterator getUnderlyingIterator() throws DException {
466     return iterator;
467   }
468
469   public _Iterator getPartialIterator(Object JavaDoc key) throws DException {
470     return getIterator(key);
471   }
472
473   public _Iterator getIterator(Object JavaDoc key) throws DException {
474     if (sortedList == null)
475       constructSortedList();
476     return new SingleRowIterator(iterator, key);
477   }
478
479   public _Iterator getBaseIterator(ColumnDetails column) throws DException {
480     return this;
481   }
482
483   public _Iterator getBaseIteratorHasRecord(ColumnDetails columnDetails) throws
484       DException {
485     return this;
486   }
487
488   public _IndexInformation[] getUniqueInformation() throws DException {
489     throw new UnsupportedOperationException JavaDoc("getUniqueInformation");
490   }
491
492   public Object JavaDoc[] getUniqueColumnReference() throws DException {
493     return null; // done by Gaurav
494
}
495
496   public Object JavaDoc getUpdatedEffect(_Reference references[], Object JavaDoc values[]) throws
497       DException {
498     throw new UnsupportedOperationException JavaDoc("getUpdatedEffect() Not Supported");
499   }
500
501   public void insert(_DatabaseUser parm1, Object JavaDoc parm2) throws DException {
502     allignUnderLineIterator();
503     ( (_UserTableOperations) iterator).insert(parm1, parm2);
504     insertIntoSortedList();
505   }
506
507   public void insert(Object JavaDoc parm1) throws DException {
508     allignUnderLineIterator();
509     ( (_TableOperations) iterator).insert(parm1);
510     insertIntoSortedList();
511   }
512
513   public boolean isRefreshType() {
514     return false;
515   }
516
517   public boolean first() throws DException {
518     if (sortedList == null)
519       constructSortedList();
520     return sortedList.length != 0 ? setStatusAndPosition(0, true, 0, "first") :
521         setStatusAndPosition( -1, false, -1, "first");
522   }
523
524   public boolean last() throws DException {
525     if (sortedList == null)
526       constructSortedList();
527     return sortedList.length != 0 ?
528         setStatusAndPosition(0, true, sortedList.length - 1, "last") :
529         setStatusAndPosition(1, false, 1, "last");
530   }
531
532   public boolean next() throws DException {
533     if (status != 0)
534       return status == 1 ? false : first();
535     return currentPosition < sortedList.length - 1 ?
536         setStatusAndPosition(0, true, ++currentPosition, "next") :
537         setStatusAndPosition(1, false, sortedList.length, "next");
538
539   }
540
541   public boolean previous() throws DException {
542     if (status != 0)
543       return status == -1 ? false : last();
544     return currentPosition > 0 ?
545         setStatusAndPosition(0, true, --currentPosition, "previous") :
546         setStatusAndPosition( -1, false, -1, "previous");
547   }
548
549   public boolean locateKey(Object JavaDoc key, boolean top) throws DException {
550     int i = Arrays.binarySearch(sortedList, new SortKeyEntry(key, null),
551                                 comparator);
552     return i >= 0 ? setStatusAndPosition(0, true, i, "LocateKey") : false;
553   }
554
555   public void move(Object JavaDoc key) throws DException {
556     if(!seek(key))
557       throw new DException("DSE5518", null);
558     status = 0;
559   }
560
561   public void moveByteKey(byte[] key) throws DException {
562    for(int i=0,j=0;i<key.length && j< objectToMove.length ;){
563      BufferRange bf = key[i]==0?new BufferRange(true):new BufferRange(key,i+1,key[i]);
564      ((FieldBase)objectToMove[j]).setBufferRange(bf);
565      i+=key[i]+1;
566      j++;
567    }
568      move(objectToMove);
569  }
570
571
572   public void refreshDataSource() throws DException {
573     throw new UnsupportedOperationException JavaDoc(
574         "Method getPhysicalAddress() not yet implemented.");
575   }
576
577   public void releaseResource() throws DException {
578     iterator.releaseResource();
579     order.releaseResource();
580   }
581
582   public boolean seek(Object JavaDoc indexKey) throws DException {
583
584     if (sortedList == null)
585       constructSortedList();
586     int i = Arrays.binarySearch(sortedList, new SortKeyEntry(indexKey, null),
587                                 comparator);
588     return i >= 0 ? setStatusAndPosition(0, true, i, "SEEK") : false;
589   }
590
591   public boolean seekFromTop(_IndexPredicate[] condition) throws DException {
592     throw new UnsupportedOperationException JavaDoc("METHOD NOT IMPLEMENTED");
593   }
594
595   public boolean seekFromBottom(_IndexPredicate[] condition) throws DException {
596     throw new UnsupportedOperationException JavaDoc("METHOD NOT IMPLEMENTED");
597   }
598
599   /*Need to ASK ,In TemporaryIndexIterator it is implemented */
600   public boolean seekFromTopRelative(Object JavaDoc indexKey) throws DException {
601     throw new UnsupportedOperationException JavaDoc("METHOD NOT IMPLEMENTED");
602   }
603
604   public boolean seekKeyAddress(Object JavaDoc parm1) throws DException {
605     throw new UnsupportedOperationException JavaDoc(
606         "Method seekKeyAddress() not yet implemented.");
607   }
608
609   public void setIterator(_Iterator iterator) throws DException {
610     /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.utils._VariableValues method*/
611     throw new java.lang.UnsupportedOperationException JavaDoc(
612         "Method setIterator() not yet implemented.");
613   }
614
615   public void setConditionVariableValue(_Reference references[], Object JavaDoc values[],
616                                         int priority) throws DException {
617     if (underlyingRef != null){
618       references = GeneralPurposeStaticClass.getJointReferences(references,
619           underlyingRef);
620     values=GeneralPurposeStaticClass.getJointValues(this, values,underlyingRef.length);
621     }
622     order.setIterator(iterator);
623     order.setConditionVariableValue(references, values, priority);
624     iterator.setConditionVariableValue(references, values, priority);
625     sortedList = null;
626     status = -1;
627     currentPosition = -1;
628   }
629
630   public void setRowReader(_AllColumnRowReader rowReader0) throws DException {
631   }
632
633   public void setKeyCount(Object JavaDoc[][] tableAndKeyCount) throws DException {
634   }
635
636   public void setKeyColumnInformation(_KeyColumnInformation[]
637                                       keyColumnInformation) {
638     this.keyColumnInformation = keyColumnInformation; // done by Gaurav and to confirm with Nitin Nijhawan
639
}
640
641   public String JavaDoc toString() {
642     return "Memory IndexIterator[" + iterator + "][" + order + "]";
643   }
644
645   public void update(_DatabaseUser parm1, int parm2[], Object JavaDoc parm3[]) throws
646       DException {
647     allignUnderLineIterator();
648     ( (_UserTableOperations) iterator).update(parm1, parm2, parm3);
649     updateSortedList();
650
651   }
652
653   public void update(_DatabaseUser parm1, Object JavaDoc parm2) throws DException {
654     allignUnderLineIterator();
655     ( (_UserTableOperations) iterator).update(parm1, parm2);
656     updateSortedList();
657   }
658
659   public void update(int parm1[], Object JavaDoc parm2[]) throws DException {
660     allignUnderLineIterator();
661     ( (_TableOperations) iterator).update(parm1, parm2);
662     updateSortedList();
663   }
664
665   public void update(Object JavaDoc parm1) throws DException {
666     allignUnderLineIterator();
667     ( (_TableOperations) iterator).update(parm1);
668     updateSortedList();
669   }
670
671   public void updateRow(_Iterator baseIterator, int[] columns, Object JavaDoc[] values) throws
672       com.daffodilwoods.database.resource.DException {
673     sortedList = null;
674     constructSortedList();
675   }
676
677   public void insertRow(Object JavaDoc row) throws com.daffodilwoods.database.resource.
678
      DException {
679     sortedList = null;
680     constructSortedList();
681
682   }
683
684   public void deleteRow(_Iterator iterator) throws com.daffodilwoods.database.
685
      resource.DException {
686     sortedList = null;
687     constructSortedList();
688   }
689
690   public void replaceRow(_Iterator iterator, Object JavaDoc row) throws com.
691
      daffodilwoods.database.resource.DException {
692     sortedList = null;
693     constructSortedList();
694
695   }
696
697   private void updateSortedList() throws DException {
698
699     if (sortedList[currentPosition].getKey() != iterator.getKey()) {
700       deleteFromSortedList();
701       insertIntoSortedList();
702     }
703     else
704       sortedList[currentPosition].setColumnValues(iterator.getColumnValues());
705   }
706
707   private void deleteFromSortedList() throws DException {
708     SortKeyEntry[] newSortedList = new SortKeyEntry[sortedList.length - 1];
709     int j = 0;
710     for (; j < currentPosition; j++) {
711       newSortedList[j] = sortedList[j];
712     }
713     for (j = currentPosition; j < newSortedList.length; j++) {
714       newSortedList[j] = sortedList[j + 1];
715     }
716     sortedList = newSortedList;
717
718   }
719
720   private void insertIntoSortedList() throws DException {
721     int i = 0, j = 0;
722     int insertionPoint = Arrays.binarySearch(sortedList,
723                                              new SortKeyEntry(iterator.getKey(), null),
724                                              comparator);
725     insertionPoint = -1 * (insertionPoint + 1);
726     SortKeyEntry[] newSortedList = new SortKeyEntry[sortedList.length + 1];
727     for (; i < insertionPoint; i++) {
728       newSortedList[i] = sortedList[j++];
729     }
730     newSortedList[i++] = new SortKeyEntry(iterator.getKey(),
731                                           iterator.getColumnValues());
732     for (; j < sortedList.length; i++) {
733       newSortedList[i] = sortedList[j++];
734     }
735     sortedList = newSortedList;
736   }
737   private Object JavaDoc[] getObjectArray() throws DException {
738       keyColumnDetails = order.getKeyColumnDetails();
739       FieldBase[] list = new FieldBase[keyColumnDetails.length];
740       for (int i = 0; i < keyColumnDetails.length; i++) {
741         list[i] = FieldUtility.getField(keyColumnDetails[i].getDatatype(),null);
742       }
743       return list;
744     }
745
746
747 }
748
749 class SortKeyEntry {
750
751   public SortKeyEntry(Object JavaDoc key0, Object JavaDoc value0) {
752     key = key0;
753     value = value0;
754   }
755
756   public SortKeyEntry(Object JavaDoc key0, Object JavaDoc value0, Object JavaDoc row) {
757     key = key0;
758     value = value0;
759     this.columnValues = row;
760   }
761
762   /*represents the key for this iterator*/
763   public Object JavaDoc getKey() {
764     return key;
765   }
766
767   /*return the key of underLying iterator*/
768   public Object JavaDoc getValue() {
769     return value;
770   }
771
772   /*return all column values*/
773   public Object JavaDoc getColumnValues() {
774     return columnValues;
775   }
776
777   public void setColumnValues(Object JavaDoc val) {
778     columnValues = val;
779   }
780
781   public String JavaDoc toString() {
782     return "Key =" + (key instanceof Object JavaDoc[] ?P.print(key):key )+ " : value " +(columnValues instanceof Object JavaDoc[] ? P.print(columnValues ):columnValues);
783   }
784
785   private Object JavaDoc key;
786   private Object JavaDoc value;
787   private Object JavaDoc columnValues;
788
789   private int keyCode;
790 }
791
792 class SortComparator
793     /* extends SuperComparator*/
794     implements Comparator {
795   SuperComparator sc;
796   SortComparator(SuperComparator p0) {
797     sc = p0;
798   }
799
800   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
801     try {
802       return sc.compare( ( (SortKeyEntry) o1).getKey(),
803                         ( (SortKeyEntry) o2).getKey());
804     }
805     catch (DException ex) {
806       return 0;
807     }
808
809   }
810 }
811
Popular Tags