KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > eval > lib > MedorEvaluator


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

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 JavaDoc;
45 import java.util.Stack JavaDoc;
46 import java.util.Map JavaDoc;
47
48 /**
49  * This class is a simple evaluator of query tree, supporting the data
50  * prefetching
51  *
52  * @author S.Chassande-Barrioz
53  */

54 public class MedorEvaluator implements QueryEvaluator {
55
56     /**
57      * The query tree which must be evaluated
58      */

59     private QueryTree query;
60
61     /**
62      * The query tree to evalued, stored as a stack
63      */

64     private Stack JavaDoc queryTreeStack;
65
66     /**
67      * The factory of wrappers (leaf evaluator)
68      */

69     private WrapperFactory wrapperFactory;
70
71     /**
72      * The factory of node evaluator (QueryNode evaluator)
73      */

74     private NodeEvaluatorFactory nodeEvaluatorFactory;
75
76     /**
77      * The cache size for each node evaluator
78      */

79     private long cacheSize;
80
81     public MedorEvaluator(QueryTree query, long cacheSize)
82             throws EvaluationException {
83         this.query = query;
84         this.cacheSize = cacheSize; // The all cahe size
85
nodeEvaluatorFactory = new IteratifNodeEvaluatorFactory();
86         wrapperFactory = new MedorWrapperFactory();
87         queryTreeStack = new Stack JavaDoc();
88         toStack(query, queryTreeStack);
89     }
90
91     private void toStack(QueryTree query, Stack JavaDoc 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     // IMPLEMENTATION OF THE QueryEvaluator INTERFACE //
104
//------------------------------------------------//
105

106     public WrapperFactory getWrapperFactory() {
107         return wrapperFactory;
108     }
109
110     /**
111      * First, it allocates a tree of Evaluator (Wrapper or NodeEvaluator) for
112      * the query tree associated to this evaluator. In a second step it launches
113      * the evalution of the query.
114      * @param parameters is the array of parameters
115      * @param resources is the holder of connection used by the wrapper to
116      * access the data support.
117      */

118     public TupleCollection evaluate(ParameterOperand[] parameters,
119                                     ConnectionResources resources,
120                                     Map JavaDoc evaluationMDMap)
121             throws EvaluationException {
122         return evaluate(parameters, resources, null, evaluationMDMap);
123     }
124
125     /**
126      * @param parameters is a ConnectionResources containing associations between
127      * the DataStore names used in the QueryTree and the connection objects to
128      * use for the evaluation.
129      * @param prefetchBuffer is the prefetchBuffer instance where buffer data
130      * must be put. If this field is null, then no data prefetching mus be done.
131      */

132     public TupleCollection evaluate(ParameterOperand[] parameters,
133                                     ConnectionResources resources,
134                                     PrefetchBuffer prefetchBuffer,
135                                     Map JavaDoc evalMDMap)
136             throws EvaluationException {
137         try {
138             Stack JavaDoc runTimeStack = new Stack JavaDoc();
139             for (int cpt = queryTreeStack.size() - 1; cpt >= 0; cpt--) {
140                 Object JavaDoc queryTree = queryTreeStack.elementAt(cpt);
141                 if (queryTree instanceof QueryLeaf) {
142                     //allocate a wrapper for the query leaf
143
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                     //allocate a node evaluator for the query node
155
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 JavaDoc qls = new HashMap JavaDoc(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