1 22 23 package org.xquark.mediator.plan; 24 25 import java.util.ArrayList ; 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 private static final String RCSRevision = "$Revision: 1.8 $"; 37 private static final String RCSName = "$Name: $"; 38 private ArrayList orderbys = null; 39 45 46 public OpSort(ExecutionPlan plan, Operator algebra, ArrayList 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 } 59 60 public ArrayList getOrderBys() { 61 return this.orderbys; 62 } 63 64 68 public void accept(OperatorVisitor visitor) throws MediatorException { 69 visitor.visit(this); 70 } 71 72 78 protected ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException { 79 return new SortResultSet(this, context, child); 80 } 81 82 91 93 97 public String toCompleteString(int indent) { 98 StringBuffer buf = new StringBuffer (); 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 (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 private static final String RCSRevision = "$Revision: 1.8 $"; 139 private static final String RCSName = "$Name: $"; 140 private int index = 0; 141 147 public SortResultSet(OpSort operator, DynamicContext context, OperatorRunnable child) throws MediatorException { 148 super(operator, context, child); 149 } 150 151 160 protected void evaluate(boolean non_blocking) throws MediatorException { 161 162 164 boolean islet = this.operator.isLet(); 165 ArrayList 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 |