1 28 29 package org.objectweb.jonas_ejb.container.jorm; 30 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import javax.ejb.EJBException ; 36 37 import org.objectweb.jonas_ejb.container.JContainer; 38 import org.objectweb.jonas_ejb.container.TraceEjb; 39 import org.objectweb.jonas_ejb.deployment.api.DeploymentDescEjb2; 40 import org.objectweb.jonas_ejb.deployment.api.EntityCmp2Desc; 41 import org.objectweb.jonas_ejb.deployment.api.EntityDesc; 42 import org.objectweb.jonas_ejb.deployment.api.MethodCmp2Desc; 43 import org.objectweb.jonas_ejb.lib.EjbqlLimiterRange; 44 import org.objectweb.jonas_ejb.lib.EjbqlQueryTreeHolder; 45 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 46 import org.objectweb.jorm.metainfo.api.Manager; 47 import org.objectweb.medor.api.EvaluationException; 48 import org.objectweb.medor.api.MedorException; 49 import org.objectweb.medor.eval.api.ConnectionResources; 50 import org.objectweb.medor.eval.api.QueryEvaluator; 51 import org.objectweb.medor.eval.lib.BasicEvaluationMetaData; 52 import org.objectweb.medor.eval.prefetch.api.PrefetchBuffer; 53 import org.objectweb.medor.eval.prefetch.lib.PrefetchBufferFactoryImpl; 54 import org.objectweb.medor.expression.api.ParameterOperand; 55 import org.objectweb.medor.expression.api.TypingException; 56 import org.objectweb.medor.lib.Log; 57 import org.objectweb.medor.optim.api.ExecPlanGenerator; 58 import org.objectweb.medor.optim.api.QueryTransformer; 59 import org.objectweb.medor.optim.jorm.Jorm2Rdb; 60 import org.objectweb.medor.optim.lib.BasicQueryRewriter; 61 import org.objectweb.medor.optim.lib.FlattenQueryTreeRule; 62 import org.objectweb.medor.optim.lib.IndexesGenerator; 63 import org.objectweb.medor.query.api.QueryLeaf; 64 import org.objectweb.medor.query.api.QueryTree; 65 import org.objectweb.medor.tuple.api.TupleCollection; 66 import org.objectweb.util.monolog.api.BasicLevel; 67 68 74 public abstract class MedorFactory extends JormFactory { 75 76 80 protected QueryTransformer queryTransformer = null; 81 82 protected Manager miManager = null; 83 84 protected ExecPlanGenerator indexesGenerator; 85 86 protected boolean optimizeAtInit = false; 87 88 91 private int prefetchIndex; 92 93 96 private EjbqlLimiterRange[] limiterRanges = null; 97 98 public MedorFactory() { 99 super(); 100 if (Log.loggerFactory != TraceEjb.loggerFactory) { 101 Log.loggerFactory = TraceEjb.loggerFactory; 102 } 103 } 104 105 112 public synchronized QueryEvaluator getOptimizedRequest(MethodCmp2Desc methodDesc) throws MedorException { 113 if (TraceEjb.isVerbose()) { 114 TraceEjb.logger.log(BasicLevel.DEBUG, "getOptimizedRequest(method name: " + methodDesc.getMethod().getName() 115 + ")"); 116 } 117 try { 118 EjbqlQueryTreeHolder qth = methodDesc.getQueryTreeHolder(mapper); 119 setPrefetchIndex(qth.getPrefetchIndex()); 121 if (qth.getQueryOptimizer() == null) { 123 if (queryTransformer == null) { 124 ArrayList rules = new ArrayList (2); 125 rules.add(new FlattenQueryTreeRule()); 126 rules.add(new Jorm2Rdb()); 127 queryTransformer = new BasicQueryRewriter(rules); 128 } 129 qth.setQueryOptimizer(new QueryTransformer() { 130 131 public QueryTree transform(QueryTree qt) throws MedorException { 132 QueryTree qt1 = queryTransformer.transform(qt); 133 if (indexesGenerator == null) { 138 throw new Error ("getOptimizedRequest: indexesGenerator == null"); 139 } 140 return indexesGenerator.transform(qt1); 141 } 142 }); 143 } 144 limiterRanges = qth.getLimiterRanges(); 145 return qth.getOptimizedQueryTree(); 146 } catch (Exception e) { 147 throw new MedorException("Impossible to optimize the query " + methodDesc.getQuery(), e); 148 } 149 } 150 151 154 161 162 171 public TupleCollection evaluate(Object conn, int methodIndex, ParameterOperand[] parameters) throws MedorException { 172 173 MethodCmp2Desc mcd = (MethodCmp2Desc) dd.getMethodDesc(methodIndex); 174 QueryEvaluator evaluator = getOptimizedRequest(mcd); 175 176 ConnectionResources cr = evaluator.getRequiredConnectionResources(); 178 179 QueryLeaf[] leaves = cr.getRequiredQueryLeafConnection(); 181 182 if (conn == null) { 187 throw new MedorException("invalid connection handle [null]"); 188 } 189 for (int i = 0; (i < leaves.length); i++) { 190 cr.setConnection(leaves[i], conn); 191 } 192 PrefetchBuffer prefetchBuffer = null; 194 if (mcd.getPrefetch()) { 195 Object tx = null; 196 try { 197 tx = this.getTransactionManager().getTransaction(); 198 if (tx != null) { 199 prefetchBuffer = mapper.getPrefetchCache().createPrefetchBuffer(new PrefetchBufferFactoryImpl(), 200 this, tx, getPrefetchIndex(), true); 201 } 202 } catch (javax.transaction.SystemException e) { 203 TraceEjb.logger.log(BasicLevel.ERROR, 205 "Cannot get the current transaction to create the Prefetch buffer:", e); 206 } 207 } 208 Map evalMDMap = null; 210 if (limiterRanges.length > 0) { 211 try { 212 evalMDMap = new HashMap (); 214 BasicEvaluationMetaData evalMD = new BasicEvaluationMetaData(); 215 EjbqlLimiterRange range = limiterRanges[0]; 216 if (range.getKind() == EjbqlLimiterRange.KIND_PARAMETER) { 217 evalMD.setLimitedRangeStartAt(parameters[range.getValue()].getInt()); 218 } else { 219 evalMD.setLimitedRangeStartAt(range.getValue()); 220 } 221 if (TraceEjb.isDebugQuery()) { 222 TraceEjb.query.log(BasicLevel.LEVEL_DEBUG, "Query with LIMITer Range Start At = " 223 + evalMD.getLimitedRangeStartAt() + " for method name " + mcd.getMethod().getName()); 224 } 225 if (limiterRanges.length > 1) { 226 range = limiterRanges[1]; 227 if (range.getKind() == EjbqlLimiterRange.KIND_PARAMETER) { 228 System.out.println("parameters[range.getValue()].getInt() = " 229 + parameters[range.getValue()].getInt()); 230 evalMD.setLimitedRangeSize(parameters[range.getValue()].getInt()); 231 } else { 232 evalMD.setLimitedRangeSize(range.getValue()); 233 } 234 if (TraceEjb.isDebugQuery()) { 235 TraceEjb.query.log(BasicLevel.LEVEL_DEBUG, "Query with LIMITer Range Size = " 236 + evalMD.getLimitedRangeSize() + " for method name " + mcd.getMethod().getName()); 237 } 238 } 239 evalMDMap.put(leaves[0], evalMD); 240 } catch (TypingException te) { 241 throw new MedorException("Invalid parameter for a LIMIT range", te); 242 } 243 } 244 try { 245 return evaluator.evaluate(parameters, cr, prefetchBuffer, evalMDMap); 246 } catch (EvaluationException e) { 247 throw e; 248 } 249 } 250 251 public void init(EntityDesc ed, JContainer c, String mapperName) { 252 super.init(ed, c, mapperName); 253 254 ecd = (EntityCmp2Desc) ed; 255 256 if (TraceEjb.isVerbose()) { 258 TraceEjb.logger.log(BasicLevel.DEBUG, "Jorm Meta information building"); 259 } 260 DeploymentDescEjb2 dd = ecd.getDeploymentDescEjb2(); 261 try { 262 miManager = dd.getJormManager(); 263 } catch (DeploymentDescException e) { 264 TraceEjb.logger.log(BasicLevel.ERROR, "impossible to load the jorm meta information into the manager", e); 265 throw new EJBException ("impossible to load the jorm meta information into the manager", e); 266 } 267 268 if (TraceEjb.isVerbose()) { 270 TraceEjb.logger.log(BasicLevel.DEBUG, "Medor initialisation"); 271 } 272 indexesGenerator = new IndexesGenerator(); 273 274 } 275 276 private void setPrefetchIndex(int i) { 277 prefetchIndex = i; 278 } 279 280 private int getPrefetchIndex() { 281 return prefetchIndex; 282 } 283 284 } | Popular Tags |