Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / DocumentRequest.java
index f9c425248c891675dc153deca746133ee5f386f5..5b76cd344043f10dd05428642bee688c418ed12f 100644 (file)
-package org.simantics.document.server.request;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.Comparator;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Set;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.request.VariableRead;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.document.server.JSONObject;\r
-\r
-public class DocumentRequest extends VariableRead<List<JSONObject>> {\r
-       \r
-       public static boolean PROFILE = false;\r
-\r
-    public DocumentRequest(Variable var) {\r
-        super(var);\r
-       }\r
-\r
-       @Override\r
-       public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {\r
-               \r
-               long s = System.nanoTime();\r
-               \r
-        Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());\r
-        HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result\r
-        if(nodes.isEmpty()) {\r
-            return Collections.emptyList();\r
-        }\r
-        \r
-        \r
-        /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();\r
-        \r
-        for (Variable node : nodes) {\r
-               nodeMap.put(node.getURI(graph), node);\r
-        }\r
-        System.out.println("*************************************************************************");\r
-        for (Variable node : nodeMap.values()) {\r
-               System.out.println("               " + node.getURI(graph));\r
-        }*/\r
-        \r
-        for(Variable node : nodes) {\r
-            rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));\r
-        }\r
-\r
-               ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);\r
-               Collections.sort(result, new Comparator<JSONObject>() {\r
-\r
-                       @Override\r
-                       public int compare(JSONObject o1, JSONObject o2) {\r
-                               return o1.id.compareTo(o2.id);\r
-                       }\r
-                       \r
-               });\r
-        \r
-        if(PROFILE) {\r
-               long dura = System.nanoTime()-s;\r
-               System.err.println("DocumentRequest " + System.identityHashCode(this) + " " + variable.getURI(graph) + " in " + 1e-6*dura + "ms.");\r
-        }\r
-\r
-               return result;\r
-\r
-       }\r
+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;
+import java.util.List;
+import java.util.Set;
+
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.common.request.UnaryAsyncRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.VariableRead;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.procedure.AsyncProcedure;
+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 DocumentRequest(Variable var) {
+        super(var);
+       }
+
+    static class CollectNodesRequest extends UnaryAsyncRead<Collection<Variable>, Collection<JSONObject>> {
+
+        public CollectNodesRequest(Collection<Variable> nodes) {
+            super(nodes);
+        }
+
+        @Override
+        public void perform(AsyncReadGraph graph, AsyncProcedure<Collection<JSONObject>> procedure) {
+            HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
+
+            for(Variable node : parameter) {
+                graph.asyncRequest(new NodeRequest(node), new AsyncProcedure<JSONObject> () {
+
+                    @Override
+                    public void execute(AsyncReadGraph graph, JSONObject result) {
+                        synchronized(rs) {
+                            rs.add(result);
+                        }
+                    }
+
+                    @Override
+                    public void exception(AsyncReadGraph graph, Throwable throwable) {
+                    }
+
+                });
+
+            }
+            procedure.execute(graph, rs);
+
+        }
+
+    }
+
+    @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());
+        if(nodes.isEmpty()) {
+            return Collections.emptyList();
+        }
+
+        if(PROFILE) {
+            long dura = System.nanoTime()-s;
+            System.err.println("DocumentRequest1 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
+        }
+
+        Collection<JSONObject> rs = graph.syncRequest(new CollectNodesRequest(nodes));
+
+        if(PROFILE) {
+            long dura = System.nanoTime()-s;
+            System.err.println("DocumentRequest2 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
+        }
+
+               ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
+               Collections.sort(result, new Comparator<JSONObject>() {
+
+                       @Override
+                       public int compare(JSONObject o1, JSONObject o2) {
+                               return o1.id.compareTo(o2.id);
+                       }
+                       
+               });
+        
+        if(PROFILE) {
+               long dura = System.nanoTime()-s;
+               System.err.println("DocumentRequest3 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
+        }
+
+               return result;
+
+       }
 }
\ No newline at end of file