]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/RelationInfoQuery.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / RelationInfoQuery.java
index d55768aaa1a04cea220b146882cdf614e0353fd9..b0a2a1198e2dd304692717b1329ff5a5d7678796 100644 (file)
@@ -17,7 +17,7 @@ import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.request.RequestFlags;
 
-final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<RelationInfo>> {
+final public class RelationInfoQuery extends UnaryQueryP<RelationInfo> {
 
        RelationInfoQuery(final int resource) {
         super(resource);
@@ -28,50 +28,48 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
                provider.cache.remove(this);
        }
 
-       private static void computeAssertions(ReadGraphImpl graph, int r, final boolean isFinal, final boolean isFunctional, RelationInfoQuery entry, final InternalProcedure<RelationInfo> proc) throws DatabaseException {
+       private static void computeAssertions(ReadGraphImpl graph, int r, final boolean isFinal, final boolean isFunctional, RelationInfoQuery parent, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
 
                QueryProcessor processor = graph.processor;
                
            final int isUsedInAssertion = processor.getHasPredicateInverse();
         assert(isUsedInAssertion != 0);
                
-        QueryCache.runnerDirectObjects(graph, r, isUsedInAssertion, entry, null, new IntProcedure() {
+        QueryCache.runnerDirectObjects(graph, r, isUsedInAssertion, parent, null, new IntProcedure() {
 
                boolean done = false;
                        
                        @Override
                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                               if(done) return;
                                done = true;
                                RelationInfo result = new RelationInfo(r, isFunctional, isFinal, true);
-                               if(entry != null) entry.setResult(result);
-                               proc.execute(graph, result);
+                               procedure.execute(graph, result);
                        }
 
                        @Override
                        public void finished(ReadGraphImpl graph) throws DatabaseException {
-                               if(!done) {
-                                       done = true;
-                                       RelationInfo result = new RelationInfo(r, isFunctional, isFinal, false);
-                                       if(entry != null) entry.setResult(result);
-                                       proc.execute(graph, result);
-                               }
+                               if(done) return;
+                               done = true;
+                               RelationInfo result = new RelationInfo(r, isFunctional, isFinal, false);
+                               procedure.execute(graph, result);
                        }
 
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
-                               if(!done) {
-                                       done = true;
-                                       DatabaseException e = new DatabaseException("Internal error in RelationInfoQuery");
-                                       if(entry != null) entry.except(e);
-                                       proc.exception(graph, e);
-                               }
+                               if(done) return;
+                               done = true;
+                               DatabaseException e = new DatabaseException("Internal error in RelationInfoQuery");
+                               procedure.exception(graph, e);
                        }
                        
                });
                
        }
        
-       public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
+       public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure<RelationInfo> procedure_) throws DatabaseException {
+           
+           InternalProcedure<RelationInfo> procedure = entry != null ? entry : procedure_;
                
                QueryProcessor provider = graph.processor;
                
@@ -82,77 +80,30 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
         IntSet types = QueryCache.resultTypes(graph, r, entry, null);
         
        computeAssertions(graph, r, !direct.contains(superRelationOf), types.contains(graph.processor.getFunctionalRelation()), entry, procedure);
+       
+       if(entry != null) entry.performFromCache(graph, procedure_);
         
        }
 
        @Override
-       public Object compute(ReadGraphImpl graph, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
+       public void compute(ReadGraphImpl graph, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
                computeForEach(graph, id, this, procedure);
-               return getResult();
        }
     
     @Override
     public String toString() {
        return "RelationInfoQuery[" + id + "]";
     }
-
-//    public void addOrSet(ReadGraphImpl graph, final RelationInfo result, final QueryProcessor provider) {
-//        
-//     assert(isPending());
-//     
-//     synchronized(this) {
-//
-//         setResult(result);
-//         setReady();
-//             
-//     }
-//        
-//    }
     
     @Override
     public void setResult(Object result) {
        super.setResult(result);
-       if(!(result instanceof RelationInfo) && !(result == NO_RESULT))
-               System.err.println("foo");
        setReady();
     }
-
-    @Override
-    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
-
-        assert(isReady());
-        
-       if(handleException(graph, procedure)) return EXCEPTED;
-        
-       RelationInfo result = getResult();
-       
-        procedure.execute(graph, result);
-        
-        return result;
-
-    }
     
     @Override
     public int type() {
         return RequestFlags.IMMEDIATE_UPDATE;
     }
-
-       @Override
-       public void recompute(ReadGraphImpl graph) throws DatabaseException {
-               
-        compute(graph, new InternalProcedure<RelationInfo>() {
-
-            @Override
-            public void execute(ReadGraphImpl graph, RelationInfo result) {
-            }
-            
-            @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
-                throw new Error("Error in recompute.", t);
-            }
-
-        });
-               
-       }
     
 }