package org.simantics.document.server.request; import java.util.ArrayList; import java.util.Collections; 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> { public static boolean PROFILE = true; // Thresholds in microseconds public static int PROFILE_THRESHOLD_NODEREQUEST = 2000; public static int PROFILE_THRESHOLD_VALUEREQUEST = 500; public DocumentRequest(Variable var) { 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 perform(ReadGraph graph) throws DatabaseException { Task task = ThreadLog.BEGIN("DocumentRequest " + variable.getURI(graph)); try { long s = System.nanoTime(); Set nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.>instance()); HashSet rs = new HashSet(); // result if(nodes.isEmpty()) { return Collections.emptyList(); } /*TreeMap nodeMap = new TreeMap(); 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() { @Override public void execute(JSONObject result) { synchronized(rs) { rs.add(result); } done.release(); } @Override public void exception(Throwable t) { t.printStackTrace(); done.release(); } @Override public boolean isDisposed() { return true; } }); // rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.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 result = new ArrayList(rs); Collections.sort(result, new Comparator() { @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 { task.end(); } } }