Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

org.objectweb.cjdbc.controller.cache.parsing.ParsingCache Class Reference

List of all members.

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 ()

Detailed Description

This class implements a request parsing cache.

Author:
Emmanuel Cecchet
Version:
1.0

Definition at line 45 of file ParsingCache.java.


Constructor & Destructor Documentation

org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.ParsingCache int  size,
boolean  backgroundParsing
 

Constructor for ParsingCache.

Parameters:
size maximum cache size in nb of entries
backgroundParsing true if the parsing should be done in background by a ParserThread

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   }


Member Function Documentation

int org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.getGranularity  ) 
 

Returns the granularity value.

Returns:
Returns the granularity.

Definition at line 130 of file ParsingCache.java.

00131   {
00132     return granularity;
00133   }

void org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.getParsingFromCache AbstractRequest  request  ) 
 

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.

Parameters:
request the request you look for

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   }

void org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.getParsingFromCacheAndParseIfMissing AbstractRequest  request  )  throws SQLException
 

Method getParsingFromCacheAndParseIfMissing.

Parameters:
request the request we look for
Exceptions:
SQLException if an error occurs

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   }

RequestManager org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.getRequestManager  ) 
 

Returns the requestManager value.

Returns:
Returns the requestManager.

Definition at line 150 of file ParsingCache.java.

00151   {
00152     return requestManager;
00153   }

String org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.getXml  ) 
 

Get xml information about this ParsingCache

Returns:
String in xml formatted text

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   }

boolean org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.isBackgroundParsing  ) 
 

Returns the backgroundParsing.

Returns:
boolean

Definition at line 344 of file ParsingCache.java.

00345   {
00346     return backgroundParsing;
00347   }

boolean org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.isCaseSensitiveParsing  ) 
 

Returns the caseSensitiveParsin.

Returns:
boolean

Definition at line 375 of file ParsingCache.java.

00376   {
00377     return caseSensitiveParsing;
00378   }

void org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.setBackgroundParsing boolean  backgroundParsing  ) 
 

Sets the background parsing. If true the request are parsed in background by a separate thread that is created for this purpose.

Parameters:
backgroundParsing The backgroundParsing to set

Definition at line 355 of file ParsingCache.java.

00356   {
00357     this.backgroundParsing = backgroundParsing;
00358   }

void org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.setCaseSensitiveParsing boolean  isCaseSensitiveParsing  ) 
 

Sets the parsing case sensitivity

Parameters:
isCaseSensitiveParsing true if parsing is case sensitive

Definition at line 365 of file ParsingCache.java.

00366   {
00367     this.caseSensitiveParsing = isCaseSensitiveParsing;
00368   }

void org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.setGranularity int  granularity  ) 
 

Sets the granularity value.

Parameters:
granularity The granularity to set.

Definition at line 140 of file ParsingCache.java.

00141   {
00142     this.granularity = granularity;
00143   }

void org.objectweb.cjdbc.controller.cache.parsing.ParsingCache.setRequestManager RequestManager  requestManager  ) 
 

Sets the requestManager value.

Parameters:
requestManager The requestManager to set.

Definition at line 160 of file ParsingCache.java.

00161   {
00162     this.requestManager = requestManager;
00163   }


The documentation for this class was generated from the following file:
Generated on Mon Apr 11 22:03:27 2005 for C-JDBC by  doxygen 1.3.9.1