package org.simantics.document.server.request; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; import org.simantics.db.AsyncReadGraph; import org.simantics.db.ReadGraph; import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener; import org.simantics.db.common.request.UnaryAsyncRead; 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.AsyncProcedure; import org.simantics.document.server.JSONObject; public class DocumentRequest extends VariableRead> { public static boolean PROFILE = false; // 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 CollectNodesRequest extends UnaryAsyncRead, Collection> { public CollectNodesRequest(Collection nodes) { super(nodes); } @Override public void perform(AsyncReadGraph graph, AsyncProcedure> procedure) { HashSet rs = new HashSet(); // result for(Variable node : parameter) { graph.asyncRequest(new NodeRequest(node), new AsyncProcedure () { @Override public void execute(AsyncReadGraph graph, JSONObject result) { synchronized(rs) { rs.add(result); } } @Override public void exception(AsyncReadGraph graph, Throwable throwable) { } }); } procedure.execute(graph, rs); } } @Override public List perform(ReadGraph graph) throws DatabaseException { long s = System.nanoTime(); Set nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.>instance()); if(nodes.isEmpty()) { return Collections.emptyList(); } if(PROFILE) { long dura = System.nanoTime()-s; System.err.println("DocumentRequest1 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph)); } Collection rs = graph.syncRequest(new CollectNodesRequest(nodes)); if(PROFILE) { long dura = System.nanoTime()-s; System.err.println("DocumentRequest2 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph)); } 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("DocumentRequest3 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph)); } return result; } }