X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.acorn%2Fsrc%2Forg%2Fsimantics%2Facorn%2Flru%2FClusterLRU.java;h=e94abf7f208379efbc739a212a7338a545e5babe;hp=1f0db54354184c89af52d102fe622cb88d9c9926;hb=0df63f38249cbbc584d3a130773680c309fb1f71;hpb=35a513a86eb0e30c29ebf094cc778b08e5bbded4 diff --git a/bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ClusterLRU.java b/bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ClusterLRU.java index 1f0db5435..e94abf7f2 100644 --- a/bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ClusterLRU.java +++ b/bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ClusterLRU.java @@ -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 { + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ClusterLRU.class); + final private BijectionMap clusterMapping = new BijectionMap(); - 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 { 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 { } 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 { /* * 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 { 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 { } - 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 { } - 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 { } - public int getClusterKeyByClusterUIDOrMake(ClusterUID clusterUID) { + public int getClusterKeyByClusterUIDOrMake(ClusterUID clusterUID) throws AcornAccessVerificationException { if(VERIFY) verifyAccess(); @@ -154,18 +154,20 @@ public class ClusterLRU extends LRU { } - 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 { 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 { } - 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 getClusterByClusterUIDOrMakeProxy(ClusterUID uid) throws DatabaseException { + public T getClusterByClusterUIDOrMakeProxy(ClusterUID uid) throws DatabaseException, AcornAccessVerificationException, IllegalAcornStateException { return (T) getClusterByClusterUIDOrMake(uid); } @SuppressWarnings("unchecked") - public T getClusterProxyByResourceKey(int resourceKey) throws DatabaseException { + public T getClusterProxyByResourceKey(int resourceKey) throws DatabaseException, AcornAccessVerificationException, IllegalAcornStateException { if(VERIFY) verifyAccess(); @@ -217,7 +217,7 @@ public class ClusterLRU extends LRU { } - 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 { } - 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 { 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 { 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."); + } } }