1 23 package org.objectweb.medor.datasource.lib; 24 25 import org.objectweb.medor.api.EvaluationException; 26 import org.objectweb.medor.api.MedorException; 27 import org.objectweb.medor.api.TupleStructure; 28 import org.objectweb.medor.datasource.api.Wrapper; 29 import org.objectweb.medor.expression.api.ParameterOperand; 30 import org.objectweb.medor.expression.api.Expression; 31 import org.objectweb.medor.expression.api.VariableOperand; 32 import org.objectweb.medor.expression.api.Operand; 33 import org.objectweb.medor.expression.api.ExpressionException; 34 import org.objectweb.medor.expression.lib.BasicVariableOperand; 35 import org.objectweb.medor.query.api.QueryLeaf; 36 import org.objectweb.medor.query.api.TCQueryLeaf; 37 import org.objectweb.medor.tuple.api.Tuple; 38 import org.objectweb.medor.tuple.api.TupleCollection; 39 import org.objectweb.medor.tuple.lib.MemoryTuple; 40 import org.objectweb.medor.eval.api.EvaluationMetaData; 41 42 public class TCWrapper implements Wrapper { 43 44 public TupleCollection fetchData(QueryLeaf ql, 45 ParameterOperand[] parameters, 46 Object conn, 47 EvaluationMetaData evalmd) 48 throws EvaluationException { 49 return new FilteredTC((TCQueryLeaf) ql, parameters); 51 } 52 53 class FilteredTC implements TupleCollection { 54 55 private TCQueryLeaf tcQueryLeaf; 56 private Expression filter; 57 private TupleCollection subTupleCollection; 58 private ParameterOperand[] parameters; 59 private Tuple tupleBuffer1,tupleBuffer2; 60 private VariableOperand[] operandBuffer1,operandBuffer2; 61 private int cursor; 62 private boolean empty, isTheLast = false, isBuffer2Free = false; 63 Operand filterResult; 64 private boolean closed = false; 65 66 public FilteredTC(TCQueryLeaf tcQueryLeaf, ParameterOperand[] parameters) 67 throws EvaluationException { 68 this.tcQueryLeaf = tcQueryLeaf; 69 this.parameters = parameters; 70 this.filter = tcQueryLeaf.getQueryFilter(); 71 try { 72 this.filterResult = filter.compileExpression(); 73 } 74 catch (ExpressionException e) { 75 throw new EvaluationException(e); 76 } 77 78 try { 80 operandBuffer1 = new BasicVariableOperand[tcQueryLeaf.getTupleStructure().getSize()]; 81 operandBuffer2 = new BasicVariableOperand[tcQueryLeaf.getTupleStructure().getSize()]; 82 for (int cpt = 0; (cpt < tcQueryLeaf.getTupleStructure().getSize()); cpt++) { 83 operandBuffer1[cpt] = new BasicVariableOperand(tcQueryLeaf.getTupleStructure().getField(cpt + 1).getType()); 84 operandBuffer2[cpt] = new BasicVariableOperand(tcQueryLeaf.getTupleStructure().getField(cpt + 1).getType()); 85 } 86 87 tupleBuffer1 = new MemoryTuple(operandBuffer1); tupleBuffer2 = new MemoryTuple(operandBuffer2); 89 subTupleCollection = tcQueryLeaf.getTupleCollection(); 90 empty = (subTupleCollection.isEmpty()); 91 if (empty) 92 cursor = 0; 93 else 94 this.init(); 95 } catch (MedorException mexp) { 96 throw new EvaluationException(mexp); 97 } 98 } 99 100 public void close() throws MedorException { 101 closed = true; 102 } 103 104 public TupleStructure getMetaData() throws MedorException { 105 return tcQueryLeaf.getTupleStructure(); 106 } 107 108 public boolean isLast() throws MedorException { 109 return isTheLast; 110 } 111 112 public int card() throws MedorException { 113 return cursor; 114 } 115 116 public boolean next() throws MedorException { 117 Tuple currentTuple = null; 118 boolean moved = false; 119 120 if (isEmpty() || (cursor == -2)) 121 moved = false; 122 else if (isLast()) { 123 cursor = -2; 125 moved = false; 126 } else { 127 try { 128 moved = true; 130 cursor++; 131 do { 132 if (subTupleCollection.next()) { 134 currentTuple = subTupleCollection.getTuple(); 135 filter.evaluate(parameters, currentTuple); 136 } else { 137 isTheLast = true; 138 } 139 } while (!(isTheLast) && !(filterResult.getBoolean())); 140 if (!isTheLast) { 141 if (isBuffer2Free) { 142 tcQueryLeaf.getTupleLoader().loadTuple(currentTuple, operandBuffer2, parameters); 144 isBuffer2Free = false; 145 } else { 146 tcQueryLeaf.getTupleLoader().loadTuple(currentTuple, operandBuffer1, parameters); 148 isBuffer2Free = true; 149 } 150 } else 151 isBuffer2Free = !isBuffer2Free; 152 } 153 catch (ExpressionException e) { 154 throw new MedorException(e); 155 } 156 } 157 return moved; 158 } 159 160 161 165 private boolean init() throws MedorException { 166 isTheLast = false; 167 Tuple currentTuple; 168 isBuffer2Free = false; 169 subTupleCollection.first(); 170 currentTuple = subTupleCollection.getTuple(); 171 try { 172 filter.evaluate(parameters, currentTuple); 173 while (!empty && !filterResult.getBoolean()) { 174 if (subTupleCollection.next()) { 175 currentTuple = subTupleCollection.getTuple(); 176 filter.evaluate(parameters, currentTuple); 177 } else { 178 empty = true; } 180 } 181 if (empty) { 182 cursor = -2; 183 } else { 184 tcQueryLeaf.getTupleLoader().loadTuple(currentTuple, operandBuffer1, parameters); 186 cursor = 1; 187 do { 188 if (subTupleCollection.next()) { 190 currentTuple = subTupleCollection.getTuple(); 191 filter.evaluate(parameters, currentTuple); 192 } else { 193 isTheLast = true; 195 } 196 } while (!(isTheLast) && !(filterResult.getBoolean())); 197 if (!isTheLast) { 198 tcQueryLeaf.getTupleLoader().loadTuple(currentTuple, operandBuffer2, parameters); 200 } 201 } 202 } 203 catch (ExpressionException e) { 204 throw new MedorException(e); 205 } 206 return (!empty); 207 } 208 209 public void first() throws MedorException { 210 init(); 212 } 213 214 public int getRow() throws MedorException { 215 return cursor; 216 } 217 218 public Tuple getTuple() throws MedorException { 219 if (!isEmpty() && getRow() >= 1) { 220 if (isBuffer2Free) 221 return tupleBuffer2; 222 else 223 return tupleBuffer1; 224 } else 225 throw new MedorException(" No elements fetched in this TupleCollection"); 226 } 227 228 public synchronized Tuple getTuple(int numTuple) throws MedorException { 229 row(numTuple); 230 return getTuple(); 231 } 232 233 public boolean isEmpty() throws MedorException { 234 return empty; 235 } 236 237 public boolean row(int numTuple) throws MedorException { 238 first(); 239 int cpt = 1; 240 boolean stop = false; 241 while ((cpt < numTuple) && !stop) { 242 if (!next()) 243 stop = true; 244 else 245 cpt++; 246 } 247 if (cpt < numTuple) 248 return false; 249 else { 250 cursor = numTuple; 251 return true; 252 } 253 } 254 } 255 } 256 | Popular Tags |