]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Merge changes I7cd072f0,I499dc390
authorJani Simomaa <jani.simomaa@semantum.fi>
Mon, 30 Jul 2018 05:27:25 +0000 (05:27 +0000)
committerGerrit Code Review <gerrit2@simantics>
Mon, 30 Jul 2018 05:27:25 +0000 (05:27 +0000)
* changes:
  Add progress monitoring for DeleteHandler
  Improve performance of CommandMetadata (de)serialization

bundles/org.simantics.db.common/src/org/simantics/db/common/CommandMetadata.java
bundles/org.simantics.diagram/src/org/simantics/diagram/handler/DeleteHandler.java

index 03c9ce9aadefc50ae4c689297a6ce181b075fd6e..d0c3a62b4af37172c2911f490c2a4df6cbc313ec 100644 (file)
@@ -1,57 +1,86 @@
 package org.simantics.db.common;
 
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 
 import org.simantics.databoard.Bindings;
 import org.simantics.databoard.binding.Binding;
-import org.simantics.databoard.serialization.SerializationException;
 import org.simantics.databoard.serialization.Serializer;
 import org.simantics.db.Metadata;
 import org.simantics.db.Session;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-public class CommandMetadata implements Metadata {
+import gnu.trove.list.array.TByteArrayList;
+
+public final class CommandMetadata implements Metadata {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(CommandMetadata.class);
 
     public static final boolean DEBUG = false;
     public static final String RESET_COMMAND = "// RESET";
-    
+
     private static final Binding BINDING = 
             Bindings.getBindingUnchecked(CommandMetadata.class);
     private static final Serializer SERIALIZER = 
             Bindings.getSerializerUnchecked(BINDING);
-    
+
     public List<Command> commands;
-    
-    public static class Command {
+
+    public static final class Command {
         public long modelId;
         public String command;
-        
-        public Command() {         
+
+        public Command() {
         }
-        
+
         public Command(long modelId, String command) {
             super();
             this.modelId = modelId;
             this.command = command;
         }
     }
-    
+
     public CommandMetadata() {
     }
 
     @Override
     public byte[] serialise(Session session) {
         try {
-            return SERIALIZER.serialize(this);
-        } catch (IOException e) {
-            e.printStackTrace();
-            throw new RuntimeException(e);
+            TByteArrayList commandsSerialized = new TByteArrayList(4096);
+            byte[] bytes = new byte[8];
+
+            int commandsSize = commands.size();
+            Bytes.writeLE(bytes, 0, commandsSize);
+            commandsSerialized.add(bytes, 0, 4);
+
+            for (Command command : commands) {
+                Bytes.writeLE8(bytes, 0, command.modelId);
+                commandsSerialized.add(bytes);
+                byte[] commandBytes = command.command.getBytes(StandardCharsets.UTF_8);
+                Bytes.writeLE(bytes, 0, commandBytes.length);
+                commandsSerialized.add(bytes, 0, 4);
+                commandsSerialized.add(commandBytes);
+            }
+            return commandsSerialized.toArray();
+        } catch (Exception ee) {
+            // Ok, backwards compatibility required
+            // This is rather slow operation hence the new implementation above
+            try {
+                return SERIALIZER.serialize(this);
+            } catch (IOException e) {
+                LOGGER.error("Could not serialize", e);
+                LOGGER.error("Original exception for new serialisation", ee);
+                throw new RuntimeException(e);
+            }
         }
     }
-    
+
     public static CommandMetadata deserialise(Session session, byte[] input) {
         if(input == null) {
             CommandMetadata metadata = new CommandMetadata();
@@ -59,13 +88,36 @@ public class CommandMetadata implements Metadata {
             return metadata;
         }
         try {
-            return (CommandMetadata)SERIALIZER.deserialize(input);
-        } catch (SerializationException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
+            int byteIndex = 0;
+            int commandsSize = Bytes.readLE4(input, byteIndex);
+            byteIndex += 4;
+            
+            List<Command> commands = new ArrayList<>(commandsSize);
+            for (int i = 0; i < commandsSize; i++) {
+                long modelId = Bytes.readLE8(input, byteIndex);
+                byteIndex += 8;
+                int commandsLength = Bytes.readLE4(input, byteIndex);
+                byteIndex += 4;
+                
+                String command = new String(input, byteIndex, commandsLength);
+                byteIndex += commandsLength;
+                Command comm = new Command(modelId, command);
+                commands.add(comm);
+            }
+            CommandMetadata metadata = new CommandMetadata();
+            metadata.commands = commands;
+            return metadata;
+        } catch (Exception ee) {
+            // Ok, backwards compatibility required
+            // This is rather slow operation hence the new implementation above
+            try {
+                return (CommandMetadata)SERIALIZER.deserialize(input);
+            } catch (Exception e) {
+                LOGGER.error("Could not deserialise", e);
+                LOGGER.error("Original exception for new deserialisation", ee);
+            }
+            return null;
         }
-        return null;
     }
 
     public CommandMetadata add(Command command) {
index 7fdd56947ad726f0284c165237b093968b6c2b57..5fa94ebc7a2787d1c9b3a51db9fe508ef04106dc 100644 (file)
@@ -22,6 +22,7 @@ import java.util.Set;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.jface.action.IStatusLineManager;
 import org.eclipse.swt.widgets.Display;
 import org.simantics.DatabaseJob;
@@ -286,6 +287,7 @@ public class DeleteHandler extends AbstractDiagramParticipant {
             }
 
             private void delete(IProgressMonitor monitor) throws DatabaseException {
+                SubMonitor subMonitor = SubMonitor.convert(monitor, 4);
                 Simantics.getSession().syncRequest(new WriteRequest() {
                     Set<Resource> connectionsToRemove = new HashSet<Resource>();
                     Set<Resource> touchedConnections = new HashSet<Resource>();
@@ -296,7 +298,9 @@ public class DeleteHandler extends AbstractDiagramParticipant {
                         graph.markUndoPoint();
 
                         ConnectionUtil cu = new ConnectionUtil(graph);
-
+                        SubMonitor edgeElementMonitor = subMonitor.split(2); // Consume 50 % of the monitor
+                        edgeElementMonitor.setWorkRemaining(edges.size() + elements.size()); // set work remaining to size of edges and elements
+                        edgeElementMonitor.setTaskName("Removing edges");
                         // Remove edges
                         for (IElement edge : edges) {
                             ConnectionEntity ce = edge.getHint(ElementHints.KEY_CONNECTION_ENTITY);
@@ -308,8 +312,9 @@ public class DeleteHandler extends AbstractDiagramParticipant {
                             if (obj instanceof EdgeResource) {
                                 cu.remove((EdgeResource) obj);
                             }
+                            edgeElementMonitor.worked(1);
                         }
-
+                        edgeElementMonitor.setTaskName("Removing elements");
                         // Remove elements
                         for (IElement element : elements) {
                             ConnectionHandler ch = element.getElementClass().getAtMostOneItemOfClass(ConnectionHandler.class);
@@ -343,8 +348,11 @@ public class DeleteHandler extends AbstractDiagramParticipant {
                                     }
                                 }
                             }
+                            edgeElementMonitor.worked(1);
                         }
-
+                        SubMonitor connectionsMonitor = subMonitor.split(1); // Consume 50 % of the monitor
+                        connectionsMonitor.setWorkRemaining(touchedConnections.size()); // set work remaining to size of edges and elements
+                        connectionsMonitor.setTaskName("Pruning connectors");
                         // Check all touched connections to see if they are empty.
                         for (Resource connection : touchedConnections) {
                             int removedConnectors = cu.removeUnusedConnectors(connection);
@@ -363,13 +371,17 @@ public class DeleteHandler extends AbstractDiagramParticipant {
                             if (connectors < 2) {
                                 connectionsToRemove.add(connection);
                             }
+                            connectionsMonitor.worked(1);
                         }
-
+                        SubMonitor connectionsMonitor2 = subMonitor.split(1); // Consume 50 % of the monitor
+                        connectionsMonitor2.setWorkRemaining(connectionsToRemove.size()); // set work remaini
+                        connectionsMonitor2.setTaskName("Removing leftover connections");
                         // Remove selected/left-over empty connections
                         for (Resource connection : connectionsToRemove) {
                             if (DEBUG_DELETE)
                                 System.out.println("REMOVING CONNECTION: " + connection);
                             RemoveElement.removeElement(graph, (Resource)d.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE), connection);
+                            connectionsMonitor2.worked(1);
                         }
                     }