KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > iterator > table > AbstractQualifiedJoinIterator


1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table;
2
3 import com.daffodilwoods.daffodildb.server.sql99.common.*;
4 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
5 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
6 import com.daffodilwoods.database.resource.*;
7 import com.daffodilwoods.database.sqlinitiator.*;
8 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Utility;
9 import com.daffodilwoods.daffodildb.utils.field.FieldLiteral;
10 import com.daffodilwoods.daffodildb.utils.FieldUtility;
11
12 /**
13  *
14  * <p>Title: AbstractQualifiedJoinIterator </p>
15  * <p>Description: This Class provides the common functionality required for
16  * retrieval of the records from QUALIFIED JOIN including LOJ, ROJ and FOJ. </p>
17  * <p>Copyright: Copyright (c) 2004</p>
18  * <p>Company: </p>
19  * @author not attributable
20  * @version 1.0
21  */

22 public class AbstractQualifiedJoinIterator extends BaseJoinIterator implements SimpleConstants {
23
24   /**
25    * Variable representing the total number of columns in the left table.
26    */

27   protected int leftColumnCount = -1;
28   /**
29    * Variable representing the total number of columns in the right table.
30    */

31   protected int rightColumnCount = -1;
32   /**
33    * Variable representing the key column information of left table
34    */

35   protected _KeyColumnInformation[] leftKeyColumnInformation;
36   /**
37    * Variable representing the key column information of right table
38    */

39   protected _KeyColumnInformation[] rightKeyColumnInformation;
40   /**
41    * Mapping of tables and flag indicating that table lies on left
42    * or right side of the join condition.
43    */

44   protected Object JavaDoc[][] tableDetailsMapping;
45   /**
46    * Number of columns of left table involved in group by.
47    */

48   protected int leftKeyCount = 0;
49   /**
50    * Number of columns of right table involved in group by.
51    */

52   protected int rightKeyCount = 0;
53   /**
54    * left iterator tables
55    */

56   protected TableDetails[] leftTableDetails = null;
57   /**
58    * right iterator tables
59    */

60   protected TableDetails[] rightTableDetails = null;
61
62    /**
63     * Variable used for LOJ/ROJ - for Optimization Purpose.
64     * rightNull is used to indicate whether values corresponding to the right
65     * Iterator be valid value from the right Iterators or NULL 'll be appended
66     * instead. Initially it is set to false.
67     */

68    protected boolean rightNull;
69
70
71    /**
72     * variable will be used in LOJ/ROJ/FOJ .If one side need to append NULLs
73     * this array will be used.
74     */

75    protected Object JavaDoc[] nullFieldLiterals;
76
77
78    public AbstractQualifiedJoinIterator(_Iterator leftIterator0, _Iterator rightIterator0, _KeyColumnInformation[] leftKeyColumnInformation0, _KeyColumnInformation[] rightKeyColumnInformation0, ColumnDetails[] hasRecordReferences0) {
79       super(leftIterator0, rightIterator0, hasRecordReferences0);
80       leftKeyColumnInformation = leftKeyColumnInformation0;
81       rightKeyColumnInformation = rightKeyColumnInformation0;
82       state = INVALIDSTATE;
83    }
84
85    /**
86     * This method is responsible to set the left and right tables and number of
87     * columns involved in the left and the right table involved in the group by
88     * clause.
89     * @param tableAndKeyCount mapping of all the tables involved in the query
90     * and number of columns involed in the group by clause
91     * @throws DException
92     */

93    public void setKeyCount(Object JavaDoc[][] tableAndKeyCount) throws DException {
94           int[] count = GeneralPurposeStaticClass.getLeftandRightKeyCount(leftIterator,rightIterator,tableAndKeyCount,tableDetailsMapping);
95           leftTableDetails = leftIterator.getTableDetails();
96           rightTableDetails = rightIterator.getTableDetails();
97           leftKeyCount = count[0];
98           rightKeyCount = count[1];
99       }
100
101
102       /**
103        * This method is used to get all the tables involved in the qualified
104        * join. This method also maintains a mapping of tables and flag indicating
105        * table belongs to left and right iterator.
106        * @return resultant tables involved in qualifed join
107        * @throws com.daffodilwoods.database.resource.DException
108        */

109       public TableDetails[] getTableDetails() throws com.daffodilwoods.database.resource.DException {
110         TableDetails[] tableDetails1 = leftIterator.getTableDetails();
111         leftColumnCount = GeneralPurposeStaticClass.getColumnCount(tableDetails1);
112         TableDetails[] tableDetails2 = rightIterator.getTableDetails();
113         rightColumnCount = GeneralPurposeStaticClass.getColumnCount(tableDetails2);
114         int len = tableDetails1.length + tableDetails2.length;
115         tableDetailsMapping = new Object JavaDoc[len][2];
116         int i = 0;
117         Integer JavaDoc flag = new Integer JavaDoc(SimpleConstants.LEFT);
118         for (int j = 0; j < tableDetails1.length; j++) {
119           tableDetailsMapping[i][0] = tableDetails1[j];
120           tableDetailsMapping[i++][1] = flag;
121         }
122         flag = new Integer JavaDoc(SimpleConstants.RIGHT);
123         for (int j = 0; j < tableDetails2.length; j++) {
124           tableDetailsMapping[i][0] = tableDetails2[j];
125           tableDetailsMapping[i++][1] = flag;
126         }
127         TableDetails[] resultantTableDetails = new TableDetails[len];
128         System.arraycopy(tableDetails1, 0, resultantTableDetails, 0, tableDetails1.length);
129         System.arraycopy(tableDetails2, 0, resultantTableDetails, tableDetails1.length, tableDetails2.length);
130         return resultantTableDetails;
131       }
132
133       /**
134        * This method is used to get the join order of left and right iterator.
135        * Join order is composed of total columns involved in the order by clause,
136        * sorting criterion and the comparator to compare the one set of values of
137        * specified columns in the order by clause with that of other set.
138        * @return joined order of given query
139        * @throws DException
140        */

141       public _Order getDefaultOrder() throws DException {
142       return GeneralPurposeStaticClass.getJoinOrdered(leftIterator.getDefaultOrder(), rightIterator.getDefaultOrder());
143    }
144
145    /**
146     * This method is used to get the index information for the iterator.
147     * It merges the index of left iterator with index of right iterator.
148     * @return key/index columns of the iterator
149     * @throws DException
150     */

151    public _KeyColumnInformation[] getKeyColumnInformations() throws DException {
152       int len = leftKeyColumnInformation.length + rightKeyColumnInformation.length;
153       _KeyColumnInformation[] resultantKeyColumnInformation = new _KeyColumnInformation[len];
154       System.arraycopy(leftKeyColumnInformation, 0, resultantKeyColumnInformation, 0, leftKeyColumnInformation.length);
155       System.arraycopy(rightKeyColumnInformation, 0, resultantKeyColumnInformation, leftKeyColumnInformation.length, rightKeyColumnInformation.length);
156       return resultantKeyColumnInformation;
157    }
158
159    /**
160     * This method assists to move the iterator to a particular key. For LOJ,
161     * a valid key of the iterator may have NULLs for right iterator corresponding
162     * to NON-NULL keys of left iterator. This method is used to determine whether
163     * the passed key has corresponding rows in right iterator or NULL is appended.
164     * @param rightKeys extracted right iterator keys
165     * @return true if right iterator keys are null, false otherwise.
166     * @throws DException
167     */

168    protected boolean isKeyNull(byte[] rightKeys) throws DException {
169      int length = rightKeys.length;
170      for (int i = 0; i < length; i++)
171          if(rightKeys[i] != -1)
172              return false;
173      return true;
174    }
175
176    /**
177     * This method assists to move the iterator to a particular key. For LOJ,
178     * a valid key of the iterator may have NULLs for right iterator corresponding
179     * to NON-NULL keys of left iterator. This method is used to determine whether
180     * the passed key has corresponding rows in right iterator or NULL is appended.
181     * @param rightKeys extracted right iterator keys
182     * @return true if right iterator keys are null, false otherwise.
183     * @throws DException
184     */

185    protected boolean isKeyNull(Object JavaDoc[] rightKeys) throws DException {
186       int length = rightKeys.length;
187       for (int i = 0; i < length; i++) {
188          if (rightKeys[i] != null) {
189             return false;
190          }
191       }
192       return true;
193    }
194
195    /**
196     * This method is not used at this level.
197     * @param indexKey
198     * @return
199     * @throws DException
200     */

201    public boolean seek(Object JavaDoc indexKey) throws DException {
202       return GeneralPurposeStaticClass.seek(indexKey, leftIterator, rightIterator);
203    }
204
205    /**
206     * This method is used to act as an intermediate to pass the unique columns
207     * to session level iterators. This method transfers the call to underlying
208     * iterator and passes this information to upper level iterators.
209     * @return unique columns
210     * @throws DException
211     */

212    public Object JavaDoc[] getUniqueColumnReference() throws DException {
213       return GeneralPurposeStaticClass.getUniqueColumnReference(leftIterator, rightIterator);
214    }
215
216    /**
217     * This method is used to get Base Level iterator for HASRECORD column.
218     * HasRecord Column containes the values depending on the qualified join type
219     * and for the particular table to which column belongs.
220     * @param hasRecordColumn HASRecord column given in query
221     * @return base level iterator for passed hasrecord column
222     * @throws DException
223     */

224    public _Iterator getBaseIteratorHasRecord(ColumnDetails hasRecordColumn) throws DException {
225       if (hasRecordReferences != null) {
226          for (int i = 0, length = hasRecordReferences.length; i < length; i++) {
227             if (hasRecordReferences[i] == hasRecordColumn) {
228                return this;
229             }
230          }
231       }
232       _Iterator iterator = leftIterator.getBaseIteratorHasRecord(hasRecordColumn);
233       if (iterator != null) {
234          return iterator;
235       }
236       return rightIterator.getBaseIteratorHasRecord(hasRecordColumn);
237    }
238
239
240    /**
241     * This method is used to get the values corresponding to HASRECORD column.
242     * HasRecord Column has boolean value.
243     * @return HASRECORD column value - true if right table has non-null
244     * matching values for the given LOJ, false otherwise.
245     * @throws DException
246     */

247    public Object JavaDoc getHasRecordColumnValues() throws DException {
248      /*Done by vibha acc to findbug*/
249          return Utility.getBooleanValue(!rightNull);
250    }
251
252    /**
253     * Method written by Sandeep to solve problem in MemeoryIndexIterator
254     * This mehtod return an array of NULLFieldLiteral if one side of the qualified Join
255     * has NULL values.
256     * @param length
257     * @return an array of NullFieldLiteral length
258     * @throws DException
259     */

260    protected Object JavaDoc[] makeNullFieldLiteralArray(int length) throws DException {
261      if (nullFieldLiterals != null && nullFieldLiterals.length == length)
262        return nullFieldLiterals;
263
264      nullFieldLiterals = new FieldLiteral[length];
265      for (int i = 0; i < nullFieldLiterals.length; i++) {
266        nullFieldLiterals[i] = new FieldLiteral(FieldUtility.NULLBUFFERRANGE, -1);
267      }
268      return nullFieldLiterals;
269    }
270
271
272 }
273
Popular Tags