]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / SuperTypes.java
index 1fce568707564691fe7420ef072af0c8f0b7924e..06fabb2d7380c73a803948912bd899727e88f247 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 SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
        
-    private SuperTypes(final int resource) {
+    SuperTypes(final int resource) {
         super(resource);
     }
     
-    final static SuperTypes runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
-
-       SuperTypes entry = (SuperTypes)provider.cache.superTypesMap.get(r);
-        if(entry == null) {
-               
-               entry = new SuperTypes(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-            return entry;
-            
-        } 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 entry;
-                    }
-               }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-        
-        return entry;
-
-    }
-    
-    final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
-    
-        if(parent == null && listener == null) {
-               SuperTypes entry = (SuperTypes)provider.cache.superTypesMap.get(r);
-               if(entry != null && entry.isReady()) { 
-                       entry.performFromCache(graph, provider, procedure);
-                       return entry;
-               }
-        }
-        
-        return runner(graph, r, parent, provider, listener, procedure);
-         
-    }
+//    final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+//        return QueryCache.runnerSuperTypes(graph, r, parent, listener, procedure);
+//    }
        
        @Override
-       public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.cache.superTypesMap.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.superTypesMap.put(id, this);
+       final public void removeEntry(QueryProcessor provider) {
+               provider.cache.remove(this);
        }
 
        @Override
-       final public void removeEntry(QueryProcessor provider) {
-               provider.cache.superTypesMap.remove(id);
+       public Object compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+               return computeForEach(graph, id, this, procedure);
+       }
+       
+       private static void addOrSet(ReadGraphImpl graph, SuperTypes entry, IntSet value) {
+               if(entry != null) {
+                       entry.addOrSet(graph, value, graph.processor);
+               }
        }
+       
+       public static Object computeForEach(ReadGraphImpl graph, int r, SuperTypes entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
 
-       @Override
-       public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {
+               QueryProcessor provider = graph.processor;
 
         final int inherits = provider.getInherits();
-        
-        final CacheEntry parent = graph.parent;
-        
-        assert(graph.parent != this);
 
         final IntSet result = new IntSet(provider.querySupport);
         
@@ -109,20 +62,20 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
             }
         };
 
-        DirectObjects.queryEach(graph, id, inherits, provider, this, null, new SyncIntProcedure() {
+        QueryCache.runnerDirectObjects(graph, r, inherits, entry, null, new SyncIntProcedure() {
             
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
 
-                addOrSet(graph, result, provider);
+                addOrSet(graph, entry, result);
                 procedure.execute(graph, result);
                 
             }
             
             @Override
-            public void execute(ReadGraphImpl graph, final int i) {
+            public void execute(ReadGraphImpl graph, final int i) throws DatabaseException {
                 
-               assert(graph.parent == parent);
+//             assert(graph.parent == parent);
                
                synchronized(result) {
                        result.add(i);
@@ -130,10 +83,10 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
                 
                 inc();
 
-                SuperTypes.queryEach(graph, i, provider, SuperTypes.this, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, IntSet types) {
+                    public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
 
                         types.forEach(addToResult);
                         dec(graph);
@@ -141,7 +94,7 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        procedure.exception(graph, t);
                    }
 
@@ -150,7 +103,7 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
             
@@ -169,29 +122,18 @@ final public class SuperTypes 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());
 
@@ -206,28 +148,20 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-        computeForEach(graph, provider, new InternalProcedure<IntSet>() {
+        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);
-        }
+        });
         
     }