]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PrincipalTypes.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / PrincipalTypes.java
index af4d20ebca440dca89c2cdedef24bf11248f02f4..a5a4a89970aacb1a0c3dcc28afdb58279f0a6b50 100644 (file)
@@ -13,19 +13,17 @@ package org.simantics.db.impl.query;
 
 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;
-import org.simantics.db.procedure.ListenerBase;
 
 import gnu.trove.procedure.TIntProcedure;
 import gnu.trove.set.hash.TIntHashSet;
 
-final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
+final public class PrincipalTypes extends CollectionUnaryQuery {
 
        PrincipalTypes(final int resource) {
                super(resource);
@@ -75,26 +73,34 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
        }
 
-       //@Override
-       public Object compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
-               return computeForEach(graph, id, this, proc);
+       @Override
+       public void compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
+               computeForEach(graph, id, this, proc);
        }
        
-       public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException {
+       public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure procedure_) throws DatabaseException {
+
+           IntProcedure procedure = entry != null ? entry : procedure_;
+           
+           Object result = computeForEach2(graph, id, entry, procedure);
+           
+           if(entry != null) entry.performFromCache(graph, procedure_);
+           
+           return result;
+           
+       }
+           
+       public static Object computeForEach2(final ReadGraphImpl graph, final int id, final PrincipalTypes parent, final IntProcedure procedure) throws DatabaseException {
 
-               QueryProcessor provider = graph.processor;
+           QueryProcessor provider = graph.processor;
                
                provider.querySupport.ensureLoaded(graph, id);
                assert(id != 0);
 
                int ret = provider.querySupport.getSingleInstance(id);
                if(ret > 0) {
-                       if(entry != null) {
-                               entry.add(ret);
-                               entry.finish(graph, provider);
-                       }
-                       proc.execute(graph, ret);
-                       proc.finished(graph);
+                       procedure.execute(graph, ret);
+                       procedure.finished(graph);
                        return ret;
                }
 
@@ -114,7 +120,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                               proc.exception(graph, t);
+                               procedure.exception(graph, t);
                        }
 
                        @Override
@@ -132,7 +138,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                               proc.exception(graph, t);
+                               procedure.exception(graph, t);
                        }
 
                        @Override
@@ -148,26 +154,19 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                if(indirect.size() == 0) {
                        int size = material.size(); 
                        if(size == 0) {
-                               if(entry != null) entry.finish(graph, provider);
-                               proc.finished(graph);
+                               procedure.finished(graph);
                                return null;
                        } else if(size == 1) {
                                int single = material.single; 
-                               if(entry != null) {
-                                       entry.add(single);
-                                       entry.finish(graph, provider);
-                               }
-                               proc.execute(graph, single);
-                               proc.finished(graph);
+                               procedure.execute(graph, single);
+                               procedure.finished(graph);
                                return single;
                        } else {
-                               addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
+                               addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
                                return null;
                        }
                }
 
-//             final AtomicInteger finishes = new AtomicInteger(0);
-
                indirect.forEach(new TIntProcedure() {
 
                        int finishes = 0;
@@ -190,27 +189,22 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                                        if(current == indirect.size()) {
                                                int size = material.size(); 
                                                if(size == 0) {
-                                                       if(entry != null) entry.finish(graph, provider);
-                                                       proc.finished(graph);
+                                                       procedure.finished(graph);
                                                        return true;
                                                } else if(size == 1) {
                                                        int single = material.single; 
-                                                       if(entry != null) {
-                                                               entry.add(single);
-                                                               entry.finish(graph, provider);
-                                                       }
-                                                       proc.execute(graph, single);
-                                                       proc.finished(graph);
+                                                       procedure.execute(graph, single);
+                                                       procedure.finished(graph);
                                                        return true;
                                                } else {
-                                                       addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
+                                                       addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
                                                        return true;
                                                }
                                        }
                                        return true;
                                }
 
-                               QueryCache.runnerPrincipalTypes(graph, arg0, entry, null, new IntProcedure() {
+                               QueryCache.runnerPrincipalTypes(graph, arg0, parent, null, new IntProcedure() {
 
                                        @Override
                                        public void execute(ReadGraphImpl graph, int i) {
@@ -226,20 +220,15 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                                                if(current == indirect.size()) {
                                                        int size = material.size(); 
                                                        if(size == 0) {
-                                                               if(entry != null) entry.finish(graph, provider);
-                                                               proc.finished(graph);
+                                                               procedure.finished(graph);
                                                                return;
                                                        } else if(size == 1) {
                                                                int single = material.single; 
-                                                               if(entry != null) {
-                                                                       entry.add(single);
-                                                                       entry.finish(graph, provider);
-                                                               }
-                                                               proc.execute(graph, single);
-                                                               proc.finished(graph);
+                                                               procedure.execute(graph, single);
+                                                               procedure.finished(graph);
                                                                return;
                                                        } else {
-                                                               addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
+                                                               addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
                                                                return;
                                                        }
                                                }
@@ -248,7 +237,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
                                        @Override
                                        public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                               proc.exception(graph, t);
+                                               procedure.exception(graph, t);
                                        }
 
                                });
@@ -263,16 +252,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) throws DatabaseException {
-
-               if(entry != null) {
-                       for(int i : material) {
-                               if(!rejects.contains(i)) {
-                                       entry.add(i);
-                               }
-                       }
-                       entry.finish(graph, provider);
-               }
+       private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final IntProcedure proc) throws DatabaseException {
 
                for(int i : material) {
                        if(!rejects.contains(i)) {
@@ -286,26 +266,25 @@ 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) throws DatabaseException {
-
-               //        if((counter++ % 500) == 0) System.out.println("PT " + counter + " mat = " + material.length);
+       private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes parent, final IntProcedure proc) throws DatabaseException {
 
                if(index == material.length) { 
-                       finish(graph, rejects, material, entry, provider, proc);
+                       finish(graph, rejects, material, proc);
                        return;
                }
 
                int type = material[index++];
                while(rejects.contains(type)) {
                        if(index == material.length) { 
-                               finish(graph, rejects, material, entry, provider, proc);
+                               finish(graph, rejects, material, proc);
                                return;
                        }
                        type = material[index++];
                }
+               
                final int nextIndex = index;
 
-               QueryCache.runnerSuperTypes(graph, type, entry, null, new InternalProcedure<IntSet>() {
+               QueryCache.runnerSuperTypes(graph, type, parent, null, new InternalProcedure<IntSet>() {
 
                        @Override
                        public void execute(ReadGraphImpl graph, IntSet supers) throws DatabaseException {
@@ -324,7 +303,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
                                }
 
-                               addPrincipalType(graph, rejects, material, nextIndex, provider, entry, proc);
+                               addPrincipalType(graph, rejects, material, nextIndex, provider, parent, proc);
 
                        }
 
@@ -341,65 +320,6 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
        public String toString() {
                return "PrincipalTypes[" + id + "]";
        }
-
-       final private void add(int val) {
-               assert(isPending());
-               IntArray v = (IntArray)getResult();
-               v.add(val);
-       }
-
-       final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
-
-               assert(isPending());
-               synchronized(this) {
-                       setReady();
-               }
-
-       }
-
-       @Override
-       public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
-
-               assert(isReady());
-
-       if(handleException(graph, procedure)) return EXCEPTED;
-               
-               final IntArray value = (IntArray)getResult();
-               if(value.data == null) {
-                       if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
-               } else {
-                       for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
-               }
-
-               procedure.finished(graph);
-               
-               return getResult();
-
-       }
-
-       @Override
-       public void recompute(ReadGraphImpl graph) throws DatabaseException {
-
-               compute(graph, new IntProcedureAdapter() {
-
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               new Error("Error in recompute.", t).printStackTrace();
-                       }
-
-               });
-
-       }
-
-
-    @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(id);
-    }
     
     @Override
     protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {