]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Objects.java
index 1883d9c1f52e67d647e4c011c628c2aa38f4c47a..4d5279c51534fce6b7177567e9fba1bdfdf33099 100644 (file)
@@ -14,7 +14,6 @@ package org.simantics.db.impl.query;
 import org.simantics.db.RelationInfo;
 import org.simantics.db.Resource;
 import org.simantics.db.common.exception.DebugException;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
@@ -23,8 +22,6 @@ import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.AsyncMultiProcedure;
 import org.simantics.db.request.RequestFlags;
 
-import gnu.trove.procedure.TIntProcedure;
-
 /*
  * Size analysis:
  * java 8 byte
@@ -38,7 +35,7 @@ import gnu.trove.procedure.TIntProcedure;
  * 
  */
 
-final public class Objects extends CollectionBinaryQuery<IntProcedure> {
+final public class Objects extends CollectionBinaryQuery<IntProcedure> implements IntProcedure {
 
        public Objects(final int r1, final int r2) {
                super(r1, r2);
@@ -117,33 +114,21 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
        }
 
-       final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException {
+       final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final IntProcedure procedure) throws DatabaseException {
 
-               IntArray map = getAssertionMap(graph, r1, r2, entry);
+               IntArray map = getAssertionMap(graph, r1, r2, parent);
                if(map == null) {
-                       if(entry != null) entry.finish(graph, procedure);
-                       else procedure.finished(graph);
+                       procedure.finished(graph);
                        return;
                }
 
                int size = map.size();
                if(size == 3) {
-
-                       int value = map.data[2];
-
-                       if(entry != null) {
-                               entry.addOrSetFunctional(value);
-                               entry.finish(graph, procedure);
-                       } else {
-                               procedure.execute(graph, value);
-                               procedure.finished(graph);
-                       }
-
+                   int value = map.data[2];
+                   procedure.execute(graph, value);
+                   procedure.finished(graph);
                } else if(size == 0) {
-
-                       if(entry != null) entry.finish(graph, procedure);
-                       else procedure.finished(graph);
-
+                       procedure.finished(graph);
                } else {
 
                        int candidateS = map.data[0];
@@ -151,9 +136,8 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                        IntSet candidateIs = null;
                        try {
-                               candidateIs = QueryCache.resultSuperTypes(graph, candidateS, entry, null);
+                               candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null);
                        } catch (DatabaseException e) {
-                               if(entry != null) entry.except(e);
                                procedure.exception(graph, e);
                                return;
                        }
@@ -173,9 +157,8 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                IntSet nextIs = null;
                                                try {
-                                                       nextIs = QueryCache.resultSuperTypes(graph, nextS, entry, null);
+                                                       nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
                                                } catch (DatabaseException e) {
-                                                       if(entry != null) entry.except(e);
                                                        procedure.exception(graph, e);
                                                        return;
                                                }
@@ -200,8 +183,6 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                        // candidate and next are unrelated => error
                                                        ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions " + r1 + ", " + r2 + " " + map , r1);
-
-                                                       if(entry != null) entry.except(exception);
                                                        procedure.exception(graph, exception);
                                                        return;                                         
 
@@ -213,13 +194,8 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                        }
 
-                       if(entry != null) {
-                               entry.addOrSetFunctional(candidateO);
-                               entry.finish(graph, procedure);
-                       } else {
-                               procedure.execute(graph, candidateO);
-                               procedure.finished(graph);
-                       }
+                       procedure.execute(graph, candidateO);
+                       procedure.finished(graph);
 
                }
 
@@ -260,7 +236,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        }
 
        // Search for one statement
-       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
+       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
 
                if(ri.isFinal) {
 
@@ -269,7 +245,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        if(result == 0) {
 
                                // Check for assertions
-                               forSingleAssertion(graph, r1, r2, entry, procedure);
+                               forSingleAssertion(graph, r1, r2, parent, procedure);
 
                        } else if (result == -1) {
 
@@ -277,8 +253,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                        @Override
                                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                               if(entry != null) entry.addOrSetFunctional(i);
-                                               else procedure.execute(graph, i);
+                                           procedure.execute(graph, i);
                                        }
 
                                        @Override
@@ -293,18 +268,13 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                });
 
                                // Check for assertions
-                               forSingleAssertion(graph, r1, r2, entry, procedure);
+                               forSingleAssertion(graph, r1, r2, parent, procedure);
 
                        } else {
 
                                // If functional relation was found there is no need to check assertions
-                               if(entry != null) {
-                                       entry.addOrSetFunctional(result);
-                                       entry.finish(graph, procedure);
-                               } else {
-                                       procedure.execute(graph, result);
-                                       procedure.finished(graph);
-                               }
+                procedure.execute(graph, result);
+                procedure.finished(graph);
 
                                
                        }
@@ -321,12 +291,11 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                public void run(ReadGraphImpl graph) throws DatabaseException {
 
                                        if(found) {
-                                               if(entry != null) entry.finish(graph, procedure);
-                                               else procedure.finished(graph);
+                                               procedure.finished(graph);
                                        } else {
 
                                                // Check for assertions
-                                               forSingleAssertion(graph, r1, r2, entry, procedure);
+                                               forSingleAssertion(graph, r1, r2, parent, procedure);
 
                                        }
 
@@ -347,15 +316,12 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                                if(!found) {
 
-                                                                       if(entry != null) entry.addOrSetFunctional(i);
-                                                                       else procedure.execute(graph, i);
-
+                                                                       procedure.execute(graph, i);
                                                                        found = true;
 
                                                                } else {
 
                                                                        ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
-                                                                       if(entry != null) entry.except(exception);
                                                                        procedure.exception(graph, exception);
 
                                                                }
@@ -375,7 +341,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                        } else {
 
-                                               QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
+                                               QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
 
                                                        @Override
                                                        public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
@@ -392,15 +358,12 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                                                        if(!found) {
 
-                                                                                               if(entry != null) entry.addOrSetFunctional(i);
-                                                                                               else procedure.execute(graph, i);
-
+                                                                                               procedure.execute(graph, i);
                                                                                                found = true;
 
                                                                                        } else {
 
                                                                                                ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
-                                                                                               if(entry != null) entry.except(exception);
                                                                                                procedure.exception(graph, exception);
 
                                                                                        }
@@ -447,25 +410,21 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
        }
 
-       final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException {
+       final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final IntProcedure procedure) throws DatabaseException {
 
                // Note! The dependency is intentionally cut!
                QueryCache.runnerPrincipalTypes(graph, r1, null, null, new SyncIntProcedure() {
 
                        @Override
                        public void run(ReadGraphImpl graph) throws DatabaseException {
-
-                               if(entry != null) entry.finish(graph, procedure);
-                               else procedure.finished(graph);
-
+                               procedure.finished(graph);
                        }
 
                        TripleIntProcedure proc = new TripleIntProcedure() {
 
                                @Override
                                public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
-                                       if(entry != null) entry.addOrSet(o);
-                                       else procedure.execute(graph, o);
+                                       procedure.execute(graph, o);
                                }
 
                                @Override
@@ -486,7 +445,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
 
                                inc();
-                               QueryCache.runnerAssertedStatements(graph, type, r2, entry, null, proc);
+                               QueryCache.runnerAssertedStatements(graph, type, r2, parent, null, proc);
 
                        }
 
@@ -501,16 +460,14 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        }
 
        final public static void computeNotFunctionalFinalIndex(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, RelationInfo ri, AsyncMultiProcedure<Resource> procedure) {
-
                throw new Error();
-
        }
 
        final public void computeNotFunctionalIndex(ReadGraphImpl graph, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
                computeNotFunctionalIndex(graph, r1(), r2(), this, ri, procedure);
        }
 
-       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
+       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
 
                if(ri.isFinal) {
 
@@ -518,8 +475,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                @Override
                                public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                       if(entry != null) entry.addOrSet(i);
-                                       else procedure.execute(graph, i);
+                                       procedure.execute(graph, i);
                                }
 
                                @Override
@@ -535,10 +491,9 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        });
 
                        if(ri.isAsserted) {
-                               forAssertions(graph, r1, r2, entry, procedure);
+                               forAssertions(graph, r1, r2, parent, procedure);
                        } else {
-                               if(entry != null) entry.finish(graph, procedure);
-                               else procedure.finished(graph);
+                               procedure.finished(graph);
                        }
 
                } else {
@@ -549,9 +504,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                @Override
                                public void run(ReadGraphImpl graph) throws DatabaseException {
-
-                                       forAssertions(graph, r1, r2, entry, procedure);
-
+                                       forAssertions(graph, r1, r2, parent, procedure);
                                }
 
                                @Override
@@ -566,8 +519,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                        @Override
                                                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                                               if(entry != null) entry.addOrSet(i);
-                                                               else procedure.execute(graph, i);
+                                                               procedure.execute(graph, i);
                                                        }
 
                                                        @Override
@@ -587,7 +539,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                inc();
 
-                                               QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
+                                               QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
 
                                                        @Override
                                                        public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
@@ -601,8 +553,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                                                @Override
                                                                                public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                                                                       if(entry != null) entry.addOrSet(i);
-                                                                                       else procedure.execute(graph, i);
+                                                                                       procedure.execute(graph, i);
                                                                                }
 
                                                                                @Override
@@ -654,15 +605,19 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                return getResult();
        }
 
-       public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException {
-
-               RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null);
-               graph.ensureLoaded(r1, r2);       
-               if(ri.isFunctional) {
-                       computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               } else {
-                       computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               }
+       public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure_) throws DatabaseException {
+
+           IntProcedure procedure = entry != null ? entry : procedure_;
+           
+        RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null);
+        graph.ensureLoaded(r1, r2);
+        if(ri.isFunctional) {
+            computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
+        } else {
+            computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
+        }
+               
+               if(entry != null) entry.performFromCache(graph, procedure_);
 
        }
 
@@ -671,49 +626,49 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                return "Objects[" + r1() + " - " + r2() + "]";
        }
 
-       final private void finish(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
-
-               assert(assertPending());
-
-               synchronized(this) {
-                       setReady();
-               }
-
-               IntArray v = (IntArray)getResult();
-
-               if(v.data == null) {
-                       if(v.sizeOrData != IntArray.NO_DATA) {
-                               procedure.execute(graph, v.sizeOrData);
-                       }
-               } else {
-                       for(int i = 0;i < v.sizeOrData ; i++) {
-                               procedure.execute(graph, v.data[i]);
-                       }
-               }
-
-               procedure.finished(graph);
-
-       }
-
-       final public void addOrSet(int add) {
-
-               assert(assertPending());
-
-               IntArray value = (IntArray)getResult();
-               synchronized(value) {
-                       value.add(add);
-               }
-
-       }
-
-       final public void addOrSetFunctional(int add) {
-
-               assert(isPending());
-
-               IntArray value = (IntArray)getResult();
-               value.add(add);
-
-       }
+//     final private void finish(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
+//
+//             assert(assertPending());
+//
+//             synchronized(this) {
+//                     setReady();
+//             }
+//
+//             IntArray v = (IntArray)getResult();
+//
+//             if(v.data == null) {
+//                     if(v.sizeOrData != IntArray.NO_DATA) {
+//                             procedure.execute(graph, v.sizeOrData);
+//                     }
+//             } else {
+//                     for(int i = 0;i < v.sizeOrData ; i++) {
+//                             procedure.execute(graph, v.data[i]);
+//                     }
+//             }
+//
+//             procedure.finished(graph);
+//
+//     }
+
+//     final public void addOrSet(int add) {
+//
+//             assert(assertPending());
+//
+//             IntArray value = (IntArray)getResult();
+//             synchronized(value) {
+//                     value.add(add);
+//             }
+//
+//     }
+
+//     final public void addOrSetFunctional(int add) {
+//
+//             assert(isPending());
+//
+//             IntArray value = (IntArray)getResult();
+//             value.add(add);
+//
+//     }
 
        @Override
        public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
@@ -763,4 +718,22 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                return graph.processor.isImmutable(r1());
        }
 
+    @Override
+    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+      IntArray value = (IntArray)getResult();
+      synchronized(value) {
+          value.add(i);
+      }
+    }
+
+    @Override
+    public void finished(ReadGraphImpl graph) throws DatabaseException {
+        setReady();
+    }
+
+    @Override
+    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
+    }
+
 }