]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Undo did not wait for pending cluster updates to finish. 79/279/3
authorAntti Villberg <antti.villberg@semantum.fi>
Fri, 20 Jan 2017 07:23:40 +0000 (09:23 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 20 Jan 2017 10:51:54 +0000 (12:51 +0200)
refs #6918

Change-Id: I60e8858fa09d69f8793cd5a9c1a3cb20b6682362

bundles/org.simantics.acorn/src/org/simantics/acorn/GraphClientImpl2.java
bundles/org.simantics.acorn/src/org/simantics/acorn/MainProgram.java

index 982c743fdeadb9ebe5d9939e5512e9e6bc638af9..904427b6d617f6682c78547db1dde17a419dff48 100644 (file)
@@ -21,6 +21,7 @@ import java.util.concurrent.Semaphore;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 
+import org.simantics.acorn.MainProgram.MainProgramRunnable;
 import org.simantics.acorn.exception.AcornAccessVerificationException;
 import org.simantics.acorn.exception.IllegalAcornStateException;
 import org.simantics.acorn.internal.ClusterChange;
@@ -604,90 +605,111 @@ public class GraphClientImpl2 implements Database.Session {
                        clusters.clusterLRU.releaseMutex();
                }
        }
-       
+
        @Override
        public boolean undo(long[] changeSetIds, OnChangeSetUpdate onChangeSetUpdate) throws SDBException {
-           try {
-               final ArrayList<Pair<ClusterUID, byte[]>> clusterChanges = new ArrayList<Pair<ClusterUID, byte[]>>();
-               
-               UndoClusterSupport support = new UndoClusterSupport(clusters);
-               
-               final int changeSetId = clusters.state.headChangeSetId;
-               
-               if(ClusterUpdateProcessorBase.DEBUG)
-                       System.err.println(" === BEGIN UNDO ===");
-               
-               for(int i=0;i<changeSetIds.length;i++) {
-                       final long id = changeSetIds[changeSetIds.length-1-i];
-                       ArrayList<String> ccss = clusters.getChanges(id);
-    
-                       for(int j=0;j<ccss.size();j++) {
-                               String ccsid = ccss.get(ccss.size()-j-1);
-                               try {
-                                       if(ClusterUpdateProcessorBase.DEBUG)
-                                               System.err.println("performUndo " + ccsid);
-                                       performUndo(ccsid, clusterChanges, support);
-                               } catch (DatabaseException e) {
-                                       e.printStackTrace();
-                               }
-                       }
-               }
-    
-               if(ClusterUpdateProcessorBase.DEBUG)
-                       System.err.println(" === END UNDO ===");
-    
-               for(int i=0;i<clusterChanges.size();i++) {
-                       
-                       final int changeSetIndex = i;
-                       
-                       final Pair<ClusterUID, byte[]> pair = clusterChanges.get(i);
-                       
-                       final ClusterUID cuid = pair.first;
-                       final byte[] data = pair.second;
-    
-                       onChangeSetUpdate.onChangeSetUpdate(new ChangeSetUpdate() {
-    
-                               @Override
-                               public long getChangeSetId() {
-                                       return changeSetId;
-                               }
-    
-                               @Override
-                               public int getChangeSetIndex() {
-                                       return 0;
-                               }
-    
-                               @Override
-                               public int getNumberOfClusterChangeSets() {
-                                       return clusterChanges.size();
-                               }
-    
-                               @Override
-                               public int getIndexOfClusterChangeSet() {
-                                       return changeSetIndex;
-                               }
-    
-                               @Override
-                               public byte[] getClusterId() {
-                                       return cuid.asBytes();
-                               }
-    
-                               @Override
-                               public boolean getNewCluster() {
-                                       return false;
-                               }
-    
-                               @Override
-                               public byte[] getData() {
-                                       return data;
-                               }
-    
-                       });
-               }
-        } catch (AcornAccessVerificationException | IllegalAcornStateException e1) {
-            throw new ProCoreException(e1);
-        }
+
+               Exception exception = mainProgram.runIdle(new MainProgramRunnable() {
+
+                       @Override
+                       public void run() throws Exception {
+
+                               try {
+
+                               final ArrayList<Pair<ClusterUID, byte[]>> clusterChanges = new ArrayList<Pair<ClusterUID, byte[]>>();
+                               
+                               UndoClusterSupport support = new UndoClusterSupport(clusters);
+                               
+                               final int changeSetId = clusters.state.headChangeSetId;
+                               
+                               if(ClusterUpdateProcessorBase.DEBUG)
+                                       System.err.println(" === BEGIN UNDO ===");
+                               
+                               for(int i=0;i<changeSetIds.length;i++) {
+                                       final long id = changeSetIds[changeSetIds.length-1-i];
+                                       ArrayList<String> ccss = clusters.getChanges(id);
+                   
+                                       for(int j=0;j<ccss.size();j++) {
+                                               String ccsid = ccss.get(ccss.size()-j-1);
+                                               try {
+                                                       if(ClusterUpdateProcessorBase.DEBUG)
+                                                               System.err.println("performUndo " + ccsid);
+                                                       performUndo(ccsid, clusterChanges, support);
+                                               } catch (DatabaseException e) {
+                                                       e.printStackTrace();
+                                               }
+                                       }
+                               }
+                   
+                               if(ClusterUpdateProcessorBase.DEBUG)
+                                       System.err.println(" === END UNDO ===");
+                   
+                               for(int i=0;i<clusterChanges.size();i++) {
+                                       
+                                       final int changeSetIndex = i;
+                                       
+                                       final Pair<ClusterUID, byte[]> pair = clusterChanges.get(i);
+                                       
+                                       final ClusterUID cuid = pair.first;
+                                       final byte[] data = pair.second;
+                   
+                                       onChangeSetUpdate.onChangeSetUpdate(new ChangeSetUpdate() {
+                   
+                                               @Override
+                                               public long getChangeSetId() {
+                                                       return changeSetId;
+                                               }
+                   
+                                               @Override
+                                               public int getChangeSetIndex() {
+                                                       return 0;
+                                               }
+                   
+                                               @Override
+                                               public int getNumberOfClusterChangeSets() {
+                                                       return clusterChanges.size();
+                                               }
+                   
+                                               @Override
+                                               public int getIndexOfClusterChangeSet() {
+                                                       return changeSetIndex;
+                                               }
+                   
+                                               @Override
+                                               public byte[] getClusterId() {
+                                                       return cuid.asBytes();
+                                               }
+                   
+                                               @Override
+                                               public boolean getNewCluster() {
+                                                       return false;
+                                               }
+                   
+                                               @Override
+                                               public byte[] getData() {
+                                                       return data;
+                                               }
+                   
+                                       });
+                               }
+                       } catch (AcornAccessVerificationException | IllegalAcornStateException e1) {
+                           throw new ProCoreException(e1);
+                       }
+
+                       }
+
+                       @Override
+                       public void done() {
+
+                       }
+
+               });
+
+               if(exception instanceof SDBException) throw (SDBException)exception;
+               else if(exception != null) throw new IllegalAcornStateException(exception);
+               
                return false;
+               
        }
        
        public ServiceLocator getServiceLocator() {
index 78ff9e8995c4162fc347edc9b51bd6f7c69ec4ff..450ad0618fe84445d68a39427a9095f464d8c81a 100644 (file)
@@ -250,7 +250,27 @@ public class MainProgram implements Runnable, Closeable {
                        deathBarrier.release();
                }
        }
+       
+       static interface MainProgramRunnable {
+               
+               public void run() throws Exception;
+               public void done();
+               
+       }
 
+       public Exception runIdle(MainProgramRunnable runnable) {
+               try {
+                       mutex.acquire();
+                       runnable.run();
+                       return null;
+               } catch (Exception e) {
+                       return e;
+               } finally {
+                       runnable.done();
+                       mutex.release();
+               }
+       }
+       
        /*
         * Mutex for streamLRU is assumed here
         *