1 8 package org.ozoneDB.core.storage; 9 10 import java.util.Collection ; 11 import java.util.Comparator ; 12 import java.util.Iterator ; 13 import java.util.LinkedHashMap ; 14 import java.util.LinkedHashSet ; 15 import java.util.LinkedList ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Properties ; 19 import java.util.Set ; 20 import java.util.Map.Entry; 21 import java.util.SortedSet ; 22 import java.util.TreeMap ; 23 import java.util.TreeSet ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import org.ozoneDB.OzoneInternalException; 27 import org.ozoneDB.core.ConfigurationException; 28 29 30 47 public class FixedSizeDelayCache extends DelayCache { 48 49 private static final Logger log = Logger.getLogger(FixedSizeDelayCache.class.getName()); 50 51 public static final PropertyInfo MAXCAPACITY = new PropertyInfo( 52 ".maxCapacity", 53 "int", 54 "1", 55 "number of objects that this cache should hold as a maximum", 56 new String [] {"100", "100000"} 57 ); 58 59 private int maxCapacity; 60 61 public FixedSizeDelayCache(Properties properties, String prefix) { 62 super(properties, prefix); 63 try { 64 setMaxCapacity(Integer.parseInt(properties.getProperty(prefix + MAXCAPACITY.getKey(), MAXCAPACITY.getDefaultValue()))); 65 log.config(getPrefix() + " using a max capacity of " + getMaxCapacity()); 66 } catch (NumberFormatException e) { 67 throw new ConfigurationException(e); 68 } 69 } 70 71 public final void setMaxCapacity(int maxCapacity) { 72 synchronized(getSynchronizer()) { 73 this.maxCapacity = maxCapacity; 74 ourTrim(); 75 } 76 } 77 78 public final int getMaxCapacity() { 79 return maxCapacity; 81 } 82 83 public Collection getPropertyInfos() { 84 Collection result = super.getPropertyInfos(); 85 result.add(MAXCAPACITY); 86 return result; 87 } 88 89 public void put(Object key, Object value) { 90 synchronized(getSynchronizer()) { 91 super.put(key, value); 92 ourTrim(); 93 } 94 } 95 96 private void ourTrim() { 97 while (size() > getMaxCapacity()) { 98 TimedEntry last = (TimedEntry) getEntries().last(); 99 if (getTrimHandler() != null) { 100 getTrimHandler().trimming(last.key, last.value); 101 } 102 getEntries().remove(last); 103 getKeysToEntries().remove(last.key); 104 } 105 } 106 107 } 108 | Popular Tags |