1 18 package org.objectweb.speedo.query.lib; 19 20 import org.objectweb.medor.query.api.QueryTree; 21 import org.objectweb.medor.query.api.QueryLeaf; 22 import org.objectweb.medor.eval.api.QueryEvaluator; 23 import org.objectweb.medor.eval.api.ConnectionResources; 24 import org.objectweb.medor.eval.lib.BasicEvaluationMetaData; 25 import org.objectweb.medor.eval.prefetch.api.IntermediaryPrefetchBuffer; 26 import org.objectweb.medor.eval.prefetch.api.PrefetchBuffer; 27 import org.objectweb.medor.eval.prefetch.lib.IntermediaryPrefetchBufferImpl; 28 import org.objectweb.medor.tuple.api.TupleCollection; 29 import org.objectweb.medor.expression.api.ParameterOperand; 30 import org.objectweb.medor.api.MedorException; 31 import org.objectweb.medor.api.EvaluationException; 32 import org.objectweb.jorm.api.PClassMapping; 33 import org.objectweb.jorm.mapper.rdb.lib.RdbPPolymorphicClass; 34 import org.objectweb.speedo.pm.api.ProxyManager; 35 import org.objectweb.speedo.query.api.QueryDefinition; 36 import org.objectweb.speedo.api.ExceptionHelper; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 import javax.jdo.JDOFatalException; 40 41 import java.util.HashMap ; 42 import java.util.Map ; 43 44 52 public class QueryEvalContext { 53 56 public QueryTree query; 57 58 61 public QueryEvaluator evaluator; 62 63 66 public int pnIndex; 67 68 71 public PClassMapping pcm; 72 73 76 public SpeedoCompiledQuery sqc; 77 78 82 public QueryEvalContext(QueryTree qt, 83 SpeedoCompiledQuery sqc) { 84 query = qt; 85 this.sqc = sqc; 86 } 87 88 97 public TupleCollection eval(ProxyManager pm, 98 ParameterOperand[] pos, 99 Object connection, 100 QueryDefinition userqd) throws MedorException { 101 ConnectionResources connRess = 103 evaluator.getRequiredConnectionResources(); 104 105 Map evalMDMap = null; 106 if (userqd != null && (userqd.getIndexFirst() > 0 107 || userqd.getIndexLast() < Integer.MAX_VALUE)) { 108 evalMDMap = new HashMap (); 110 } 111 112 QueryLeaf[] leafs = connRess.getRequiredQueryLeafConnection(); 114 for (int cpt = 0; (cpt < leafs.length); cpt++) { 116 connRess.setConnection(leafs[cpt], connection); 117 if (evalMDMap != null) { 118 BasicEvaluationMetaData evalMD = new BasicEvaluationMetaData(); 120 evalMD.setLimitedRangeStartAt((int) userqd.getIndexFirst()); 121 evalMD.setLimitedRangeSize((int) (userqd.getIndexLast() - userqd.getIndexFirst())); 122 evalMDMap.put(leafs[cpt], evalMD); 123 } 124 } 125 126 PrefetchBuffer prefetchBuffer = null; 127 if (pcm != null) { 128 boolean isPolymorphic = (pcm instanceof RdbPPolymorphicClass); 129 prefetchBuffer = sqc.getMapper().getPrefetchCache().createPrefetchBuffer( 130 sqc.getPrefetchBufferFactory(), 131 pcm, pm.currentTransaction(), 132 pnIndex, 133 pm.getMultithreaded(), 134 !isPolymorphic); 135 if (isPolymorphic) { 137 IntermediaryPrefetchBuffer ipb = new IntermediaryPrefetchBufferImpl( 138 prefetchBuffer, 139 pcm.getIndexesTable(pcm)); 140 if (!pcm.getPMapper().getPrefetchCache().registerPrefetchBuffer( 141 ipb, pcm, pm.currentTransaction())) { 142 throw new MedorException("No prefetch buffer registered for the pcm " + pcm.getClassName() + "."); 143 } 144 try{ 146 PClassMapping[] subPCMs = pcm.getSubPCMs(); 148 if (subPCMs != null) { 149 for (int i = 0; i < subPCMs.length; i++) { 150 IntermediaryPrefetchBuffer intermediaryPb = new IntermediaryPrefetchBufferImpl( 154 prefetchBuffer, 155 subPCMs[i].getIndexesTable(pcm)); 156 if (!subPCMs[i].getPMapper().getPrefetchCache().registerPrefetchBuffer( 157 intermediaryPb, subPCMs[i], pm.currentTransaction())) { 158 throw new MedorException("No prefetch buffer registered for the pcm " + subPCMs[i].getClassName() + "."); 159 } 160 } 161 } 162 } 163 catch(Exception e){ 164 throw new MedorException("Error while trying to register the prefetchBuffer with the subclasses. ", e); 165 } 166 } 167 } 168 if (sqc.getLogger().isLoggable(BasicLevel.DEBUG)) { 170 sqc.getLogger().log(BasicLevel.DEBUG, "Parameters: " + posToString(pos)); 171 } 172 TupleCollection queryResult = null; 173 try { 174 queryResult = evaluator.evaluate(pos, connRess, prefetchBuffer, evalMDMap); 175 } catch (EvaluationException e) { 176 throw new JDOFatalException( 177 "Impossible to evaluate the query: ", 178 ExceptionHelper.getNested(e) 179 ); 180 } 181 return queryResult; 182 } 183 184 187 protected String posToString(ParameterOperand[] pos) { 188 if (pos == null) { 189 return "null"; 190 } 191 StringBuffer sb = new StringBuffer ("["); 192 String sep = ""; 193 for(int i=0; i<pos.length; i++) { 194 sb.append(sep); 195 sb.append("(name='"); 196 sb.append("'"); 197 sb.append(pos[i].getName()); 198 sb.append("', value="); 199 sb.append(pos[i]); 200 sb.append(")"); 201 } 202 sb.append("]"); 203 return sb.toString(); 204 } 205 } 206 | Popular Tags |