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