X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.document.server%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fserver%2Frequest%2FDocumentRequest.java;fp=bundles%2Forg.simantics.document.server%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fserver%2Frequest%2FDocumentRequest.java;h=87b244c88ba28e1c43129cdf098a43e2f7781201;hb=5bc1c2b950d374ecfcc206981724c3b9286dcbd6;hp=9a53060bef2c0b246a21e465da1f0e53756d2f17;hpb=8e67ae458c3ee4711eff252f971d9a4219d51210;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java index 9a53060be..87b244c88 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java @@ -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> { - 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> { 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)); - long s = System.nanoTime(); + try { - 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)); - }*/ - - for(Variable node : nodes) { - rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.instance())); - } + 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() { - ArrayList result = new ArrayList(rs); - Collections.sort(result, new Comparator() { + @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.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 { - }); - - 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