]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Statements.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Statements.java
index ad4700aa826d6178f9447f81fa8e606ddfd13f0f..a71df27fd38a06d8fbecf0cd6b08b63bec68895d 100644 (file)
@@ -23,7 +23,7 @@ import org.simantics.db.impl.procedure.TripleIntProcedureAdapter;
 import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
 
-final public class Statements extends CollectionBinaryQuery<TripleIntProcedure> {
+final public class Statements extends CollectionBinaryQuery<TripleIntProcedure> implements TripleIntProcedure {
        
     public Statements(final int r1, final int r2) {
         super(r1, r2);
@@ -133,16 +133,11 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
        
     }
        
-    final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException {
-        
-       if(entry != null) {
-               assert(entry.isPending());
-       }
+    final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final TripleIntProcedure 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;
        }
         
@@ -153,18 +148,12 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                int p = map.data[1];
                int o = map.data[2];
                
-            if(entry != null) {
-               entry.addOrSetFunctional(s,p,o);
-               entry.finish(graph, procedure);
-            } else {
                        procedure.execute(graph, s,p,o);
                        procedure.finished(graph);
-               }
 
         } else if(size == 0) {
 
-            if(entry != null) entry.finish(graph, procedure);
-               else procedure.finished(graph);
+               procedure.finished(graph);
                
         } else {
 
@@ -174,22 +163,12 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                
                        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;
                        }
 
-
-//                     SuperTypes candidate = SuperTypes.queryEach(graph, candidateS, graph.processor, entry, null, NOP);
-//                     if(candidate.isExcepted()) {
-//                             if(entry != null) entry.except((Throwable)candidate.getResult());
-//                             procedure.exception(graph, (Throwable)candidate.getResult());
-//                             return;
-//                     }
-//                     IntSet candidateIs = candidate.getResult();
-               
                        for(int i=3;i<map.size();i+=3) {
 
                                int nextS = map.data[i];
@@ -206,20 +185,11 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                
                                        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;
                                        }
-                                       
-//                                     SuperTypes next = SuperTypes.queryEach(graph, nextS, graph.processor, entry, null, NOP);
-//                                     if(next.isExcepted()) {
-//                                             if(entry != null) entry.except((Throwable)next.getResult());
-//                                             procedure.exception(graph, (Throwable)next.getResult());
-//                                             return;
-//                                     }
-//                                     IntSet nextIs = next.getResult();
                                        
                                        if(nextIs.contains(candidateS)) {
 
@@ -231,14 +201,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                                candidateIs = nextIs;
                                                
                                        } else {
-
                                                // candidate and next are unrelated => error
                                                ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions.", r1);
-                                               
-                                               if(entry != null) entry.except(exception);
                                                procedure.exception(graph, exception);
                                                return;                                         
-                                               
                                        }
                                
                                }
@@ -247,13 +213,8 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                        
                }
                
-               if(entry != null) {
-               entry.addOrSetFunctional(candidateS, candidateP, candidateO);
-               entry.finish(graph, procedure);
-            } else {
-                       procedure.execute(graph, candidateS, candidateP, candidateO);
-                       procedure.finished(graph);
-               }
+            procedure.execute(graph, candidateS, candidateP, candidateO);
+            procedure.finished(graph);
                
         }
         
@@ -272,7 +233,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
     };
        
        // Search for one statement
-       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
+       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
         
         if(ri.isFinal) {
             
@@ -281,7 +242,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                if(result == 0) {
 
                // Check for assertions
-               forSingleAssertion(graph, r1, r2, entry, procedure);
+               forSingleAssertion(graph, r1, r2, parent, procedure);
 
                } else if(result == -1) {
 
@@ -289,8 +250,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                        @Override
                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                               if(entry != null) entry.addOrSetFunctional(r1, r2, i);
-                               else procedure.execute(graph, r1, r2, i);
+                               procedure.execute(graph, r1, r2, i);
                        }
 
                        @Override
@@ -305,18 +265,13 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                });
 
                // 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(r1, r2, result);
-                               entry.finish(graph, procedure);
-                       } else {
-                               procedure.execute(graph, r1, r2, result);
-                               procedure.finished(graph);
-                       }
+                procedure.execute(graph, r1, r2, result);
+                procedure.finished(graph);
                        
                }
 
@@ -333,13 +288,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                 public void run(ReadGraphImpl graph) throws DatabaseException {
                     
                     if(found.get()) {
-                       if(entry != null) entry.finish(graph, procedure);
-                       else procedure.finished(graph);
+                       procedure.finished(graph);
                     } else {
-                    
-                           // Check for assertions
-                       forSingleAssertion(graph, r1, r2, entry, procedure);
-                           
+                        // Check for assertions
+                        forSingleAssertion(graph, r1, r2, parent, procedure);
                     }
                     
                 }
@@ -360,16 +312,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                 
                                if(found.compareAndSet(false, true)) {
-                                    
-                                    if(entry != null) entry.addOrSetFunctional(r1, pred, i);
-                                    else procedure.execute(graph, r1, pred, i);
-                                       
+                                    procedure.execute(graph, r1, pred, i);
                                } else {
-
                                        ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
-                                       if(entry != null) entry.except(exception);
                                        procedure.exception(graph, exception);
-                                       
                                }
 
                             }
@@ -391,7 +337,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                         
                         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 {
@@ -412,16 +358,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                         public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                             
                                                if(found.compareAndSet(false, true)) {
-                                                
-                                               if(entry != null) entry.addOrSetFunctional(r1, pred, i);
-                                               else procedure.execute(graph, r1, pred, i);
-                                                       
+                                               procedure.execute(graph, r1, pred, i);
                                                } else {
-
                                                ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
-                                               if(entry != null) entry.except(exception);
                                                procedure.exception(graph, exception);
-                                                       
                                                }
                                             
                                         }
@@ -468,24 +408,20 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
        }
     
-    final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException {
+    final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final TripleIntProcedure procedure) throws DatabaseException {
 
-       QueryCache.runnerPrincipalTypes(graph, r1, entry, null, new SyncIntProcedure() {
+       QueryCache.runnerPrincipalTypes(graph, r1, parent, 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 TripleIntProcedureAdapter() {
 
                 @Override
                 public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
-                       if(entry != null) entry.addOrSet(s, p, o);
-                       else procedure.execute(graph, s, p, o);
+                       procedure.execute(graph, s, p, o);
                 }
 
                 @Override
@@ -503,10 +439,8 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
             @Override
             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);
             }
             
             @Override
@@ -524,7 +458,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
     }
 
-       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry,  final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
+       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
 
                if(ri.isFinal) {
 
@@ -532,8 +466,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                                @Override
                                public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                       if(entry != null) entry.addOrSet(r1, r2, i);
-                                       else procedure.execute(graph, r1, r2, i);
+                                       procedure.execute(graph, r1, r2, i);
                                }
 
                                @Override
@@ -549,10 +482,9 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                        });
 
                        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 {
@@ -563,9 +495,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                 
                 @Override
                 public void run(ReadGraphImpl graph) throws DatabaseException {
-                    
-                    forAssertions(graph, r1, r2, entry, procedure);
-                    
+                    forAssertions(graph, r1, r2, parent, procedure);
                 }
 
                 @Override
@@ -580,8 +510,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                             @Override
                             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                               if(entry != null) entry.addOrSet(r1, pred2, i);
-                               else procedure.execute(graph, r1, pred2, i);
+                               procedure.execute(graph, r1, pred2, i);
                             }
 
                             @Override
@@ -603,7 +532,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                         try {
                             
-                            IntSet result = QueryCache.resultSuperRelations(graph, pred2, entry, null);
+                            IntSet result = QueryCache.resultSuperRelations(graph, pred2, parent, null);
                             if(result.contains(r2)) {
 
                                 inc();
@@ -613,8 +542,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                                     @Override
                                     public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                        if(entry != null) entry.addOrSet(r1, pred2, i);
-                                        else procedure.execute(graph, r1, pred2, i);
+                                        procedure.execute(graph, r1, pred2, i);
 
                                     }
 
@@ -652,32 +580,19 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
         
     }
        
-    public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException {
-       
-       if(entry != null) {
-               assert(entry.isPending());
-       }
+    public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure_) throws DatabaseException {
+        
+        TripleIntProcedure procedure = entry != null ? entry : procedure_;
        
-       QueryCache.runnerRelationInfoQuery(graph, r2, entry, null, new InternalProcedure<RelationInfo>() {
-
-            @Override
-            public void execute(ReadGraphImpl graph, final RelationInfo ri) throws DatabaseException {
-               
-               graph.ensureLoaded(r1, r2);
-               if(ri.isFunctional) {
-                       computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               } else {
-                       computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               }
-                
-            }
-                       
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                               procedure.exception(graph, t);
-            }
+        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_);
         
     }
     
@@ -789,5 +704,20 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
     boolean isImmutable(ReadGraphImpl graph) {
        return graph.processor.isImmutable(r1());
     }
+
+    @Override
+    public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
+        addOrSet(s, p, o);
+    }
+
+    @Override
+    public void finished(ReadGraphImpl graph) throws DatabaseException {
+        setReady();
+    }
+
+    @Override
+    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
+    }
     
 }