001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S. Chassande-Barrioz
025: *
026: */package org.objectweb.speedo.query.lib;
027:
028: import org.objectweb.fractal.api.control.BindingController;
029: import org.objectweb.jorm.api.PMapper;
030: import org.objectweb.medor.eval.prefetch.api.PrefetchBufferFactory;
031: import org.objectweb.medor.eval.prefetch.lib.PrefetchBufferFactoryImpl;
032: import org.objectweb.medor.lib.Log;
033: import org.objectweb.perseus.cache.api.CacheEntryFactory;
034: import org.objectweb.perseus.cache.api.CacheEvent;
035: import org.objectweb.perseus.cache.api.CacheEventListener;
036: import org.objectweb.perseus.cache.api.CacheException;
037: import org.objectweb.perseus.cache.api.CacheManager;
038: import org.objectweb.perseus.cache.api.FixableCacheEntry;
039: import org.objectweb.speedo.mapper.api.JormFactory;
040: import org.objectweb.speedo.pm.api.POManagerFactoryItf;
041: import org.objectweb.speedo.query.api.CompiledQuery;
042: import org.objectweb.speedo.query.api.QueryDefinition;
043: import org.objectweb.speedo.query.api.QueryManager;
044: import org.objectweb.speedo.query.api.QueryManagerAttribute;
045: import org.objectweb.util.monolog.api.BasicLevel;
046: import org.objectweb.util.monolog.api.Logger;
047: import org.objectweb.util.monolog.api.LoggerFactory;
048:
049: /**
050: * SpeedoQueryManager manages the association between SpeedoQuery and the
051: * the compiled query: SpeedoCompiledQuery.
052: *
053: * @author S.Chassande-Barrioz
054: */
055: public abstract class SpeedoQueryManager implements QueryManager,
056: CacheEntryFactory, CacheEventListener, BindingController,
057: QueryManagerAttribute {
058:
059: public final static String MAPPER_BINDING = "mapper";
060: public final static String JORM_FACTORY_BINDING = "jorm-factory";
061: public final static String COMPILED_QUERY_CACHE_BINDING = "compiled-query-cache";
062: public final static String PMF_BINDING = "po-manager-factory";
063:
064: protected PMapper mapper = null;
065:
066: protected JormFactory jormFactory = null;
067:
068: private CacheManager compiledQueriesCache = null;
069: protected PrefetchBufferFactory prefetchBufferFactory;
070:
071: /**
072: * The PMF
073: */
074: private POManagerFactoryItf pmf = null;
075:
076: protected Logger logger = null;
077: protected Logger cqlogger = null;
078: protected Logger cqpvlogger = null;
079: protected Logger cqpflogger = null;
080: private boolean prefetchingOnQuery = true;
081: private boolean prefetchingOnExtent = true;
082:
083: /**
084: * creates a new SpeedoQueryManager object.
085: */
086: public SpeedoQueryManager() {
087: prefetchBufferFactory = new PrefetchBufferFactoryImpl();
088: }
089:
090: public FixableCacheEntry create(Object id, Object obj) {
091: return (CompiledQuery) obj;
092: }
093:
094: public boolean getPrefetchActivatedOnQuery() {
095: return prefetchingOnQuery;
096: }
097:
098: public void setPrefetchActivatedOnQuery(boolean prefetch) {
099: prefetchingOnQuery = prefetch;
100: }
101:
102: public boolean getPrefetchActivatedOnExtent() {
103: return prefetchingOnExtent;
104: }
105:
106: public void setPrefetchActivatedOnExtent(boolean prefetch) {
107: prefetchingOnExtent = prefetch;
108: }
109:
110: // IMPLEMENTATION OF UserBindingController INTERFACE //
111: //---------------------------------------------------//
112:
113: public String[] listFc() {
114: return new String[] { MAPPER_BINDING, JORM_FACTORY_BINDING,
115: COMPILED_QUERY_CACHE_BINDING, PMF_BINDING };
116: }
117:
118: public Object lookupFc(String s) {
119: if (MAPPER_BINDING.equals(s))
120: return mapper;
121: else if (JORM_FACTORY_BINDING.equals(s))
122: return jormFactory;
123: else if (COMPILED_QUERY_CACHE_BINDING.equals(s))
124: return compiledQueriesCache;
125: else if (PMF_BINDING.equals(s))
126: return pmf;
127: else
128: return null;
129: }
130:
131: public void bindFc(String s, Object o) {
132: if ("monolog-factory".equals(s)) {
133: Log.loggerFactory = (LoggerFactory) o;
134: String bn = logger.getName();
135: cqlogger = Log.loggerFactory.getLogger(bn
136: + ".compiled-query");
137: cqpvlogger = Log.loggerFactory.getLogger(cqlogger.getName()
138: + ".parser.variable");
139: cqpflogger = Log.loggerFactory.getLogger(cqlogger.getName()
140: + ".parser.filter");
141: logger = Log.loggerFactory.getLogger(cqlogger.getName()
142: + ".allocator");
143: } else if ("logger".equals(s)) {
144: logger = (Logger) o;
145: } else if (PMF_BINDING.equals(s)) {
146: pmf = (POManagerFactoryItf) o;
147: } else if (MAPPER_BINDING.equals(s))
148: mapper = (PMapper) o;
149: else if (COMPILED_QUERY_CACHE_BINDING.equals(s))
150: compiledQueriesCache = (CacheManager) o;
151: else if (JORM_FACTORY_BINDING.equals(s))
152: jormFactory = (JormFactory) o;
153: }
154:
155: public void unbindFc(String s) {
156: if (MAPPER_BINDING.equals(s))
157: mapper = null;
158: else if (JORM_FACTORY_BINDING.equals(s))
159: jormFactory = null;
160: else if (COMPILED_QUERY_CACHE_BINDING.equals(s))
161: compiledQueriesCache = null;
162: }
163:
164: // IMPLEMENTATION OF CacheEventListener INTERFACE //
165: //------------------------------------------------//
166:
167: /**
168: * An entry has been added in the cache.
169: * @param event describes the added entry
170: */
171: public void entryBound(CacheEvent event) {
172: }
173:
174: /**
175: * An entry has been evicted from the cache.
176: * @param event describes the evicted entry
177: */
178: public void entryUnbound(CacheEvent event) {
179: }
180:
181: // IMPLEMENTATION OF QueryManager INTERFACE //
182: //------------------------------------------//
183:
184: /**
185: * returns a CompiledQuery implementation, creates it if it does not exists,
186: * or just returns an existing one.
187: * @param qd a QueryDefinition
188: * @return a (new/existing) CompiledQuery instance.
189: */
190: public synchronized CompiledQuery getQueryCompiler(
191: QueryDefinition qd) {
192: CompiledQuery cq = (CompiledQuery) compiledQueriesCache
193: .lookup(qd);
194: if (cq == null) {
195: if (logger.isLoggable(BasicLevel.DEBUG)) {
196: logger.log(BasicLevel.DEBUG,
197: "Allocation of a new compiled query: "
198: + qd.qdToString(false));
199: }
200: cq = createCompileQuery(qd);
201: qd.withPrefetch(prefetchingOnExtent && qd.withPrefetch());
202: try {
203: compiledQueriesCache.bind(cq.getCeIdentifier(), cq);
204: } catch (CacheException e) {
205: logger
206: .log(
207: BasicLevel.WARN,
208: "Impossible to bind a new compiled query from the cache: ",
209: e);
210: //nothing to do, the compiled query is not cached
211: }
212: } else {
213: if (logger.isLoggable(BasicLevel.DEBUG)) {
214: logger.log(BasicLevel.DEBUG, "Reuse a compiled query: "
215: + qd.qdToString(false));
216: }
217: }
218: return cq;
219: }
220:
221: public POManagerFactoryItf getPMF() {
222: return pmf;
223: }
224:
225: public void setPMF(POManagerFactoryItf pmf) {
226: this .pmf = pmf;
227: }
228:
229: public void clean() {
230: try {
231: //clean the compiled query cache
232: compiledQueriesCache.unbindAll();
233: } catch (CacheException e) {
234: logger
235: .log(
236: BasicLevel.WARN,
237: "Impossible to clean the compiled query cache: ",
238: e);
239: }
240: }
241:
242: protected abstract CompiledQuery createCompileQuery(
243: QueryDefinition qd);
244:
245: }
|