1 25 package org.ofbiz.base.util.cache; 26 27 import java.io.IOException ; 28 import java.io.Serializable ; 29 import java.util.Collection ; 30 import java.util.Collections ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 import javolution.util.FastList; 38 import javolution.util.FastMap; 39 40 import org.apache.commons.collections.map.LRUMap; 41 import org.ofbiz.base.util.Debug; 42 import org.ofbiz.base.util.ObjectType; 43 44 50 public class CacheLineTable implements Serializable { 51 52 public static final String module = CacheLineTable.class.getName(); 53 protected static transient jdbm.RecordManager jdbmMgr = null; 54 55 protected transient jdbm.htree.HTree fileTable = null; 56 protected Map memoryTable = null; 57 protected String fileStore = null; 58 protected String cacheName = null; 59 protected int maxInMemory = 0; 60 61 public CacheLineTable(String fileStore, String cacheName, boolean useFileSystemStore, int maxInMemory) { 62 this.fileStore = fileStore; 63 this.cacheName = cacheName; 64 this.maxInMemory = maxInMemory; 65 if (useFileSystemStore) { 66 if (CacheLineTable.jdbmMgr == null) { 68 synchronized (this) { 69 if (CacheLineTable.jdbmMgr == null) { 70 try { 71 Debug.logImportant("Creating file system cache store for cache with name: " + cacheName, module); 72 CacheLineTable.jdbmMgr = new JdbmRecordManager(fileStore); 73 } catch (IOException e) { 74 Debug.logError(e, "Error creating file system cache store for cache with name: " + cacheName, module); 75 } 76 } 77 } 78 } 79 if (CacheLineTable.jdbmMgr != null) { 80 try { 81 long recno = CacheLineTable.jdbmMgr.getNamedObject(cacheName); 82 if (recno != 0) { 83 this.fileTable = jdbm.htree.HTree.load(CacheLineTable.jdbmMgr, recno); 84 } else { 85 this.fileTable = jdbm.htree.HTree.createInstance(CacheLineTable.jdbmMgr); 86 CacheLineTable.jdbmMgr.setNamedObject(cacheName, this.fileTable.getRecid()); 87 CacheLineTable.jdbmMgr.commit(); 88 } 89 } catch (IOException e) { 90 Debug.logError(e, module); 91 } 92 } 93 } 94 this.setLru(maxInMemory); 95 } 96 97 public synchronized Object put(Object key, Object value) { 98 if (key == null) { 99 if (Debug.verboseOn()) Debug.logVerbose("In CacheLineTable tried to put with null key, using NullObject" + this.cacheName, module); 100 key = ObjectType.NULL; 101 } 102 memoryTable.put(key, value); 103 if (fileTable != null) { 104 try { 105 fileTable.put(key, value); 106 CacheLineTable.jdbmMgr.commit(); 107 } catch (IOException e) { 108 Debug.logError(e, module); 109 } 110 } 111 return value; 112 } 113 114 public Object get(Object key) { 115 if (key == null) { 116 if (Debug.verboseOn()) Debug.logVerbose("In CacheLineTable tried to get with null key, using NullObject" + this.cacheName, module); 117 key = ObjectType.NULL; 118 } 119 Object value = memoryTable.get(key); 120 if (value == null) { 121 if (fileTable != null) { 122 try { 123 value = fileTable.get(key); 124 } catch (IOException e) { 125 Debug.logError(e, module); 126 } 127 } 128 } 129 return value; 130 } 131 132 public synchronized Object remove(Object key) { 133 if (key == null) { 134 if (Debug.verboseOn()) Debug.logVerbose("In CacheLineTable tried to remove with null key, using NullObject" + this.cacheName, module); 135 key = ObjectType.NULL; 136 } 137 Object value = this.get(key); 138 if (fileTable != null) { 139 try { 140 fileTable.remove(key); 141 } catch (IOException e) { 142 Debug.logError(e, module); 143 } 144 } 145 memoryTable.remove(key); 146 return value; 147 } 148 149 public synchronized Collection values() { 150 List values = FastList.newInstance(); 151 152 if (fileTable != null) { 153 try { 154 jdbm.helper.FastIterator iter = fileTable.values(); 155 Object value = iter.next(); 156 while (value != null) { 157 values.add(value); 158 value = iter.next(); 159 } 160 } catch (IOException e) { 161 Debug.logError(e, module); 162 } 163 } else { 164 values.addAll(memoryTable.values()); 165 } 166 167 return values; 168 } 169 170 174 public synchronized Set keySet() { 175 Set keys = new HashSet (); 177 178 if (fileTable != null) { 179 try { 180 jdbm.helper.FastIterator iter = fileTable.keys(); 181 Object key = null; 182 while ((key = iter.next()) != null) { 183 if (key instanceof ObjectType.NullObject) { 184 keys.add(null); 185 } else { 186 keys.add(key); 187 } 188 } 189 } catch (IOException e) { 190 Debug.logError(e, module); 191 } 192 } else { 193 keys.addAll(memoryTable.keySet()); 194 if (keys.contains(ObjectType.NULL)) { 195 keys.remove(ObjectType.NULL); 196 keys.add(null); 197 } 198 } 199 200 return Collections.unmodifiableSet(keys); 201 } 202 203 public synchronized void clear() { 204 if (fileTable != null && this.size() > 0) { 205 try { 206 long recid = fileTable.getRecid(); 208 CacheLineTable.jdbmMgr.delete(recid); 209 CacheLineTable.jdbmMgr.commit(); 210 this.fileTable = null; 211 212 this.fileTable = jdbm.htree.HTree.createInstance(CacheLineTable.jdbmMgr); 214 CacheLineTable.jdbmMgr.setNamedObject(cacheName, this.fileTable.getRecid()); 215 CacheLineTable.jdbmMgr.commit(); 216 } catch (IOException e) { 217 Debug.logError(e, module); 218 } 219 } 220 memoryTable.clear(); 221 } 222 223 public int size() { 224 if (fileTable != null) { 225 return this.keySet().size(); 226 } else { 227 return memoryTable.size(); 228 } 229 } 230 231 public synchronized void setLru(int newSize) { 232 this.maxInMemory = newSize; 233 234 Map oldmap = null; 235 if (this.memoryTable != null) { 236 oldmap = FastMap.newInstance(); 238 oldmap.putAll(this.memoryTable); 239 } 240 241 if (newSize > 0) { 242 this.memoryTable = new LRUMap(newSize); 243 } else { 244 this.memoryTable = FastMap.newInstance(); 245 } 246 247 if (oldmap != null) { 248 this.memoryTable.putAll(oldmap); 249 } 250 } 251 252 public synchronized Object getKeyFromMemory(int index) { 253 Iterator i = null; 254 if (memoryTable instanceof LRUMap) { 255 i = ((LRUMap) memoryTable).orderedMapIterator(); 256 } else { 257 i = memoryTable.keySet().iterator(); 258 } 259 260 int currentIdx = 0; 261 while (i.hasNext()) { 262 Object key = i.next(); 263 if (currentIdx == index) { 264 return key; 265 } 266 } 267 return null; 268 } 269 } 270 271 | Popular Tags |