Public Member Functions | |
ParsingCache (int size, boolean backgroundParsing) | |
int | getGranularity () |
void | setGranularity (int granularity) |
RequestManager | getRequestManager () |
void | setRequestManager (RequestManager requestManager) |
void | getParsingFromCache (AbstractRequest request) |
void | getParsingFromCacheAndParseIfMissing (AbstractRequest request) throws SQLException |
boolean | isBackgroundParsing () |
void | setBackgroundParsing (boolean backgroundParsing) |
void | setCaseSensitiveParsing (boolean isCaseSensitiveParsing) |
boolean | isCaseSensitiveParsing () |
String | getXml () |
Definition at line 45 of file ParsingCache.java.
|
Constructor for ParsingCache.
Definition at line 110 of file ParsingCache.java. 00111 { 00112 cache = new Hashtable(size == 0 ? 10000 : size); 00113 currentlyParsing = new Hashtable(); 00114 if (size < 0) 00115 throw new RuntimeException(Translate.get("cache.parsing.invalid.size", 00116 size)); 00117 if (size == 0) 00118 this.maxNbOfEntries = Integer.MAX_VALUE; 00119 else 00120 this.maxNbOfEntries = size; 00121 this.backgroundParsing = backgroundParsing; 00122 caseSensitiveParsing = false; 00123 }
|
|
Returns the granularity value.
Definition at line 130 of file ParsingCache.java. 00131 {
00132 return granularity;
00133 }
|
|
If the same SQL query is found in the cache, the parsing is cloned into the given request. If backgroundParsing is set to true, then a ParserThread starts parsing the request in background else nothing is done on a cache miss.
Definition at line 173 of file ParsingCache.java. References org.objectweb.cjdbc.common.sql.AbstractRequest.cloneParsing(), org.objectweb.cjdbc.common.sql.AbstractRequest.getSQL(), org.objectweb.cjdbc.common.sql.AbstractRequest.getSqlSkeleton(), and org.objectweb.cjdbc.common.sql.AbstractRequest.isParsed. 00174 { 00175 if (request.isParsed()) 00176 return; 00177 00178 String sql = request.getSqlSkeleton(); 00179 if (sql == null) 00180 sql = request.getSQL(); 00181 AbstractRequest parsedRequest = (AbstractRequest) cache.get(sql); 00182 00183 if (parsedRequest != null) 00184 { // Cache hit, clone the parsing 00185 request.cloneParsing(parsedRequest); 00186 return; 00187 } 00188 else if (backgroundParsing) 00189 { // Cache miss, start parsing the request in background 00190 synchronized (currentlyParsing) 00191 { 00192 if (!currentlyParsing.contains(sql)) 00193 { // Nobody else is trying to parse the same SQL query 00194 ParserThread pt = new ParserThread(request, requestManager 00195 .getDatabaseSchema(), granularity, caseSensitiveParsing); 00196 currentlyParsing.put(sql, new CurrentlyParsingEntry(pt, request)); 00197 } 00198 } 00199 } 00200 }
|
|
Method getParsingFromCacheAndParseIfMissing.
Definition at line 208 of file ParsingCache.java. References org.objectweb.cjdbc.common.sql.AbstractRequest.cloneParsing(), org.objectweb.cjdbc.common.sql.AbstractRequest.getSQL(), and org.objectweb.cjdbc.common.sql.AbstractRequest.getSqlSkeleton(). 00210 { 00211 if (request.isParsed()) 00212 return; 00213 00214 // Check cache 00215 String instanciatedSQL = request.getSQL(); 00216 AbstractRequest parsedRequest = (AbstractRequest) cache 00217 .get(instanciatedSQL); 00218 00219 00220 try 00221 { 00222 00223 if (parsedRequest == null) 00224 { // Cache miss 00225 String sqlSkeleton = request.getSqlSkeleton(); 00226 String sql; 00227 if (sqlSkeleton != null) 00228 { // Missed with instanciated query, try with skeleton 00229 sql = sqlSkeleton; 00230 parsedRequest = (AbstractRequest) cache.get(sql); 00231 if (parsedRequest != null) 00232 { // Cache hit with skeleton 00233 request.cloneParsing(parsedRequest); 00234 return; 00235 } 00236 } 00237 else 00238 sql = instanciatedSQL; 00239 00240 // Full cache miss. Note that the underlying cache Hashtable is 00241 // synchronized and we usually do not need to synchronize on it. 00242 // As we will have to add a cache entry, check if the cache size is ok 00243 // else remove the first entry of the hashtable. 00244 while (cache.size() > maxNbOfEntries) 00245 { // Remove first entry from Hashtable. We need to synchronize here to be 00246 // sure that we are not trying to concurrently remove the first cache 00247 // entry. 00248 synchronized (cache) 00249 { 00250 try 00251 { 00252 cache.remove(cache.keys().nextElement()); 00253 } 00254 catch (Exception ignore) 00255 { 00256 break; 00257 } 00258 } 00259 } 00260 00261 00262 // Both skeleton and instanciated missed 00263 if (backgroundParsing) 00264 { 00265 // Find the parsing thread and request (note that Hasthtable is 00266 // synchronized) 00267 CurrentlyParsingEntry cpe = (CurrentlyParsingEntry) currentlyParsing 00268 .get(sql); 00269 if (cpe != null) 00270 { 00271 ParserThread pt = cpe.getParserThread(); 00272 try 00273 { 00274 if (pt != null) 00275 { 00276 // Wait for completion 00277 pt.join(); 00278 synchronized (currentlyParsing) 00279 { 00280 currentlyParsing.remove(sql); 00281 } 00282 00283 // Update cache 00284 if ((granularity != ParsingGranularities.COLUMN_UNIQUE) 00285 || (sqlSkeleton == null)) 00286 // No skeleton or no uniqueness criteria, add the query 00287 cache.put(instanciatedSQL, cpe.getRequest()); 00288 else 00289 { // We have a skeleton and COLUMN_UNIQUE parsing 00290 if (request.getCacheAbility() != RequestType.UNIQUE_CACHEABLE) 00291 // It is NOT UNIQUE, add the skeleton 00292 cache.put(sqlSkeleton, cpe.getRequest()); 00293 else 00294 // It is UNIQUE, add the instanciated query 00295 cache.put(instanciatedSQL, cpe.getRequest()); 00296 } 00297 } 00298 } 00299 catch (InterruptedException failed) 00300 { 00301 throw new SQLException(Translate.get( 00302 "cache.parsing.failed.join.parser.thread", new String[]{ 00303 "" + request.getId(), failed.getMessage()})); 00304 } 00305 } 00306 } 00307 // Parse it now because we didn't parse in background or 00308 // backgroundParsing has failed for any obscure reason. 00309 request.parse(requestManager.getDatabaseSchema(), granularity, 00310 caseSensitiveParsing); 00311 00312 // Update cache 00313 if ((sqlSkeleton != null) 00314 && (granularity == ParsingGranularities.COLUMN_UNIQUE) 00315 && (request.getCacheAbility() == RequestType.UNIQUE_CACHEABLE)) 00316 // If this is a unique request, we must put the instanciated query in 00317 // the cache to retrieve the exact pk value. 00318 cache.put(instanciatedSQL, request); 00319 else 00320 cache.put(sql, request); 00321 } 00322 else 00323 // Cache hit 00324 request.cloneParsing(parsedRequest); 00325 00326 } 00327 catch (OutOfMemoryError oome) 00328 { 00329 synchronized (cache) 00330 { 00331 cache.clear(); 00332 } 00333 System.gc(); 00334 logger.warn(Translate.get("cache.memory.error.cache.flushed", this 00335 .getClass())); 00336 } 00337 }
|
|
Returns the requestManager value.
Definition at line 150 of file ParsingCache.java. 00151 {
00152 return requestManager;
00153 }
|
|
Get xml information about this ParsingCache
Definition at line 385 of file ParsingCache.java. 00386 { 00387 return "<" + DatabasesXmlTags.ELT_ParsingCache + " " 00388 + DatabasesXmlTags.ATT_backgroundParsing + "=\"" + backgroundParsing 00389 + "\" " + DatabasesXmlTags.ATT_maxNbOfEntries + "=\"" + maxNbOfEntries 00390 + "\"/>"; 00391 }
|
|
Returns the backgroundParsing.
Definition at line 344 of file ParsingCache.java. 00345 {
00346 return backgroundParsing;
00347 }
|
|
Returns the caseSensitiveParsin.
Definition at line 375 of file ParsingCache.java. 00376 {
00377 return caseSensitiveParsing;
00378 }
|
|
Sets the background parsing. If true the request are parsed in background by a separate thread that is created for this purpose.
Definition at line 355 of file ParsingCache.java. 00356 {
00357 this.backgroundParsing = backgroundParsing;
00358 }
|
|
Sets the parsing case sensitivity
Definition at line 365 of file ParsingCache.java. 00366 {
00367 this.caseSensitiveParsing = isCaseSensitiveParsing;
00368 }
|
|
Sets the granularity value.
Definition at line 140 of file ParsingCache.java. 00141 {
00142 this.granularity = granularity;
00143 }
|
|
Sets the requestManager value.
Definition at line 160 of file ParsingCache.java. 00161 {
00162 this.requestManager = requestManager;
00163 }
|