]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ReadEntry.java
index edc32454929542ab76a0f4a51155559a52b22324..56267cc12d39be24f0d245897977dafe0ec04dfd 100644 (file)
@@ -14,153 +14,167 @@ package org.simantics.db.impl.query;
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
-import org.simantics.db.impl.graph.WriteGraphImpl;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.request.Read;
 import org.simantics.db.request.ReadExt;
 import org.simantics.db.request.RequestFlags;
 
-final public class ReadEntry<T> extends CacheEntryBase {
+final public class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
 
-       protected Read<T> request;
+    protected Read<T> request;
 
-       public ReadEntry(Read<T> request) {
-       this.request = request;
+    public ReadEntry(Read<T> request) {
+        this.request = request;
     }
-    
+
     @Override
     int makeHash() {
-       return request.hashCode();
+        return request.hashCode();
     }
-       
+
     @Override
     public Object getOriginalRequest() {
         return request;
     }
-    
+
     @Override
     public void discard() {
-       super.discard();
-       setResult(null);
-    }
-       
-    final public void addOrSet(AsyncReadGraph graph, Object item) {
-
-       assert(assertPending());
-       
-//        ArrayList<Pair<AsyncProcedure<T>, AsyncBarrier>> p = null;
-
-       synchronized(this) {
-           
-            setResult(item);
-               setReady();
-//             p = procs;
-//             procs = null;
-               
-       }
-
-//        if(p != null)
-//             for(Pair<AsyncProcedure<T>, AsyncBarrier> proc : p) {
-//                     proc.first.execute(graph, (T)item);
-//                     proc.second.dec();
-//             }
-        
+        super.discard();
+        setResult(null);
     }
 
     @Override
     final public Query getQuery() {
-        
+
         return new Query() {
 
-                       @Override
-                       public void recompute(ReadGraphImpl graph_, Object provider, CacheEntry entry) {
-                               
-                               QueryProcessor qp = (QueryProcessor)provider;
-                               
-                       WriteGraphImpl write = qp.getCore().getSession().getService(WriteGraphImpl.class);
-                               
-                       ReadGraphImpl graph = write.newSync(entry);
+            @Override
+            public void recompute(ReadGraphImpl graph) {
+
+                try {
 
-                               try {
+                    T result = request.perform(graph);
+                    setResult(result);
+                    setReady();
 
-                                       entry.setPending();
-                                   T result = request.perform(graph);
-                                   addOrSet(graph, result);
+                } catch (Throwable t) {
 
-                               } catch (Throwable t) {
+                    except(t);
 
-                                       except(t);
-                    
                 }
-                               
-                       }
-
-                       @Override
-                       public void removeEntry(QueryProcessor processor) {
-                               processor.cache.readMap.remove(request);
-                       }
-
-                       @Override
-                       public int type() {
-                               if(request instanceof ReadExt) {
-                                       return ((ReadExt)request).getType();
-                               } else {
-                                       return RequestFlags.INVALIDATE;
-                               }
-                       }
-                       
-                       @Override
-                       public String toString() {
-                               if(request == null) return "DISCARDED";
-                               else return request.toString() + statusOrException;
-                       }
-               
+
+            }
+
+            @Override
+            public void removeEntry(QueryProcessor processor) {
+                processor.cache.remove(ReadEntry.this);
+            }
+
+            @Override
+            public int type() {
+                if (request instanceof ReadExt) {
+                    return ((ReadExt) request).getType();
+                } else {
+                    return RequestFlags.INVALIDATE;
+                }
+            }
+
+            @Override
+            public String toString() {
+                if (request == null)
+                    return "DISCARDED";
+                else
+                    return request.toString() + statusOrException;
+            }
+
         };
-        
+
+    }
+
+    public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry,
+            AsyncProcedure<T> procedure_) throws DatabaseException {
+
+        AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
+
+        ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
+
+        try {
+
+            T result = request.perform(queryGraph);
+            procedure.execute(graph, result);
+
+        } catch (DatabaseException e) {
+
+            procedure.exception(graph, e);
+
+        } catch (Throwable t) {
+
+            DatabaseException dbe = new DatabaseException(t);
+            procedure.exception(graph, dbe);
+
+        }
+
+        if (entry != null)
+            entry.performFromCache(queryGraph, procedure_);
+
     }
-    
-       public Object performFromCache(ReadGraphImpl graph, Object provider,    Object procedure) {
-           
-        AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
-
-        if(proc != null) {
-               if(isExcepted()) {
-                       try {
-                               proc.exception(graph, (Throwable)getResult());
-                       } catch (Throwable t) {
-                               t.printStackTrace();
-                       }
-               } else {
-                       try {
-                               proc.execute(graph, (T)getResult());
-                       } catch (Throwable t) {
-                               t.printStackTrace();
-                       }
-               }
+
+    public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
+
+        AsyncProcedure<T> proc = (AsyncProcedure<T>) procedure;
+
+        if (proc != null) {
+            if (isExcepted()) {
+                try {
+                    proc.exception(graph, (Throwable) getResult());
+                } catch (Throwable t) {
+                    t.printStackTrace();
+                }
+            } else {
+                try {
+                    proc.execute(graph, (T) getResult());
+                } catch (Throwable t) {
+                    t.printStackTrace();
+                }
+            }
         }
-               
-           return (T)getResult();
-           
-       }
-       
-       @Override
-       public String toString() {
-               if(request == null) return "DISCARDED";
-               else return request.toString() + " - " + statusOrException;
-       }
-       
-       public Object get(ReadGraphImpl graph, QueryProcessor processor, Object procedure) throws DatabaseException {
-               if(procedure != null) performFromCache(graph, processor, procedure);
-               checkAndThrow();
-               return getResult();
-       }
-       
-       @Override
-       boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
-               if(request instanceof ReadExt) {
-                       return ((ReadExt)request).isImmutable(graph);
-               }
-               return false;
-       }
+
+        return (T) getResult();
+
+    }
+
+    @Override
+    public String toString() {
+        if (request == null)
+            return "DISCARDED";
+        else
+            return request.toString() + " - " + statusOrException;
+    }
+
+    public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+        if (procedure != null)
+            performFromCache(graph, procedure);
+        checkAndThrow();
+        return getResult();
+    }
+
+    @Override
+    boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
+        if (request instanceof ReadExt) {
+            return ((ReadExt) request).isImmutable(graph);
+        }
+        return false;
+    }
+
+    @Override
+    public void execute(AsyncReadGraph graph, T result) {
+        setResult(result);
+        setReady();
+    }
+
+    @Override
+    public void exception(AsyncReadGraph graph, Throwable throwable) {
+        except(throwable);
+    }
 
 }