X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.procore%2Fsrc%2Ffi%2Fvtt%2Fsimantics%2Fprocore%2Finternal%2FClusterTable.java;h=123f346a4dd0322254aa74bfd1a259b9bd07357c;hp=355764d178138df429450ab7eac5ab565dac8d00;hb=cec265e1216a0a211e6e6dc4f91d5fda4c5747db;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java index 355764d17..123f346a4 100644 --- a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java +++ b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java @@ -22,9 +22,9 @@ import java.util.concurrent.Semaphore; import org.simantics.databoard.Bindings; import org.simantics.db.ClusterCreator; import org.simantics.db.Database; +import org.simantics.db.Database.Session.ClusterChanges; import org.simantics.db.DevelopmentKeys; import org.simantics.db.SessionVariables; -import org.simantics.db.Database.Session.ClusterChanges; import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.ClusterDoesNotExistException; import org.simantics.db.exception.DatabaseException; @@ -529,17 +529,19 @@ public final class ClusterTable implements IClusterTable { ClusterImpl getNewResourceCluster(ClusterSupport cs, GraphSession graphSession, boolean writeOnly) throws DatabaseException { + ClusterImpl result = null; if (Constants.NewClusterId == newResourceClusterId) { newResourceClusterId = graphSession.newClusterId(); - return getClusterByClusterIdOrMake(newResourceClusterId, writeOnly); + result = getClusterByClusterIdOrMake(newResourceClusterId, writeOnly); } else { ClusterImpl cluster = getClusterByClusterIdOrThrow(newResourceClusterId); if (cluster.getNumberOfResources(cs) >= CLUSTER_FILL_SIZE) { newResourceClusterId = graphSession.newClusterId(); cluster = getClusterByClusterIdOrMake(newResourceClusterId, writeOnly); } - return cluster; + result = cluster; } + return ensureLoaded(result); } void flushCluster(GraphSession graphSession) { @@ -818,25 +820,9 @@ public final class ClusterTable implements IClusterTable { TLongIntHashMap clusterLoadHistogram = new TLongIntHashMap(); int clusterLoadCounter = 0; - @SuppressWarnings("unchecked") - public final T getClusterByResourceKey(final int resourceKey) { - int clusterKey = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(resourceKey); - if (ClusterTraitsBase.isVirtualClusterKey(clusterKey)) - throw new RuntimeException("Tried to get a persistent cluster for a virtual resource."); - ClusterI c = clusterArray[clusterKey]; - if (c == null) - return null; - if (c.isLoaded()) { - if ((counter++ & 4095) == 0) - refreshImportance((ClusterImpl) c); - return (T) c; - } - if (!(c instanceof ClusterSmall)) { - Logger.defaultLogError("Proxy must be instance of ClusterSmall"); - return null; - } + private T ensureLoaded(T c) { ClusterI cluster; - ClusterSmall cs = (ClusterSmall) c; + ClusterImpl cs = (ClusterImpl) c; try { if(DebugPolicy.REPORT_CLUSTER_LOADING) { long start = System.nanoTime(); @@ -861,13 +847,32 @@ public final class ClusterTable implements IClusterTable { Logger.defaultLogError(e); if (DebugPolicy.REPORT_CLUSTER_EVENTS) e.printStackTrace(); - String msg = "Failed to load cluster " + cs.getClusterUID() + " for resource key " + resourceKey - + " resourceId=" + (((cs.getClusterId() << 16 + (resourceKey & 65535)))); + String msg = "Failed to load cluster " + cs.getClusterUID();// + " resourceId=" + (((cs.getClusterId() << 16 + (resourceKey & 65535)))); // TODO: this jams the system => needs refactoring. throw new RuntimeDatabaseException(msg, e); } return (T) cluster; } + + @SuppressWarnings("unchecked") + public final T getClusterByResourceKey(final int resourceKey) { + int clusterKey = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(resourceKey); + if (ClusterTraitsBase.isVirtualClusterKey(clusterKey)) + throw new RuntimeException("Tried to get a persistent cluster for a virtual resource."); + ClusterI c = clusterArray[clusterKey]; + if (c == null) + return null; + if (c.isLoaded()) { + if ((counter++ & 4095) == 0) + refreshImportance((ClusterImpl) c); + return (T) c; + } + if (!(c instanceof ClusterSmall)) { + Logger.defaultLogError("Proxy must be instance of ClusterSmall"); + return null; + } + return ensureLoaded((T)c); + } @SuppressWarnings("unchecked") final T checkedGetClusterByResourceKey(final int resourceKey) { @@ -1139,11 +1144,14 @@ public final class ClusterTable implements IClusterTable { if(exist != null) return exist; ClusterI cluster = getClusterByResourceKey(id); - boolean result = cluster == null ? false : cluster.getImmutable(); - - markImmutable(cluster, result); - return result; - + if(cluster == null) { + return false; + } else { + boolean result = cluster.getImmutable(); + markImmutable(cluster, result); + return result; + } + } public void markImmutable(ClusterI cluster, boolean value) {