]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java
Yet another fixing commit
[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.Listener;
18 import org.simantics.document.server.JSONObject;
19 import org.simantics.threadlog.Task;
20 import org.simantics.threadlog.ThreadLog;
21
22 public class DocumentRequest extends VariableRead<List<JSONObject>> {
23         
24         public static boolean PROFILE = true;
25         // Thresholds in microseconds
26         public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
27         public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
28
29     public DocumentRequest(Variable var) {
30         super(var);
31         }
32
33     static class NodeRequestE extends NodeRequest {
34
35         static int count1 = 0;
36         static int count = 0;
37         
38                 public NodeRequestE(Variable node) {
39                         super(node);
40                         count1++;
41                         System.err.println("create NodeRequest count = " + count1);
42                         if(count1 == 600)
43                                 System.err.println("asd");
44                 }
45                 
46                 @Override
47                 public JSONObject perform(ReadGraph graph) throws DatabaseException {
48                         count++;
49                         System.err.println("perform NodeRequest count = " + count);
50                         
51                         return super.perform(graph);
52                 }
53         
54     }
55     
56         @Override
57         public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
58         
59                 Task task = ThreadLog.BEGIN("DocumentRequest " + variable.getURI(graph));
60                 
61                 try {
62                 
63                         long s = System.nanoTime();
64                         
65                 Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
66                 HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
67                 if(nodes.isEmpty()) {
68                     return Collections.emptyList();
69                 }
70                 
71                 
72                 /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
73                 
74                 for (Variable node : nodes) {
75                         nodeMap.put(node.getURI(graph), node);
76                 }
77                 System.out.println("*************************************************************************");
78                 for (Variable node : nodeMap.values()) {
79                         System.out.println("               " + node.getURI(graph));
80                 }*/
81
82                 graph.syncRequest(new AsyncReadRequest() {
83                                 
84                                 @Override
85                                 public void run(AsyncReadGraph graph) {
86                                         
87                                 for(Variable node : nodes) {
88                                         
89                                         graph.asyncRequest(new NodeRequestE(node), new Listener<JSONObject>() {
90
91                                                         @Override
92                                                         public void execute(JSONObject result) {
93                                                                 synchronized(rs) {
94                                                                         rs.add(result);
95                                                                 }
96                                                         }
97
98                                                         @Override
99                                                         public void exception(Throwable t) {
100                                                                 t.printStackTrace();
101                                                         }
102
103                                                         @Override
104                                                         public boolean isDisposed() {
105                                                                 return true;
106                                                         }
107                                                         
108                                                 });
109                                         
110                                 }
111                                         
112                                 }
113                                 
114                         });
115         
116                         ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
117                         Collections.sort(result, new Comparator<JSONObject>() {
118         
119                                 @Override
120                                 public int compare(JSONObject o1, JSONObject o2) {
121                                         return o1.id.compareTo(o2.id);
122                                 }
123                                 
124                         });
125                 
126                 if(PROFILE) {
127                         long dura = System.nanoTime()-s;
128                         System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
129                 }
130         
131                         return result;
132                         
133                 } finally {
134                         
135                         task.end();
136
137                 }
138
139         }
140 }