diff options
Diffstat (limited to 'source/com/c2kernel/persistency/ClusterStorageManager.java')
| -rw-r--r-- | source/com/c2kernel/persistency/ClusterStorageManager.java | 111 |
1 files changed, 54 insertions, 57 deletions
diff --git a/source/com/c2kernel/persistency/ClusterStorageManager.java b/source/com/c2kernel/persistency/ClusterStorageManager.java index 560e022..5ce8d06 100644 --- a/source/com/c2kernel/persistency/ClusterStorageManager.java +++ b/source/com/c2kernel/persistency/ClusterStorageManager.java @@ -1,6 +1,10 @@ package com.c2kernel.persistency;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.ConcurrentModificationException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.StringTokenizer;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.C2KLocalObject;
@@ -57,7 +61,7 @@ public class ClusterStorageManager { allStores.put(newStorage.getId(), newStorage);
rootStores.add(newStorage);
clusterPriority[clusterNo++] = newStorage.getId();
-
+
} catch (ClusterStorageException ex) {
Logger.error(ex);
throw new ClusterStorageException("ClusterStorageManager.init() - Error initialising storage handler " + newStorageClass +
@@ -78,8 +82,7 @@ public class ClusterStorageManager { }
public void close() {
- for (Iterator<ClusterStorage> iter = allStores.values().iterator(); iter.hasNext();) {
- ClusterStorage thisStorage = (ClusterStorage)iter.next();
+ for (ClusterStorage thisStorage : allStores.values()) {
try {
thisStorage.close();
} catch (ClusterStorageException ex) {
@@ -94,28 +97,28 @@ public class ClusterStorageManager { * Collection, Property) Must specify if the request is a read or a write.
*/
private ArrayList<ClusterStorage> findStorages(String clusterType, boolean forWrite) {
-
+
if (!ready) {
Logger.error("ClusterStorageManager.findStorages() - called before init!");
return null;
}
-
+
// choose the right cache for readers or writers
HashMap<String, ArrayList<ClusterStorage>> cache;
if (forWrite)
cache = clusterWriters;
else
cache = clusterReaders;
-
+
// check to see if we've been asked to do this before
if (cache.containsKey(clusterType))
- return (ArrayList<ClusterStorage>)cache.get(clusterType);
-
+ return cache.get(clusterType);
+
// not done yet, we'll have to query them all
Logger.msg(7, "ClusterStorageManager.findStorages() - finding storage for "+clusterType+" forWrite:"+forWrite);
ArrayList<ClusterStorage> useableStorages = new ArrayList<ClusterStorage>();
- for (int i = 0; i < clusterPriority.length; i++) {
- ClusterStorage thisStorage = (ClusterStorage)allStores.get(clusterPriority[i]);
+ for (String element : clusterPriority) {
+ ClusterStorage thisStorage = allStores.get(element);
short requiredSupport = forWrite ? ClusterStorage.WRITE : ClusterStorage.READ;
if ((thisStorage.queryClusterSupport(clusterType) & requiredSupport) == requiredSupport) {
Logger.msg(7, "ClusterStorageManager.findStorages() - Got "+thisStorage.getName());
@@ -126,20 +129,18 @@ public class ClusterStorageManager { return useableStorages;
}
- /**
- * Retrieves the ids of the next level of a cluster
+ /**
+ * Retrieves the ids of the next level of a cluster
* Does not look in any currently open transactions.
*/
public String[] getClusterContents(Integer sysKey, String path) throws ClusterStorageException {
//String[] retArr = new String[0];
ArrayList<String> contents = new ArrayList<String>();
// get all readers
- String type = ClusterStorage.getClusterType(path);
Logger.msg(8, "ClusterStorageManager.getClusterContents() - Finding contents of "+path);
ArrayList<ClusterStorage> readers = findStorages(ClusterStorage.getClusterType(path), false);
// try each in turn until we get a result
- for (Iterator<ClusterStorage> i = readers.iterator(); i.hasNext();) {
- ClusterStorage thisReader = (ClusterStorage)i.next();
+ for (ClusterStorage thisReader : readers) {
try {
String[] thisArr = thisReader.getClusterContents(sysKey, path);
if (thisArr != null) {
@@ -154,10 +155,10 @@ public class ClusterStorageManager { " could not retrieve contents of " + sysKey + "/" + path + ": " + e.getMessage());
}
}
-
+
String[] retArr = new String[0];
- retArr = (String[])contents.toArray(retArr);
- return retArr;
+ retArr = contents.toArray(retArr);
+ return retArr;
}
/** Internal get method. Retrieves clusters from ClusterStorages & maintains the memory cache */
@@ -166,16 +167,16 @@ public class ClusterStorageManager { // check cache first
SoftCache<String, C2KLocalObject> sysKeyMemCache = null;
if (memoryCache.containsKey(sysKeyIntObj)) {
- sysKeyMemCache = (SoftCache<String, C2KLocalObject>)memoryCache.get(sysKeyIntObj);
+ sysKeyMemCache = memoryCache.get(sysKeyIntObj);
synchronized(sysKeyMemCache) {
- C2KLocalObject obj = (C2KLocalObject)sysKeyMemCache.get(path);
+ C2KLocalObject obj = sysKeyMemCache.get(path);
if (obj != null) {
Logger.msg(7, "ClusterStorageManager.get() - found "+sysKeyIntObj+"/"+path+" in memcache");
return obj;
}
}
}
-
+
// special case - loading viewpoint contents
if (path.startsWith(ClusterStorage.VIEWPOINT) &&
path.endsWith("/data")) {
@@ -188,11 +189,10 @@ public class ClusterStorageManager { return data;
}
}
-
+
// else try each reader in turn until we find it
ArrayList<ClusterStorage> readers = findStorages(ClusterStorage.getClusterType(path), false);
- for (Iterator<ClusterStorage> i = readers.iterator(); i.hasNext(); ) {
- ClusterStorage thisReader = (ClusterStorage)i.next();
+ for (ClusterStorage thisReader : readers) {
try {
result = thisReader.get(sysKeyIntObj, path);
Logger.msg(7, "ClusterStorageManager.get() - reading "+path+" from "+thisReader.getName() + " for intkey=" + sysKeyIntObj);
@@ -222,8 +222,7 @@ public class ClusterStorageManager { public void put(Integer sysKeyIntObj, C2KLocalObject obj) throws ClusterStorageException {
String path = ClusterStorage.getPath(obj);
ArrayList<ClusterStorage> writers = findStorages(ClusterStorage.getClusterType(path), true);
- for (Iterator<ClusterStorage> i = writers.iterator(); i.hasNext(); ) {
- ClusterStorage thisWriter = (ClusterStorage)i.next();
+ for (ClusterStorage thisWriter : writers) {
try {
Logger.msg(7, "ClusterStorageManager.put() - writing "+path+" to "+thisWriter.getName());
thisWriter.put(sysKeyIntObj, obj);
@@ -236,18 +235,18 @@ public class ClusterStorageManager { // put in mem cache if that worked
SoftCache<String, C2KLocalObject> sysKeyMemCache;
if (memoryCache.containsKey(sysKeyIntObj))
- sysKeyMemCache = (SoftCache<String, C2KLocalObject>)memoryCache.get(sysKeyIntObj);
+ sysKeyMemCache = memoryCache.get(sysKeyIntObj);
else {
sysKeyMemCache = new SoftCache<String, C2KLocalObject>();
- synchronized (memoryCache) {
+ synchronized (memoryCache) {
memoryCache.put(sysKeyIntObj, sysKeyMemCache);
}
}
-
+
synchronized(sysKeyMemCache) {
sysKeyMemCache.put(path, obj);
}
-
+
if (Logger.doLog(9)) dumpCacheContents(9);
// transmit proxy event
@@ -257,8 +256,7 @@ public class ClusterStorageManager { /** Deletes a cluster from all writers */
public void remove(Integer sysKeyIntObj, String path) throws ClusterStorageException {
ArrayList<ClusterStorage> writers = findStorages(ClusterStorage.getClusterType(path), true);
- for (Iterator<ClusterStorage> i = writers.iterator(); i.hasNext(); ) {
- ClusterStorage thisWriter = (ClusterStorage)i.next();
+ for (ClusterStorage thisWriter : writers) {
try {
Logger.msg(7, "ClusterStorageManager.delete() - removing "+path+" from "+thisWriter.getName());
thisWriter.delete(sysKeyIntObj, path);
@@ -268,24 +266,24 @@ public class ClusterStorageManager { throw e;
}
}
-
+
if (memoryCache.containsKey(sysKeyIntObj)) {
- SoftCache<?, ?> sysKeyMemCache = (SoftCache<?, ?>)memoryCache.get(sysKeyIntObj);
+ SoftCache<?, ?> sysKeyMemCache = memoryCache.get(sysKeyIntObj);
synchronized (sysKeyMemCache) {
sysKeyMemCache.remove(path);
}
}
-
+
// transmit proxy event
EntityProxyManager.sendProxyEvent( new ProxyMessage(sysKeyIntObj.intValue(), path, ProxyMessage.DELETED));
}
-
+
public void clearCache(Integer sysKeyIntObj, String path) {
Logger.msg(7, "CSM.clearCache() - removing "+sysKeyIntObj+"/"+path);
-
+
if (memoryCache.containsKey(sysKeyIntObj)) {
- SoftCache<?, ?> sysKeyMemCache = (SoftCache<?, ?>)memoryCache.get(sysKeyIntObj);
+ SoftCache<?, ?> sysKeyMemCache = memoryCache.get(sysKeyIntObj);
synchronized(sysKeyMemCache) {
for (Iterator<?> iter = sysKeyMemCache.keySet().iterator(); iter.hasNext();) {
String thisPath = (String)iter.next();
@@ -297,15 +295,15 @@ public class ClusterStorageManager { }
}
}
-
+
public void clearCache(Integer sysKeyIntObj) {
Logger.msg(5, "CSM.clearCache() - removing entire cache of "+sysKeyIntObj);
-
+
if (memoryCache.containsKey(sysKeyIntObj)) {
synchronized (memoryCache) {
if (Logger.doLog(6)) {
- SoftCache<?, ?> sysKeyMemCache = (SoftCache<?, ?>)memoryCache.get(sysKeyIntObj);
+ SoftCache<?, ?> sysKeyMemCache = memoryCache.get(sysKeyIntObj);
int size = sysKeyMemCache.size();
Logger.msg(6, "CSM.clearCache() - "+size+" objects to remove.");
}
@@ -315,25 +313,24 @@ public class ClusterStorageManager { else
Logger.msg(6, "CSM.clearCache() - No objects cached");
}
-
+
public void clearCache() {
synchronized (memoryCache) {
memoryCache.clear();
}
Logger.msg(5, "CSM.clearCache() - cleared entire cache, "+memoryCache.size()+" entities.");
}
-
+
public void dumpCacheContents(int logLevel) {
if (!Logger.doLog(logLevel)) return;
synchronized(memoryCache) {
- for (Iterator<Integer> iter = memoryCache.keySet().iterator(); iter.hasNext();) {
- Integer sysKey = (Integer) iter.next();
+ for (Integer sysKey : memoryCache.keySet()) {
Logger.msg(logLevel, "Cached Objects of Entity "+sysKey);
- SoftCache<?, ?> sysKeyMemCache = (SoftCache<?, ?>)memoryCache.get(sysKey);
+ SoftCache<?, ?> sysKeyMemCache = memoryCache.get(sysKey);
try {
synchronized(sysKeyMemCache) {
- for (Iterator<?> iterator = sysKeyMemCache.keySet().iterator();iterator.hasNext();) {
- String path = (String) iterator.next();
+ for (Object name : sysKeyMemCache.keySet()) {
+ String path = (String) name;
try {
Logger.msg(logLevel, " Path "+path+": "+sysKeyMemCache.get(path).getClass().getName());
} catch (NullPointerException e) {
@@ -345,20 +342,20 @@ public class ClusterStorageManager { Logger.msg(logLevel, "Cache modified - aborting");
}
}
- Logger.msg(logLevel, "Total number of cached entities: "+memoryCache.size());
+ Logger.msg(logLevel, "Total number of cached entities: "+memoryCache.size());
}
- }
-
+ }
+
public Object query(String id, Object query) throws ClusterStorageException {
- ClusterStorage requiredStorage = (ClusterStorage)allStores.get(id);
- if (requiredStorage == null)
+ ClusterStorage requiredStorage = allStores.get(id);
+ if (requiredStorage == null)
throw new ClusterStorageException("Storage "+id+" not found.");
return requiredStorage.query(query);
}
-
+
public String queryToXML(String id, String query, boolean genericFormat) throws ClusterStorageException {
- ClusterStorage requiredStorage = (ClusterStorage)allStores.get(id);
- if (requiredStorage == null)
+ ClusterStorage requiredStorage = allStores.get(id);
+ if (requiredStorage == null)
throw new ClusterStorageException("Storage "+id+" not found.");
return requiredStorage.queryToXML(query, genericFormat);
}
|
