]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java
Still working for multiple readers
[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.procedure.adapter.TransientCacheAsyncListener;
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                 Semaphore done = new Semaphore(0);
83                 
84                 for(Variable node : nodes) {
85                         
86                         graph.asyncRequest(new NodeRequestE(node), new Listener<JSONObject>() {
87
88                                         @Override
89                                         public void execute(JSONObject result) {
90                                                 synchronized(rs) {
91                                                         rs.add(result);
92                                                 }
93                                                 done.release();
94                                         }
95
96                                         @Override
97                                         public void exception(Throwable t) {
98                                                 t.printStackTrace();
99                                                 done.release();
100                                         }
101
102                                         @Override
103                                         public boolean isDisposed() {
104                                                 return true;
105                                         }
106                                         
107                                 });
108                         
109 //                  rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));
110                         
111                 }
112                 
113                 try {
114                         boolean success = done.tryAcquire(nodes.size(), 10, TimeUnit.MILLISECONDS);
115                         while(!success) {
116                                 success = done.tryAcquire(nodes.size(), 10, TimeUnit.MILLISECONDS);
117                                 System.err.println("still trying to acquire semaphore, avail = " + done.availablePermits() );
118                         }
119                                 
120                         } catch (InterruptedException e) {
121                                 e.printStackTrace();
122                         }
123         
124                         ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
125                         Collections.sort(result, new Comparator<JSONObject>() {
126         
127                                 @Override
128                                 public int compare(JSONObject o1, JSONObject o2) {
129                                         return o1.id.compareTo(o2.id);
130                                 }
131                                 
132                         });
133                 
134                 if(PROFILE) {
135                         long dura = System.nanoTime()-s;
136                         System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
137                 }
138         
139                         return result;
140                         
141                 } finally {
142                         
143                         task.end();
144
145                 }
146
147         }
148 }