src/org/objectweb/cjdbc/controller/cache/metadata/MetadataCache.java

説明を見る。
00001 00025 package org.objectweb.cjdbc.controller.cache.metadata; 00026 00027 import java.util.Hashtable; 00028 00029 import org.objectweb.cjdbc.common.i18n.Translate; 00030 import org.objectweb.cjdbc.common.sql.AbstractRequest; 00031 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 00032 import org.objectweb.cjdbc.driver.Field; 00033 00043 public class MetadataCache 00044 { 00045 private Hashtable metadataCache; // SQL -> Field[] 00046 private Hashtable fieldCache; // Column name -> Field 00047 private int maxNbOfMetadata; 00048 private int maxNbOfField; 00049 00056 public MetadataCache(int maxNbOfMetadata, int maxNbOfField) 00057 { 00058 metadataCache = new Hashtable(maxNbOfMetadata == 0 00059 ? 10000 00060 : maxNbOfMetadata); 00061 fieldCache = new Hashtable(maxNbOfField == 0 ? 100 : maxNbOfField); 00062 if (maxNbOfMetadata < 0) 00063 throw new RuntimeException(Translate.get("cache.metadata.invalid.size", 00064 maxNbOfMetadata)); 00065 if (maxNbOfMetadata == 0) 00066 this.maxNbOfMetadata = Integer.MAX_VALUE; 00067 else 00068 this.maxNbOfMetadata = maxNbOfMetadata; 00069 if (maxNbOfField < 0) 00070 throw new RuntimeException(Translate.get("cache.metadata.invalid.size", 00071 maxNbOfField)); 00072 if (maxNbOfField == 0) 00073 this.maxNbOfField = Integer.MAX_VALUE; 00074 else 00075 this.maxNbOfField = maxNbOfField; 00076 } 00077 00086 public Field[] getMetadata(AbstractRequest request) 00087 { 00088 String sqlSkeleton = request.getSqlSkeleton(); 00089 if (sqlSkeleton != null) 00090 return (Field[]) metadataCache.get(sqlSkeleton); 00091 else 00092 return (Field[]) metadataCache.get(request.getSQL()); 00093 } 00094 00101 public void addMetadata(AbstractRequest request, Field[] metadata) 00102 { 00103 // Note that the underlying cache Hashtable is synchronized and we usually 00104 // do not need to synchronize on it. 00105 // As we will have to add a cache entry, check if the cache size is ok 00106 // else remove the first entry of the hashtable. 00107 while (metadataCache.size() > maxNbOfMetadata) 00108 { // Remove first entry from Hashtable. We need to synchronize here to be 00109 // sure that we are not trying to concurrently remove the first cache 00110 // entry. 00111 synchronized (metadataCache) 00112 { 00113 try 00114 { 00115 metadataCache.remove(metadataCache.keys().nextElement()); 00116 } 00117 catch (Exception ignore) 00118 { 00119 break; 00120 } 00121 } 00122 } 00123 00124 // Add to cache 00125 String sqlSkeleton = request.getSqlSkeleton(); 00126 if (sqlSkeleton != null) 00127 metadataCache.put(sqlSkeleton, metadata); 00128 else 00129 metadataCache.put(request.getSQL(), metadata); 00130 } 00131 00140 public Field getField(String columnName) 00141 { 00142 return (Field) fieldCache.get(columnName); 00143 } 00144 00151 public void addField(String columnName, Field field) 00152 { 00153 // Note that the underlying cache Hashtable is synchronized and we usually 00154 // do not need to synchronize on it. 00155 // As we will have to add a cache entry, check if the cache size is ok 00156 // else remove the first entry of the hashtable. 00157 while (fieldCache.size() > maxNbOfField) 00158 { // Remove first entry from Hashtable. We need to synchronize here to be 00159 // sure that we are not trying to concurrently remove the first cache 00160 // entry. 00161 synchronized (fieldCache) 00162 { 00163 try 00164 { 00165 fieldCache.remove(fieldCache.keys().nextElement()); 00166 } 00167 catch (Exception ignore) 00168 { 00169 break; 00170 } 00171 } 00172 } 00173 // Add to cache 00174 fieldCache.put(columnName, field); 00175 } 00176 00182 public String getXml() 00183 { 00184 return "<" + DatabasesXmlTags.ELT_MetadataCache + " " 00185 + DatabasesXmlTags.ATT_maxNbOfMetadata + "=\"" + maxNbOfMetadata 00186 + "\" " + DatabasesXmlTags.ATT_maxNbOfField + "=\"" + (maxNbOfField == Integer.MAX_VALUE ? 0 : maxNbOfField) 00187 + "\"/>"; 00188 } 00189 00190 }

CJDBCversion1.0.4に対してTue Oct 12 15:16:00 2004に生成されました。 doxygen 1.3.8