KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > query > lib > QueryEvalContext


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

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 JavaDoc;
42 import java.util.Map JavaDoc;
43
44 /**
45  * Represent a Medor query executed for a JDO query. It contains the query, the
46  * evaluator, a link to the SpeedoCompiledQuery and some information about
47  * the data prefetching. A JDO can correspond to several Medor query in case of
48  * inheritance for example.
49  *
50  * @author S.Chassande-Barrioz
51  */

52 public class QueryEvalContext {
53     /**
54      * The medor querytree representing the query
55      */

56     public QueryTree query;
57
58     /**
59      * the evaluator of the query
60      */

61     public QueryEvaluator evaluator;
62
63     /**
64      * is the index of the identifier (prefetch index too)
65      */

66     public int pnIndex;
67
68     /**
69      * the PClassMapping of the prefetched class
70      */

71     public PClassMapping pcm;
72
73     /**
74      * The original speedo compiled query
75      */

76     public SpeedoCompiledQuery sqc;
77
78     /**
79      * @param qt is the medor querytree representing the query
80      * @param sqc is the original speedo compiled query
81      */

82     public QueryEvalContext(QueryTree qt,
83                             SpeedoCompiledQuery sqc) {
84         query = qt;
85         this.sqc = sqc;
86     }
87
88     /**
89      * Executes the sub query
90      * @param pm is the current persistence manager
91      * @param pos is the parameter of the query
92      * @param connection is the way to reach the persistence support
93      * @return a TupleCollection containing the result
94      * @throws MedorException
95      *
96      */

97     public TupleCollection eval(ProxyManager pm,
98                                 ParameterOperand[] pos,
99                                 Object JavaDoc connection,
100                                 QueryDefinition userqd) throws MedorException {
101         // Calculates and gets the required connection ressources for this query
102
ConnectionResources connRess =
103             evaluator.getRequiredConnectionResources();
104
105         Map JavaDoc evalMDMap = null;
106         if (userqd != null && (userqd.getIndexFirst() > 0
107                 || userqd.getIndexLast() < Integer.MAX_VALUE)) {
108             //range values specified
109
evalMDMap = new HashMap JavaDoc();
110         }
111
112         // Gets the QueryLeafs that require connections
113
QueryLeaf[] leafs = connRess.getRequiredQueryLeafConnection();
114         // Setting QueryLeaf's appropriated connection Object
115
for (int cpt = 0; (cpt < leafs.length); cpt++) {
116             connRess.setConnection(leafs[cpt], connection);
117             if (evalMDMap != null) {
118                 //range values specified
119
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 the class is polymorphic, register an intermediary prefetch buffer
136
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                 //register the prefetch buffer for all the subclasses
145
try{
146                     //get the list of all the sub pclassmappings
147
PClassMapping[] subPCMs = pcm.getSubPCMs();
148                     if (subPCMs != null) {
149                         for (int i = 0; i < subPCMs.length; i++) {
150                             //create an intermediary prefetchBuffer that has the prefetchBuffer
151
//of the superclass as delegate prefetch buffer
152
//and the association table for the pcm of the superclass
153
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 JavaDoc e){
164                     throw new MedorException("Error while trying to register the prefetchBuffer with the subclasses. ", e);
165                 }
166             }
167         }
168         // Launching Medor Evaluator...
169
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     /**
185      * Produces a String representation of parameter for logging.
186      */

187     protected String JavaDoc posToString(ParameterOperand[] pos) {
188         if (pos == null) {
189             return "null";
190         }
191         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("[");
192         String JavaDoc 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