]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncProcedure.java
Wrong graph was used when performing async query from session
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / BlockingAsyncProcedure.java
index c491fb377464e366f5b34e8e8eac2e0e205840c5..8f96bb9e6d3a51a48c7b98baa40f962fc83f5617 100644 (file)
@@ -18,31 +18,32 @@ import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.query.AsyncReadEntry;
 import org.simantics.db.impl.query.PendingTaskSupport;
 import org.simantics.db.procedure.AsyncProcedure;
-
+import org.simantics.db.request.AsyncRead;
 public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
 
     private static final Object NO_RESULT = new Object();
 
-    public final Object key;
-    public final ReadGraphImpl queryGraph;
-    public final ReadGraphImpl callerGraph;
-    public final AsyncProcedure<Result> procedure;
-    public PendingTaskSupport pendingTaskSupport;
-    public Object result = NO_RESULT;
-    public Throwable exception = null;
+    private final Object key;
+    private final ReadGraphImpl queryGraph;
+    private final ReadGraphImpl callerGraph;
+    private final AsyncProcedure<Result> procedure;
+    private PendingTaskSupport pendingTaskSupport;
+    private final boolean needsToBlock;
+    private Object result = NO_RESULT;
+    private Throwable exception = null;
 
     private ReadGraphImpl queryGraph() {
         return queryGraph;
     }
     
     public BlockingAsyncProcedure(ReadGraphImpl callerGraph, AsyncReadEntry<Result> entry, AsyncProcedure<Result> procedure, Object key, boolean needsToBlock) {
-
+        
         // A new graph for evaluating the query with correct parent and asyncBarrier
         queryGraph = callerGraph.withParent(entry, () -> {
 
             dispatchProcedure(queryGraph(), callerGraph, entry, procedure, needsToBlock);
             
-        });
+        }, needsToBlock);
         
         queryGraph.asyncBarrier.inc();
 
@@ -50,6 +51,7 @@ public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
         this.key = key;
         this.queryGraph.asyncBarrier.inc();
         this.callerGraph = callerGraph;
+        this.needsToBlock = needsToBlock;
         if (BarrierTracing.BOOKKEEPING) {
             BarrierTracing.registerBAP(this);
         }
@@ -84,7 +86,8 @@ public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
     @SuppressWarnings("unchecked")
     public Result get() throws DatabaseException {
 
-        queryGraph.asyncBarrier.waitBarrier(key, queryGraph);
+        if(needsToBlock)
+            queryGraph.asyncBarrier.waitBarrier(key, queryGraph);
 
         if(exception != null) {
             if(exception instanceof DatabaseException) throw (DatabaseException)exception;
@@ -113,7 +116,7 @@ public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
         
         AsyncProcedure<Result> procedure = entry != null ? entry : procedure_;
 
-        ReadGraphImpl executeGraph = parentGraph.withParent(parentGraph.parent);
+        ReadGraphImpl executeGraph = parentGraph.withParent(parentGraph.parent, null, needsToBlock);
         executeGraph.asyncBarrier.inc();
         try {
             if(procedure != null) {
@@ -144,5 +147,33 @@ public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
         }
 
     }
+    
+    public void print() {
+        System.err.println("BlockingAsyncProcedure");
+        System.err.println("-key: " + key);
+        System.err.println("-queryGraph: " + queryGraph);
+        System.err.println("-callerGraph: " + callerGraph);
+        System.err.println("-procedure: " + procedure);
+        System.err.println("-pendingTaskSupport: " + pendingTaskSupport);
+        System.err.println("-result: " + result);
+        System.err.println("-exception: " + exception);
+    }
+    
+    public Result performSync(AsyncRead<Result> request) throws DatabaseException {
+        try {
+            request.perform(queryGraph, this);
+        } finally {
+            dec();
+        }
+        return get();
+    }
+
+    public void performAsync(AsyncRead<Result> request) throws DatabaseException {
+        try {
+            request.perform(queryGraph, this);
+        } finally {
+            dec();
+        }
+    }
 
 }