KickJava   Java API By Example, From Geeks To Geeks.

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


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.execution.*;
5 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
6 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
7 import com.daffodilwoods.daffodildb.utils.*;
8 import com.daffodilwoods.daffodildb.utils.field.*;
9 import com.daffodilwoods.database.resource.*;
10 import com.daffodilwoods.database.utility.*;
11 import com.daffodilwoods.daffodildb.server.sql99.expression.valueexpression;
12 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession;
13
14 /**
15  * <p>Title: AggregateGroupByIterator </p>
16  * <p>Description:
17  * This class is responsible for retrival of records from the query having
18  * Aggregates (count,sum,min,max or/and avg) except Group By clause.
19  * For e.g - select sum(col1), max(col2) from table1
20  * For these queries, all records of the table which are treated as ONE GROUP.
21  * Hence, given aggregate functions is computed for all records of the table.
22  * This iterator always has only ONE RECORD i.e ONE GROUP. The purpose of this
23  * class is to compute the given aggregates columns for all the records
24  * of the iterator. This class extends class named GroupByIterator to reuse
25  * variables and methods defined in super class. </p>
26  * <p>Copyright: Copyright (c) 2003</p>
27  * <p>Company: </p>
28  * @author unascribed
29  * @version 1.0
30  */

31
32 public class AggregateGroupByIterator extends GroupByIterator {
33
34   /**
35    * Initializes the variables required for aggregate function computation. It
36    * intializes the aggregate column values to their default values.
37    * @param iterator0 underlying iterator
38    * @param aggregateColumnDetails0 list of aggregate columns
39    * @throws DException
40    */

41   public AggregateGroupByIterator(_Iterator iterator0, ColumnDetails[] aggregateColumnDetails0,_ServerSession serverSession) throws DException {
42           super(iterator0, null, aggregateColumnDetails0,null,serverSession);
43       initializingExecutables();
44    }
45
46    /**
47     * Initializes the results of aggregates to their corresponding default
48     * values before the computation of aggregates starts.
49     * @throws DException
50     */

51    private void initializingExecutables() throws DException {
52       int len = mapping.length;
53       for (int i = 0; i < len; i++) {
54          ( (_Aggregate) mapping[i][1]).initialize();
55       }
56    }
57
58    /**
59     * NAVIGATION METHODS
60     */

61
62    /**
63     * First record is retrived from the iterator. Computes the given aggregates for
64     * all the records from the iterator. If iterator has record(s), iterator state
65     * is set to valid state, after last otherwise.
66     * @return true if underlying iterator has records, false otherwise.
67     * @throws DException
68     */

69    public boolean first() throws DException { //to change in the the last method also.....
70
if (iterator.first()) {
71          state = FORWARD; //State Forward Set as we are Doing fetching forward.
72
boolean b = (status = CheckForSameGroup() ? VALIDSTATE : AFTERLAST) == VALIDSTATE;
73          return b;
74       }else{
75         int len = mapping.length;
76        for (int i = 0; i < len; i++) {
77           ( (_Aggregate) mapping[i][1]).initialize();
78        }
79       }
80       status = VALIDSTATE;
81       return true;
82    }
83
84    /**
85     * Last record is retrived from the iterator. Computes the aggregates for
86     * all the records from the iterator. If iterator has record(s), iterator state
87     * is set to valid state, before first otherwise.
88     * @return true if underlying iterator has records, false otherwise.
89     * @throws DException
90     */

91    public boolean last() throws DException {
92       if (iterator.last()) {
93          state = BACKWARD; // State Forward Set as we are Doing fetching forward.
94
return (status = CheckForSameGroupPrevious() ? VALIDSTATE : BEFOREFIRST) == VALIDSTATE;
95       }else{
96         int len = mapping.length;
97         for (int i = 0; i < len; i++) {
98           ( (_Aggregate) mapping[i][1]).initialize();
99         }
100       }
101       status = VALIDSTATE;
102       return true;
103    }
104
105    /**
106     * This iterator can have only one record. If the state of iterator is
107     * AFTERLAST, first record is retrived from the iterator, otherwise,
108     * iterator state is set to BEFORE FIRST.
109     * @return true if record found, false otherwise.
110     * @throws DException
111     */

112    public boolean previous() throws com.daffodilwoods.database.resource.DException {
113       if (status == AFTERLAST) {
114          return last();
115       }
116       status = BEFOREFIRST;
117       return false;
118    }
119
120    /**
121     * This iterator can have only one record. If the state of iterator is
122     * BEFOREFIRST, last record is retrived from the iterator, otherwise,
123     * iterator state is set to AFTER LAST.
124     * @return true if record found, false otherwise.
125     * @throws DException
126     */

127    public boolean next() throws com.daffodilwoods.database.resource.DException {
128       if (status == BEFOREFIRST) {
129          return first();
130       }
131       status = AFTERLAST;
132       return false;
133    }
134
135    /**
136     * This method is responsible for the computation of given aggregates in the
137     * select query during the forward fetching. When group starts,
138     * aggregate's result are initialized to their default values and next records
139     * are fetched from underlying iterator, till the end of iterator, aggregate
140     * results are updated after performing corresponding computation on aggregate
141     * column values retrived from underlying iterator for all the records of iterator.
142     * @return true
143     * @throws DException
144     */

145    protected boolean CheckForSameGroup() throws DException {
146       int len = mapping.length;
147       for (int i = 0; i < len; i++) {
148          ( (_Aggregate) mapping[i][1]).initialize();
149       }
150       do {
151          evaluateAggregates();
152       } while (iterator.next());
153       return true;
154    }
155
156    /**
157     * This method is responsible for the computation of given aggregates in the
158     * select query during the backward direction fetching. When group starts,
159     * aggregate's result are initialized to their default values and previous
160     * records are fetched from underlying iterator, aggregate results are updated
161     * after performing corresponding computation on aggregate column values
162     * retrived from underlying iterator for all the records of iterator.
163     * @return true
164     * @throws DException
165     */

166    protected boolean CheckForSameGroupPrevious() throws DException {
167       int len = mapping.length;
168       for (int i = 0; i < len; i++) {
169          ( (_Aggregate) mapping[i][1]).initialize();
170       }
171       do {
172          evaluateAggregates();
173       } while (iterator.previous());
174       return true;
175    }
176
177    /**
178     * Used to return the key for this iterator. As there is no need of this key
179     * in the move, so zeros are returned. Key is not needed in move, because it
180     * always results into one row and we can move by calling first or last
181     * method.
182     * @return null key for aggregate group by iterator
183     * @throws DException
184     */

185
186    public Object JavaDoc getKey() throws DException {
187      if (keyColumnInformation == null)
188          return null;
189        int length = keyColumnInformation.length;
190        Object JavaDoc[] key = new Object JavaDoc[length];
191        for (int i = 0; i < length; i++) {
192          key[i] = FieldUtility.getField(Datatypes.LONG, new Long JavaDoc(0)); // done for LOJ case mentioned below
193
}
194        return key;
195      }
196
197
198
199   public byte[] getByteKey() throws DException {
200     if (keyColumnInformation == null)
201       return null;
202       int length = keyColumnInformation.length;
203       byte[] key = new byte[length];
204       for (int i = 0; i < length; i++) {
205         key[i] = 0; // done for LOJ case mentioned below
206
}
207       return key;
208
209   }
210
211
212    /**
213     * This method calls the first/last method for forward/backward directions
214     * respectively irrespective of key passed.
215     * @param keys destination record key
216     * @throws DException
217     */

218    public void move(Object JavaDoc keys) throws DException {
219     switch (state) {
220        case FORWARD:
221           last();
222           break;
223        case BACKWARD:
224           first();
225           break;
226     }
227     status = VALIDSTATE;
228  }
229
230
231   public void moveByteKey(byte[] key) throws DException {
232     move(key);
233   }
234
235    /**
236     * RETRIEVAL METHODS
237     * The following methods are used for retrival of records from aggregate
238     * group by iterator. The method checks the position of aggregate column in
239     * mapping and retrives the result from the corresponding executable aggregate.
240     **/

241
242    /**
243     * This method is used to retrieve the values of passed column references.
244     * @param references reference for which values are to be retrived.
245     * @return NonShared FieldBases denoting the value of References. Non Shared
246     * FieldBases are those for which BufferRange is not shared with some other
247     * FieldBase.
248     * @throws DException
249     */

250    public Object JavaDoc getColumnValues(_Reference[] references) throws DException {
251       int len = references.length;
252       Object JavaDoc[] result = new Object JavaDoc[len];
253       for (int i = 0; i < len; i++) {
254          result[i] = getColumnValues(references[i]);
255       }
256       return result;
257    }
258
259    /**
260     * This method is used to retrieve the value of passed column reference.
261     * @param references reference for which value is to be retrived.
262     * @return NonShared FieldBase denoting the value of Reference. Non Shared
263     * FieldBases are those for which BufferRange is not shared with some other
264     * FieldBase.
265     * @throws DException
266     */

267    public Object JavaDoc getColumnValues(_Reference references) throws DException {
268       Object JavaDoc result = null;
269       int length = mapping.length;
270       for (int i = 0; i < length; i++) {
271          if ( ( (ColumnDetails) mapping[i][0]).getColumn().trim().equalsIgnoreCase(references.getColumn().trim())) {
272             result = ( (_Aggregate) mapping[i][1]).getResult();
273             return result;
274          }
275       }
276       result = FieldUtility.getField(Datatypes.LONG, new Long JavaDoc(0)); // for order by rowid col handling
277
return result;
278    }
279
280    /**
281     * This method return the shared FieldBase for the passed reference. By Shared
282     * FieldBase we mean, BufferRange of FieldBase is shared with other FieldBase
283     * objects.
284     * @param references references for which value is to be retrived
285     * @return shared field base correspondition to passed column reference
286     * @throws DException
287     */

288
289    public FieldBase field(_Reference references) throws com.daffodilwoods.database.resource.DException {
290       FieldBase result = null;
291       int length = mapping.length;
292       for (int i = 0; i < length; i++) {
293          if ( ( (ColumnDetails) mapping[i][0]).getColumn().trim().equalsIgnoreCase(references.getColumn().trim())) {
294             result = (FieldBase) ( (_Aggregate) mapping[i][1]).getResult();
295             return result;
296          }
297       }
298       result = FieldUtility.getField(Datatypes.LONG, new Long JavaDoc(0));
299       return result;
300    }
301
302    /**
303     * This method return the shared FieldBase for the passed references. By Shared
304     * FieldBase we mean, BufferRange of FieldBase is shared with other FieldBase
305     * objects.
306     * @param references references for which values are to be retrived
307     * @return shared field base correspondition to passed column reference
308     * @throws DException
309     */

310    public FieldBase[] fields(_Reference[] references) throws com.daffodilwoods.database.resource.DException {
311       int len = references.length;
312       FieldBase[] result = new FieldBase[len];
313       for (int i = 0; i < len; i++) {
314          result[i] = field(references[i]);
315       }
316       return result;
317    }
318
319    public String JavaDoc toString() {
320       String JavaDoc str = "AggregateGroupByIterator";
321       try {
322          if (iterator != null) {
323             str += "[" + iterator.toString() + "]";
324          }
325       } catch (Exception JavaDoc e) {
326          e.printStackTrace();
327       }
328       return str;
329    }
330 }
331
Popular Tags