]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java
DB request scheduling scheme fails with district diagrams
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ReadEntry.java
index c95242defc77a45ae859f49042a2fc1e13df4f20..8f547d22b3e950a9c3b87e035fd9a0e07a4aef08 100644 (file)
@@ -25,20 +25,20 @@ public final class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implem
 
     private static final Logger LOGGER = LoggerFactory.getLogger(ReadEntry.class);
 
-    protected Read<T> request;
+    protected Read<T> id;
 
     public ReadEntry(Read<T> request) {
-        this.request = request;
+        this.id = request;
     }
 
     @Override
     int makeHash() {
-        return request.hashCode();
+        return id.hashCode();
     }
 
     @Override
     public Object getOriginalRequest() {
-        return request;
+        return id;
     }
 
     @Override
@@ -57,7 +57,7 @@ public final class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implem
 
                 try {
 
-                    T result = request.perform(graph);
+                    T result = id.perform(graph);
                     setResult(result);
                     setReady();
 
@@ -76,8 +76,8 @@ public final class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implem
 
             @Override
             public int type() {
-                if (request instanceof ReadExt) {
-                    return ((ReadExt) request).getType();
+                if (id instanceof ReadExt) {
+                    return ((ReadExt) id).getType();
                 } else {
                     return RequestFlags.INVALIDATE;
                 }
@@ -85,74 +85,107 @@ public final class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implem
 
             @Override
             public String toString() {
-                if (request == null)
+                if (id == null)
                     return "DISCARDED";
                 else
-                    return request.toString() + statusOrException;
+                    return id.toString() + statusOrException;
             }
 
         };
 
     }
 
-    public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry,
-            AsyncProcedure<T> procedure_) throws DatabaseException {
+    public static <T> T computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry,
+            AsyncProcedure<T> procedure_, boolean needsToBlock) throws DatabaseException {
 
         AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
 
-        ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
+        ReadGraphImpl queryGraph = graph.withParent(entry, null, needsToBlock);
+        queryGraph.asyncBarrier.inc();
 
+        ReadGraphImpl executeGraph = graph.withParent(graph.parent, null, needsToBlock);
+        executeGraph.asyncBarrier.inc();
+        
         try {
 
+            // This throws
             T result = request.perform(queryGraph);
-            procedure.execute(graph, result);
+
+            if(procedure != null) procedure.execute(executeGraph, result);
+            return (T)result;
 
         } catch (DatabaseException e) {
 
-            procedure.exception(graph, e);
+            if(procedure != null) procedure.exception(executeGraph, e);
+            throw e;
 
         } catch (Throwable t) {
 
             DatabaseException dbe = new DatabaseException(t);
-            procedure.exception(graph, dbe);
+            if(procedure != null) procedure.exception(executeGraph, dbe);
+            throw dbe;
 
-        }
+        } finally {
+
+            queryGraph.asyncBarrier.dec();
+
+            try {
+            
+                if (entry != null) {
+                    // This also throws so must dec barrier finally
+                    entry.performFromCache(executeGraph, procedure_);
+                }
+            
+            } finally {
+                executeGraph.asyncBarrier.dec();
+                if(needsToBlock)
+                    executeGraph.asyncBarrier.waitBarrier(procedure, executeGraph);
 
-        if (entry != null)
-            entry.performFromCache(queryGraph, procedure_);
+            }
+                
+        }
 
     }
 
-    public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
+    public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
 
         AsyncProcedure<T> proc = (AsyncProcedure<T>) procedure;
 
-        if (proc != null) {
-            if (isExcepted()) {
-                try {
-                    proc.exception(graph, (Throwable) getResult());
-                } catch (Throwable t) {
-                    LOGGER.error("performFromCache proc.exception failed", t);
+        if (isExcepted()) {
+                if(proc != null) {
+                    try {
+                        proc.exception(graph, (Throwable) getResult());
+                    } catch (Throwable t) {
+                        LOGGER.error("performFromCache proc.exception failed", t);
+                    }
                 }
-            } else {
-                try {
-                    proc.execute(graph, (T) getResult());
-                } catch (Throwable t) {
-                    LOGGER.error("performFromCache proc.execute failed", t);
+                Throwable t = (Throwable) getResult();
+                if(t instanceof DatabaseException) {
+                    throw (DatabaseException)t;
+                } else {
+                    throw new DatabaseException(t);
                 }
-            }
+        } else {
+                if(proc != null) {
+                    try {
+                        proc.execute(graph, (T) getResult());
+                    } catch (Throwable t) {
+                        LOGGER.error("performFromCache proc.execute failed", t);
+                    }
+                }
+                return (T)getResult();
         }
 
-        return (T) getResult();
-
     }
 
     @Override
     public String toString() {
-        if (request == null)
+        if (id == null)
             return "DISCARDED";
         else
-            return request.toString() + " - " + statusOrException;
+            return id.toString() + " - " + statusOrException;
     }
 
     public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
@@ -164,8 +197,8 @@ public final class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implem
 
     @Override
     boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
-        if (request instanceof ReadExt) {
-            return ((ReadExt) request).isImmutable(graph);
+        if (id instanceof ReadExt) {
+            return ((ReadExt) id).isImmutable(graph);
         }
         return false;
     }