]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java
Fixed asynchronous recompute problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AsyncReadEntry.java
index 767a82bd5644e8759a282b8820c49b570a8fdd60..75169d02e9946ca0a1dea3f1e03a3dba2ce49b97 100644 (file)
@@ -12,6 +12,7 @@
 package org.simantics.db.impl.query;
 
 import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.common.GraphSemaphore;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.DebugPolicy;
 import org.simantics.db.impl.graph.ReadGraphImpl;
@@ -45,6 +46,8 @@ final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
     
     final public void addOrSet(AsyncReadGraph graph, Object item) {
 
+//     System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + item);
+       
        assert(isPending());
 
         synchronized(this) {
@@ -76,20 +79,26 @@ final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
 
                                try {
 
+                                       GraphSemaphore s = new GraphSemaphore(graph, 0);
+                                       
                                    request.perform(graph , new AsyncProcedure<T>() {
 
                         @Override
                         public void execute(AsyncReadGraph graph, T result) {
                             addOrSet(graph, result);
+                            s.release();
                         }
                                
                                @Override
                                public void exception(AsyncReadGraph graph, Throwable t) {
                                    except(t);
+                            s.release();
                         }
 
                     });
 
+                    s.waitFor(1);
+
                                } catch (Throwable t) {
                                    except(t);
                 }
@@ -142,11 +151,87 @@ final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
            
        }
 
-    @Override
+    //@Override
     public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
-       return graph.processor.cache.performQuery(graph, request, this, procedure);
+
+       ReadGraphImpl queryGraph = graph.withParent(this);
+
+       request.perform(queryGraph, new AsyncProcedure<T>() {
+
+               @Override
+               public void execute(AsyncReadGraph returnGraph, T result) {
+                       ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+                       AsyncReadEntry.this.addOrSet(graph, result);
+                       try {
+                               procedure.execute(graph, result);
+                       } catch (Throwable t) {
+                               t.printStackTrace();
+                       }
+                       //                                      parentBarrier.dec(query);
+               }
+
+               @Override
+               public void exception(AsyncReadGraph returnGraph, Throwable t) {
+                       ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+                       //                                      AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
+                       AsyncReadEntry.this.except(graph, t);
+                       try {
+                               procedure.exception(graph, t);
+                       } catch (Throwable t2) {
+                               t2.printStackTrace();
+                       }
+                       //                                      parentBarrier.dec(query);
+               }
+
+               @Override
+               public String toString() {
+                       return procedure.toString();
+               }
+
+       });
+
+               return getResult();
+       
     }
     
+    public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
+
+               ReadGraphImpl queryGraph = parentGraph.withParent(entry);
+
+               request.perform(queryGraph, new AsyncProcedure<T>() {
+
+                       @Override
+                       public void execute(AsyncReadGraph returnGraph, T result) {
+                               ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+                               if(entry != null) entry.addOrSet(parentGraph, result);
+                               try {
+                                       procedure.execute(parentGraph, result);
+                               } catch (Throwable t) {
+                                       t.printStackTrace();
+                               }
+                       }
+
+                       @Override
+                       public void exception(AsyncReadGraph returnGraph, Throwable t) {
+                               ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+                               if(entry != null) entry.except(parentGraph, t);
+                               try {
+                                       procedure.exception(parentGraph, t);
+                               } catch (Throwable t2) {
+                                       t2.printStackTrace();
+                               }
+                       }
+
+                       @Override
+                       public String toString() {
+                               return procedure.toString();
+                       }
+
+               });
+       
+    }
+    
+    
        @Override
        public String toString() {
                if(isDiscarded()) return "DISCARDED " + request.toString();