]> 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 115a4a5a1509db803a88140b038d9f475b1e3305..56267cc12d39be24f0d245897977dafe0ec04dfd 100644 (file)
@@ -14,185 +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<AsyncProcedure<T>> {
+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 Object addOrSet(AsyncReadGraph graph, Object result) {
-       
-//     System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + result);
-
-       assert(assertPending());
-       
-       setResult(result);
-       setReady();
-       
-       return result;
-        
+        super.discard();
+        setResult(null);
     }
 
     @Override
     final public Query getQuery() {
-        
+
         return new Query() {
 
-                       @Override
-                       public void recompute(ReadGraphImpl graph) {
-                               
-                               try {
+            @Override
+            public void recompute(ReadGraphImpl graph) {
+
+                try {
+
+                    T result = request.perform(graph);
+                    setResult(result);
+                    setReady();
+
+                } catch (Throwable t) {
+
+                    except(t);
 
-                                   T result = request.perform(graph);
-                                   addOrSet(graph, result);
+                }
+
+            }
 
-                               } catch (Throwable t) {
+            @Override
+            public void removeEntry(QueryProcessor processor) {
+                processor.cache.remove(ReadEntry.this);
+            }
 
-                                       except(t);
-                    
+            @Override
+            public int type() {
+                if (request instanceof ReadExt) {
+                    return ((ReadExt) request).getType();
+                } else {
+                    return RequestFlags.INVALIDATE;
                 }
-                               
-                       }
-
-                       @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;
-                       }
-               
+            }
+
+            @Override
+            public String toString() {
+                if (request == null)
+                    return "DISCARDED";
+                else
+                    return request.toString() + statusOrException;
+            }
+
         };
-        
+
     }
-    
-    //@Override
-    public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
 
-               ReadGraphImpl queryGraph = graph.withParent(this);
+    public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry,
+            AsyncProcedure<T> procedure_) throws DatabaseException {
 
-               try {
+        AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
 
-                       addOrSet(queryGraph, request.perform(queryGraph));
-                       return get(queryGraph, procedure);
+        ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
 
-               }  catch (DatabaseException e) {
+        try {
 
-                       except(e);
-                       return get(graph, procedure);
+            T result = request.perform(queryGraph);
+            procedure.execute(graph, result);
 
-               }  catch (Throwable t) {
+        } catch (DatabaseException e) {
 
-                       except(new DatabaseException(t));
-                       return get(graph, procedure);
+            procedure.exception(graph, e);
 
-               }
-       
-    }
+        } catch (Throwable t) {
 
-    public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
-       
-               ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
+            DatabaseException dbe = new DatabaseException(t);
+            procedure.exception(graph, dbe);
 
-               try {
+        }
 
-                       T result = request.perform(queryGraph);
-                       if(entry != null) entry.addOrSet(queryGraph, result);
-                       procedure.execute(graph, result);
+        if (entry != null)
+            entry.performFromCache(queryGraph, procedure_);
 
-               }  catch (DatabaseException e) {
+    }
 
-                       if(entry != null) entry.except(e);
-                       procedure.exception(graph, e);
+    public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
 
-               }  catch (Throwable t) {
+        AsyncProcedure<T> proc = (AsyncProcedure<T>) procedure;
 
-                       DatabaseException dbe = new DatabaseException(t);
-                       if(entry != null) entry.except(dbe);
-                       procedure.exception(graph, dbe);
+        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();
 
-               }
-       
     }
-    
-       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();
-                       }
-               }
+
+    @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 (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;
-       }
+        return false;
+    }
+
+    @Override
+    public void execute(AsyncReadGraph graph, T result) {
+        setResult(result);
+        setReady();
+    }
+
+    @Override
+    public void exception(AsyncReadGraph graph, Throwable throwable) {
+        except(throwable);
+    }
 
 }