KickJava   Java API By Example, From Geeks To Geeks.

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


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.DOMUtils;
28 import org.xquark.mediator.DOMUtils.EvaluationVisitor;
29 import org.xquark.mediator.DOMUtils.Tuple;
30 import org.xquark.mediator.algebra.Algebra;
31 import org.xquark.mediator.runtime.MediatorException;
32 import org.xquark.xpath.datamodel.TypedNode;
33 import org.xquark.xquery.parser.Variable;
34 import org.xquark.xquery.parser.XQueryException;
35 import org.xquark.xquery.parser.XQueryExpression;
36 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionCOUNT;
37
38 public class OpSimpleMerge extends OpUn {
39     // **********************************************************************
40
// * VERSIONING
41
// **********************************************************************
42
private static final String JavaDoc RCSRevision = "$Revision: 1.14 $";
43     private static final String JavaDoc RCSName = "$Name: $"; // * INITIALIZATION
44
// ***********************************************************************
45
private boolean onlyVar = false;
46
47     public OpSimpleMerge(ExecutionPlan plan, XQueryExpression expression, Variable var, Operator childexp, boolean isLet) throws MediatorException {
48         super(plan, expression, childexp);
49         init(var);
50         this.isLet(isLet);
51     }
52
53     public OpSimpleMerge(ExecutionPlan plan, Algebra algebra, Operator childexp) throws MediatorException {
54         super(plan, algebra.getExpression(), childexp);
55         Variable tmpVar = (Variable) algebra.getVariables().get(0);
56         if (algebra.getPaths().isEmpty())
57             init(tmpVar);
58         else {
59             ArrayList JavaDoc tmpPaths = algebra.getPaths();
60             addPaths(tmpPaths);
61             size = childOperator.getSize() + tmpPaths.size();
62             idsize = childOperator.getIdSize();
63             if (plan.getIdVars().contains(tmpVar))
64                 idsize++;
65             if (tmpPaths.size() == 1 && ((XQueryExpression) tmpPaths.get(0)).getStringValue().equals(tmpVar.getStringValue()))
66                 onlyVar = true;
67         }
68         this.isLet(algebra.isLet());
69     }
70
71     private void init(Variable var) {
72         //resetPaths();
73
addPath(var);
74         size = childOperator.getSize() + 1;
75         idsize = childOperator.getIdSize();
76         if (plan.getIdVars().contains(var))
77             idsize++;
78         onlyVar = true;
79     }
80
81     public boolean isOnlyVar() {
82         return onlyVar;
83     }
84
85     // #############################################################################
86
// VISITOR STUFF
87
// #############################################################################
88

89     public void accept(OperatorVisitor visitor) throws MediatorException {
90         visitor.visit(this);
91     }
92
93     // ***********************************************************************
94
// * EXECUTE QUERY
95
// ***********************************************************************
96
/**
97      *
98      */

99     protected ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException {
100         return new SimpleMergeResultSet(this, context, child);
101     }
102
103     // **********************************************************************
104
// * OPTIMIZE
105
// **********************************************************************
106
/**
107      *
108      */

109
110     // **********************************************************************
111
// * DEBUG
112
// **********************************************************************
113
/**
114      *
115      */

116 }
117
118 class SimpleMergeResultSet extends UnResultSet {
119     // **********************************************************************
120
// * VERSIONING
121
// **********************************************************************
122
private static final String JavaDoc RCSRevision = "$Revision: 1.14 $";
123     private static final String JavaDoc RCSName = "$Name: $"; // * INITIALIZATION
124
// ***********************************************************************
125
EvaluationVisitor visitor = null;
126     /**
127      *
128      */

129     public SimpleMergeResultSet(OpSimpleMerge operator, DynamicContext context, OperatorRunnable child) throws MediatorException {
130         super(operator, context, child);
131     }
132
133     // ***********************************************************************
134
// * EVALUATE IMPLEMENTATION
135
// ***********************************************************************
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
142         ArrayList JavaDoc idxsteps = new ArrayList JavaDoc();
143         idxsteps.add(new Integer JavaDoc(0));
144         boolean islet = this.operator.isLet();
145         XQueryExpression expression = this.operator.getExpression();
146  
147         if (!resultset.hasNext()) {
148             try {
149                 EvaluationVisitor visitor = new EvaluationVisitor(this.operator.getPlan().getSchemaManager());
150                 Tuple newtuple = new Tuple(operator.getSize()-resultset.getOperator().getSize(),operator.getIdSize()-resultset.getOperator().getIdSize());
151                 newtuple.setParent(buftuples);
152                 visitor.reset(newtuple);
153                 visitor.setReturnType(EvaluationVisitor.NODE_TYPE);
154                 expression.accept(visitor);
155                 ArrayList JavaDoc nodes = visitor.getResNodes();
156                 if (nodes != null)
157                     merge(islet,newtuple,nodes);
158             } catch (XQueryException xqe) {
159                 throw new MediatorException(xqe.getMessage(), xqe);
160             }
161             setFinishedWhenEmpty();
162             resultset.close();
163             return;
164         }
165
166         while (true) {
167             if (resultset.hasNext()) {
168                 Tuple tuple = resultset.next();
169                 ArrayList JavaDoc nodes = tuple.getPath(expression.getStringValue());
170                 if (nodes == null) {
171                     try {
172                         if (visitor == null)
173                             visitor = new EvaluationVisitor(this.operator.getPlan().getSchemaManager());
174                         visitor.reset(tuple);
175                         visitor.setReturnType(EvaluationVisitor.NODE_TYPE);
176                         expression.accept(visitor);
177                         nodes = visitor.getResNodes();
178                     } catch (XQueryException e) {
179                         throw new MediatorException(e.getMessage(), e);
180                     }
181                 }
182                 merge(islet, tuple, nodes);
183                 if (non_blocking) {
184                     // can make higher buffering by modifying this test
185
if ((buftuples != null) && (!buftuples.isEmpty())) {
186                         break;
187                     }
188                 }
189             } else {
190                 setFinishedWhenEmpty();
191                 resultset.close();
192                 return;
193             }
194         }
195     }
196
197     private void merge(boolean islet, Tuple tuple, ArrayList JavaDoc nodes) throws MediatorException {
198         Tuple newtuple = null;
199         if (islet) {
200             if (((OpSimpleMerge) operator).isOnlyVar()) {
201                 tuple.mergeInPlace(nodes, this.operator);
202                 buftuples.add(tuple);
203             } else {
204                 newtuple = buftuples.newTuple();
205                 for (int j = 0; j < paths.size(); j++) {
206                     String JavaDoc pathStrj = ((XQueryExpression) paths.get(j)).getStringValue();
207                     // test if paths already in tuple
208
int index = tuple.getParent().getPathIndex(pathStrj);
209                     if (index != -1) {
210                         newtuple.addNodesAtIndex(j, tuple.getNodesAtIndex(index));
211                     } else if (pathStrj.equals(expression.getStringValue())) {
212                         newtuple.addNodesAtIndex(j, nodes);
213                     } else {
214                         for (int i = 0; i < nodes.size(); i++) {
215                             ArrayList JavaDoc reslist = DOMUtils.getSteps((TypedNode) nodes.get(i), (XQueryExpression) paths.get(j));
216                             if (reslist != null)
217                                 newtuple.addNodesAtIndex(j, reslist);
218                         }
219                     }
220                 }
221                 newtuple.fillIdentifiers();
222                 buftuples.add(newtuple);
223             }
224         } else {
225             if (nodes != null) {
226                 if (((OpSimpleMerge) operator).isOnlyVar()) {
227                     for (int i = 0; i < nodes.size(); i++) {
228                         newtuple = new Tuple(tuple);
229                         newtuple.mergeInPlace((TypedNode) nodes.get(i), this.operator);
230                         buftuples.add(newtuple);
231                     }
232                 } else {
233                     for (int i = 0; i < nodes.size(); i++) {
234                         ArrayList JavaDoc nodelist = new ArrayList JavaDoc(1);
235                         nodelist.add(nodes.get(i));
236                         newtuple = buftuples.newTuple();
237                         // get paths but test if path is expression !!!
238
for (int j = 0; j < paths.size(); j++) {
239                             String JavaDoc pathStrj = ((XQueryExpression) paths.get(j)).getStringValue();
240                             // test if paths already in tuple
241
int index = tuple.getParent().getPathIndex(pathStrj);
242                             if (index != -1) {
243                                 newtuple.addNodesAtIndex(j, tuple.getNodesAtIndex(index));
244                             } else if (pathStrj.equals(expression.getStringValue())) {
245                                 newtuple.addNodesAtIndex(j, nodes);
246                             } else {
247                                 ArrayList JavaDoc reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j));
248                                 if (reslist != null)
249                                     newtuple.addNodesAtIndex(j, reslist);
250                             }
251                         }
252                         newtuple.fillIdentifiers();
253                         buftuples.add(newtuple);
254                     }
255                 }
256
257             }
258         }
259     }
260
261 }
262
Popular Tags