1 23 24 package org.objectweb.medor.eval.lib; 25 26 import org.objectweb.medor.api.MedorException; 27 import org.objectweb.medor.api.TupleStructure; 28 import org.objectweb.medor.eval.api.BinaryEvaluatedTC; 29 import org.objectweb.medor.eval.api.NodeEvaluator; 30 import org.objectweb.medor.eval.prefetch.api.PrefetchBuffer; 31 import org.objectweb.medor.eval.prefetch.api.PrefetchBufferHolder; 32 import org.objectweb.medor.expression.api.ParameterOperand; 33 import org.objectweb.medor.expression.api.VariableOperand; 34 import org.objectweb.medor.expression.api.ExpressionException; 35 import org.objectweb.medor.expression.lib.BasicVariableOperand; 36 import org.objectweb.medor.query.api.QueryNode; 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.util.monolog.api.BasicLevel; 41 42 43 47 public class ProjectEvaluatedTC 48 extends BasicBinaryEvalutedTC 49 implements BinaryEvaluatedTC, PrefetchBufferHolder { 50 51 private QueryNode query; 52 private TupleCollection subResultTC; 53 private ParameterOperand[] parameters; 54 private Tuple currentBuffer; 55 private VariableOperand[] currentAttBuffers; 56 private int cursor,size; 57 private boolean empty; 58 private boolean userHasClosed = false; 60 private PrefetchBuffer prefetchBuffer; 61 62 public ProjectEvaluatedTC(QueryNode query, 63 NodeEvaluator subNodeEvaluator, 64 ParameterOperand[] parameters, 65 PrefetchBuffer pb) throws MedorException { 66 super(); 67 this.parameters = parameters; 68 this.query = query; 69 this.prefetchBuffer = pb; 70 if (prefetchBuffer != null) { 71 pb.setTupleCollection(this); 72 } 73 size = query.getTupleStructure().getSize(); 75 currentAttBuffers = new BasicVariableOperand[size]; 76 for (int cpt = 0; (cpt < query.getTupleStructure().getSize()); cpt++) { 77 currentAttBuffers[cpt] = new BasicVariableOperand( 78 query.getTupleStructure().getField(cpt + 1).getType()); 79 } 80 currentBuffer = new MemoryTuple(currentAttBuffers); 82 subResultTC = subNodeEvaluator.fetchData(parameters); 83 empty = (subResultTC.isEmpty()); 84 if (empty) { 85 cursor = 0; 86 } else { 87 init(); 88 } 89 } 90 91 public void invalidatePrefetchBuffer() throws MedorException { 92 if (debug) { 93 log.log(BasicLevel.DEBUG, "Invalidating PrefetchBuffer " + prefetchBuffer + " for " + this); 94 } 95 prefetchBuffer = null; 96 if (userHasClosed) { 97 if (debug) { 98 log.log(BasicLevel.DEBUG, "The tupleCollection was closed earlier: closing it for real"); 99 } 100 this.close(); 101 } 102 } 103 104 public void close() throws MedorException { 105 if (debug) { 106 log.log(BasicLevel.DEBUG, "Closing TupleCollection " + this); 107 } 108 userHasClosed = true; 109 if (prefetchBuffer == null || prefetchBuffer.isClosed()) { 111 if (debug) { 112 log.log(BasicLevel.DEBUG, "PrefetchBuffer " + prefetchBuffer + " is null or was closed previously: real close of the TupleCollection"); 113 } 114 prefetchBuffer = null; 115 super.close(); 116 if (subResultTC != null) { 117 subResultTC.close(); 118 } 119 } 120 } 121 122 public TupleStructure getMetaData() throws MedorException { 123 return query.getTupleStructure(); 124 } 125 126 public boolean isLast() throws MedorException { 127 return subResultTC.isLast(); 128 } 129 130 public int card() throws MedorException { 131 return cursor; 132 } 133 134 public boolean next() throws MedorException { 135 boolean moved; 136 if (subResultTC.isLast()) { 137 cursor = -2; 139 moved = false; 140 } else { 141 cursor++; 142 subResultTC.next(); 143 getNextResult(); 145 if (prefetchBuffer != null) { 146 prefetchBuffer.addPrefetchTuple(); 147 } 148 moved = true; 149 } 150 return moved; 151 } 152 153 private void init() throws MedorException { 154 subResultTC.first(); 155 getNextResult(); 156 cursor = 1; 157 if (prefetchBuffer != null) { 158 prefetchBuffer.addPrefetchTuple(); 159 } 160 } 161 162 public void first() throws MedorException { 163 init(); 165 } 166 167 public int getRow() throws MedorException { 168 return cursor; 169 } 170 171 public Tuple getTuple() throws MedorException { 172 if (!isEmpty() && (getRow() >= 1)) 173 return currentBuffer; 174 else 175 throw new MedorException(" No elements fetched in this TupleCollection " + getRow()); 176 } 177 178 public synchronized Tuple getTuple(int numTuple) throws MedorException { 179 row(numTuple); 180 return getTuple(); 181 } 182 183 public boolean isEmpty() throws MedorException { 184 return (subResultTC.isEmpty()); 185 } 186 187 public boolean row(int numTuple) throws MedorException { 188 boolean go = subResultTC.row(numTuple); 189 if (!go) { 190 return false; 191 } else { 192 getNextResult(); 193 cursor = numTuple; 194 return true; 195 } 196 } 197 198 public int getLeftTCCursor() throws MedorException { 199 return subResultTC.getRow(); 200 } 201 202 public int getRightTCCursor() throws MedorException { 203 return -1; 204 } 205 206 private void getNextResult() throws MedorException { 207 try { 208 query.getTupleLoader().loadTuple( 209 subResultTC.getTuple(), currentAttBuffers, parameters); 210 } 211 catch (ExpressionException e) { 212 throw new MedorException(e); 213 } 214 } 215 } 216 | Popular Tags |