]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Types.java
index 512045e17be63f354fd12a41a9d87cfd17d1afca..35c43885adc83161416041709b83e10f5abcb6d2 100644 (file)
@@ -19,154 +19,156 @@ import org.simantics.db.impl.procedure.InternalProcedure;
 
 import gnu.trove.procedure.TIntProcedure;
 
-final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
-       
+final public class Types extends UnaryQueryP<IntSet> {
+
     Types(final int resource) {
         super(resource);
     }
 
-       @Override
-       final public void removeEntry(QueryProcessor provider) {
-               provider.cache.remove(this);
-       }
+    @Override
+    final public void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
+    }
 
-       //@Override
-       public Object compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
-               computeForEach(graph, id, this, procedure);
-               return getResult();
-       }
+    @Override
+    public void compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+        computeForEach(graph, id, this, procedure);
+    }
 
-       public static void computeForEach(final ReadGraphImpl graph, int id, Types entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+    public static void computeForEach(final ReadGraphImpl graph, int id, Types entry,
+            final InternalProcedure<IntSet> procedure_) throws DatabaseException {
 
-               assert(procedure != null);
+        InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
+
+        computeForEach2(graph, id, entry, procedure);
+
+        if (entry != null)
+            entry.performFromCache(graph, procedure_);
+
+    }
 
-               QueryProcessor processor = graph.processor;
+    public static void computeForEach2(final ReadGraphImpl graph, int id, Types parent,
+            final InternalProcedure<IntSet> procedure) throws DatabaseException {
 
-               processor.querySupport.ensureLoaded(graph, id);
-               
-               int ret = processor.querySupport.getSingleInstance(id);
-               if(ret > 0) {
+        QueryProcessor processor = graph.processor;
 
-                       TypeHierarchy.queryEach(graph, ret, processor, entry, null, new InternalProcedure<IntSet>() {
+        processor.querySupport.ensureLoaded(graph, id);
 
-                               @Override
-                               public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
+        int ret = processor.querySupport.getSingleInstance(id);
+        if (ret > 0) {
 
-                                       if(entry != null) {
-                                               entry.addOrSet(graph, types, processor);
-                                               entry.finish();
-                                       }
-                                       procedure.execute(graph, types);
+            TypeHierarchy.queryEach(graph, ret, processor, parent, null, new InternalProcedure<IntSet>() {
 
-                               }
+                @Override
+                public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
+                    procedure.execute(graph, types);
+                }
 
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       procedure.exception(graph, t);
-                               }
+                @Override
+                public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                    procedure.exception(graph, t);
+                }
 
-                       });
-                       
-                       return;
+            });
 
-               }
-               
-               final int instanceOf = processor.getInstanceOf();
+            return;
+
+        }
+
+        final int instanceOf = processor.getInstanceOf();
         final int inherits = processor.getInherits();
         final int subrelationOf = processor.getSubrelationOf();
 
         final IntSet result = new IntSet(processor.querySupport);
-        
+
         final TIntProcedure addToResult = new TIntProcedure() {
             @Override
             public boolean execute(int r) {
-               synchronized(result) {
-                       result.add(r);
-               }
+                synchronized (result) {
+                    result.add(r);
+                }
                 return true;
             }
         };
-        
+
         final AtomicInteger finishes = new AtomicInteger(0);
-        
+
         SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
-            
+
             @Override
             public void run(ReadGraphImpl graph) throws DatabaseException {
-               
-                if(finishes.addAndGet(1) == 3) {
-                    if(entry != null) entry.addOrSet(graph, result, processor);
+
+                if (finishes.addAndGet(1) == 3) {
                     procedure.execute(graph, result);
-               }
-                
+                }
+
             }
-            
+
             @Override
             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                
-               result.add(i);
-                
+
+                result.add(i);
+
                 inc();
 
-                QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerSuperTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
 
                     @Override
                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
                         types.forEach(addToResult);
                         dec(graph);
                     }
-                               
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       procedure.exception(graph, t);
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                        procedure.exception(graph, t);
                         dec(graph);
-                   }
+                    }
 
                 });
-                
+
             }
 
             @Override
             public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
-            
-        }; 
-        
+
+        };
+
         SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
-            
+
             @Override
             public void run(ReadGraphImpl graph) throws DatabaseException {
 
-               int current = finishes.addAndGet(1);
-               if(current == 3) {
-                    if(entry != null) entry.addOrSet(graph, result, processor);
+                int current = finishes.addAndGet(1);
+                if (current == 3) {
                     procedure.execute(graph, result);
-               }
-                
+                }
+
             }
-            
+
             @Override
             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                
+
                 inc();
 
-                QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
 
                     @Override
                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
                         types.forEach(addToResult);
                         dec(graph);
                     }
-                               
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       procedure.exception(graph, t);
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                        procedure.exception(graph, t);
                         dec(graph);
-                   }
+                    }
 
                 });
-                
+
             }
 
             @Override
@@ -175,124 +177,67 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
                 dec(graph);
 
             }
-            
-        }; 
+
+        };
 
         SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
 
             @Override
             public void run(ReadGraphImpl graph) throws DatabaseException {
 
-               int current = finishes.addAndGet(1);
-               if(current == 3) {
-                    if(entry != null) entry.addOrSet(graph, result, processor);
+                int current = finishes.addAndGet(1);
+                if (current == 3) {
                     procedure.execute(graph, result);
-               }
-                
+                }
+
             }
-            
+
             @Override
             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                
+
                 inc();
-                
-                QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
+
+                QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
 
                     @Override
                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
 
                         types.forEach(addToResult);
                         dec(graph);
-                        
+
                     }
-                               
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       procedure.exception(graph, t);
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                        procedure.exception(graph, t);
                         dec(graph);
-                   }
+                    }
 
                 });
-                
+
             }
 
             @Override
             public void finished(ReadGraphImpl graph) throws DatabaseException {
-                
+
                 dec(graph);
 
             }
-            
-        }; 
-        
+
+        };
+
         processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
         instanceOfProcedure.finished(graph);
         processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
         inheritsProcedure.finished(graph);
         processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
         subrelationOfProcedure.finished(graph);
-        
-        if(entry != null) entry.finish();
-        
-    }
-    
-    @Override
-    public String toString() {
-       return "Types[" + id + "]";
-    }
-    
-    private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
-       
-        assert(!isReady());
 
-        setResult(value);
-        
     }
-    
-    void finish() {
 
-       IntSet result = getResult(); 
-       result.trim();
-       setReady();
-       
-    }
-    
-    @Override
-    final public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
-        
-       assert(isReady());
-       
-       if(handleException(graph, procedure)) return EXCEPTED;
-       
-       IntSet result = getResult();
-       
-        procedure.execute(graph, result);
-        
-        return result;
-        
-    }
-    
     @Override
-    public void recompute(ReadGraphImpl graph) throws DatabaseException {
-
-        compute(graph, new InternalProcedure<IntSet>() {
-
-               @Override
-               public void execute(ReadGraphImpl graph, IntSet result) {
-               }
-
-            @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
-                new Error("Error in recompute.", t).printStackTrace();
-            }
-
-        });
-        
+    public String toString() {
+        return "Types[" + id + "]";
     }
 
-    @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(id);
-    }
-    
 }