package com.c2kernel.persistency; import java.util.ArrayList; import java.util.ConcurrentModificationException; import java.util.HashMap; import java.util.Map; import com.c2kernel.entity.C2KLocalObject; import com.c2kernel.process.auth.Authenticator; import com.c2kernel.utils.Logger; public class MemoryOnlyClusterStorage extends ClusterStorage { HashMap> memoryCache = new HashMap>(); /** * */ public MemoryOnlyClusterStorage() { memoryCache = new HashMap>(); } @Override public void open(Authenticator auth) throws ClusterStorageException { } @Override public void close() throws ClusterStorageException { } @Override public short queryClusterSupport(String clusterType) { return ClusterStorage.READWRITE; } @Override public String getName() { return "Memory Cache"; } @Override public String getId() { return "Memory Cache"; } @Override public C2KLocalObject get(Integer sysKey, String path) throws ClusterStorageException { Map sysKeyMemCache = memoryCache.get(sysKey); if (sysKeyMemCache != null) return sysKeyMemCache.get(path); return null; } @Override public void put(Integer sysKey, C2KLocalObject obj) throws ClusterStorageException { // create item cache if not present Map sysKeyMemCache; synchronized (memoryCache) { if (memoryCache.containsKey(sysKey)) sysKeyMemCache = memoryCache.get(sysKey); else { sysKeyMemCache = new HashMap(); memoryCache.put(sysKey, sysKeyMemCache); } } // store object in the cache String path = ClusterStorage.getPath(obj); synchronized(sysKeyMemCache) { sysKeyMemCache.put(path, obj); } } @Override public void delete(Integer sysKey, String path) throws ClusterStorageException { Map sysKeyMemCache = memoryCache.get(sysKey); if (sysKeyMemCache != null) { synchronized (sysKeyMemCache) { if (sysKeyMemCache.containsKey(path)) { sysKeyMemCache.remove(path); if (sysKeyMemCache.isEmpty()) { synchronized (memoryCache) { memoryCache.remove(sysKey); } } } } } } @Override public String[] getClusterContents(Integer sysKey, String path) throws ClusterStorageException { Map sysKeyMemCache = memoryCache.get(sysKey); ArrayList result = new ArrayList(); if (sysKeyMemCache != null) { while (path.endsWith("/")) path = path.substring(0,path.length()-1); path = path+"/"; for (String thisPath : sysKeyMemCache.keySet()) { if (thisPath.startsWith(path)) { String end = thisPath.substring(path.length()); int slash = end.indexOf('/'); String suffix = slash>-1?end.substring(0, slash):end; if (!result.contains(suffix)) result.add(suffix); } } } return result.toArray(new String[result.size()]); } public void dumpContents(int sysKey) { synchronized(memoryCache) { Logger.msg(0, "Cached Objects of Entity "+sysKey); Map sysKeyMemCache = memoryCache.get(sysKey); if (sysKeyMemCache == null) { Logger.msg(0, "No cache found"); return; } try { synchronized(sysKeyMemCache) { for (Object name : sysKeyMemCache.keySet()) { String path = (String) name; try { Logger.msg(0, " Path "+path+": "+sysKeyMemCache.get(path).getClass().getName()); } catch (NullPointerException e) { Logger.msg(0, " Path "+path+": reaped"); } } } } catch (ConcurrentModificationException ex) { Logger.msg(0, "Cache modified - aborting"); } } Logger.msg(0, "Total number of cached entities: "+memoryCache.size()); } }