1 27 28 package org.objectweb.speedo.query.lib; 29 30 import org.objectweb.fractal.api.control.BindingController; 31 import org.objectweb.jorm.api.PMapper; 32 import org.objectweb.medor.lib.Log; 33 import org.objectweb.medor.eval.prefetch.api.PrefetchBufferFactory; 34 import org.objectweb.medor.eval.prefetch.lib.PrefetchBufferFactoryImpl; 35 import org.objectweb.perseus.cache.api.CacheEntryFactory; 36 import org.objectweb.perseus.cache.api.CacheEventListener; 37 import org.objectweb.perseus.cache.api.CacheException; 38 import org.objectweb.perseus.cache.api.CacheManager; 39 import org.objectweb.perseus.cache.api.FixableCacheEntry; 40 import org.objectweb.perseus.cache.api.CacheEvent; 41 import org.objectweb.speedo.mapper.api.JormFactory; 42 import org.objectweb.speedo.query.api.CompiledQuery; 43 import org.objectweb.speedo.query.api.QueryDefinition; 44 import org.objectweb.speedo.query.api.QueryManager; 45 import org.objectweb.speedo.query.api.QueryManagerAttribute; 46 import org.objectweb.util.monolog.api.BasicLevel; 47 import org.objectweb.util.monolog.api.Logger; 48 import org.objectweb.util.monolog.api.LoggerFactory; 49 50 56 public class SpeedoQueryManager 57 implements QueryManager, CacheEntryFactory, CacheEventListener, BindingController, QueryManagerAttribute { 58 59 public final static String MAPPER_BINDING = "mapper"; 60 public final static String JORM_FACTORY_BINDING = "jorm-factory"; 61 public final static String COMPILED_QUERY_CACHE_BINDING = "compiled-query-cache"; 62 63 private PMapper mapper = null; 64 65 private JormFactory jormFactory = null; 66 67 private CacheManager compiledQueriesCache = null; 68 private PrefetchBufferFactory prefetchBufferFactory = null; 69 70 private Logger logger = null; 71 private Logger cqlogger = null; 72 private Logger cqpvlogger = null; 73 private Logger cqpflogger = null; 74 private boolean prefetchingOnQuery = true; 75 private boolean prefetchingOnExtent = true; 76 77 80 public SpeedoQueryManager() { 81 prefetchBufferFactory = new PrefetchBufferFactoryImpl(); 82 } 83 84 public FixableCacheEntry create(Object id, Object obj) { 85 return (SpeedoCompiledQuery) obj; 86 } 87 88 public boolean getPrefetchActivatedOnQuery() { 89 return prefetchingOnQuery; 90 } 91 92 public void setPrefetchActivatedOnQuery(boolean prefetch) { 93 prefetchingOnQuery = prefetch; 94 } 95 96 public boolean getPrefetchActivatedOnExtent() { 97 return prefetchingOnExtent; 98 } 99 100 public void setPrefetchActivatedOnExtent(boolean prefetch) { 101 prefetchingOnExtent = prefetch; 102 } 103 104 107 public String [] listFc() { 108 return new String [] { 109 MAPPER_BINDING, 110 JORM_FACTORY_BINDING, 111 COMPILED_QUERY_CACHE_BINDING 112 }; 113 } 114 115 public Object lookupFc(String s) { 116 if (MAPPER_BINDING.equals(s)) 117 return mapper; 118 else if (JORM_FACTORY_BINDING.equals(s)) 119 return jormFactory; 120 else if (COMPILED_QUERY_CACHE_BINDING.equals(s)) 121 return compiledQueriesCache; 122 else 123 return null; 124 } 125 126 public void bindFc(String s, Object o) { 127 if ("monolog-factory".equals(s)) { 128 Log.loggerFactory = (LoggerFactory) o; 129 String bn = logger.getName(); 130 cqlogger = Log.loggerFactory.getLogger(bn + ".compiled-query"); 131 cqpvlogger = Log.loggerFactory.getLogger(cqlogger.getName() + ".parser.variable"); 132 cqpflogger = Log.loggerFactory.getLogger(cqlogger.getName() + ".parser.filter"); 133 logger = Log.loggerFactory.getLogger(cqlogger.getName() + ".allocator"); 134 } else if ("logger".equals(s)) { 135 logger = (Logger) o; 136 } else if (MAPPER_BINDING.equals(s)) 137 mapper = (PMapper) o; 138 else if (COMPILED_QUERY_CACHE_BINDING.equals(s)) 139 compiledQueriesCache = (CacheManager) o; 140 else if (JORM_FACTORY_BINDING.equals(s)) 141 jormFactory = (JormFactory) o; 142 } 143 144 public void unbindFc(String s) { 145 if (MAPPER_BINDING.equals(s)) 146 mapper = null; 147 else if (JORM_FACTORY_BINDING.equals(s)) 148 jormFactory = null; 149 else if (COMPILED_QUERY_CACHE_BINDING.equals(s)) 150 compiledQueriesCache = null; 151 } 152 153 154 157 161 public void entryBound(CacheEvent event) { 162 } 163 164 168 public void entryUnbound(CacheEvent event) { 169 } 170 171 174 180 public synchronized CompiledQuery getQueryCompiler(QueryDefinition qd) { 181 SpeedoCompiledQuery scq = (SpeedoCompiledQuery) compiledQueriesCache.lookup(qd); 182 if (scq == null) { 183 if (logger.isLoggable(BasicLevel.DEBUG)) { 184 logger.log(BasicLevel.DEBUG, "Allocation of a new compiled query: " 185 + qd.qdToString(false)); 186 } 187 scq = new SpeedoCompiledQuery(); 188 scq.init(cqlogger, cqpvlogger, cqpflogger, mapper, 189 prefetchBufferFactory, jormFactory); 190 qd.withPrefetch(prefetchingOnExtent && qd.withPrefetch()); 191 scq.defineQuery(qd); 192 try { 193 compiledQueriesCache.bind(scq.getCeIdentifier(), scq); 194 } catch (CacheException e) { 195 logger.log(BasicLevel.WARN, 196 "Impossible to bind a new compiled query from the cache: ", e); 197 } 199 } else { 200 if (logger.isLoggable(BasicLevel.DEBUG)) { 201 logger.log(BasicLevel.DEBUG, "Reuse a compiled query: " 202 + qd.qdToString(false)); 203 } 204 } 205 return scq; 206 } 207 } 208 | Popular Tags |