1 23 24 package org.objectweb.medor.eval.lib; 25 26 import org.objectweb.medor.api.EvaluationException; 27 import org.objectweb.medor.api.MedorException; 28 import org.objectweb.medor.datasource.api.Wrapper; 29 import org.objectweb.medor.datasource.api.WrapperFactory; 30 import org.objectweb.medor.datasource.lib.MedorWrapperFactory; 31 import org.objectweb.medor.eval.api.ConnectionResources; 32 import org.objectweb.medor.eval.api.NodeEvaluator; 33 import org.objectweb.medor.eval.api.NodeEvaluatorFactory; 34 import org.objectweb.medor.eval.api.QueryEvaluator; 35 import org.objectweb.medor.eval.api.EvaluationMetaData; 36 import org.objectweb.medor.eval.prefetch.api.PrefetchBuffer; 37 import org.objectweb.medor.expression.api.ParameterOperand; 38 import org.objectweb.medor.query.api.QueryLeaf; 39 import org.objectweb.medor.query.api.QueryNode; 40 import org.objectweb.medor.query.api.QueryTree; 41 import org.objectweb.medor.query.rdb.lib.AggregateRdbQueryNode; 42 import org.objectweb.medor.tuple.api.TupleCollection; 43 44 import java.util.HashMap ; 45 import java.util.Stack ; 46 import java.util.Map ; 47 48 54 public class MedorEvaluator implements QueryEvaluator { 55 56 59 private QueryTree query; 60 61 64 private Stack queryTreeStack; 65 66 69 private WrapperFactory wrapperFactory; 70 71 74 private NodeEvaluatorFactory nodeEvaluatorFactory; 75 76 79 private long cacheSize; 80 81 public MedorEvaluator(QueryTree query, long cacheSize) 82 throws EvaluationException { 83 this.query = query; 84 this.cacheSize = cacheSize; nodeEvaluatorFactory = new IteratifNodeEvaluatorFactory(); 86 wrapperFactory = new MedorWrapperFactory(); 87 queryTreeStack = new Stack (); 88 toStack(query, queryTreeStack); 89 } 90 91 private void toStack(QueryTree query, Stack pile) 92 throws EvaluationException { 93 pile.push(query); 94 if ((query instanceof QueryNode) && 95 !(query instanceof AggregateRdbQueryNode)) { 96 QueryTree[] children = ((QueryNode) query).getChildren(); 97 for (int i = (children.length - 1); i >= 0; i--) { 98 toStack(children[i], pile); 99 } 100 } 101 } 102 103 106 public WrapperFactory getWrapperFactory() { 107 return wrapperFactory; 108 } 109 110 118 public TupleCollection evaluate(ParameterOperand[] parameters, 119 ConnectionResources resources, 120 Map evaluationMDMap) 121 throws EvaluationException { 122 return evaluate(parameters, resources, null, evaluationMDMap); 123 } 124 125 132 public TupleCollection evaluate(ParameterOperand[] parameters, 133 ConnectionResources resources, 134 PrefetchBuffer prefetchBuffer, 135 Map evalMDMap) 136 throws EvaluationException { 137 try { 138 Stack runTimeStack = new Stack (); 139 for (int cpt = queryTreeStack.size() - 1; cpt >= 0; cpt--) { 140 Object queryTree = queryTreeStack.elementAt(cpt); 141 if (queryTree instanceof QueryLeaf) { 142 QueryLeaf ql = (QueryLeaf) queryTree; 144 Wrapper w = wrapperFactory.getWrapper(ql.getDataStore()); 145 TupleCollection tc = w.fetchData(ql, parameters, 146 (resources != null 147 ? resources.getConnection(ql) 148 : null), 149 (evalMDMap != null) 150 ? (EvaluationMetaData) evalMDMap.get(ql) 151 : null); 152 runTimeStack.push(new TupleCollectionNodeEvaluator(tc)); 153 } else { 154 QueryNode qn = (QueryNode) queryTree; 156 QueryTree[] children = qn.getChildren(); 157 NodeEvaluator[] evals = new NodeEvaluator[children.length]; 158 for (int i = 0; i < children.length; i++) { 159 evals[i] = (NodeEvaluator) runTimeStack.pop(); 160 evals[i].setCacheSize(cacheSize); 161 } 162 runTimeStack.push(nodeEvaluatorFactory 163 .createNodeEvaluator(qn, 164 evals, 165 (evalMDMap != null) 166 ? (EvaluationMetaData) evalMDMap.get(qn) 167 : null)); 168 } 169 } 170 NodeEvaluator queryTreeHead = ((NodeEvaluator) runTimeStack.pop()); 171 queryTreeHead.setCacheSize(cacheSize); 172 queryTreeHead.setPrefetchBuffer(prefetchBuffer); 173 return queryTreeHead.fetchData(parameters); 174 } catch (MedorException mexp) { 175 throw new EvaluationException(mexp); 176 } 177 } 178 179 public QueryTree getQueryTree() { 180 return query; 181 } 182 183 public long getCacheSize() { 184 return cacheSize; 185 } 186 187 public ConnectionResources getRequiredConnectionResources() { 188 HashMap qls = new HashMap (1); 189 int size = queryTreeStack.size(); 190 for (int i = 0; i < size; i++) { 191 if (queryTreeStack.elementAt(i) instanceof QueryLeaf) { 192 qls.put(queryTreeStack.elementAt(i), null); 193 } 194 } 195 return new MedorConnectionResources(qls); 196 } 197 } 198 | Popular Tags |