1 22 23 package org.continuent.sequoia.controller.cache.metadata; 24 25 import java.util.Hashtable ; 26 27 import org.continuent.sequoia.common.i18n.Translate; 28 import org.continuent.sequoia.common.log.Trace; 29 import org.continuent.sequoia.common.protocol.Field; 30 import org.continuent.sequoia.common.xml.DatabasesXmlTags; 31 import org.continuent.sequoia.controller.requests.AbstractRequest; 32 33 42 public class MetadataCache 43 { 44 private static Trace logger = Trace.getLogger(MetadataCache.class.getName()); 45 46 private Hashtable metadataCache; 48 49 private Hashtable fieldCache; 51 private int maxNbOfMetadata; 52 private int maxNbOfField; 53 54 60 public MetadataCache(int maxNbOfMetadata, int maxNbOfField) 61 { 62 metadataCache = new Hashtable (maxNbOfMetadata == 0 63 ? 10000 64 : maxNbOfMetadata); 65 fieldCache = new Hashtable (maxNbOfField == 0 ? 100 : maxNbOfField); 66 if (maxNbOfMetadata < 0) 67 throw new RuntimeException (Translate.get("cache.metadata.invalid.size", 68 maxNbOfMetadata)); 69 if (maxNbOfMetadata == 0) 70 this.maxNbOfMetadata = Integer.MAX_VALUE; 71 else 72 this.maxNbOfMetadata = maxNbOfMetadata; 73 if (maxNbOfField < 0) 74 throw new RuntimeException (Translate.get("cache.metadata.invalid.size", 75 maxNbOfField)); 76 if (maxNbOfField == 0) 77 this.maxNbOfField = Integer.MAX_VALUE; 78 else 79 this.maxNbOfField = maxNbOfField; 80 } 81 82 85 public void flushCache() 86 { 87 synchronized (metadataCache) 88 { 89 metadataCache.clear(); 90 } 91 synchronized (fieldCache) 92 { 93 fieldCache.clear(); 94 } 95 } 96 97 105 public Field[] getMetadata(AbstractRequest request) 106 { 107 return (Field[]) metadataCache.get(request.getUniqueKey()); 108 } 109 110 116 public void addMetadata(AbstractRequest request, Field[] metadata) 117 { 118 while (metadataCache.size() > maxNbOfMetadata) 123 { synchronized (metadataCache) 127 { 128 try 129 { 130 metadataCache.remove(metadataCache.keys().nextElement()); 131 } 132 catch (Exception ignore) 133 { 134 break; 135 } 136 } 137 } 138 139 try 141 { 142 metadataCache.put(request.getUniqueKey(), metadata); 143 } 144 catch (OutOfMemoryError oome) 145 { 146 flushCache(); 147 System.gc(); 148 logger.warn(Translate.get("cache.memory.error.cache.flushed", this 149 .getClass())); 150 } 151 } 152 153 162 public Field getField(String fullyQualifiedFieldName) 163 { 164 return (Field) fieldCache.get(fullyQualifiedFieldName); 165 } 166 167 174 public void addField(String fullyQualifiedFieldName, Field field) 175 { 176 while (fieldCache.size() > maxNbOfField) 181 { synchronized (fieldCache) 185 { 186 try 187 { 188 fieldCache.remove(fieldCache.keys().nextElement()); 189 } 190 catch (Exception ignore) 191 { 192 break; 193 } 194 } 195 } 196 try 198 { 199 fieldCache.put(fullyQualifiedFieldName, field); 200 } 201 catch (OutOfMemoryError oome) 202 { 203 flushCache(); 204 System.gc(); 205 logger.warn(Translate.get("cache.memory.error.cache.flushed", this 206 .getClass())); 207 } 208 } 209 210 215 public String getXml() 216 { 217 return "<" + DatabasesXmlTags.ELT_MetadataCache + " " 218 + DatabasesXmlTags.ATT_maxNbOfMetadata + "=\"" + maxNbOfMetadata 219 + "\" " + DatabasesXmlTags.ATT_maxNbOfField + "=\"" 220 + (maxNbOfField == Integer.MAX_VALUE ? 0 : maxNbOfField) + "\"/>"; 221 } 222 223 } | Popular Tags |