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.AsyncReadGraph;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.common.GraphSemaphore;
15 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
16 import org.simantics.db.common.request.AsyncReadRequest;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.request.VariableRead;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.procedure.Listener;
21 import org.simantics.document.server.JSONObject;
22 import org.simantics.threadlog.Task;
23 import org.simantics.threadlog.ThreadLog;
25 public class DocumentRequest extends VariableRead<List<JSONObject>> {
27 public static boolean PROFILE = true;
28 // Thresholds in microseconds
29 public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
30 public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
32 public DocumentRequest(Variable var) {
36 static class NodeRequestE extends NodeRequest {
38 static int count1 = 0;
41 public NodeRequestE(Variable node) {
44 System.err.println("create NodeRequest count = " + count1);
46 System.err.println("asd");
50 public JSONObject perform(ReadGraph graph) throws DatabaseException {
52 System.err.println("perform NodeRequest count = " + count);
54 return super.perform(graph);
60 public List<JSONObject> perform(ReadGraph graph) throws DatabaseException {
62 Task task = ThreadLog.BEGIN("DocumentRequest " + variable.getURI(graph));
66 long s = System.nanoTime();
68 Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
69 HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
71 return Collections.emptyList();
75 /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
77 for (Variable node : nodes) {
78 nodeMap.put(node.getURI(graph), node);
80 System.out.println("*************************************************************************");
81 for (Variable node : nodeMap.values()) {
82 System.out.println(" " + node.getURI(graph));
85 <<<<<<< Upstream, based on branch 'private/antti_threads' of ssh://villberg@gerrit.simantics.org:29418/simantics/platform.git
86 GraphSemaphore done = new GraphSemaphore(graph, 0);
88 for(Variable node : nodes) {
90 graph.asyncRequest(new NodeRequestE(node), new Listener<JSONObject>() {
93 public void execute(JSONObject result) {
101 public void exception(Throwable t) {
107 public boolean isDisposed() {
113 // rs.add(graph.syncRequest(new NodeRequest(node), TransientCacheAsyncListener.<JSONObject>instance()));
118 done.waitFor(nodes.size());
119 } catch (InterruptedException e) {
123 graph.syncRequest(new AsyncReadRequest() {
126 public void run(AsyncReadGraph graph) {
128 for(Variable node : nodes) {
130 graph.asyncRequest(new NodeRequestE(node), new Listener<JSONObject>() {
133 public void execute(JSONObject result) {
140 public void exception(Throwable t) {
145 public boolean isDisposed() {
156 >>>>>>> 82fa68e Generate parts of db client query code
158 ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
159 Collections.sort(result, new Comparator<JSONObject>() {
162 public int compare(JSONObject o1, JSONObject o2) {
163 return o1.id.compareTo(o2.id);
169 long dura = System.nanoTime()-s;
170 System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));