KickJava   Java API By Example, From Geeks To Geeks.

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


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.decomposer.Utils;
30 import org.xquark.mediator.runtime.MediatorException;
31
32 public class OpSort extends OpUn {
33     // **********************************************************************
34
// * VERSIONING
35
// **********************************************************************
36
private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38     private ArrayList JavaDoc orderbys = null;
39     // ***********************************************************************
40
// * INITIALIZATION
41
// ***********************************************************************
42
/**
43      *
44      */

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

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

78     protected ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException {
79         return new SortResultSet(this, context, child);
80     }
81
82     // **********************************************************************
83
// * OPTIMIZE
84
// **********************************************************************
85
/**
86      * Merge *this* node with the subnode(s). As it should be called
87      * only from source, the returned node must be of type AlgSource.
88      * The return node is a AlgSource where the XQueryExpression is the merging
89      * of all child(ren) nodes' expression.
90      */

91     // protected Algebra mergeWithSub() throws MediatorException { return this ; }
92

93     // **********************************************************************
94
// * DEBUG
95
// **********************************************************************
96

97     public String JavaDoc toCompleteString(int indent) {
98         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
99         buf.append(Utils.makeIndent(indent) + "<" + getClass().getName() + " isLet=\"" + isLet() + "\">\n");
100         buf.append(Utils.makeIndent(indent + 1) + "<Expression>\n");
101         buf.append(Utils.makeIndent(indent + 2) + expression + "\n");
102         buf.append(Utils.makeIndent(indent + 1) + "</Expression>\n");
103         // if (variables != null) {
104
// buf.append(Utils.makeIndent(indent + 1) + "<Variables>\n") ;
105
// for (int i = 0 ; i < variables.size() ; i ++) {
106
// buf.append(Utils.makeIndent(indent + 2) + "<Variable>" + variables.get(i) + "</Variable>\n") ;
107
// }
108
// buf.append(Utils.makeIndent(indent + 1) + "</Variables>\n") ;
109
// }
110
if (paths != null) {
111             buf.append(Utils.makeIndent(indent + 1) + "<Paths>\n");
112             for (int i = 0; i < paths.size(); i++) {
113                 buf.append(Utils.makeIndent(indent + 2) + "<Path>" + paths.get(i) + "</Path>\n");
114             }
115             buf.append(Utils.makeIndent(indent + 1) + "</Paths>\n");
116         }
117         if (orderbys != null) {
118             buf.append(Utils.makeIndent(indent + 1) + "<Orderbys>\n");
119             for (int i = 0; i < orderbys.size(); i++) {
120                 buf.append(Utils.makeIndent(indent + 2) + "<Orderby>" + orderbys.get(i) + "</Orderby>\n");
121             }
122             buf.append(Utils.makeIndent(indent + 1) + "</Orderbys>\n");
123         }
124         buf.append(Utils.makeIndent(indent + 1) + "<Element>\n");
125         if (this.childOperator != null)
126             buf.append(this.childOperator.toCompleteString(indent + 2));
127         buf.append(Utils.makeIndent(indent + 1) + "</Element>\n");
128         buf.append(Utils.makeIndent(indent) + "</" + getClass().getName() + ">\n");
129         return buf.toString();
130     }
131
132 }
133
134 class SortResultSet extends UnResultSet {
135     // **********************************************************************
136
// * VERSIONING
137
// **********************************************************************
138
private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
139     private static final String JavaDoc RCSName = "$Name: $";
140     private int index = 0;
141     // ***********************************************************************
142
// * INITIALIZATION
143
// ***********************************************************************
144
/**
145      *
146      */

147     public SortResultSet(OpSort operator, DynamicContext context, OperatorRunnable child) throws MediatorException {
148         super(operator, context, child);
149     }
150
151     // ***********************************************************************
152
// * EVALUATE IMPLEMENTATION
153
// ***********************************************************************
154
/**
155      *
156      * @param non_blocking if the evaluation is blocking, we must
157      * generate all the results before passing.
158      * A cartesian product is necessary blocking.
159      */

160     protected void evaluate(boolean non_blocking) throws MediatorException {
161
162         //non_blocking = false;
163

164         boolean islet = this.operator.isLet();
165         ArrayList JavaDoc orderbys = ((OpSort) operator).getOrderBys();
166         int index = 0;
167         if (!resultset.hasNext()) {
168             resultset.close();
169             setFinishedWhenEmpty();
170             return;
171         }
172         this.buftuples.add(resultset.next());
173         while (resultset.hasNext()) {
174             Tuple tuple = resultset.next();
175             int i;
176             boolean inserted = false;
177             for (i = this.buftuples.size() - 1; i >= 0; i--) {
178                 Tuple tuplei = (Tuple) this.buftuples.get(i);
179                 if (!DOMUtils.orderTuple(tuple, tuplei, orderbys)) {
180                     this.buftuples.add(i + 1, tuple);
181                     inserted = true;
182                     break;
183                 }
184             }
185             if (!inserted)
186                 this.buftuples.add(0, tuple);
187         }
188         this.setFinishedWhenEmpty();
189         return;
190     }
191 }
192
Popular Tags