1 24 25 package org.objectweb.cjdbc.controller.cache.metadata; 26 27 import java.util.Hashtable ; 28 29 import org.objectweb.cjdbc.common.i18n.Translate; 30 import org.objectweb.cjdbc.common.log.Trace; 31 import org.objectweb.cjdbc.common.sql.AbstractRequest; 32 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 33 import org.objectweb.cjdbc.driver.Field; 34 35 44 public class MetadataCache 45 { 46 private static Trace logger = Trace.getLogger(MetadataCache.class.getName()); 47 48 private Hashtable metadataCache; 50 51 private Hashtable fieldCache; 53 private int maxNbOfMetadata; 54 private int maxNbOfField; 55 56 62 public MetadataCache(int maxNbOfMetadata, int maxNbOfField) 63 { 64 metadataCache = new Hashtable (maxNbOfMetadata == 0 65 ? 10000 66 : maxNbOfMetadata); 67 fieldCache = new Hashtable (maxNbOfField == 0 ? 100 : maxNbOfField); 68 if (maxNbOfMetadata < 0) 69 throw new RuntimeException (Translate.get("cache.metadata.invalid.size", 70 maxNbOfMetadata)); 71 if (maxNbOfMetadata == 0) 72 this.maxNbOfMetadata = Integer.MAX_VALUE; 73 else 74 this.maxNbOfMetadata = maxNbOfMetadata; 75 if (maxNbOfField < 0) 76 throw new RuntimeException (Translate.get("cache.metadata.invalid.size", 77 maxNbOfField)); 78 if (maxNbOfField == 0) 79 this.maxNbOfField = Integer.MAX_VALUE; 80 else 81 this.maxNbOfField = maxNbOfField; 82 } 83 84 87 public void flushCache() 88 { 89 synchronized (metadataCache) 90 { 91 metadataCache.clear(); 92 } 93 synchronized (fieldCache) 94 { 95 fieldCache.clear(); 96 } 97 } 98 99 107 public Field[] getMetadata(AbstractRequest request) 108 { 109 String sqlSkeleton = request.getSqlSkeleton(); 110 if (sqlSkeleton != null) 111 return (Field[]) metadataCache.get(sqlSkeleton); 112 else 113 return (Field[]) metadataCache.get(request.getSQL()); 114 } 115 116 122 public void addMetadata(AbstractRequest request, Field[] metadata) 123 { 124 while (metadataCache.size() > maxNbOfMetadata) 129 { synchronized (metadataCache) 133 { 134 try 135 { 136 metadataCache.remove(metadataCache.keys().nextElement()); 137 } 138 catch (Exception ignore) 139 { 140 break; 141 } 142 } 143 } 144 145 try 147 { 148 String sqlSkeleton = request.getSqlSkeleton(); 149 if (sqlSkeleton != null) 150 metadataCache.put(sqlSkeleton, metadata); 151 else 152 metadataCache.put(request.getSQL(), metadata); 153 } 154 catch (OutOfMemoryError oome) 155 { 156 flushCache(); 157 System.gc(); 158 logger.warn(Translate.get("cache.memory.error.cache.flushed", this 159 .getClass())); 160 } 161 } 162 163 171 public Field getField(String fullyQualifiedFieldName) 172 { 173 return (Field) fieldCache.get(fullyQualifiedFieldName); 174 } 175 176 183 public void addField(String fullyQualifiedFieldName, Field field) 184 { 185 while (fieldCache.size() > maxNbOfField) 190 { synchronized (fieldCache) 194 { 195 try 196 { 197 fieldCache.remove(fieldCache.keys().nextElement()); 198 } 199 catch (Exception ignore) 200 { 201 break; 202 } 203 } 204 } 205 try 207 { 208 fieldCache.put(fullyQualifiedFieldName, field); 209 } 210 catch (OutOfMemoryError oome) 211 { 212 flushCache(); 213 System.gc(); 214 logger.warn(Translate.get("cache.memory.error.cache.flushed", this 215 .getClass())); 216 } 217 } 218 219 224 public String getXml() 225 { 226 return "<" + DatabasesXmlTags.ELT_MetadataCache + " " 227 + DatabasesXmlTags.ATT_maxNbOfMetadata + "=\"" + maxNbOfMetadata 228 + "\" " + DatabasesXmlTags.ATT_maxNbOfField + "=\"" 229 + (maxNbOfField == Integer.MAX_VALUE ? 0 : maxNbOfField) + "\"/>"; 230 } 231 232 } | Popular Tags |