]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java
Fixed bug in cluster collection logic that caused it to not operate
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ClusterTable.java
index 5daf534fef481ca3e6b3bd43dcf637d82e2bb4ef..a52870a1560301e6e372210d827818785fc217c1 100644 (file)
@@ -289,17 +289,30 @@ public final class ClusterTable implements IClusterTable {
         }
     }
 
-    synchronized void replaceCluster(ClusterI cluster) {
-        //printMaps("replaceCluster");
+    synchronized void replaceCluster(ClusterI cluster_) {
+        ClusterImpl cluster = (ClusterImpl) cluster_;
         checkCollect();
         int clusterKey = cluster.getClusterKey();
-        ClusterI existing = clusterArray[clusterKey];
+        ClusterImpl existing = (ClusterImpl) clusterArray[clusterKey];
         if (existing.hasVirtual())
             cluster.markVirtual();
 
-        long existingImportance = existing.getImportance();
-        if (existingImportance != 0)
-            importanceMap.remove(existingImportance);
+        if (existing.cc != null) {
+            if (existing.isLoaded()) {
+                // This shall be promoted to actual exception in the future -
+                // for now, minimal changes
+                new Exception("Trying to replace cluster with pending changes " + existing.getClusterUID())
+                        .printStackTrace();
+            } else {
+                // Adopt changes to loaded cluster
+                cluster.cc = existing.cc;
+                cluster.cc.adopt(cluster);
+                cluster.foreignLookup = existing.foreignLookup;
+                cluster.change = existing.change;
+            }
+        }
+        
+        importanceMap.remove(existing.getImportance());
         if (collectorPolicy != null)
             collectorPolicy.removed((ClusterImpl)existing);
 
@@ -511,10 +524,11 @@ public final class ClusterTable implements IClusterTable {
                 ArrayList<CollectorCluster> toRelease = new ArrayList<CollectorCluster>();
 
                 for (CollectorCluster cluster : support.getResidentClusters()) {
-                    target -= support.getClusterSize(cluster);
-                    if (target > 0) {
-                        toRelease.add(cluster);
-                    } else {
+                    toRelease.add(cluster);
+                    long clusterSize = support.getClusterSize(cluster);
+                    //System.err.println("release cluster with " + (clusterSize/1024) + " kiB - " + cluster);
+                    target -= clusterSize;
+                    if (target <= 0) {
                         break;
                     }
                 }
@@ -591,7 +605,6 @@ public final class ClusterTable implements IClusterTable {
             public boolean execute(int clusterKey) {
                 ClusterImpl proxy = clusterArray[clusterKey];
                 ClusterUID clusterUID = proxy.getClusterUID();
-                System.err.println("writeOnlyInvalidate " + clusterUID);
                 clusters.freeProxy(proxy);
                 return true;
             }
@@ -1207,8 +1220,8 @@ public final class ClusterTable implements IClusterTable {
         int ims = importanceMap.size();
         int ihms = countImportantClusters();
 
-//        System.out.format("[%s] Validating ClusterTable: byteSize=%d, hashMap=%d/%d, importanceMap=%d%n",
-//                place, sizeInBytes, ihms, clusters.hashMap.size(), ims);
+//        System.out.format("[ClusterTable.%s] Validating: byteSize=%d (%d MB), hashMap=%d/%d, importanceMap=%d%n",
+//                place, sizeInBytes, sizeInBytes / (1024*1024), ihms, clusters.hashMap.size(), ims);
 
         int i = clusterArray.length;
         long size = 0;