Merge "(refs #7316) Improved error locations for invalid field access"
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / DocumentRequest.java
1 package org.simantics.document.server.request;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Set;
9
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.request.VariableRead;
14 import org.simantics.db.layer0.variable.Variable;
15 import org.simantics.document.server.JSONObject;
16
17 public class DocumentRequest extends VariableRead<List<JSONObject>> {
18         
19         public static boolean PROFILE = false;
20         // Thresholds in microseconds
21         public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
22         public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
23
24     public DocumentRequest(Variable var) {
25         super(var);
26         }
27
28         @Override
29         public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
30                 
31                 long s = System.nanoTime();
32                 
33         Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
34         HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
35         if(nodes.isEmpty()) {
36             return Collections.emptyList();
37         }
38         
39         
40         /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
41         
42         for (Variable node : nodes) {
43                 nodeMap.put(node.getURI(graph), node);
44         }
45         System.out.println("*************************************************************************");
46         for (Variable node : nodeMap.values()) {
47                 System.out.println("               " + node.getURI(graph));
48         }*/
49         
50         for(Variable node : nodes) {
51             rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));
52         }
53
54                 ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
55                 Collections.sort(result, new Comparator<JSONObject>() {
56
57                         @Override
58                         public int compare(JSONObject o1, JSONObject o2) {
59                                 return o1.id.compareTo(o2.id);
60                         }
61                         
62                 });
63         
64         if(PROFILE) {
65                 long dura = System.nanoTime()-s;
66                 System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
67         }
68
69                 return result;
70
71         }
72 }