]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java
Generate parts of db client query code
[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 import java.util.concurrent.Semaphore;
10 import java.util.concurrent.TimeUnit;
11
12 import org.simantics.db.AsyncReadGraph;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.common.GraphSemaphore;
15 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
16 import org.simantics.db.common.request.AsyncReadRequest;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.request.VariableRead;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.procedure.Listener;
21 import org.simantics.document.server.JSONObject;
22 import org.simantics.threadlog.Task;
23 import org.simantics.threadlog.ThreadLog;
24
25 public class DocumentRequest extends VariableRead<List<JSONObject>> {
26         
27         public static boolean PROFILE = true;
28         // Thresholds in microseconds
29         public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
30         public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
31
32     public DocumentRequest(Variable var) {
33         super(var);
34         }
35
36     static class NodeRequestE extends NodeRequest {
37
38         static int count1 = 0;
39         static int count = 0;
40         
41                 public NodeRequestE(Variable node) {
42                         super(node);
43                         count1++;
44                         System.err.println("create NodeRequest count = " + count1);
45                         if(count1 == 600)
46                                 System.err.println("asd");
47                 }
48                 
49                 @Override
50                 public JSONObject perform(ReadGraph graph) throws DatabaseException {
51                         count++;
52                         System.err.println("perform NodeRequest count = " + count);
53                         
54                         return super.perform(graph);
55                 }
56         
57     }
58     
59         @Override
60         public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
61         
62                 Task task = ThreadLog.BEGIN("DocumentRequest " + variable.getURI(graph));
63                 
64                 try {
65                 
66                         long s = System.nanoTime();
67                         
68                 Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
69                 HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
70                 if(nodes.isEmpty()) {
71                     return Collections.emptyList();
72                 }
73                 
74                 
75                 /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
76                 
77                 for (Variable node : nodes) {
78                         nodeMap.put(node.getURI(graph), node);
79                 }
80                 System.out.println("*************************************************************************");
81                 for (Variable node : nodeMap.values()) {
82                         System.out.println("               " + node.getURI(graph));
83                 }*/
84
85 <<<<<<< Upstream, based on branch 'private/antti_threads' of ssh://villberg@gerrit.simantics.org:29418/simantics/platform.git
86                 GraphSemaphore done = new GraphSemaphore(graph, 0);
87                 
88                 for(Variable node : nodes) {
89                         
90                         graph.asyncRequest(new NodeRequestE(node), new Listener<JSONObject>() {
91
92                                         @Override
93                                         public void execute(JSONObject result) {
94                                                 synchronized(rs) {
95                                                         rs.add(result);
96                                                 }
97                                                 done.release();
98                                         }
99
100                                         @Override
101                                         public void exception(Throwable t) {
102                                                 t.printStackTrace();
103                                                 done.release();
104                                         }
105
106                                         @Override
107                                         public boolean isDisposed() {
108                                                 return true;
109                                         }
110                                         
111                                 });
112                         
113 //                  rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));
114                         
115                 }
116                 
117                 try {
118                         done.waitFor(nodes.size());
119                         } catch (InterruptedException e) {
120                                 e.printStackTrace();
121                         }
122 =======
123                 graph.syncRequest(new AsyncReadRequest() {
124                                 
125                                 @Override
126                                 public void run(AsyncReadGraph graph) {
127                                         
128                                 for(Variable node : nodes) {
129                                         
130                                         graph.asyncRequest(new NodeRequestE(node), new Listener<JSONObject>() {
131
132                                                         @Override
133                                                         public void execute(JSONObject result) {
134                                                                 synchronized(rs) {
135                                                                         rs.add(result);
136                                                                 }
137                                                         }
138
139                                                         @Override
140                                                         public void exception(Throwable t) {
141                                                                 t.printStackTrace();
142                                                         }
143
144                                                         @Override
145                                                         public boolean isDisposed() {
146                                                                 return true;
147                                                         }
148                                                         
149                                                 });
150                                         
151                                 }
152                                         
153                                 }
154                                 
155                         });
156 >>>>>>> 82fa68e Generate parts of db client query code
157         
158                         ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
159                         Collections.sort(result, new Comparator<JSONObject>() {
160         
161                                 @Override
162                                 public int compare(JSONObject o1, JSONObject o2) {
163                                         return o1.id.compareTo(o2.id);
164                                 }
165                                 
166                         });
167                 
168                 if(PROFILE) {
169                         long dura = System.nanoTime()-s;
170                         System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
171                 }
172         
173                         return result;
174                         
175                 } finally {
176                         
177                         task.end();
178
179                 }
180
181         }
182 }