import java.util.List;
import java.util.Set;
+import org.simantics.db.AsyncReadGraph;
import org.simantics.db.ReadGraph;
import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.common.request.AsyncReadRequest;
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<List<JSONObject>> {
- 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;
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<JSONObject> perform(ReadGraph graph) throws DatabaseException {
+
+ Task task = ThreadLog.BEGIN("DocumentRequest " + variable.getURI(graph));
- long s = System.nanoTime();
+ try {
- Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
- HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
- if(nodes.isEmpty()) {
- return Collections.emptyList();
- }
-
-
- /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
-
- 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.<JSONObject>instance()));
- }
+ long s = System.nanoTime();
+
+ Set<Variable> nodes = graph.syncRequest(new NodesRequest(variable), TransientCacheAsyncListener.<Set<Variable>>instance());
+ HashSet<JSONObject> rs = new HashSet<JSONObject>(); // result
+ if(nodes.isEmpty()) {
+ return Collections.emptyList();
+ }
+
+
+ /*TreeMap<String, Variable> nodeMap = new TreeMap<String, Variable>();
+
+ for (Variable node : nodes) {
+ nodeMap.put(node.getURI(graph), node);
+ }
+ System.out.println("*************************************************************************");
+ for (Variable node : nodeMap.values()) {
+ System.out.println(" " + node.getURI(graph));
+ }*/
+
+ graph.syncRequest(new AsyncReadRequest() {
+
+ @Override
+ public void run(AsyncReadGraph graph) {
+
+ for(Variable node : nodes) {
+
+ graph.asyncRequest(new NodeRequestE(node), new Listener<JSONObject>() {
- ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
- Collections.sort(result, new Comparator<JSONObject>() {
+ @Override
+ public void execute(JSONObject result) {
+ synchronized(rs) {
+ rs.add(result);
+ }
+ }
- @Override
- public int compare(JSONObject o1, JSONObject o2) {
- return o1.id.compareTo(o2.id);
- }
+ @Override
+ public void exception(Throwable t) {
+ t.printStackTrace();
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return true;
+ }
+
+ });
+
+ }
+
+ }
+
+ });
+
+ ArrayList<JSONObject> result = new ArrayList<JSONObject>(rs);
+ Collections.sort(result, new Comparator<JSONObject>() {
+
+ @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