]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodesRequest.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 / NodesRequest.java
index f3f1177fc4d1f613950bc7b4f7d56e2ec010105f..17666df6fbcd87e5c9b49a57d0f8256b7041c307 100644 (file)
@@ -1,12 +1,12 @@
 package org.simantics.document.server.request;
 
-import gnu.trove.set.hash.THashSet;
-
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Set;
 
 import org.simantics.db.ReadGraph;
+import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.request.VariableChildren;
 import org.simantics.db.layer0.request.VariableRead;
@@ -17,31 +17,46 @@ public class NodesRequest extends VariableRead<Set<Variable>> {
 
     public NodesRequest(Variable var) {
         super(var);
-       }
-
-       @Override
-       public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
-
-               long s = System.nanoTime();
-               
-               StructuralResource2.getInstance(graph);
-               if(variable == null)
-                       return Collections.emptySet();
-               
-               Set<Variable> nodes = new THashSet<Variable>();
-               Collection<Variable> children = graph.syncRequest(new VariableChildren(variable));
-               for(Variable child : children) {
-                       Set<Variable> childNodes = graph.syncRequest(new NodesRequest2(child));
-                       nodes.addAll(childNodes);
-               }
-               
+    }
+
+    static class CollectNodesRequest2 extends UnaryRead<Collection<Variable>, Set<Variable>> {
+
+        public CollectNodesRequest2(Collection<Variable> nodes) {
+            super(nodes);
+        }
+
+        @Override
+        public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
+            HashSet<Variable> rs = new HashSet<>(); // result
+            for(Variable child : parameter) {
+                Set<Variable> childNodes = graph.syncRequest(new NodesRequest2(child));
+                rs.addAll(childNodes);
+            }
+            return rs;
+        }
+
+    }
+
+    @Override
+    public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
+
+        long s = System.nanoTime();
+
+        StructuralResource2.getInstance(graph);
+        if(variable == null)
+            return Collections.emptySet();
+
+        Collection<Variable> children = graph.syncRequest(new VariableChildren(variable));
+
+        Set<Variable> nodes = graph.syncRequest(new CollectNodesRequest2(children));
+
         if(DocumentRequest.PROFILE) {
-               long dura = System.nanoTime()-s;
-               System.err.println("NodesRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
+            long dura = System.nanoTime()-s;
+            System.err.println("NodesRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
         }
-               
-               return nodes;
 
-       }
+        return nodes;
+
+    }
 
 }
\ No newline at end of file