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.procedure.adapter.TransientCacheAsyncListener;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.request.VariableRead;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.db.procedure.Listener;
18 import org.simantics.document.server.JSONObject;
19 import org.simantics.threadlog.Task;
20 import org.simantics.threadlog.ThreadLog;
22 public class DocumentRequest extends VariableRead<List<JSONObject>> {
24 public static boolean PROFILE = true;
25 // Thresholds in microseconds
26 public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
27 public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
29 public DocumentRequest(Variable var) {
33 static class NodeRequestE extends NodeRequest {
35 static int count1 = 0;
38 public NodeRequestE(Variable node) {
41 System.err.println("create NodeRequest count = " + count1);
43 System.err.println("asd");
47 public JSONObject perform(ReadGraph graph) throws DatabaseException {
49 System.err.println("perform NodeRequest count = " + count);
51 return super.perform(graph);
57 public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
59 Task task = ThreadLog.BEGIN("DocumentRequest " + variable.getURI(graph));
63 long s = System.nanoTime();
65 Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
66 HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
68 return Collections.emptyList();
72 /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
74 for (Variable node : nodes) {
75 nodeMap.put(node.getURI(graph), node);
77 System.out.println("*************************************************************************");
78 for (Variable node : nodeMap.values()) {
79 System.out.println(" " + node.getURI(graph));
82 Semaphore done = new Semaphore(0);
84 for(Variable node : nodes) {
86 graph.asyncRequest(new NodeRequestE(node), new Listener<JSONObject>() {
89 public void execute(JSONObject result) {
97 public void exception(Throwable t) {
103 public boolean isDisposed() {
109 // rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));
114 boolean success = done.tryAcquire(nodes.size(), 10, TimeUnit.MILLISECONDS);
116 success = done.tryAcquire(nodes.size(), 10, TimeUnit.MILLISECONDS);
117 System.err.println("still trying to acquire semaphore, avail = " + done.availablePermits() );
120 } catch (InterruptedException e) {
124 ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
125 Collections.sort(result, new Comparator<JSONObject>() {
128 public int compare(JSONObject o1, JSONObject o2) {
129 return o1.id.compareTo(o2.id);
135 long dura = System.nanoTime()-s;
136 System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));