]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ClusterLRU.java
Replace System.err and System.out with SLF4J Logging
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / lru / ClusterLRU.java
index 1f0db54354184c89af52d102fe622cb88d9c9926..e94abf7f208379efbc739a212a7338a545e5babe 100644 (file)
@@ -6,6 +6,8 @@ import java.util.concurrent.atomic.AtomicInteger;
 
 import org.simantics.acorn.ClusterManager;
 import org.simantics.acorn.cluster.ClusterImpl;
+import org.simantics.acorn.exception.AcornAccessVerificationException;
+import org.simantics.acorn.exception.IllegalAcornStateException;
 import org.simantics.acorn.internal.BijectionMap;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.ClusterDoesNotExistException;
@@ -13,24 +15,24 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.ClusterBase;
 import org.simantics.db.impl.ClusterI;
 import org.simantics.db.service.ClusterUID;
+import org.slf4j.LoggerFactory;
+
+import gnu.trove.map.hash.TIntIntHashMap;
 
-import gnu.trove.TIntIntHashMap;
 
 public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
 
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ClusterLRU.class);
+    
        final private BijectionMap<ClusterUID, Integer> clusterMapping = new BijectionMap<ClusterUID, Integer>();
-       final private ClusterManager manager;
        
        public ClusterLRU(ClusterManager manager, String identifier, Path writeDir) {
-               
-               super(identifier, writeDir);
-               this.manager = manager;
+               super(manager, identifier, writeDir);
                
                clusterMapping.map(ClusterUID.make(0,2), clusterMapping.size() + 1);
-
        }
 
-       public ClusterInfo getOrCreate(ClusterUID uid, boolean makeIfNull) {
+       public ClusterInfo getOrCreate(ClusterUID uid, boolean makeIfNull) throws IllegalAcornStateException, AcornAccessVerificationException {
                
                try {
                        
@@ -40,7 +42,7 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
 
                        if (info == null) {
                                
-                               if(!makeIfNull) throw new IllegalStateException("Asked for an existing cluster " + uid + " that was not found.");
+                               if(!makeIfNull) throw new IllegalAcornStateException("Asked for an existing cluster " + uid + " that was not found.");
 
                                Integer clusterKey = clusterMapping.getRight(uid);
                                if (clusterKey == null) {
@@ -54,9 +56,10 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
                        }
 
                        return info;
-                       
+               } catch (IllegalAcornStateException | AcornAccessVerificationException e) {
+                   throw e;
                } catch (Throwable t) {
-                       throw new IllegalStateException(t);
+                       throw new IllegalAcornStateException(t);
                } finally {
                        
                        releaseMutex();
@@ -68,16 +71,15 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
        /*
         * This method waits - we have no locks here
         */
-       public void ensureUpdates(ClusterUID uid) throws DatabaseException {
+       public void ensureUpdates(ClusterUID uid) throws ClusterDoesNotExistException, AcornAccessVerificationException, IllegalAcornStateException {
 
                ClusterInfo info = getWithoutMutex(uid);
                if(info == null)
                    throw new ClusterDoesNotExistException("Asked a cluster which does not exist: " + uid);
                info.waitForUpdates();
-               
        }
 
-       public ClusterInfo get(ClusterUID uid, boolean makeIfNull, boolean ensureUpdates) throws DatabaseException {
+       public ClusterInfo get(ClusterUID uid, boolean makeIfNull, boolean ensureUpdates) throws AcornAccessVerificationException, IllegalAcornStateException {
 
                if (ensureUpdates) {
                    try {
@@ -86,19 +88,18 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
                        if (makeIfNull) {
                            Logger.defaultLogError("For debug purposes, creating cluster which does not exist", e);
                        } else {
-                           throw e;
+                           throw new IllegalAcornStateException(e);
                        }
                    }
                }
                return getOrCreate(uid, makeIfNull);
        }
        
-       public ClusterInfo get(ClusterUID uid, boolean makeIfNull) throws DatabaseException {
+       public ClusterInfo get(ClusterUID uid, boolean makeIfNull) throws AcornAccessVerificationException, IllegalAcornStateException {
                return get(uid, makeIfNull, true);
-               
        }
 
-       public int getResourceKey(ClusterUID uid, int index) {
+       public int getResourceKey(ClusterUID uid, int index) throws AcornAccessVerificationException {
 
                if(VERIFY) verifyAccess();
 
@@ -111,20 +112,19 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
 
        }
 
-       public int getResourceKeyWithoutMutex(ClusterUID uid, int index) {
+       public int getResourceKeyWithoutMutex(ClusterUID uid, int index) throws IllegalAcornStateException {
                
                acquireMutex();
                try {
                        return getResourceKey(uid, index);
                } catch (Throwable t) {
-                       throw new IllegalStateException(t);
+                       throw new IllegalAcornStateException(t);
                } finally {
                        releaseMutex();
                }
-               
        }
 
-       public int createClusterKeyByClusterUID(ClusterUID uid) {
+       public int createClusterKeyByClusterUID(ClusterUID uid) throws AcornAccessVerificationException {
                
                if(VERIFY) verifyAccess();
                
@@ -137,7 +137,7 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
                
        }
 
-       public ClusterBase getClusterByClusterUIDOrMake(ClusterUID uid) throws DatabaseException {
+       public ClusterBase getClusterByClusterUIDOrMake(ClusterUID uid) throws AcornAccessVerificationException, IllegalAcornStateException {
                
                if(VERIFY) verifyAccess();
                
@@ -146,7 +146,7 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
                
        }
 
-       public int getClusterKeyByClusterUIDOrMake(ClusterUID clusterUID) {
+       public int getClusterKeyByClusterUIDOrMake(ClusterUID clusterUID) throws AcornAccessVerificationException {
 
                if(VERIFY) verifyAccess();
 
@@ -154,18 +154,20 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
                        
        }
        
-       public int getClusterKeyByClusterUIDOrMakeWithoutMutex(ClusterUID clusterUID) {
+       public int getClusterKeyByClusterUIDOrMakeWithoutMutex(ClusterUID clusterUID) throws IllegalAcornStateException, AcornAccessVerificationException {
                acquireMutex();
                try {
                        return getClusterKeyByClusterUIDOrMake(clusterUID);
+               } catch (AcornAccessVerificationException e) {
+            throw e;
                } catch (Throwable t) {
-                       throw new IllegalStateException(t);
+                       throw new IllegalAcornStateException(t);
                } finally {
                        releaseMutex();
                }
        }
 
-       public ClusterBase getClusterByClusterKey(int clusterKey) throws DatabaseException {
+       public ClusterBase getClusterByClusterKey(int clusterKey) throws AcornAccessVerificationException, IllegalAcornStateException {
                
                if(VERIFY) verifyAccess();
                
@@ -174,16 +176,16 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
                info.acquireMutex();
                try {
                        return info.getCluster();
+               } catch (IllegalAcornStateException | AcornAccessVerificationException e) {
+                   throw e;
                } catch (Throwable t) {
-                       throw new IllegalStateException(t);
+                       throw new IllegalAcornStateException(t);
                } finally {
                        info.releaseMutex();
                }
-               
        }
 
-       public ClusterUID getClusterUIDByResourceKey(int resourceKey)
-                       throws DatabaseException {
+       public ClusterUID getClusterUIDByResourceKey(int resourceKey) throws AcornAccessVerificationException {
                
                if(VERIFY) verifyAccess();
                
@@ -192,24 +194,22 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
                
        }
        
-       public ClusterUID getClusterUIDByResourceKeyWithoutMutex(int resourceKey) throws DatabaseException {
+       public ClusterUID getClusterUIDByResourceKeyWithoutMutex(int resourceKey) throws IllegalAcornStateException, AcornAccessVerificationException {
                acquireMutex();
                try {
                        return getClusterUIDByResourceKey(resourceKey);
-               } catch (Throwable t) {
-                       throw new IllegalStateException(t);
                } finally {
                        releaseMutex();
                }
        }
 
        @SuppressWarnings("unchecked")
-       public <T extends ClusterI> T getClusterByClusterUIDOrMakeProxy(ClusterUID uid) throws DatabaseException {
+       public <T extends ClusterI> T getClusterByClusterUIDOrMakeProxy(ClusterUID uid) throws DatabaseException, AcornAccessVerificationException, IllegalAcornStateException {
                return (T) getClusterByClusterUIDOrMake(uid);
        }
 
        @SuppressWarnings("unchecked")
-       public <T extends ClusterI> T getClusterProxyByResourceKey(int resourceKey) throws DatabaseException {
+       public <T extends ClusterI> T getClusterProxyByResourceKey(int resourceKey) throws DatabaseException, AcornAccessVerificationException, IllegalAcornStateException {
                
                if(VERIFY) verifyAccess();
 
@@ -217,7 +217,7 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
                
        }
 
-       public int getClusterKeyByUID(long id1, long id2) throws DatabaseException {
+       public int getClusterKeyByUID(long id1, long id2) throws DatabaseException, AcornAccessVerificationException {
                
                if(VERIFY) verifyAccess();
 
@@ -225,17 +225,15 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
                
        }
        
-       public int getClusterKeyByUIDWithoutMutex(long id1, long id2) throws DatabaseException {
-
+       public int getClusterKeyByUIDWithoutMutex(long id1, long id2) throws DatabaseException, IllegalAcornStateException {
                acquireMutex();
                try {
                        return getClusterKeyByClusterUIDOrMake(ClusterUID.make(id1, id2));
                } catch (Throwable t) {
-                       throw new IllegalStateException(t);
+                       throw new IllegalAcornStateException(t);
                } finally {
                        releaseMutex();
                }
-
        }
 
        
@@ -289,7 +287,7 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
                                                        synchronized(map) {
                                                                value = map.get(key);
                                                                if(key != value) {
-                                                                       System.err.println("Read failed for real " + key + " vs. " + value);
+                                                                       LOGGER.warn("Read failed for real " + key + " vs. " + value);
                                                                }
                                                                //ws.release();
                                                        }
@@ -307,9 +305,10 @@ public class ClusterLRU extends LRU<ClusterUID, ClusterInfo> {
                write.join();
                read.join();
                
-               long duration = System.nanoTime() - start;
-               System.err.println("took " + 1e-9*duration + "s.");
-               
+               if (LOGGER.isDebugEnabled()) {
+                   long duration = System.nanoTime() - start;
+                   LOGGER.debug("took " + 1e-9*duration + "s.");
+               }
        }
        
 }