KickJava   Java API By Example, From Geeks To Geeks.

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


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 OpMerge extends OpBin {
33     // **********************************************************************
34
// * VERSIONING
35
// **********************************************************************
36
private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38
39     // **********************************************************************
40
// * CLASS VARIABLES
41
// **********************************************************************
42
// ***********************************************************************
43
// * INITIALIZATION
44
// ***********************************************************************
45
/**
46      *
47      */

48     public OpMerge(ExecutionPlan plan, XQueryExpression expression, Operator left, Operator right) throws MediatorException {
49         super(plan, expression, left, right);
50         init();
51     }
52
53 // public OpMerge(ExecutionPlan plan, XQueryExpression expression, ArrayList algebralist) throws MediatorException {
54
// super(plan, expression, algebralist);
55
// init();
56
// }
57

58     private void init() throws MediatorException {
59         // Sometimes, it can happens that the two members of merge product have common attributes. In that case, we must mark them
60
// as common. So when we will generate the tuples, we can access them efficiently.
61
ArrayList JavaDoc childpaths1 = getLeftAlgebra().getPathsList();
62         ArrayList JavaDoc childpaths2 = getRightAlgebra().getPathsList();
63         size = getLeftAlgebra().getSize() + getRightAlgebra().getSize();
64
65 // equivalences = new ArrayList();
66
// int index = -1;
67
// for (int i = 0; i < childpaths2.size(); i++) {
68
// if ((index = childpaths1.indexOf(childpaths2.get(i))) != -1) {
69
// addEquivalence(equivalences, index, i);
70
// size--;
71
// break;
72
// }
73
// }
74

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

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

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

104     protected ResultSet getResultSet(DynamicContext context, OperatorRunnable[] children) throws MediatorException {
105         try {
106             return new MergeResultSet(this, context, children);
107         } catch (XMLDBCException e) {
108             throw new MediatorException("Can't construct " + getClass() + ": " + e.getMessage(), e);
109         }
110     }
111
112     // **********************************************************************
113
// * OPTIMIZE
114
// **********************************************************************
115
/**
116      * Merge *this* node with the subnode(s). As it should be called
117      * only from source, the returned node must be of type AlgSource.
118      * The return node is a AlgSource where the XQueryExpression is the merging
119      * of all child(ren) nodes' expression.
120      */

121     // protected Algebra mergeWithSub() throws MediatorException { return this ; }
122

123     // **********************************************************************
124
// * DEBUG
125
// **********************************************************************
126
/**
127      *
128      */

129 }
130
131 class MergeResultSet extends BinResultSet {
132     // **********************************************************************
133
// * VERSIONING
134
// **********************************************************************
135
private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
136     private static final String JavaDoc RCSName = "$Name: $"; // * CLASS VARIABLES
137
// ***********************************************************************
138

139     private Tuple halftuple2 = null;
140     // ***********************************************************************
141
// * INITIALIZATION
142
// ***********************************************************************
143
public MergeResultSet(OpMerge operator, DynamicContext context, OperatorRunnable[] children) throws XMLDBCException {
144         super(operator, context, children);
145     }
146
147     // ***********************************************************************
148
// * EVALUATE IMPLEMENTATION
149
// ***********************************************************************
150
/**
151      * In a merge product, we can all do in parallel, so we
152      * must generate all results of both source and then make the merge
153      * product.
154      * @param non_blocking if the evaluation is blocking, we must
155      * generate all the results before passing.
156      * A merge is necessarly blocking.
157      */

158     protected void evaluate(boolean non_blocking) throws MediatorException {
159         
160         if (!children[0].getResultSet().hasNext()) {
161             children[0].getResultSet().close();
162             if (halftuple2 == null)
163                 children[1].getResultSet().close();
164             setFinishedWhenEmpty();
165             return;
166         }
167         
168         if (halftuple2 == null) {
169             if (children[1].getResultSet().hasNext())
170                 halftuple2 = children[1].getResultSet().next() ;
171             children[1].getResultSet().close();
172         }
173
174         while (true) {
175             if (children[0].getResultSet().hasNext()) {
176                 Tuple halftuple1 = children[0].getResultSet().next() ;
177                 if (halftuple2 != null)
178                     buftuples.add(halftuple1.merge(halftuple2, operator));
179                 else
180                     buftuples.add(halftuple1.completeTupleForLeft(operator.getSize()));
181                 if (non_blocking) {
182                     // can make higher buffering by modifying this test
183
if ((buftuples != null) && (! buftuples.isEmpty()))
184                         break ;
185                 }
186             }
187             else {
188                 children[0].getResultSet().close();
189                 setFinishedWhenEmpty() ;
190                 return ;
191             }
192         }
193         if (! children[0].getResultSet().hasNext()) {
194             children[0].getResultSet().close();
195             setFinishedWhenEmpty() ;
196         }
197     }
198
199
200 }
201
Popular Tags