]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Types.java
index 0f49188506099bfb4a13c64faffb05daea1c02c3..96afa8c4f586bd8fb83135e4eca2c4803542c3f8 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.procedure.TIntProcedure;
-
-import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicInteger;
 
+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 Types extends UnaryQuery<InternalProcedure<IntSet>> {
-    
-//     public ArrayList<InternalProcedure<IntSet>> procs;
        
-    private Types(final int resource) {
+    Types(final int resource) {
         super(resource);
     }
-    
-    final static Types entry(final QueryProcessor provider, final int r) {
-        return (Types)provider.cache.typesMap.get(r);
-    }
-    
-   final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, Types cached, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
-
-       Types entry = cached != null ? cached : (Types)provider.cache.typesMap.get(r); 
-        if(entry == null) {
-               
-               entry = new Types(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-
-               if(!entry.isReady()) {
-                   throw new IllegalStateException();
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-       
-    }
-    
-    final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
-
-       Types entry = (Types)provider.cache.typesMap.get(r); 
-        if(entry == null) {
-               
-               entry = new Types(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
-            
-        } else {
-
-               if(!entry.isReady()) {
-                   throw new IllegalStateException();
-            }
-            return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
-            
-        }
-       
-    }
-    
-    final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
-        
-       final Types entry = (Types)provider.cache.typesMap.get(r);
-       
-       if(parent == null && listener == null) {
-               if(entry != null && entry.isReady()) { 
-                       entry.performFromCache(graph, provider, procedure);
-                       return;
-               }
-        }
-       
-        runner(graph, r, provider, entry, parent, listener, procedure);
-         
-    }
 
-    final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
-        
-        if(parent == null) {
-            Types entry = (Types)provider.cache.typesMap.get(r);
-            if(entry != null && entry.isReady()) {
-                return (IntSet)entry.get(graph, provider, null);
-            }
-        }
-        
-        return runner2(graph, r, provider, parent);
-         
-    }
-    
        @Override
-       public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.cache.typesMap.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.typesMap.put(id, this);
+       final public void removeEntry(QueryProcessor provider) {
+               provider.cache.remove(this);
        }
 
        @Override
-       final public void removeEntry(QueryProcessor provider) {
-               provider.cache.typesMap.remove(id);
+       public Object compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+               computeForEach(graph, id, this, procedure);
+               return getResult();
        }
 
-       @Override
-       public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor queryProvider, final InternalProcedure<IntSet> procedure, final boolean store) {
+       public static void computeForEach(final ReadGraphImpl graph, int id, Types entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+
+               if(entry != null)
+                       if(entry.isReady())
+                               System.err.println("asd");
+               
+               assert(procedure != null);
 
-               queryProvider.querySupport.ensureLoaded(graph, id);
-               int ret = queryProvider.querySupport.getSingleInstance(id);
+               QueryProcessor processor = graph.processor;
+
+               processor.querySupport.ensureLoaded(graph, id);
+               
+               int ret = processor.querySupport.getSingleInstance(id);
                if(ret > 0) {
 
-                       TypeHierarchy.queryEach(graph, ret, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+                       TypeHierarchy.queryEach(graph, ret, processor, entry, null, new InternalProcedure<IntSet>() {
 
                                @Override
-                               public void execute(ReadGraphImpl graph, IntSet types) {
+                               public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
 
-                                       addOrSet(graph, types, queryProvider);
+                                       if(entry != null) entry.addOrSet(graph, types, processor);
                                        procedure.execute(graph, types);
 
                                }
 
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        procedure.exception(graph, t);
                                }
 
                        });
 
-                       return getResult();
-
                }
                
-               final int instanceOf = queryProvider.getInstanceOf();
-        final int inherits = queryProvider.getInherits();
-        final int subrelationOf = queryProvider.getSubrelationOf();
+               final int instanceOf = processor.getInstanceOf();
+        final int inherits = processor.getInherits();
+        final int subrelationOf = processor.getSubrelationOf();
 
-        final IntSet result = new IntSet(queryProvider.querySupport);
+        final IntSet result = new IntSet(processor.querySupport);
         
         final TIntProcedure addToResult = new TIntProcedure() {
             @Override
@@ -169,17 +91,17 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
         SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
             
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
                
                 if(finishes.addAndGet(1) == 3) {
-                    if(store) addOrSet(graph, result, queryProvider);
+                    if(entry != null) entry.addOrSet(graph, result, processor);
                     procedure.execute(graph, result);
                }
                 
             }
             
             @Override
-            public void execute(ReadGraphImpl graph, int i) {
+            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                 
                synchronized(result) {
                        result.add(i);
@@ -187,16 +109,16 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
                 
                 inc();
 
-                SuperTypes.queryEach(graph, i, queryProvider, store ? Types.this : null, 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);
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        procedure.exception(graph, t);
                         dec(graph);
                    }
@@ -206,7 +128,7 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
             
@@ -215,31 +137,31 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
         SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
             
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
 
                int current = finishes.addAndGet(1);
                if(current == 3) {
-                    if(store) addOrSet(graph, result, queryProvider);
+                    if(entry != null) entry.addOrSet(graph, result, processor);
                     procedure.execute(graph, result);
                }
                 
             }
             
             @Override
-            public void execute(ReadGraphImpl graph, int i) {
+            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                 
                 inc();
 
-                Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerTypes(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);
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        procedure.exception(graph, t);
                         dec(graph);
                    }
@@ -249,7 +171,7 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
 
                 dec(graph);
 
@@ -260,25 +182,25 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
         SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
 
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
 
                int current = finishes.addAndGet(1);
                if(current == 3) {
-                    if(store) addOrSet(graph, result, queryProvider);
+                    if(entry != null) entry.addOrSet(graph, result, processor);
                     procedure.execute(graph, result);
                }
                 
             }
             
             @Override
-            public void execute(ReadGraphImpl graph, int i) {
+            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                 
                 inc();
                 
-                Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerTypes(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);
@@ -286,7 +208,7 @@ final public class Types 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);
                         dec(graph);
                    }
@@ -296,7 +218,7 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 
                 dec(graph);
 
@@ -304,14 +226,14 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
             
         }; 
         
-        queryProvider.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
+        processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
         instanceOfProcedure.finished(graph);
-        queryProvider.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
+        processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
         inheritsProcedure.finished(graph);
-        queryProvider.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
+        processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
         subrelationOfProcedure.finished(graph);
         
-        return result;
+        if(entry != null) entry.finish();
         
     }
     
@@ -324,18 +246,20 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
        
         assert(!isReady());
 
-        synchronized(this) {
-        
-            value.trim();
-            setResult(value);
-            setReady();
-        
-        }
+        setResult(value);
         
     }
     
+    void finish() {
+
+       IntSet result = getResult(); 
+       result.trim();
+       setReady();
+       
+    }
+    
     @Override
-    final public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
+    final public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
         
        assert(isReady());
        
@@ -350,28 +274,20 @@ final public class Types 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);
-        }
+        });
         
     }