]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / TypeHierarchy.java
index 6a96e11eaf65f857789740f77caa5e22ec48de91..9da45946999098752d3a5322f1e13eb66874b046 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.procedure.TIntProcedure;
-
-import java.util.concurrent.Semaphore;
-
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.ListenerBase;
 
+import gnu.trove.procedure.TIntProcedure;
+
 final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
     
-//     public ArrayList<InternalProcedure<IntSet>> procs = null;
-       
-    private TypeHierarchy(final int resource) {
+    TypeHierarchy(final int resource) {
         super(resource);
     }
     
-    final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
-
-       TypeHierarchy entry = (TypeHierarchy)provider.cache.typeHierarchyMap.get(r);
-        if(entry == null) {
-
-               entry = new TypeHierarchy(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-               
-            if(!entry.isReady()) {
-               synchronized(entry) {
-                    if(!entry.isReady()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<IntSet>>();
-//                     entry.procs.add(procedure);
-//                                             provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                     return;
-                    }
-               }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-
-    }
-    
-    final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
-        
-        if(parent == null && listener == null) {
-               TypeHierarchy entry = (TypeHierarchy)provider.cache.typeHierarchyMap.get(r);
-               if(entry != null && entry.isReady()) { 
-                       entry.performFromCache(graph, provider, procedure);
-                       return;
-               }
-        }
-        
-        runner(graph, r, parent, provider, listener, procedure);
-         
+    final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+        QueryCache.runnerTypeHierarchy(graph, r, parent, listener, procedure);
     }
-       
-       @Override
-       public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.cache.typeHierarchyMap.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.typeHierarchyMap.put(id, this);
-       }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.cache.typeHierarchyMap.remove(id);
+               provider.cache.remove(this);
        }
 
        @Override
-       public IntSet computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {
+       public IntSet compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
 
-        final IntSet result = new IntSet(provider.querySupport, id);
+               QueryProcessor processor = graph.processor;
+               
+        final IntSet result = new IntSet(processor.querySupport, id);
         
         final TIntProcedure addToResult = new TIntProcedure() {
             @Override
@@ -99,19 +48,19 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
             }
         };
         
-        SuperTypes.queryEach(graph, id, provider, TypeHierarchy.this, null, new InternalProcedure<IntSet>() {
+        QueryCache.runnerSuperTypes(graph, id, TypeHierarchy.this, null, new InternalProcedure<IntSet>() {
 
                @Override
-               public void execute(ReadGraphImpl graph, IntSet types) {
+               public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
 
                        types.forEach(addToResult);
-                       addOrSet(graph, result, provider);
+                       addOrSet(graph, result, processor);
                        procedure.execute(graph, result); 
 
                }
 
                @Override
-               public void exception(ReadGraphImpl graph, Throwable t) {
+               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                        procedure.exception(graph, t);
                }
 
@@ -130,29 +79,18 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
        
         assert(!isReady());
 
-//        ArrayList<InternalProcedure<IntSet>> p = null;
-
         synchronized(this) {
         
             value.trim();
             setResult(value);
             setReady();
-//            p = procs;
-//            procs = null; 
         
         }
-
-//        if(p != null) {
-//             IntSet v = (IntSet)getResult();
-//             if(v != null) {
-//                 for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
-//             }
-//        }
         
     }
 
     @Override
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
+    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
        
        assert(isReady());
 
@@ -167,33 +105,26 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
-
-        computeForEach(graph, provider, new InternalProcedure<IntSet>() {
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
+    
+        compute(graph, new InternalProcedure<IntSet>() {
 
                @Override
                public void execute(ReadGraphImpl graph, IntSet result) {
-                s.release();
                }
 
             @Override
             public void exception(ReadGraphImpl graph, Throwable t) {
-               s.release();
                 new Error("Error in recompute.", t).printStackTrace();
             }
 
-        }, true);
-
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+        });
         
     }
     
     @Override
     boolean isImmutable(ReadGraphImpl graph) {
        return graph.processor.isImmutable(id);
-    }    
+    }
+
 }