1 package org.simantics.document.server.request;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashSet;
9 import java.util.concurrent.Semaphore;
10 import java.util.concurrent.TimeUnit;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.common.GraphSemaphore;
14 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
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.Listener;
19 import org.simantics.document.server.JSONObject;
20 import org.simantics.threadlog.Task;
21 import org.simantics.threadlog.ThreadLog;
23 public class DocumentRequest extends VariableRead<List<JSONObject>> {
25 public static boolean PROFILE = true;
26 // Thresholds in microseconds
27 public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
28 public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
30 public DocumentRequest(Variable var) {
34 static class NodeRequestE extends NodeRequest {
36 static int count1 = 0;
39 public NodeRequestE(Variable node) {
42 System.err.println("create NodeRequest count = " + count1);
44 System.err.println("asd");
48 public JSONObject perform(ReadGraph graph) throws DatabaseException {
50 System.err.println("perform NodeRequest count = " + count);
52 return super.perform(graph);
58 public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
60 Task task = ThreadLog.BEGIN("DocumentRequest " + variable.getURI(graph));
64 long s = System.nanoTime();
66 Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
67 HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
69 return Collections.emptyList();
73 /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
75 for (Variable node : nodes) {
76 nodeMap.put(node.getURI(graph), node);
78 System.out.println("*************************************************************************");
79 for (Variable node : nodeMap.values()) {
80 System.out.println(" " + node.getURI(graph));
83 GraphSemaphore done = new GraphSemaphore(graph, 0);
85 for(Variable node : nodes) {
87 graph.asyncRequest(new NodeRequestE(node), new Listener<JSONObject>() {
90 public void execute(JSONObject result) {
98 public void exception(Throwable t) {
104 public boolean isDisposed() {
110 // rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));
115 done.waitFor(nodes.size());
116 } catch (InterruptedException e) {
120 ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
121 Collections.sort(result, new Comparator<JSONObject>() {
124 public int compare(JSONObject o1, JSONObject o2) {
125 return o1.id.compareTo(o2.id);
131 long dura = System.nanoTime()-s;
132 System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));