]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java
Yet another fixing commit
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / DocumentRequest.java
index f9c425248c891675dc153deca746133ee5f386f5..2f4f7eec62c3f887a70d7b8871cea1a347c3ad52 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.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.AsyncReadRequest;
+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.Listener;
+import org.simantics.document.server.JSONObject;
+import org.simantics.threadlog.Task;
+import org.simantics.threadlog.ThreadLog;
+
+public class DocumentRequest extends VariableRead<List<JSONObject>> {
+       
+       public static boolean PROFILE = true;
+       // 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 NodeRequestE extends NodeRequest {
+
+       static int count1 = 0;
+       static int count = 0;
+       
+               public NodeRequestE(Variable node) {
+                       super(node);
+                       count1++;
+                       System.err.println("create NodeRequest count = " + count1);
+                       if(count1 == 600)
+                               System.err.println("asd");
+               }
+               
+               @Override
+               public JSONObject perform(ReadGraph graph) throws DatabaseException {
+                       count++;
+                       System.err.println("perform NodeRequest count = " + count);
+                       
+                       return super.perform(graph);
+               }
+       
+    }
+    
+       @Override
+       public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
+       
+               Task task = ThreadLog.BEGIN("DocumentRequest " + variable.getURI(graph));
+               
+               try {
+               
+                       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));
+               }*/
+
+               graph.syncRequest(new AsyncReadRequest() {
+                               
+                               @Override
+                               public void run(AsyncReadGraph graph) {
+                                       
+                               for(Variable node : nodes) {
+                                       
+                                       graph.asyncRequest(new NodeRequestE(node), new Listener<JSONObject>() {
+
+                                                       @Override
+                                                       public void execute(JSONObject result) {
+                                                               synchronized(rs) {
+                                                                       rs.add(result);
+                                                               }
+                                                       }
+
+                                                       @Override
+                                                       public void exception(Throwable t) {
+                                                               t.printStackTrace();
+                                                       }
+
+                                                       @Override
+                                                       public boolean isDisposed() {
+                                                               return true;
+                                                       }
+                                                       
+                                               });
+                                       
+                               }
+                                       
+                               }
+                               
+                       });
+       
+                       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("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
+               }
+       
+                       return result;
+                       
+               } finally {
+                       
+                       task.end();
+
+               }
+
+       }
 }
\ No newline at end of file