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

org.objectweb.cjdbc.controller.cache.metadata.MetadataCache Class Reference

List of all members.

Public Member Functions

 MetadataCache (int maxNbOfMetadata, int maxNbOfField)
Field[] getMetadata (AbstractRequest request)
void addMetadata (AbstractRequest request, Field[] metadata)
Field getField (String fullyQualifiedFieldName)
void addField (String fullyQualifiedFieldName, Field field)
String getXml ()

Detailed Description

This class implements a ResultSet metadata cache.

ResultSet Fields are kept here to prevent recomputing them and allocating them each time a query is executed.

Author:
Emmanuel Cecchet
Version:
1.0

Definition at line 44 of file MetadataCache.java.


Constructor & Destructor Documentation

org.objectweb.cjdbc.controller.cache.metadata.MetadataCache.MetadataCache int  maxNbOfMetadata,
int  maxNbOfField
 

Constructor for MetadataCache.

Parameters:
maxNbOfMetadata maximum nb of entries in metadata cache
maxNbOfField maximum nb of entries in field cache

Definition at line 58 of file MetadataCache.java.

00059   {
00060     metadataCache = new Hashtable(maxNbOfMetadata == 0
00061         ? 10000
00062         : maxNbOfMetadata);
00063     fieldCache = new Hashtable(maxNbOfField == 0 ? 100 : maxNbOfField);
00064     if (maxNbOfMetadata < 0)
00065       throw new RuntimeException(Translate.get("cache.metadata.invalid.size",
00066           maxNbOfMetadata));
00067     if (maxNbOfMetadata == 0)
00068       this.maxNbOfMetadata = Integer.MAX_VALUE;
00069     else
00070       this.maxNbOfMetadata = maxNbOfMetadata;
00071     if (maxNbOfField < 0)
00072       throw new RuntimeException(Translate.get("cache.metadata.invalid.size",
00073           maxNbOfField));
00074     if (maxNbOfField == 0)
00075       this.maxNbOfField = Integer.MAX_VALUE;
00076     else
00077       this.maxNbOfField = maxNbOfField;
00078   }


Member Function Documentation

void org.objectweb.cjdbc.controller.cache.metadata.MetadataCache.addField String  fullyQualifiedFieldName,
Field  field
 

Add a Field entry to the cache and associate it to the given name.

Parameters:
fullyQualifiedFieldName table.column name that uniquely identifies the field
field field to cache

Definition at line 167 of file MetadataCache.java.

00168   {
00169     // Note that the underlying cache Hashtable is synchronized and we usually
00170     // do not need to synchronize on it.
00171     // As we will have to add a cache entry, check if the cache size is ok
00172     // else remove the first entry of the hashtable.
00173     while (fieldCache.size() > maxNbOfField)
00174     { // Remove first entry from Hashtable. We need to synchronize here to be
00175       // sure that we are not trying to concurrently remove the first cache
00176       // entry.
00177       synchronized (fieldCache)
00178       {
00179         try
00180         {
00181           fieldCache.remove(fieldCache.keys().nextElement());
00182         }
00183         catch (Exception ignore)
00184         {
00185           break;
00186         }
00187       }
00188     }
00189     // Add to cache
00190     try
00191     {
00192       fieldCache.put(fullyQualifiedFieldName, field);
00193     }
00194     catch (OutOfMemoryError oome)
00195     {
00196       synchronized (fieldCache)
00197       {
00198         fieldCache.clear();
00199       }
00200       System.gc();
00201       logger.warn(Translate.get("cache.memory.error.cache.flushed", this
00202           .getClass()));
00203     }
00204   }

void org.objectweb.cjdbc.controller.cache.metadata.MetadataCache.addMetadata AbstractRequest  request,
Field[]  metadata
 

Add a metadata entry to the cache and associate it to the given request.

Parameters:
request request to which the metadata belong
metadata metadata to cache

Definition at line 103 of file MetadataCache.java.

References org.objectweb.cjdbc.common.sql.AbstractRequest.getSQL(), and org.objectweb.cjdbc.common.sql.AbstractRequest.getSqlSkeleton().

00104   {
00105     // Note that the underlying cache Hashtable is synchronized and we usually
00106     // do not need to synchronize on it.
00107     // As we will have to add a cache entry, check if the cache size is ok
00108     // else remove the first entry of the hashtable.
00109     while (metadataCache.size() > maxNbOfMetadata)
00110     { // Remove first entry from Hashtable. We need to synchronize here to be
00111       // sure that we are not trying to concurrently remove the first cache
00112       // entry.
00113       synchronized (metadataCache)
00114       {
00115         try
00116         {
00117           metadataCache.remove(metadataCache.keys().nextElement());
00118         }
00119         catch (Exception ignore)
00120         {
00121           break;
00122         }
00123       }
00124     }
00125 
00126     // Add to cache
00127     try
00128     {
00129       String sqlSkeleton = request.getSqlSkeleton();
00130       if (sqlSkeleton != null)
00131         metadataCache.put(sqlSkeleton, metadata);
00132       else
00133         metadataCache.put(request.getSQL(), metadata);
00134     }
00135     catch (OutOfMemoryError oome)
00136     {
00137       synchronized (metadataCache)
00138       {
00139         metadataCache.clear();
00140       }
00141       System.gc();
00142       logger.warn(Translate.get("cache.memory.error.cache.flushed", this
00143           .getClass()));
00144     }
00145   }

Field org.objectweb.cjdbc.controller.cache.metadata.MetadataCache.getField String  fullyQualifiedFieldName  ) 
 

Get the field corresponding to a column name.

Returns null if the cache contains no field for the given name.

Parameters:
fullyQualifiedFieldName the field name (table.column) to look for
Returns:
the corresponding Field or null if not in cache

Definition at line 155 of file MetadataCache.java.

00156   {
00157     return (Field) fieldCache.get(fullyQualifiedFieldName);
00158   }

Field [] org.objectweb.cjdbc.controller.cache.metadata.MetadataCache.getMetadata AbstractRequest  request  ) 
 

Get metadata associated to a request.

Returns null if the cache contains no metadata for the given request.

Parameters:
request the request we look for
Returns:
the metadata or null if not in cache

Definition at line 88 of file MetadataCache.java.

References org.objectweb.cjdbc.common.sql.AbstractRequest.getSQL(), and org.objectweb.cjdbc.common.sql.AbstractRequest.getSqlSkeleton().

00089   {
00090     String sqlSkeleton = request.getSqlSkeleton();
00091     if (sqlSkeleton != null)
00092       return (Field[]) metadataCache.get(sqlSkeleton);
00093     else
00094       return (Field[]) metadataCache.get(request.getSQL());
00095   }

String org.objectweb.cjdbc.controller.cache.metadata.MetadataCache.getXml  ) 
 

Get xml information about this ParsingCache

Returns:
String in xml formatted text

Definition at line 211 of file MetadataCache.java.

00212   {
00213     return "<" + DatabasesXmlTags.ELT_MetadataCache + " "
00214         + DatabasesXmlTags.ATT_maxNbOfMetadata + "=\"" + maxNbOfMetadata
00215         + "\" " + DatabasesXmlTags.ATT_maxNbOfField + "=\""
00216         + (maxNbOfField == Integer.MAX_VALUE ? 0 : maxNbOfField) + "\"/>";
00217   }


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