Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / NodesRequest.java
1 package org.simantics.document.server.request;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.Set;
7
8 import org.simantics.db.AsyncReadGraph;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.common.request.UnaryAsyncRead;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.request.VariableChildren;
13 import org.simantics.db.layer0.request.VariableRead;
14 import org.simantics.db.layer0.variable.Variable;
15 import org.simantics.db.procedure.AsyncProcedure;
16 import org.simantics.structural.stubs.StructuralResource2;
17 import org.simantics.utils.threads.logger.ITask;
18 import org.simantics.utils.threads.logger.ThreadLogger;
19
20 public class NodesRequest extends VariableRead<Set<Variable>> {
21
22     public NodesRequest(Variable var) {
23         super(var);
24     }
25
26     static class CollectNodesRequest2 extends UnaryAsyncRead<Collection<Variable>, Set<Variable>> {
27
28         public CollectNodesRequest2(Collection<Variable> nodes) {
29             super(nodes);
30         }
31
32         @Override
33         public void perform(AsyncReadGraph graph, AsyncProcedure<Set<Variable>> procedure) {
34             HashSet<Variable> rs = new HashSet<Variable>(); // result
35
36             for(Variable node : parameter) {
37                 graph.asyncRequest(new NodesRequest2(node), new AsyncProcedure<Set<Variable>> () {
38
39                     @Override
40                     public void execute(AsyncReadGraph graph, Set<Variable> result) {
41                         synchronized(rs) {
42                             rs.addAll(result);
43                         }
44                     }
45
46                     @Override
47                     public void exception(AsyncReadGraph graph, Throwable throwable) {
48                     }
49
50                 });
51
52             }
53             procedure.execute(graph, rs);
54
55         }
56
57     }
58
59     @Override
60     public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
61
62         ITask task = DocumentRequest.PROFILE ? ThreadLogger.task(this) : null;
63
64         StructuralResource2.getInstance(graph);
65         if(variable == null)
66             return Collections.emptySet();
67
68         Collection<Variable> children = graph.syncRequest(new VariableChildren(variable));
69
70         Set<Variable> nodes = graph.syncRequest(new CollectNodesRequest2(children));
71
72         if(DocumentRequest.PROFILE) task.finish();
73
74         return nodes;
75
76     }
77
78 }