]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java
Multiple readers in db client
[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.AsyncReadGraph;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
13 import org.simantics.db.common.request.AsyncReadRequest;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.request.VariableRead;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.db.procedure.AsyncProcedure;
18 import org.simantics.document.server.JSONObject;
19
20 public class DocumentRequest extends VariableRead<List<JSONObject>> {
21         
22         public static boolean PROFILE = false;
23         // Thresholds in microseconds
24         public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
25         public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
26
27     public DocumentRequest(Variable var) {
28         super(var);
29         }
30
31         @Override
32         public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
33                 
34                 long s = System.nanoTime();
35                 
36         Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
37         HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
38         if(nodes.isEmpty()) {
39             return Collections.emptyList();
40         }
41
42         if(PROFILE) {
43             long dura = System.nanoTime()-s;
44             System.err.println("DocumentRequest1 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
45         }
46
47         graph.syncRequest(new AsyncReadRequest() {
48
49             @Override
50             public void run(AsyncReadGraph graph) throws DatabaseException {
51
52                 for(Variable node : nodes) {
53                     graph.asyncRequest(new NodeRequest(node), new AsyncProcedure<JSONObject> () {
54
55                         @Override
56                         public void execute(AsyncReadGraph graph, JSONObject result) {
57                             synchronized (rs) {
58                                 rs.add(result);
59                             }
60                         }
61
62                         @Override
63                         public void exception(AsyncReadGraph graph, Throwable throwable) {
64                         }
65
66                     });
67                     
68                 }
69                 
70             }
71             
72         });
73
74
75         if(PROFILE) {
76             long dura = System.nanoTime()-s;
77             System.err.println("DocumentRequest2 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
78         }
79
80                 ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
81                 Collections.sort(result, new Comparator<JSONObject>() {
82
83                         @Override
84                         public int compare(JSONObject o1, JSONObject o2) {
85                                 return o1.id.compareTo(o2.id);
86                         }
87                         
88                 });
89         
90         if(PROFILE) {
91                 long dura = System.nanoTime()-s;
92                 System.err.println("DocumentRequest3 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
93         }
94
95                 return result;
96
97         }
98 }