]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AsyncReadEntry.java
index 010f554463373ace1edf3ac21cef13a4cc17122a..8e6cbd145a78d9a1aacf3b99f20c396a789beaae 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.ArrayList;
-
 import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.DebugPolicy;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.request.AsyncRead;
 
-final public class AsyncReadEntry<T> extends CacheEntryBase {
+final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
 
     protected AsyncRead<T> request;
 
-    public AsyncReadEntry(AsyncRead<T> request) {
+    AsyncReadEntry(AsyncRead<T> request) {
        this.request = request;
        if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
     }
@@ -41,31 +40,17 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
     @Override
     public void discard() {
        super.discard();
-       //request = null;
        setResult(null);
     }
     
     final public void addOrSet(AsyncReadGraph graph, Object item) {
 
        assert(isPending());
-        
-//        ArrayList<AsyncProcedure<T>> p = null;
 
         synchronized(this) {
-               
             setResult(item);
                setReady();
-//            p = procs;
-//            procs = null;
-            
         }
-
-//        if(p != null)
-//            for(AsyncProcedure<T> proc : p) {
-//             proc.execute(graph, (T)item);
-////                proc.first.execute(graph, (T)item);
-////                proc.second.dec();
-//            }
         
     }
     
@@ -73,21 +58,10 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
     public void except(AsyncReadGraph graph, Throwable t) {
        
        assert(isPending());
-        
-//        ArrayList<AsyncProcedure<T>> p = null;
 
         synchronized(this) {
-               
             except(t);
-////            p = procs;
-//            procs = null;
-            
         }
-
-//        if(p != null)
-//            for(AsyncProcedure<T> proc : p) {
-//             proc.exception(graph, t);
-//            }
        
     }
     
@@ -98,15 +72,11 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
         return new Query() {
 
                        @Override
-                       public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-                               
-                               QueryProcessor qp = (QueryProcessor)provider;
-
-                               final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); 
+                       public void recompute(ReadGraphImpl graph) {
 
                                try {
 
-                                   request.perform(parentGraph , new AsyncProcedure<T>() {
+                                   request.perform(graph , new AsyncProcedure<T>() {
 
                         @Override
                         public void execute(AsyncReadGraph graph, T result) {
@@ -128,7 +98,7 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
 
                        @Override
                        public void removeEntry(QueryProcessor qp) {
-                       qp.asyncReadMap.remove(request);
+                       qp.cache.remove(AsyncReadEntry.this);
                        }
 
                        @Override
@@ -147,10 +117,9 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
         
     }
 
-       public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
+    @Override
+       public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
                
-        AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
-
            if(isExcepted()) {
             
             try {
@@ -169,8 +138,91 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
             
         }
                
+           return getResult();
+           
        }
 
+    @Override
+    public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+
+       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();