]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodesRequest.java
Multiple readers in db client
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / NodesRequest.java
index 11bf514c3440793b620ec0022c3c6201f4ff01ec..99526368e8cc3e21e1b92451f4dd915db45d40ba 100644 (file)
@@ -1,47 +1,72 @@
-package org.simantics.document.server.request;\r
-\r
-import gnu.trove.set.hash.THashSet;\r
-\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.Set;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.request.VariableChildren;\r
-import org.simantics.db.layer0.request.VariableRead;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-\r
-public class NodesRequest extends VariableRead<Set<Variable>> {\r
-\r
-    public NodesRequest(Variable var) {\r
-        super(var);\r
-       }\r
-\r
-       @Override\r
-       public Set<Variable> perform(ReadGraph graph) throws DatabaseException {\r
-           \r
-               long s = System.nanoTime();\r
-               \r
-               StructuralResource2.getInstance(graph);\r
-               if(variable == null)\r
-                       return Collections.emptySet();\r
-\r
-               Set<Variable> nodes = new THashSet<Variable>();\r
-               Collection<Variable> children = graph.syncRequest(new VariableChildren(variable));\r
-               for(Variable child : children) {\r
-                       Set<Variable> childNodes = graph.syncRequest(new NodesRequest2(child));\r
-                       nodes.addAll(childNodes);\r
-               }\r
-               \r
-        if(DocumentRequest.PROFILE) {\r
-               long dura = System.nanoTime()-s;\r
-               System.err.println("NodesRequest " + System.identityHashCode(this) + " " + variable.getURI(graph) + " in " + 1e-6*dura + "ms.");\r
-        }\r
-               \r
-               return nodes;\r
-\r
-       }\r
-\r
+package org.simantics.document.server.request;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Set;
+
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.common.request.AsyncReadRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.VariableChildren;
+import org.simantics.db.layer0.request.VariableRead;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.utils.threads.logger.ITask;
+import org.simantics.utils.threads.logger.ThreadLogger;
+
+import gnu.trove.set.hash.THashSet;
+
+public class NodesRequest extends VariableRead<Set<Variable>> {
+
+    public NodesRequest(Variable var) {
+        super(var);
+    }
+
+    @Override
+    public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
+
+        ITask task = DocumentRequest.PROFILE ? ThreadLogger.task(this) : null;
+
+        StructuralResource2.getInstance(graph);
+        if(variable == null)
+            return Collections.emptySet();
+
+        Set<Variable> nodes = new THashSet<Variable>();
+
+        Collection<Variable> children = graph.syncRequest(new VariableChildren(variable));
+
+        graph.syncRequest(new AsyncReadRequest() {
+
+            @Override
+            public void run(AsyncReadGraph graph) throws DatabaseException {
+
+                for(Variable child : children) {
+                    graph.asyncRequest(new NodesRequest2(child), new AsyncProcedure<Set<Variable>>() {
+
+                        @Override
+                        public void execute(AsyncReadGraph graph, Set<Variable> result) {
+                            synchronized(nodes) {
+                                nodes.addAll(result);
+                            }
+                        }
+
+                        @Override
+                        public void exception(AsyncReadGraph graph, Throwable throwable) {
+                        }
+                        
+                    });
+                }
+
+            }
+
+        });
+
+        if(DocumentRequest.PROFILE) task.finish();
+
+        return nodes;
+
+    }
+
 }
\ No newline at end of file