KickJava   Java API By Example, From Geeks To Geeks.

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


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.Tuple;
28 import org.xquark.mediator.runtime.MediatorException;
29 import org.xquark.xml.xdbc.XMLDBCException;
30 import org.xquark.xquery.parser.XQueryExpression;
31
32 public class OpCartesian extends OpBin {
33     // **********************************************************************
34
// * VERSIONING
35
// **********************************************************************
36
private static final String JavaDoc RCSRevision = "$Revision: 1.7 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38
39     // **********************************************************************
40
// * CLASS VARIABLES
41
// **********************************************************************
42

43     // ***********************************************************************
44
// * INITIALIZATION
45
// ***********************************************************************
46

47     public OpCartesian(ExecutionPlan plan, XQueryExpression expression,Operator left, Operator right) throws MediatorException {
48         super(plan, expression, left, right) ;
49         init();
50     }
51
52     public OpCartesian(ExecutionPlan plan, XQueryExpression expression, Operator[] operators) throws MediatorException {
53         super(plan, expression, operators) ;
54         init();
55     }
56
57     private void init() {
58         // Sometimes, it can happen that the two members of cartesian
59
// product have common attributes. In that case, we must mark them
60
// as common. So when we will generate the tuples, we can access
61
// them efficiently.
62
ArrayList JavaDoc childpaths1 = getLeftAlgebra().getPathsList() ;
63         ArrayList JavaDoc childpaths2 = getRightAlgebra().getPathsList() ;
64         size = getLeftAlgebra().getSize() + getRightAlgebra().getSize();
65
66 // equivalences = new ArrayList() ;
67
// int index = -1;
68
// for (int i = 0 ; i < childpaths2.size() ; i ++) {
69
// if ((index = childpaths1.indexOf(childpaths2.get(i))) != -1) {
70
// addEquivalence(equivalences,index, i) ;
71
// size--;
72
// break ;
73
// }
74
// }
75

76 // idequivalences = new ArrayList() ;
77
// ArrayList childvars1 = getLeftAlgebra().getVariables() ;
78
// ArrayList childvars2 = getRightAlgebra().getVariables() ;
79
idsize = getLeftAlgebra().getIdSize() + getRightAlgebra().getIdSize();
80 // for (int i=0;i<childvars2.size();i++) {
81
// Variable var2i = (Variable)childvars2.get(i);
82
// if ((index = childvars1.indexOf(var2i)) != -1) {
83
// addEquivalence(idequivalences,index, i) ;
84
// idsize--;
85
// }
86
// }
87

88         ordertype = ORDER_PARALLELIZATION ;
89     }
90
91     // #############################################################################
92
// VISITOR STUFF
93
// #############################################################################
94

95     public void accept(OperatorVisitor visitor) throws MediatorException {
96         visitor.visit(this);
97     }
98
99     // ***********************************************************************
100
// * EXECUTE QUERY
101
// ***********************************************************************
102
/**
103      *
104      */

105     public ResultSet getResultSet(DynamicContext context, OperatorRunnable[] runnables) throws MediatorException {
106         try {
107             return new CartesianResultSet(this, context, runnables) ;
108         }
109         catch (XMLDBCException e) {
110             throw new MediatorException("Can't construct " + getClass() + ": " + e.getMessage(), e) ;
111         }
112     }
113
114     // **********************************************************************
115
// * OPTIMIZE
116
// **********************************************************************
117

118
119     // **********************************************************************
120
// * DEBUG
121
// **********************************************************************
122
/**
123      *
124      */

125 }
126
127 class CartesianResultSet extends BinResultSet {
128     // **********************************************************************
129
// * VERSIONING
130
// **********************************************************************
131
private static final String JavaDoc RCSRevision = "$Revision: 1.7 $";
132     private static final String JavaDoc RCSName = "$Name: $"; // * CLASS VARIABLES
133
// ***********************************************************************
134
private int index = -1;
135     private Tuple tuplepivot = null;
136     ArrayList JavaDoc tuplelist = null;
137     // ***********************************************************************
138
// * INITIALIZATION
139
// ***********************************************************************
140
/**
141      *
142      */

143     public CartesianResultSet(OpCartesian operator, DynamicContext context, OperatorRunnable[] children) throws XMLDBCException {
144         super(operator, context, children) ;
145     }
146
147     // ***********************************************************************
148
// * EVALUATE IMPLEMENTATION
149
// ***********************************************************************
150
/**
151      * In a cartesian product, we can all do in parallel, so we
152      * must generate all results of both source and then make the cartesian
153      * product.
154      *
155      * @param non_blocking if the evaluation is blocking, we must
156      * generate all the results before passing.
157      * A cartesian product is necessary blocking.
158      */

159     
160     protected void evaluate(boolean non_blocking) throws MediatorException {
161         while (true) {
162             Tuple tuple = null;
163             if (index == -1) {
164                 // verify that tuplepivot is not null and set it (if not finish)
165
if (tuplepivot == null) {
166                     if (children[0].getResultSet().hasNext())
167                         tuplepivot = children[0].getResultSet().next();
168                     else {
169                         setFinishedWhenEmpty();
170                         children[0].getResultSet().close();
171                         children[1].getResultSet().close();
172                         return;
173                     }
174                 }
175                 if (children[1].getResultSet().hasNext()) {
176                     tuple = children[1].getResultSet().next();
177                     if (tuplelist == null)
178                         tuplelist = new ArrayList JavaDoc();
179                     tuplelist.add(tuple);
180                 } else {
181                     if (tuplelist != null && children[0].getResultSet().hasNext()) {
182                         tuplepivot = children[0].getResultSet().next();
183                         index = 0;
184                         tuple = (Tuple) tuplelist.get(index++);
185                     } else {
186                         setFinishedWhenEmpty();
187                         children[0].getResultSet().close();
188                         children[1].getResultSet().close();
189                         return;
190                     }
191                 }
192             } else {
193                 if (index < tuplelist.size())
194                     tuple = (Tuple) tuplelist.get(index++);
195                 else {
196                     if (children[0].getResultSet().hasNext()) {
197                         tuplepivot = children[0].getResultSet().next();
198                         index = 0;
199                         tuple = (Tuple) tuplelist.get(index++);
200                     } else {
201                         setFinishedWhenEmpty();
202                         children[0].getResultSet().close();
203                         return;
204                     }
205                 }
206             }
207             buftuples.add(tuplepivot.merge(tuple, operator));
208             if (non_blocking)
209                 break;
210         }
211     }
212 }
213
Popular Tags