]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java
Manually "cherry-picked" changes from ae2e31aa to release/1.35.2.1
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / DocumentRequest.java
index 9a53060bef2c0b246a21e465da1f0e53756d2f17..be5570f7f5a2d80bfe8b5be65ab1e1951dba8405 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.document.server.request;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashSet;
@@ -9,64 +10,64 @@ import java.util.Set;
 
 import org.simantics.db.ReadGraph;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.request.VariableRead;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.document.server.JSONObject;
 
 public class DocumentRequest extends VariableRead<List<JSONObject>> {
-       
-       public static boolean PROFILE = false;
-       // Thresholds in microseconds
-       public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
-       public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
+
+    public static boolean PROFILE = false;
+    // Thresholds in microseconds
+    public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
+    public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
 
     public DocumentRequest(Variable var) {
         super(var);
-       }
+    }
+
+    static class CollectNodesRequest extends UnaryRead<Collection<Variable>, Collection<JSONObject>> {
+
+        public CollectNodesRequest(Collection<Variable> nodes) {
+            super(nodes);
+        }
+
+        @Override
+        public Collection<JSONObject> perform(ReadGraph graph) throws DatabaseException {
+            HashSet<JSONObject> rs = new HashSet<>(); // result
+            for(Variable node : parameter) {
+                rs.add(graph.syncRequest(new NodeRequest(node)));
+            }
+            return rs;
+        }
+
+    }
+
+    private static final Comparator<JSONObject> JSON_COMPARATOR = (o1, o2) -> o1.id.compareTo(o2.id);
+
+    @Override
+    public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
+
+        long s = System.nanoTime();
 
-       @Override
-       public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
-               
-               long s = System.nanoTime();
-               
         Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
-        HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
         if(nodes.isEmpty()) {
             return Collections.emptyList();
         }
-        
-        
-        /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
-        
-        for (Variable node : nodes) {
-               nodeMap.put(node.getURI(graph), node);
-        }
-        System.out.println("*************************************************************************");
-        for (Variable node : nodeMap.values()) {
-               System.out.println("               " + node.getURI(graph));
-        }*/
-        
-        for(Variable node : nodes) {
-            rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));
-        }
 
-               ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
-               Collections.sort(result, new Comparator<JSONObject>() {
+        Collection<JSONObject> rs = graph.syncRequest(new CollectNodesRequest(nodes));
+
+        ArrayList<JSONObject> result = new ArrayList<>(rs);
+        Collections.sort(result, JSON_COMPARATOR);
 
-                       @Override
-                       public int compare(JSONObject o1, JSONObject o2) {
-                               return o1.id.compareTo(o2.id);
-                       }
-                       
-               });
-        
         if(PROFILE) {
-               long dura = System.nanoTime()-s;
-               System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
+            long dura = System.nanoTime()-s;
+            System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
         }
 
-               return result;
+        return result;
+
+    }
 
-       }
 }
\ No newline at end of file