]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AsyncReadEntry.java
index a9726a75d6ac4eb5df323625ffd19da9369bcb9b..2fdb43d113332e4f33b6b131685fd8c80c112b22 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
+import java.util.Collection;
+
+import org.simantics.databoard.Bindings;
 import org.simantics.db.AsyncReadGraph;
-import org.simantics.db.impl.DebugPolicy;
+import org.simantics.db.DevelopmentKeys;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.BlockingAsyncProcedure;
 import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.impl.query.QueryProcessor.SessionTask;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.request.AsyncRead;
+import org.simantics.utils.Development;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T>, IPending {
 
-final public class AsyncReadEntry<T> extends CacheEntryBase {
+    private static final Logger LOGGER = LoggerFactory.getLogger(AsyncReadEntry.class);
 
-    protected AsyncRead<T> request;
+    protected AsyncRead<T> id;
+    protected PendingTaskSupport pendingTaskSupport;
 
-    public AsyncReadEntry(AsyncRead<T> request) {
-       this.request = request;
-       if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
+    AsyncReadEntry(AsyncRead<T> request) {
+        this.id = request;
+               if (Development.DEVELOPMENT) {
+                       if(Development.<Boolean>getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) {
+                       System.err.println("[QUERY STATE]: created " + this);
+                       }
+               }
     }
 
     @Override
     int makeHash() {
-       return request.hashCode();
+        return id.hashCode();
     }
-    
+
     @Override
     public Object getOriginalRequest() {
-        return request;
+        return id;
     }
-    
+
     @Override
     public void discard() {
-       super.discard();
-       //request = null;
-       setResult(null);
+        super.discard();
+        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();
-//            }
-        
-    }
-    
-    
     public void except(AsyncReadGraph graph, Throwable t) {
-       
-       assert(isPending());
-        
-//        ArrayList<AsyncProcedure<T>> p = null;
 
-        synchronized(this) {
-               
+        assert (isPending());
+
+        synchronized (this) {
             except(t);
-////            p = procs;
-//            procs = null;
-            
         }
 
-//        if(p != null)
-//            for(AsyncProcedure<T> proc : p) {
-//             proc.exception(graph, t);
-//            }
-       
     }
-    
-    
+
     @Override
     final public Query getQuery() {
-       
-        return new Query() {
 
-                       @Override
-                       public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-                               
-                               QueryProcessor qp = (QueryProcessor)provider;
+        return new Query() {
 
-                               final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); 
+            @Override
+            public void recompute(ReadGraphImpl graph) {
 
-                               try {
+                try {
 
-                                   request.perform(parentGraph , new AsyncProcedure<T>() {
+                    BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure(graph, AsyncReadEntry.this, new AsyncProcedure<T>() {
 
                         @Override
                         public void execute(AsyncReadGraph graph, T result) {
-                            addOrSet(graph, result);
+                            setResult(result);
+                            setReady();
                         }
-                               
-                               @Override
-                               public void exception(AsyncReadGraph graph, Throwable t) {
-                                   except(t);
+
+                        @Override
+                        public void exception(AsyncReadGraph graph, Throwable t) {
+                            except(t);
                         }
 
-                    });
+                    }, id, true);
+                    
+                    proc.performSync(id);
 
-                               } catch (Throwable t) {
-                                   except(t);
+                } catch (Throwable t) {
+                    except(t);
                 }
-                               
-                       }
 
-                       @Override
-                       public void removeEntry(QueryProcessor qp) {
-                       qp.asyncReadMap.remove(request);
-                       }
+            }
+
+            @Override
+            public void removeEntry(QueryProcessor qp) {
+                qp.cache.remove(AsyncReadEntry.this);
+            }
+
+            @Override
+            public int type() {
+                return id.getFlags();
+            }
+
+            @Override
+            public String toString() {
+                if (id == null)
+                    return "DISCARDED";
+                else if (isExcepted())
+                    return id.toString() + " " + getResult();
+                else
+                    return id.toString() + " " + statusOrException;
+            }
 
-                       @Override
-                       public int type() {
-                               return request.getFlags();
-                       }
-                       
-                       @Override
-                       public String toString() {
-                               if(request == null) return "DISCARDED";
-                               else if(isExcepted()) return request.toString() + " " + getResult();
-                               else return request.toString() + " " + statusOrException;
-                       }
-               
         };
-        
+
     }
 
-       @SuppressWarnings("unchecked")
-       public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-               
-        AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
+    @Override
+    public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
+
+        if (isExcepted()) {
 
-           if(isExcepted()) {
-            
             try {
-                proc.exception(graph, (Throwable)getResult());
+                proc.exception(graph, (Throwable) getResult());
             } catch (Throwable t) {
-                t.printStackTrace();
+                LOGGER.error("performFromCache proc.exception failed", t);
             }
-            
+
         } else {
-            
+
             try {
-                proc.execute(graph, (T)getResult());
+                T result = (T) getResult();
+                proc.execute(graph, result);
             } catch (Throwable t) {
-                t.printStackTrace();
+                LOGGER.error("performFromCache proc.execute failed", t);
             }
-            
+
+        }
+
+        return getResult();
+
+    }
+
+    public static <T> T computeForEach(ReadGraphImpl callerGraph, AsyncRead<T> request, AsyncReadEntry<T> entry,
+            AsyncProcedure<T> procedure_, boolean needsToBlock) throws DatabaseException {
+
+        BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure(callerGraph, entry, procedure_, request, needsToBlock);
+        if(needsToBlock) {
+            return proc.performSync(request);
+        } else {
+            proc.performAsync(request);
+            return null;
+        }
+
+    }
+
+    @Override
+    public String toString() {
+        if (isDiscarded())
+            return "DISCARDED " + id.toString();
+        else if (isExcepted())
+            return id.toString() + " " + getResult();
+        else
+            return id.toString() + " " + statusOrException;
+    }
+
+    @Override
+    public void execute(AsyncReadGraph graph, T result) {
+        Collection<SessionTask> tasks = null;
+        synchronized(this) {
+            setResult(result);
+            setReady();
+            if(pendingTaskSupport != null)
+                tasks = pendingTaskSupport.executePending();
+        }
+        if(tasks != null)
+            for(SessionTask task : tasks)
+                ((ReadGraphImpl)graph).processor.scheduleNow(task);
+    }
+
+    @Override
+    public synchronized void exception(AsyncReadGraph graph, Throwable throwable) {
+        Collection<SessionTask> tasks = null;
+        synchronized(this) {
+            except(throwable);
+            if(pendingTaskSupport != null)
+                tasks = pendingTaskSupport.executePending();
+        }
+        if(tasks != null)
+            for(SessionTask task : tasks)
+                ((ReadGraphImpl)graph).processor.scheduleNow(task);
+    }
+
+    public void executeWhenResultIsAvailable(QueryProcessor processor, SessionTask task) {
+        boolean ready = false;
+        synchronized(this) {
+            if(pendingTaskSupport == null)
+                pendingTaskSupport = new PendingTaskSupport(this);
+            ready = pendingTaskSupport.executeWhenResultIsAvailable(task);
+        }
+        if(ready) {
+            processor.scheduleNow(task);
         }
-               
-       }
-
-       @Override
-       public String toString() {
-               if(isDiscarded()) return "DISCARDED " + request.toString();
-               else if(isExcepted()) return request.toString() + " " + getResult();
-               else return request.toString() + " " + statusOrException;
-       }
+    }
+
+    @Override
+    public String classId() {
+        return null;
+    }
 
 }