KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > plan > OpAggregateFunc


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mediator.plan;
24
25 import java.util.ArrayList JavaDoc;
26
27 import org.xquark.mediator.DOMUtils.BufferTuple;
28 import org.xquark.mediator.DOMUtils.EvaluationVisitor;
29 import org.xquark.mediator.DOMUtils.Tuple;
30 import org.xquark.mediator.runtime.MediatorException;
31 import org.xquark.xquery.parser.*;
32 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionCOUNT;
33
34 public class OpAggregateFunc extends OpUn {
35     // **********************************************************************
36
// * VERSIONING
37
// **********************************************************************
38
private static final String JavaDoc RCSRevision = "$Revision: 1.9 $";
39     private static final String JavaDoc RCSName = "$Name: $"; // * INITIALIZATION
40
// ***********************************************************************
41
/**
42      *
43      */

44     public OpAggregateFunc(ExecutionPlan plan, XQueryExpression expression, Operator childexp) throws MediatorException {
45         super(plan, expression, childexp);
46         // the path number can be only one (otherwise distinct values might be incorrect)
47
if (paths != null)
48             paths.clear();
49         // calculate paths
50
if (paths == null)
51             paths = new ArrayList JavaDoc();
52         getRetPath(((FunctionCall) expression).getArgument(0), paths);
53         if (paths.isEmpty() || paths.size() > 1)
54             throw new MediatorException("Could not find paths for aggregate function call");
55         // ADD LARS 30/03/04
56
((FunctionCall) expression).getArguments().set(0, paths.get(0));
57         size = 1;
58         idsize = 0;
59     }
60
61     // TODO better!!!
62
private void getRetPath(XQueryExpression expr, ArrayList JavaDoc reslist) {
63         if (expr instanceof XQueryExpressionSequence) {
64             for (int i = 0; i < ((XQueryExpressionSequence) expr).getSubExpressions().size(); i++) {
65                 getRetPath((XQueryExpression) ((XQueryExpressionSequence) expr).getSubExpressions().get(i), reslist);
66             }
67         }
68         if (expr instanceof LocatedExpression || expr instanceof Value || expr instanceof Variable) {
69             reslist.add(expr);
70         } else if (expr instanceof FLWRExpression) {
71             getRetPath(((FLWRExpression) expr).getReturnClause(), reslist);
72         } else if (expr instanceof FunctionCall) {
73             getRetPath(((FunctionCall) expr).getArgument(0), reslist);
74         }
75     }
76
77     // #############################################################################
78
// VISITOR STUFF
79
// #############################################################################
80

81     public void accept(OperatorVisitor visitor) throws MediatorException {
82         visitor.visit(this);
83     }
84
85     // ***********************************************************************
86
// * EXECUTE QUERY
87
// ***********************************************************************
88
/**
89      *
90      */

91     public ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException {
92         return new AggregateFuncResultSet(this, context, child);
93     }
94
95     // **********************************************************************
96
// * OPTIMIZE
97
// **********************************************************************
98
/**
99      * FuncMAX *this* node with the subnode(s). As it should be called
100      * only from source, the returned node must be of type AlgSource.
101      * The return node is a AlgSource where the XQueryExpression is the merging
102      * of all child(ren) nodes' expression.
103      */

104     // protected Algebra mergeWithSub() throws MediatorException { return this ; }
105

106     // **********************************************************************
107
// * DEBUG
108
// **********************************************************************
109
/**
110      *
111      */

112 }
113
114 class AggregateFuncResultSet extends UnResultSet {
115     // **********************************************************************
116
// * VERSIONING
117
// **********************************************************************
118
private static final String JavaDoc RCSRevision = "$Revision: 1.9 $";
119     private static final String JavaDoc RCSName = "$Name: $"; // * CLASS VARIABLES
120
// ***********************************************************************
121

122     // ***********************************************************************
123
// * INITIALIZATION
124
// ***********************************************************************
125
public AggregateFuncResultSet(OpAggregateFunc algebra, DynamicContext context, OperatorRunnable child) throws MediatorException {
126         super(algebra, context, child);
127     }
128
129     // ***********************************************************************
130
// * EVALUATE IMPLEMENTATION
131
// ***********************************************************************
132
/**
133      * In a merge product, we can all do in parallel, so we
134      * must generate all results of both source and then make the merge
135      * product.
136      *
137      * @param non_blocking if the evaluation is blocking, we must
138      * generate all the results before passing.
139      */

140     protected void evaluate(boolean non_blocking) throws MediatorException {
141         //ArrayList childpaths = null ;
142
//Algebra child = ((AlgUnOp) algebra).getChildAlgebra() ;
143
//childpaths = child.getPaths() ;
144

145         // change LARS 09/03/04 to avoid exit when result set is empty but aggregage function can be calculated
146
if (!(this.getOperator().getExpression() instanceof FunctionCOUNT) && !resultset.hasNext()) {
147             //if (!resultset.hasNext()) {
148
resultset.close();
149             setFinishedWhenEmpty();
150             return;
151         }
152
153         BufferTuple tmpBufferTuple = new BufferTuple(resultset.getOperator().getPaths(), 0);
154         if (resultset.hasNext()) {
155             while (resultset.hasNext()) {
156                 Tuple tuple = resultset.next();
157                 tuple.eraseIdSize();
158                 tmpBufferTuple.add(tuple);
159             }
160         }
161
162         resultset.close();
163         try {
164             EvaluationVisitor visitor = new EvaluationVisitor(this.operator.getPlan().getSchemaManager());
165             visitor.reset(tmpBufferTuple);
166             visitor.setReturnType(EvaluationVisitor.TUPLE_TYPE);
167             this.getOperator().getExpression().accept(visitor);
168             Tuple tuple = visitor.getResultTuple();
169             if (tuple != null)
170                 this.buftuples.add(tuple);
171         } catch (XQueryException xqe) {
172             throw new MediatorException(xqe.getMessage(), xqe);
173         }
174
175         setFinishedWhenEmpty();
176     }
177 }
178
Popular Tags