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.EvaluationVisitor; 29 import org.xquark.mediator.DOMUtils.Tuple; 30 import org.xquark.mediator.algebra.Algebra; 31 import org.xquark.mediator.runtime.MediatorException; 32 import org.xquark.xpath.datamodel.TypedNode; 33 import org.xquark.xquery.parser.Variable; 34 import org.xquark.xquery.parser.XQueryException; 35 import org.xquark.xquery.parser.XQueryExpression; 36 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionCOUNT; 37 38 public class OpSimpleMerge extends OpUn { 39 private static final String RCSRevision = "$Revision: 1.14 $"; 43 private static final String RCSName = "$Name: $"; private boolean onlyVar = false; 46 47 public OpSimpleMerge(ExecutionPlan plan, XQueryExpression expression, Variable var, Operator childexp, boolean isLet) throws MediatorException { 48 super(plan, expression, childexp); 49 init(var); 50 this.isLet(isLet); 51 } 52 53 public OpSimpleMerge(ExecutionPlan plan, Algebra algebra, Operator childexp) throws MediatorException { 54 super(plan, algebra.getExpression(), childexp); 55 Variable tmpVar = (Variable) algebra.getVariables().get(0); 56 if (algebra.getPaths().isEmpty()) 57 init(tmpVar); 58 else { 59 ArrayList tmpPaths = algebra.getPaths(); 60 addPaths(tmpPaths); 61 size = childOperator.getSize() + tmpPaths.size(); 62 idsize = childOperator.getIdSize(); 63 if (plan.getIdVars().contains(tmpVar)) 64 idsize++; 65 if (tmpPaths.size() == 1 && ((XQueryExpression) tmpPaths.get(0)).getStringValue().equals(tmpVar.getStringValue())) 66 onlyVar = true; 67 } 68 this.isLet(algebra.isLet()); 69 } 70 71 private void init(Variable var) { 72 addPath(var); 74 size = childOperator.getSize() + 1; 75 idsize = childOperator.getIdSize(); 76 if (plan.getIdVars().contains(var)) 77 idsize++; 78 onlyVar = true; 79 } 80 81 public boolean isOnlyVar() { 82 return onlyVar; 83 } 84 85 89 public void accept(OperatorVisitor visitor) throws MediatorException { 90 visitor.visit(this); 91 } 92 93 99 protected ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException { 100 return new SimpleMergeResultSet(this, context, child); 101 } 102 103 109 110 116 } 117 118 class SimpleMergeResultSet extends UnResultSet { 119 private static final String RCSRevision = "$Revision: 1.14 $"; 123 private static final String RCSName = "$Name: $"; EvaluationVisitor visitor = null; 126 129 public SimpleMergeResultSet(OpSimpleMerge operator, DynamicContext context, OperatorRunnable child) throws MediatorException { 130 super(operator, context, child); 131 } 132 133 140 protected void evaluate(boolean non_blocking) throws MediatorException { 141 142 ArrayList idxsteps = new ArrayList (); 143 idxsteps.add(new Integer (0)); 144 boolean islet = this.operator.isLet(); 145 XQueryExpression expression = this.operator.getExpression(); 146 147 if (!resultset.hasNext()) { 148 try { 149 EvaluationVisitor visitor = new EvaluationVisitor(this.operator.getPlan().getSchemaManager()); 150 Tuple newtuple = new Tuple(operator.getSize()-resultset.getOperator().getSize(),operator.getIdSize()-resultset.getOperator().getIdSize()); 151 newtuple.setParent(buftuples); 152 visitor.reset(newtuple); 153 visitor.setReturnType(EvaluationVisitor.NODE_TYPE); 154 expression.accept(visitor); 155 ArrayList nodes = visitor.getResNodes(); 156 if (nodes != null) 157 merge(islet,newtuple,nodes); 158 } catch (XQueryException xqe) { 159 throw new MediatorException(xqe.getMessage(), xqe); 160 } 161 setFinishedWhenEmpty(); 162 resultset.close(); 163 return; 164 } 165 166 while (true) { 167 if (resultset.hasNext()) { 168 Tuple tuple = resultset.next(); 169 ArrayList nodes = tuple.getPath(expression.getStringValue()); 170 if (nodes == null) { 171 try { 172 if (visitor == null) 173 visitor = new EvaluationVisitor(this.operator.getPlan().getSchemaManager()); 174 visitor.reset(tuple); 175 visitor.setReturnType(EvaluationVisitor.NODE_TYPE); 176 expression.accept(visitor); 177 nodes = visitor.getResNodes(); 178 } catch (XQueryException e) { 179 throw new MediatorException(e.getMessage(), e); 180 } 181 } 182 merge(islet, tuple, nodes); 183 if (non_blocking) { 184 if ((buftuples != null) && (!buftuples.isEmpty())) { 186 break; 187 } 188 } 189 } else { 190 setFinishedWhenEmpty(); 191 resultset.close(); 192 return; 193 } 194 } 195 } 196 197 private void merge(boolean islet, Tuple tuple, ArrayList nodes) throws MediatorException { 198 Tuple newtuple = null; 199 if (islet) { 200 if (((OpSimpleMerge) operator).isOnlyVar()) { 201 tuple.mergeInPlace(nodes, this.operator); 202 buftuples.add(tuple); 203 } else { 204 newtuple = buftuples.newTuple(); 205 for (int j = 0; j < paths.size(); j++) { 206 String pathStrj = ((XQueryExpression) paths.get(j)).getStringValue(); 207 int index = tuple.getParent().getPathIndex(pathStrj); 209 if (index != -1) { 210 newtuple.addNodesAtIndex(j, tuple.getNodesAtIndex(index)); 211 } else if (pathStrj.equals(expression.getStringValue())) { 212 newtuple.addNodesAtIndex(j, nodes); 213 } else { 214 for (int i = 0; i < nodes.size(); i++) { 215 ArrayList reslist = DOMUtils.getSteps((TypedNode) nodes.get(i), (XQueryExpression) paths.get(j)); 216 if (reslist != null) 217 newtuple.addNodesAtIndex(j, reslist); 218 } 219 } 220 } 221 newtuple.fillIdentifiers(); 222 buftuples.add(newtuple); 223 } 224 } else { 225 if (nodes != null) { 226 if (((OpSimpleMerge) operator).isOnlyVar()) { 227 for (int i = 0; i < nodes.size(); i++) { 228 newtuple = new Tuple(tuple); 229 newtuple.mergeInPlace((TypedNode) nodes.get(i), this.operator); 230 buftuples.add(newtuple); 231 } 232 } else { 233 for (int i = 0; i < nodes.size(); i++) { 234 ArrayList nodelist = new ArrayList (1); 235 nodelist.add(nodes.get(i)); 236 newtuple = buftuples.newTuple(); 237 for (int j = 0; j < paths.size(); j++) { 239 String pathStrj = ((XQueryExpression) paths.get(j)).getStringValue(); 240 int index = tuple.getParent().getPathIndex(pathStrj); 242 if (index != -1) { 243 newtuple.addNodesAtIndex(j, tuple.getNodesAtIndex(index)); 244 } else if (pathStrj.equals(expression.getStringValue())) { 245 newtuple.addNodesAtIndex(j, nodes); 246 } else { 247 ArrayList reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j)); 248 if (reslist != null) 249 newtuple.addNodesAtIndex(j, reslist); 250 } 251 } 252 newtuple.fillIdentifiers(); 253 buftuples.add(newtuple); 254 } 255 } 256 257 } 258 } 259 } 260 261 } 262 | Popular Tags |