package com.c2kernel.persistency; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import com.c2kernel.entity.C2KLocalObject; public class MemoryOnlyClusterStorage extends ClusterStorage { HashMap> memoryCache = new HashMap>(); /** * */ public MemoryOnlyClusterStorage() { memoryCache = new HashMap>(); } @Override public void open() 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) { if (path.endsWith("/")) path = path.substring(0,path.length()-1); path = path+"/"; for (String thisPath : sysKeyMemCache.keySet()) { if (thisPath.startsWith(path)) { int slash = path.indexOf('/'); String suffix = slash>-1?path.substring(slash+1):path; if (!result.contains(suffix)) result.add(suffix); } } } return result.toArray(new String[result.size()]); } }