]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
index 9a53060bef2c0b246a21e465da1f0e53756d2f17..87b244c88ba28e1c43129cdf098a43e2f7781201 100644 (file)
@@ -6,17 +6,22 @@ import java.util.Comparator;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
 
 import org.simantics.db.ReadGraph;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.request.VariableRead;
 import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.procedure.Listener;
 import org.simantics.document.server.JSONObject;
+import org.simantics.threadlog.Task;
+import org.simantics.threadlog.ThreadLog;
 
 public class DocumentRequest extends VariableRead<List<JSONObject>> {
        
-       public static boolean PROFILE = false;
+       public static boolean PROFILE = true;
        // Thresholds in microseconds
        public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
        public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
@@ -25,48 +30,119 @@ public class DocumentRequest extends VariableRead<List<JSONObject>> {
         super(var);
        }
 
+    static class NodeRequestE extends NodeRequest {
+
+       static int count1 = 0;
+       static int count = 0;
+       
+               public NodeRequestE(Variable node) {
+                       super(node);
+                       count1++;
+                       System.err.println("create NodeRequest count = " + count1);
+                       if(count1 == 600)
+                               System.err.println("asd");
+               }
+               
+               @Override
+               public JSONObject perform(ReadGraph graph) throws DatabaseException {
+                       count++;
+                       System.err.println("perform NodeRequest count = " + count);
+                       
+                       return super.perform(graph);
+               }
+       
+    }
+    
        @Override
        public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
+       
+               Task task = ThreadLog.BEGIN("DocumentRequest " + variable.getURI(graph));
                
-               long s = System.nanoTime();
+               try {
                
-        Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
-        HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
-        if(nodes.isEmpty()) {
-            return Collections.emptyList();
-        }
-        
-        
-        /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
-        
-        for (Variable node : nodes) {
-               nodeMap.put(node.getURI(graph), node);
-        }
-        System.out.println("*************************************************************************");
-        for (Variable node : nodeMap.values()) {
-               System.out.println("               " + node.getURI(graph));
-        }*/
-        
-        for(Variable node : nodes) {
-            rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));
-        }
+                       long s = System.nanoTime();
+                       
+               Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
+               HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
+               if(nodes.isEmpty()) {
+                   return Collections.emptyList();
+               }
+               
+               
+               /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
+               
+               for (Variable node : nodes) {
+                       nodeMap.put(node.getURI(graph), node);
+               }
+               System.out.println("*************************************************************************");
+               for (Variable node : nodeMap.values()) {
+                       System.out.println("               " + node.getURI(graph));
+               }*/
+
+               Semaphore done = new Semaphore(0);
+               
+               for(Variable node : nodes) {
+                       
+                       graph.asyncRequest(new NodeRequestE(node), new Listener<JSONObject>() {
 
-               ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
-               Collections.sort(result, new Comparator<JSONObject>() {
+                                       @Override
+                                       public void execute(JSONObject result) {
+                                               synchronized(rs) {
+                                                       rs.add(result);
+                                               }
+                                               done.release();
+                                       }
 
-                       @Override
-                       public int compare(JSONObject o1, JSONObject o2) {
-                               return o1.id.compareTo(o2.id);
+                                       @Override
+                                       public void exception(Throwable t) {
+                                               t.printStackTrace();
+                                               done.release();
+                                       }
+
+                                       @Override
+                                       public boolean isDisposed() {
+                                               return true;
+                                       }
+                                       
+                               });
+                       
+//                 rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));
+                       
+               }
+               
+               try {
+                       boolean success = done.tryAcquire(nodes.size(), 10, TimeUnit.MILLISECONDS);
+                       while(!success) {
+                               success = done.tryAcquire(nodes.size(), 10, TimeUnit.MILLISECONDS);
+                               System.err.println("still trying to acquire semaphore, avail = " + done.availablePermits() );
+                       }
+                               
+                       } catch (InterruptedException e) {
+                               e.printStackTrace();
                        }
+       
+                       ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
+                       Collections.sort(result, new Comparator<JSONObject>() {
+       
+                               @Override
+                               public int compare(JSONObject o1, JSONObject o2) {
+                                       return o1.id.compareTo(o2.id);
+                               }
+                               
+                       });
+               
+               if(PROFILE) {
+                       long dura = System.nanoTime()-s;
+                       System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
+               }
+       
+                       return result;
+                       
+               } finally {
                        
-               });
-        
-        if(PROFILE) {
-               long dura = System.nanoTime()-s;
-               System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
-        }
+                       task.end();
 
-               return result;
+               }
 
        }
 }
\ No newline at end of file