]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PrincipalTypes.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / PrincipalTypes.java
index 60fe074115e44aceb77d4b92a5a7b17a50724c4a..81c0efc20737d50e862f0d000bdf445488b871f5 100644 (file)
@@ -15,6 +15,8 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.concurrent.Semaphore;
 
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.IntProcedureAdapter;
 import org.simantics.db.impl.procedure.InternalProcedure;
@@ -25,71 +27,13 @@ import gnu.trove.set.hash.TIntHashSet;
 
 final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
-       private PrincipalTypes(final int resource) {
+       PrincipalTypes(final int resource) {
                super(resource);
        }
 
-       final static PrincipalTypes entry(final QueryProcessor provider, final int r) {
-               return (PrincipalTypes)provider.cache.principalTypesMap.get(r);
-       }
-
-       final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-
-       QueryProcessor processor = graph.processor;
-               
-               PrincipalTypes entry = (PrincipalTypes)processor.cache.principalTypesMap.get(r); 
-               if(entry == null) {
-
-                       entry = new PrincipalTypes(r);
-               entry.setPending();
-               entry.clearResult(processor.querySupport);
-               entry.putEntry(processor);
-                       
-               processor.performForEach(graph, entry, parent, listener, procedure);
-                       
-               } else {
-                       
-            if(entry.isPending()) {
-                synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-//                                             if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>(1);
-//                                             entry.procs.add(procedure);
-//                                             processor.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                                             return;
-                                       }
-                               }
-                       }
-            processor.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 IntProcedure procedure) {
-
-               assert(r != 0);
-
-        if(parent == null && listener == null) {
-               PrincipalTypes.computeForEach(graph, r, null, graph.processor, procedure);
-        } else {
-               runner(graph, r, parent, listener, procedure);
-        }
-
-       }
-
-       @Override
-       public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return provider.cache.principalTypesMap.get(id);
-       }
-
-       @Override
-       public void putEntry(QueryProcessor provider) {
-               provider.cache.principalTypesMap.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.cache.principalTypesMap.remove(id);
+               provider.cache.remove(this);
        }
 
        static class Koss {
@@ -132,13 +76,13 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
        }
 
        @Override
-       public Object computeForEach(final ReadGraphImpl procedureGraph, final QueryProcessor provider, final IntProcedure proc, final boolean store) {
-
-               return computeForEach(procedureGraph, id, this, provider, proc);
-               
+       public Object compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
+               return computeForEach(graph, id, this, proc);
        }
        
-       public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) {
+       public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException {
+
+               QueryProcessor provider = graph.processor;
                
                provider.querySupport.ensureLoaded(graph, id);
                assert(id != 0);
@@ -169,7 +113,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                        }
 
                        @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
+                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                proc.exception(graph, t);
                        }
 
@@ -187,7 +131,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                        }
 
                        @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
+                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                proc.exception(graph, t);
                        }
 
@@ -230,6 +174,15 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                        
                        @Override
                        public boolean execute(final int arg0) {
+                               try {
+                                       return execute0(arg0);
+                               } catch (DatabaseException e) {
+                                       Logger.defaultLogError(e);
+                               }
+                               return false;
+                       }
+                       
+                       public boolean execute0(final int arg0) throws DatabaseException {
 
                                // No self-loops!
                                if(arg0 == id) {
@@ -257,7 +210,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                                        return true;
                                }
 
-                               PrincipalTypes.queryEach(graph, arg0, provider, entry, null, new IntProcedure() {
+                               QueryCache.runnerPrincipalTypes(graph, arg0, entry, null, new IntProcedure() {
 
                                        @Override
                                        public void execute(ReadGraphImpl graph, int i) {
@@ -267,7 +220,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                                        }
 
                                        @Override
-                                       public void finished(ReadGraphImpl graph) {
+                                       public void finished(ReadGraphImpl graph) throws DatabaseException {
 
                                                int current = (++finishes);
                                                if(current == indirect.size()) {
@@ -294,7 +247,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                                        }
 
                                        @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                proc.exception(graph, t);
                                        }
 
@@ -310,7 +263,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
        }
 
-       private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) {
+       private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) throws DatabaseException {
 
                if(entry != null) {
                        for(int i : material) {
@@ -333,7 +286,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
        }
 
-       private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes entry, final IntProcedure proc) {
+       private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException {
 
                //        if((counter++ % 500) == 0) System.out.println("PT " + counter + " mat = " + material.length);
 
@@ -352,10 +305,10 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                }
                final int nextIndex = index;
 
-               SuperTypes.queryEach(graph, type, provider, entry, null, new InternalProcedure<IntSet>() {
+               QueryCache.runnerSuperTypes(graph, type, entry, null, new InternalProcedure<IntSet>() {
 
                        @Override
-                       public void execute(ReadGraphImpl graph, IntSet supers) {
+                       public void execute(ReadGraphImpl graph, IntSet supers) throws DatabaseException {
 
                                synchronized(rejects) {
 
@@ -376,7 +329,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                        }
 
                        @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
+                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                proc.exception(graph, t);
                        }
 
@@ -398,41 +351,14 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
        final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
 
                assert(isPending());
-
-//             ArrayList<IntProcedure> p = null;
-
                synchronized(this) {
-
                        setReady();
-//                     p = procs;
-//                     procs = null;
-
                }
 
-//             if(p != null) {
-//
-//                     IntArray v = (IntArray)getResult();
-//                     if(v != null) {
-//                             if(v.data == null) {
-//                                     if(v.sizeOrData != IntArray.NO_DATA) {
-//                                             for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);
-//                                     }
-//                             } else {
-//                                     for(IntProcedure proc : p) {
-//                                             for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);
-//                                     }
-//                             }
-//                     }
-//
-//                     for(IntProcedure proc : p) proc.finished(graph);
-//
-//             }
-
-
        }
 
        @Override
-       public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
+       public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
 
                assert(isReady());
 
@@ -452,28 +378,20 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
        }
 
        @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 IntProcedureAdapter() {
+               compute(graph, new IntProcedureAdapter() {
 
                        @Override
                        public void finished(ReadGraphImpl graph) {
-                               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);
-       }
+               });
 
        }
 
@@ -484,8 +402,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
     }
     
     @Override
-    protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry> result) {
-//             for(Objects o : Objects.entries(processor, id)) result.add(o);
+    protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {
     }
 
 }