KickJava   Java API By Example, From Geeks To Geeks.

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


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.Tuple;
29 import org.xquark.mediator.runtime.MediatorException;
30 import org.xquark.xml.xdbc.XMLDBCException;
31
32 public class OpUnionSort extends OpMult {
33     // **********************************************************************
34
// * VERSIONING
35
// **********************************************************************
36
private static final String JavaDoc RCSRevision = "$Revision: 1.9 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38
39     private ArrayList JavaDoc orderbys = null;
40     // ***********************************************************************
41
// * INITIALIZATION
42
// ***********************************************************************
43
/**
44      *
45      */

46
47     public OpUnionSort(ExecutionPlan plan, Operator[] operators, ArrayList JavaDoc orderbys) throws MediatorException {
48         super(plan, null, operators) ;
49         this.orderbys = orderbys;
50         init();
51     }
52
53     private void init() {
54         size = childrenOperator[0].getSize();
55         idsize = childrenOperator[0].getIdSize();
56         //ordertype = ORDER_PARALLELIZATION ;
57
//ordertype = this.ORDER_SERIALIZATION ;
58
}
59
60     public ArrayList JavaDoc getOrderBys() { return this.orderbys; }
61
62     // #############################################################################
63
// VISITOR STUFF
64
// #############################################################################
65

66     public void accept(OperatorVisitor visitor) throws MediatorException {
67         visitor.visit(this);
68     }
69
70     // ***********************************************************************
71
// * EXECUTE QUERY
72
// ***********************************************************************
73
/**
74      *
75      */

76     protected ResultSet getResultSet(DynamicContext context, OperatorRunnable[] children) throws MediatorException {
77         try {
78             return new UnionSortResultSet(this, context, children) ;
79         }
80         catch (XMLDBCException e) {
81             //throw new MediatorException("Can't construct " + getClass() + ": " + e.getMessage(), e) ;
82
throw new MediatorException("Can't construct " + getClass() + ": " + e.getMessage()) ;
83         }
84     }
85
86     // **********************************************************************
87
// * OPTIMIZE
88
// **********************************************************************
89
/**
90      * Merge *this* node with the subnode(s). As it should be called
91      * only from source, the returned node must be of type AlgSource.
92      * The return node is a AlgSource where the XQueryExpression is the merging
93      * of all child(ren) nodes' expression.
94      */

95     // protected Algebra mergeWithSub() throws MediatorException { return this ; }
96

97     // **********************************************************************
98
// * DEBUG
99
// **********************************************************************
100
}
101
102 class UnionSortResultSet extends MultResultSet {
103     // **********************************************************************
104
// * VERSIONING
105
// **********************************************************************
106
private static final String JavaDoc RCSRevision = "$Revision: 1.9 $";
107     private static final String JavaDoc RCSName = "$Name: $";
108
109     private int index = 0;
110     // ***********************************************************************
111
// * INITIALIZATION
112
// ***********************************************************************
113
/**
114      *
115      */

116     public UnionSortResultSet(OpUnionSort operator, DynamicContext context, OperatorRunnable[] children) throws XMLDBCException {
117         super(operator, context, children) ;
118     }
119
120     // ***********************************************************************
121
// * EVALUATE IMPLEMENTATION
122
// ***********************************************************************
123
/**
124      *
125      * @param non_blocking if the evaluation is blocking, we must
126      * generate all the results before passing.
127      * A cartesian product is necessary blocking.
128      */

129     protected void evaluate(boolean non_blocking) throws MediatorException {
130         
131         //non_blocking = false;
132

133         boolean islet = this.operator.isLet();
134         ArrayList JavaDoc allTuples = new ArrayList JavaDoc() ;
135         int[] indexes = new int[children.length];
136         Tuple restuple = null;
137         for (int i = 0 ; i < children.length ; i ++) {
138             ArrayList JavaDoc al = new ArrayList JavaDoc() ;
139             ResultSet subalgi = children[i].getResultSet() ;
140             while (subalgi.hasNext()) {
141                 Tuple tuplei = subalgi.next() ;
142                 if (islet) {
143                     if (restuple == null) restuple = tuplei;
144                     else restuple.appendTuple(tuplei);
145                 }
146                 else al.add(tuplei) ;
147             }
148             if (islet && restuple != null) al.add(restuple);
149             allTuples.add(al) ;
150             if (al.isEmpty()) indexes[i] = -1;
151             else indexes[i] = 0;
152         }
153         ArrayList JavaDoc orderbys = ((OpUnionSort)operator).getOrderBys();
154         int index = 0;
155         int size = children.length;
156         restuple = null;
157         // get first element to be compared with
158
while (index < size) {
159             if (indexes[index] != -1) { restuple = (Tuple)((ArrayList JavaDoc)allTuples.get(index)).get(indexes[index]); break; }
160             else children[index].getResultSet().close();
161             index++;
162         }
163         if (restuple == null) {
164             this.setFinishedWhenEmpty();
165             return;
166         }
167         // it is supposed that all different lists are already ordered
168
int tmpindex = index+1;
169         if (tmpindex == size) tmpindex = 0;
170         while (true) {
171             int lowindex = index;
172             while (tmpindex != index) {
173                 if (indexes[tmpindex] != -1) {
174                     Tuple tmptuple = (Tuple)((ArrayList JavaDoc)allTuples.get(tmpindex)).get(indexes[tmpindex]);
175                     if (!DOMUtils.orderTuple(restuple,tmptuple,orderbys)) {
176                         restuple = tmptuple;
177                         lowindex = tmpindex;
178                     }
179                 }
180                 tmpindex++;
181                 if (tmpindex == size) tmpindex = 0;
182             }
183             // add to buftuples
184
this.buftuples.add(restuple);
185             indexes[lowindex]++;
186             if (indexes[lowindex] == ((ArrayList JavaDoc)allTuples.get(lowindex)).size()) {
187                 indexes[lowindex] = -1;
188                 if (lowindex == index) {
189                     index++;
190                     restuple = null;
191                     // find new index
192
while (index < size) {
193                         if (indexes[index] != -1) { restuple = (Tuple)((ArrayList JavaDoc)allTuples.get(index)).get(indexes[index]); break; }
194                         else children[index].getResultSet().close();
195                         index++;
196                     }
197                     if (restuple == null) break;
198                     tmpindex = index+1;
199                     if (tmpindex == size) tmpindex = 0;
200                 }
201                 else {
202                     restuple = null;
203                     // find new index
204
while (index < size) {
205                         if (indexes[index] != -1) { restuple = (Tuple)((ArrayList JavaDoc)allTuples.get(index)).get(indexes[index]); break; }
206                         else children[index].getResultSet().close();
207                         index++;
208                     }
209                     if (restuple == null) break;
210                     tmpindex = index+1;
211                     if (tmpindex == size) tmpindex = 0;
212                 }
213             }
214             else {
215                 if (lowindex == index) {
216                     tmpindex = index+1;
217                     if (tmpindex == size) tmpindex = 0;
218                 }
219                 else tmpindex = lowindex;
220                 restuple = (Tuple)((ArrayList JavaDoc)allTuples.get(index)).get(indexes[index]);
221             }
222         }
223         this.setFinishedWhenEmpty();
224     }
225 }
226
Popular Tags