]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java
Cluster loading problem fix made by Antti
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ClusterTable.java
index 40b2530f2a569a4dedfdb7295095895871074f1d..123f346a4dd0322254aa74bfd1a259b9bd07357c 100644 (file)
@@ -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 extends ClusterI> 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 extends ClusterI> 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 extends ClusterI> 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 extends ClusterI> T checkedGetClusterByResourceKey(final int resourceKey) {