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.runtime.MediatorException; 30 import org.xquark.xml.xdbc.XMLDBCException; 31 32 public class OpUnionSort extends OpMult { 33 private static final String RCSRevision = "$Revision: 1.9 $"; 37 private static final String RCSName = "$Name: $"; 38 39 private ArrayList orderbys = null; 40 46 47 public OpUnionSort(ExecutionPlan plan, Operator[] operators, ArrayList 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 } 59 60 public ArrayList getOrderBys() { return this.orderbys; } 61 62 66 public void accept(OperatorVisitor visitor) throws MediatorException { 67 visitor.visit(this); 68 } 69 70 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()) ; 83 } 84 } 85 86 95 97 } 101 102 class UnionSortResultSet extends MultResultSet { 103 private static final String RCSRevision = "$Revision: 1.9 $"; 107 private static final String RCSName = "$Name: $"; 108 109 private int index = 0; 110 116 public UnionSortResultSet(OpUnionSort operator, DynamicContext context, OperatorRunnable[] children) throws XMLDBCException { 117 super(operator, context, children) ; 118 } 119 120 129 protected void evaluate(boolean non_blocking) throws MediatorException { 130 131 133 boolean islet = this.operator.isLet(); 134 ArrayList allTuples = new ArrayList () ; 135 int[] indexes = new int[children.length]; 136 Tuple restuple = null; 137 for (int i = 0 ; i < children.length ; i ++) { 138 ArrayList al = new ArrayList () ; 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 orderbys = ((OpUnionSort)operator).getOrderBys(); 154 int index = 0; 155 int size = children.length; 156 restuple = null; 157 while (index < size) { 159 if (indexes[index] != -1) { restuple = (Tuple)((ArrayList )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 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 )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 this.buftuples.add(restuple); 185 indexes[lowindex]++; 186 if (indexes[lowindex] == ((ArrayList )allTuples.get(lowindex)).size()) { 187 indexes[lowindex] = -1; 188 if (lowindex == index) { 189 index++; 190 restuple = null; 191 while (index < size) { 193 if (indexes[index] != -1) { restuple = (Tuple)((ArrayList )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 while (index < size) { 205 if (indexes[index] != -1) { restuple = (Tuple)((ArrayList )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 )allTuples.get(index)).get(indexes[index]); 221 } 222 } 223 this.setFinishedWhenEmpty(); 224 } 225 } 226 | Popular Tags |