]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Statements.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Statements.java
index 935b8115962df76e3672d5479521e5dba41378f3..6652f1b6bac9aac657ec22aa8c2f2d329591b8aa 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.simantics.db.RelationInfo;
 import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
@@ -25,59 +23,13 @@ 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> {
-       
-//     public ArrayList<TripleIntProcedure> procs = null;
+public final class Statements extends CollectionBinaryQuery<TripleIntProcedure> implements TripleIntProcedure {
        
     public Statements(final int r1, final int r2) {
         super(r1, r2);
     }
-
-    final static Statements entry(final QueryProcessor processor, final int r1, final int r2) {
-        
-        return (Statements)processor.statementsMap.get(id(r1,r2));
-
-    }
-    
-       final static Collection<Statements> entries(final QueryProcessor processor, final int r1) {
-               return processor.statementsMap.values(r1);
-       }
     
-    final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
-        
-       QueryProcessor processor = graph.processor;
-
-        Statements entry = (Statements)processor.statementsMap.get(id(r1,r2));
-        if(entry == null) {
-               
-               entry = new Statements(r1, r2);
-               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<TripleIntProcedure>();
-//                     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 r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
+    final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException {
         
        assert(r1 != 0);
        assert(r2 != 0);
@@ -87,23 +39,13 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                return;
         }
         
-        runner(graph, r1, r2, parent, listener, procedure);
+        QueryCache.runnerStatements(graph, r1, r2, parent, listener, procedure);
          
     }
 
-    @Override
-    public BinaryQuery<TripleIntProcedure> getEntry(QueryProcessor provider) {
-        return provider.statementsMap.get(id);
-    }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-           provider.statementsMap.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-        provider.statementsMap.remove(id);
+        provider.cache.remove(this);
        }
        
     final static TripleIntProcedure NOPT = new TripleIntProcedure() {
@@ -123,7 +65,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
        
     };
        
-    final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Statements entry) {
+    final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Statements entry) throws DatabaseException {
        
         class AssertionMapProc implements IntProcedure {
                
@@ -153,10 +95,9 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                }
                
             @Override
-            public void execute(ReadGraphImpl graph, int type) {
-                AssertedStatements stms = AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, NOPT);
+            public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
                 if(result == null) {
-                       result = stms.getResult();
+                       result = QueryCache.resultAssertedStatements(graph, type, r2, entry, null);
                 } else {
                        if (first) {
                                IntArray ia = result;
@@ -166,7 +107,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                }
                                first = false;
                        }
-                       IntArray ia = stms.getResult();
+                       IntArray ia = QueryCache.resultAssertedStatements(graph, type, r2, entry, null);
                        if(ia.data != null) {
                                for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
                        }
@@ -186,18 +127,17 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
         AssertionMapProc amp = new AssertionMapProc();
 
         // This dependency could be cut
-       PrincipalTypes.queryEach(graph, r1, graph.processor, entry, null, amp);
+        QueryCache.runnerPrincipalTypes(graph, r1, entry, null, amp);
        
         return amp.result;
        
     }
        
-    final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) {
-        
-       IntArray map = getAssertionMap(graph, r1, r2, entry);
+    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, parent);
        if(map == null) {
-            if(entry != null) entry.finish(graph, procedure);
-               else procedure.finished(graph);
+               procedure.finished(graph);
             return;
        }
         
@@ -208,33 +148,27 @@ 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 {
 
                int candidateS = map.data[0];
                int candidateP = map.data[1];
                int candidateO = map.data[2];
-
-                       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());
+               
+                       IntSet candidateIs = null;
+                       try {
+                               candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null);
+                       } catch (DatabaseException e) {
+                               procedure.exception(graph, e);
                                return;
                        }
-                       IntSet candidateIs = candidate.getResult();
-               
+
                        for(int i=3;i<map.size();i+=3) {
 
                                int nextS = map.data[i];
@@ -249,13 +183,13 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                        
                                } else {
                                
-                                       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();
+                                       IntSet nextIs = null;
+                                       try {
+                                               nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
+                                       } catch (DatabaseException e) {
+                                               procedure.exception(graph, e);
+                                               return;
+                                       }
                                        
                                        if(nextIs.contains(candidateS)) {
 
@@ -267,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;                                         
-                                               
                                        }
                                
                                }
@@ -283,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);
                
         }
         
@@ -308,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) {
+       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) {
             
@@ -317,16 +242,15 @@ 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) {
 
                graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
                        @Override
-                       public void execute(ReadGraphImpl graph, int i) {
-                               if(entry != null) entry.addOrSetFunctional(r1, r2, i);
-                               else procedure.execute(graph, r1, r2, i);
+                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                               procedure.execute(graph, r1, r2, i);
                        }
 
                        @Override
@@ -341,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);
                        
                }
 
@@ -362,25 +281,23 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
             final AtomicBoolean found = new AtomicBoolean(false);
             
             // Note! The dependency is intentionally cut!
-            DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
+            IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null);
+            direct.forEach(graph, new SyncIntProcedure() {
                 
                 @Override
-                public void run(ReadGraphImpl graph) {
+                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);
                     }
                     
                 }
 
                 @Override
-                public void execute(ReadGraphImpl graph, final int pred) {
+                public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
                     
                     if(found.get()) return;
 
@@ -389,33 +306,27 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                         inc();
                         
                         // Note! The dependency is intentionally cut!
-                        DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+                        QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
                             @Override
-                            public void execute(ReadGraphImpl graph, int i) {
+                            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);
-                                       
                                }
 
                             }
 
                             @Override
-                            public void finished(ReadGraphImpl graph) {
+                            public void finished(ReadGraphImpl graph) throws DatabaseException {
                                 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);
                                    }
@@ -426,10 +337,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                         
                         inc();
                     
-                        SuperRelations.queryEach(graph, pred, graph.processor, entry, null, new InternalProcedure<IntSet>() {
+                        QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
     
                             @Override
-                            public void execute(ReadGraphImpl graph, IntSet result) {
+                            public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
                                 
                                 if(found.get()) {
                                     dec(graph);
@@ -441,33 +352,27 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                     inc();
                                     
                                     // Note! The dependency is intentionally cut!
-                                    DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+                                    QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
     
                                         @Override
-                                        public void execute(ReadGraphImpl graph, int i) {
+                                        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);
-                                                       
                                                }
                                             
                                         }
     
                                         @Override
-                                        public void finished(ReadGraphImpl graph) {
+                                        public void finished(ReadGraphImpl graph) throws DatabaseException {
                                             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);
                                            }
@@ -481,7 +386,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                             }
                                                
                                                @Override
-                                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                        procedure.exception(graph, t);
                                 dec(graph);
                                    }
@@ -493,10 +398,8 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                 }
 
                 @Override
-                public void finished(ReadGraphImpl graph) {
-                    
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
                     dec(graph);
-                    
                 }
                 
             });
@@ -505,33 +408,29 @@ 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) {
+    final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final TripleIntProcedure procedure) throws DatabaseException {
 
-        PrincipalTypes.queryEach(graph, r1, graph.processor, entry, null, new SyncIntProcedure() {
+       QueryCache.runnerPrincipalTypes(graph, r1, parent, null, new SyncIntProcedure() {
             
             @Override
-            public void run(ReadGraphImpl graph) {
-                
-                if(entry != null) entry.finish(graph, procedure);
-                else procedure.finished(graph);
-                
+            public void run(ReadGraphImpl graph) throws DatabaseException {
+                procedure.finished(graph);
             }
             
             TripleIntProcedure proc = new TripleIntProcedureAdapter() {
 
                 @Override
-                public void execute(ReadGraphImpl graph, int s, int p, int o) {
-                       if(entry != null) entry.addOrSet(s, p, o);
-                       else procedure.execute(graph, s, p, o);
+                public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
+                       procedure.execute(graph, s, p, o);
                 }
 
                 @Override
-                public void finished(ReadGraphImpl graph) {
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
                     dec(graph);
                 }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                     dec(graph);
                                        procedure.exception(graph, t);
                    }
@@ -539,21 +438,18 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
             }; 
 
             @Override
-            public void execute(ReadGraphImpl graph, int type) {
-
+            public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
                 inc();
-                
-                AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, proc);
-                
+                QueryCache.runnerAssertedStatements(graph, type, r2, parent, null, proc);
             }
             
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);       
             }
             
             @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
+            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                 dec(graph);
             }
             
@@ -562,20 +458,19 @@ 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) {
+       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) {
 
                        graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
                                @Override
-                               public void execute(ReadGraphImpl graph, int i) {
-                                       if(entry != null) entry.addOrSet(r1, r2, i);
-                                       else procedure.execute(graph, r1, r2, i);
+                               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                       procedure.execute(graph, r1, r2, i);
                                }
 
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
                                        procedure.exception(graph, t);
                                }
@@ -587,47 +482,44 @@ 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 {
 
             // Note! The dependency is intentionally cut!
-            DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
+            IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null);
+            direct.forEach(graph, new SyncIntProcedure() {
                 
                 @Override
-                public void run(ReadGraphImpl graph) {
-                    
-                    forAssertions(graph, r1, r2, entry, procedure);
-                    
+                public void run(ReadGraphImpl graph) throws DatabaseException {
+                    forAssertions(graph, r1, r2, parent, procedure);
                 }
 
                 @Override
-                public void execute(ReadGraphImpl graph, final int pred2) {
+                public void execute(ReadGraphImpl graph, final int pred2) throws DatabaseException {
 
                     if(pred2 == r2) {
                         
                         inc();
                         
                         // Note! The dependency is intentionally cut!
-                        DirectObjects.queryEach(graph, r1, pred2, graph.processor, null, null, new IntProcedure() {
+                        QueryCache.runnerDirectObjects(graph, r1, pred2, null, null, new IntProcedure() {
 
                             @Override
-                            public void execute(ReadGraphImpl graph, int i) {
-                               if(entry != null) entry.addOrSet(r1, pred2, i);
-                               else procedure.execute(graph, r1, pred2, i);
+                            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                               procedure.execute(graph, r1, pred2, i);
                             }
 
                             @Override
-                            public void finished(ReadGraphImpl graph) {
+                            public void finished(ReadGraphImpl graph) throws DatabaseException {
                                 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);
                                    }
@@ -640,28 +532,27 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                         try {
                             
-                            IntSet result = SuperRelations.queryEach2(graph, pred2, graph.processor, entry, null, null);
+                            IntSet result = QueryCache.resultSuperRelations(graph, pred2, parent, null);
                             if(result.contains(r2)) {
 
                                 inc();
 
                                 // Note! The dependency is intentionally cut!
-                                DirectObjects.queryEach(graph, r1, pred2, graph.processor, null, null, new IntProcedure() {
+                                QueryCache.runnerDirectObjects(graph, r1, pred2, null, null, new IntProcedure() {
 
                                     @Override
-                                    public void execute(ReadGraphImpl graph, int i) {
-                                        if(entry != null) entry.addOrSet(r1, pred2, i);
-                                        else procedure.execute(graph, r1, pred2, i);
+                                    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                        procedure.execute(graph, r1, pred2, i);
 
                                     }
 
                                     @Override
-                                    public void finished(ReadGraphImpl graph) {
+                                    public void finished(ReadGraphImpl graph) throws DatabaseException {
                                         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);
                                     }
@@ -679,7 +570,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                 }
 
                 @Override
-                public void finished(ReadGraphImpl graph) {
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
                     dec(graph);
                 }
                 
@@ -688,46 +579,20 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
         }
         
     }
-    
-    @Override
-    public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final TripleIntProcedure procedure, final boolean store) {
-       computeForEach(graph, r1(), r2(), this, procedure);
-    }
        
-    public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) {
-
-       RelationInfoQuery riEntry = RelationInfoQuery.probe(graph, r2);
-       if(riEntry != null) {
-               RelationInfo ri = riEntry.getResult();
-               graph.ensureLoaded(r1, r2);       
-               if(ri.isFunctional) {
-                       computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               } else {
-                       computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               }
-               return;
-       }
+    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_;
        
-        RelationInfoQuery.queryEach(graph, r2, graph.processor, entry, null, new InternalProcedure<RelationInfo>() {
-
-            @Override
-            public void execute(ReadGraphImpl graph, final RelationInfo ri) {
-               
-               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) {
-                               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_);
         
     }
     
@@ -736,38 +601,18 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
        return "Statements[" + r1() + " - " + r2() + "]";
     }
 
-    final private void finish(ReadGraphImpl graph, TripleIntProcedure procedure) {
+    final private void finish(ReadGraphImpl graph, TripleIntProcedure procedure) throws DatabaseException {
        
         assert(assertPending());
 
-//        ArrayList<TripleIntProcedure> p = null;
-
         synchronized(this) {
-
             setReady();
-//            p = procs;
-//            procs = null;
-
+            //new Exception(toString() + " is READY").printStackTrace();
         }
 
         IntArray v = (IntArray)getResult();
         final IntArray value = (IntArray)getResult();
 
-//        if(p != null) {
-//
-//             for(TripleIntProcedure proc : p) {
-//                     for(int i=0;i<value.size();i+=3) {
-//                             proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
-//                     }
-//             }
-//             for(int i=0;i<value.size();i+=3) {
-//                     procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
-//             }
-//
-//            for(TripleIntProcedure proc : p) proc.finished(graph);
-//
-//        }
-
         for(int i=0;i<value.size();i+=3) {
                procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
         }
@@ -776,35 +621,6 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
        
     }
 
-//    final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
-//        
-//     assert(isPending());
-//
-//        ArrayList<TripleIntProcedure> p = null;
-//
-//        synchronized(this) {
-//             
-//             setReady();
-//            p = procs;
-//            procs = null; 
-//        
-//        }
-//        
-//        if(p != null) {
-//             
-//             final IntArray value = (IntArray)getResult();
-//             for(TripleIntProcedure proc : p) {
-//                     for(int i=0;i<value.size();i+=3) {
-//                             proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
-//                     }
-//             }
-//
-//             for(TripleIntProcedure proc : p) proc.finished(graph);
-//
-//        }
-//
-//    }
-    
     synchronized public void addOrSet(int s, int p, int o) {
         
        assert(assertPending());
@@ -842,50 +658,39 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
     }
     
     @Override
-    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
+    public Object performFromCache(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
 
        assert(isReady());
 
-       if(handleException(graph, procedure)) return;
-       
         final IntArray value = (IntArray)getResult();
+
+       if(handleException(graph, procedure)) return value;
+       
         for(int i=0;i<value.size();i+=3) {
                procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
         }
 
         procedure.finished(graph);
         
+        return value;
+        
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        final Semaphore s = new Semaphore(0);
-       
-        computeForEach(graph, provider, new TripleIntProcedureAdapter() {
+        computeForEach(graph, r1(), r2(), this, new TripleIntProcedureAdapter() {
 
             @Override
             public void finished(ReadGraphImpl graph) {
-                s.release();
             }
                        
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable t) {
                                new Error("Error in recompute.", t).printStackTrace();
-                               s.release();
             }
 
-        }, true);
-        
-       while(!s.tryAcquire()) {
-               provider.resume(graph);
-       }
-        
-//        try {
-//            s.acquire();
-//        } catch (InterruptedException e) {
-//            throw new Error(e);
-//        }
+        });
         
     }
 
@@ -899,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);
+    }
     
 }