| 1 8 package org.ozoneDB.core.storage; 9 10 import java.util.Collection ; 11 import java.util.Comparator ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.LinkedHashMap ; 15 import java.util.LinkedHashSet ; 16 import java.util.LinkedList ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Properties ; 20 import java.util.Set ; 21 import java.util.Map.Entry; 22 import java.util.SortedSet ; 23 import java.util.TreeMap ; 24 import java.util.TreeSet ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 import org.ozoneDB.OzoneInternalException; 28 import org.ozoneDB.core.ConfigurationException; 29 30 31 48 public class FixedSizeCache extends AbstractTrimmingCache implements PropertyConfigurable { 49 50 private static final Logger log = Logger.getLogger(FixedSizeDelayCache.class.getName()); 51 52 public static final PropertyInfo MAXCAPACITY = new PropertyInfo( 53 ".maxCapacity", 54 "int", 55 "1", 56 "number of objects that this cache should hold as a maximum", 57 new String [] {"100", "100000"} 58 ); 59 60 public static final PropertyInfo INITIALCAPACITY = new PropertyInfo( 61 ".initialCapacity", 62 "int", 63 "1", 64 "number of objects that this cache reserves space for initially", 65 new String [] {"100", "100000"} 66 ); 67 68 public static final PropertyInfo LOADFACTOR = new PropertyInfo( 69 ".loadFactor", 70 "float", 71 "0.75", 72 "load factor for this cache; see java.util.LinkedHashMap(int initialCapacity, float loadFactor)", 73 new String [] {"0.5", "0.95"} 74 ); 75 76 private int maxCapacity; 77 78 private Map map; 79 80 83 public FixedSizeCache(Properties properties, String prefix) { 84 super(properties, prefix); 85 try { 86 float loadFactor = Float.parseFloat(properties.getProperty(prefix + LOADFACTOR.getKey(), LOADFACTOR.getDefaultValue())); 87 log.config(getPrefix() + " using a load factor of " + loadFactor); 88 int initialCapacity = Integer.parseInt(properties.getProperty(prefix + INITIALCAPACITY.getKey(), INITIALCAPACITY.getDefaultValue())); 89 log.config(getPrefix() + " using an initial capacity of " + loadFactor); 90 map = new LinkedHashMap (initialCapacity, loadFactor, true); 91 setMaxCapacity(Integer.parseInt(properties.getProperty(prefix + MAXCAPACITY.getKey(), MAXCAPACITY.getDefaultValue()))); 92 log.config(getPrefix() + " using a max capacity of " + getMaxCapacity()); 93 } catch (NumberFormatException e) { 94 throw new ConfigurationException(e); 95 } 96 } 97 98 private Map getMap() { 99 return map; 100 } 101 102 public final void setMaxCapacity(int maxCapacity) { 103 synchronized(getSynchronizer()) { 104 this.maxCapacity = maxCapacity; 105 trim(); 106 } 107 } 108 109 public final int getMaxCapacity() { 110 return maxCapacity; 112 } 113 114 public Collection getPropertyInfos() { 115 Collection result = new LinkedList (); 116 result.add(MAXCAPACITY); 117 return result; 118 } 119 120 public void put(Object key, Object value) { 121 synchronized(getSynchronizer()) { 122 getMap().put(key, value); 123 trim(); 124 } 125 } 126 127 private void trim() { 128 int size = getMap().size() - getMaxCapacity(); 129 int countDown = getMaxCapacity(); 130 if (size > countDown) { 131 for (Iterator i = getMap().entrySet().iterator(); i.hasNext(); ) { 132 Map.Entry entry = (Map.Entry ) i.next(); 133 if (--countDown < 0) { 134 if (getTrimHandler() != null) { 135 getTrimHandler().trimming(entry.getKey(), entry.getValue()); 136 } 137 i.remove(); 138 } 139 } 140 } 141 } 142 143 public Map copyToMap() { 144 synchronized(getSynchronizer()) { 145 return new HashMap (getMap()); 146 } 147 } 148 149 public Object get(Object key) { 150 synchronized(getSynchronizer()) { 151 return getMap().get(key); 152 } 153 } 154 155 public Object remove(Object key) { 156 synchronized(getSynchronizer()) { 157 return getMap().remove(key); 158 } 159 } 160 161 public int size() { 162 synchronized(getSynchronizer()) { 163 return getMap().size(); 164 } 165 } 166 167 private class OurMap extends LinkedHashMap { 168 169 public OurMap(int initialCapacity, float loadFactor) { 170 super(initialCapacity, loadFactor, true); 171 } 172 173 protected boolean removeEldestEntry(Map.Entry entry) { 174 if (size() > getMaxCapacity()) { 175 if (getTrimHandler() != null) { 176 getTrimHandler().trimming(entry.getKey(), entry.getValue()); 177 } 178 remove(entry.getKey()); 179 } 180 return false; 181 } 182 183 } 184 185 } 186 | Popular Tags |