1 package org.simantics.document.server.request;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.Comparator;
7 import java.util.HashSet;
11 import org.simantics.db.AsyncReadGraph;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
14 import org.simantics.db.common.request.UnaryAsyncRead;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.request.VariableRead;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.document.server.JSONObject;
21 public class DocumentRequest extends VariableRead<List<JSONObject>> {
23 public static boolean PROFILE = false;
24 // Thresholds in microseconds
25 public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
26 public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
28 public DocumentRequest(Variable var) {
32 static class CollectNodesRequest extends UnaryAsyncRead<Collection<Variable>, Collection<JSONObject>> {
34 public CollectNodesRequest(Collection<Variable> nodes) {
39 public void perform(AsyncReadGraph graph, AsyncProcedure<Collection<JSONObject>> procedure) {
40 HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
42 for(Variable node : parameter) {
43 graph.asyncRequest(new NodeRequest(node), new AsyncProcedure<JSONObject> () {
46 public void execute(AsyncReadGraph graph, JSONObject result) {
53 public void exception(AsyncReadGraph graph, Throwable throwable) {
59 procedure.execute(graph, rs);
66 public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
68 long s = System.nanoTime();
70 Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
72 return Collections.emptyList();
76 long dura = System.nanoTime()-s;
77 System.err.println("DocumentRequest1 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
80 Collection<JSONObject> rs = graph.syncRequest(new CollectNodesRequest(nodes));
83 long dura = System.nanoTime()-s;
84 System.err.println("DocumentRequest2 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
87 ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
88 Collections.sort(result, new Comparator<JSONObject>() {
91 public int compare(JSONObject o1, JSONObject o2) {
92 return o1.id.compareTo(o2.id);
98 long dura = System.nanoTime()-s;
99 System.err.println("DocumentRequest3 " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));