]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Objects.java
index 0b3e1be34d3e8f16a135df845e95ebdbe4fc7ccb..f74344b2fdb807dad60e1b746580f81cb106ad80 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.Collection;
-import java.util.concurrent.Semaphore;
-
-import org.simantics.databoard.Bindings;
-import org.simantics.db.DevelopmentKeys;
 import org.simantics.db.RelationInfo;
 import org.simantics.db.Resource;
 import org.simantics.db.common.exception.DebugException;
@@ -25,9 +20,7 @@ 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.AsyncMultiProcedure;
-import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
-import org.simantics.utils.Development;
 
 /*
  * Size analysis:
@@ -42,150 +35,18 @@ import org.simantics.utils.Development;
  * 
  */
 
-final public class Objects extends CollectionBinaryQuery<IntProcedure> {
+public final class Objects extends CollectionBinaryQuery<IntProcedure> implements IntProcedure {
 
        public Objects(final int r1, final int r2) {
                super(r1, r2);
        }
 
-       final static Objects entry(final QueryProcessor provider, final int r1, final int r2) {
-               return (Objects)provider.objectsMap.get(r1,r2);
-       }
-
-       final static Collection<Objects> entries(final QueryProcessor processor, final int r1) {
-               return processor.objectsMap.values(r1);
-       }
-
-       public final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) {
-
-               if(parent == null && listener == null) {
-                       Objects.computeForEach(graph, r1, r2, null, procedure);
-                       return;
-               }
-
-               QueryProcessor processor = graph.processor;
-
-               Objects entry = (Objects)processor.objectsMap.get(r1,r2);
-               if(entry == null) {
-
-                       entry = new Objects(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()) {
-                                               processor.registerDependencies(graph, entry, parent, listener, procedure, false);
-                                               computeForEach(graph, r1, r2, null, procedure);
-                                               return;
-                                       }
-                               }
-                       }
-
-                       processor.performForEach(graph, entry, parent, listener, procedure);
-
-               }
-
-       }
-
-       static class Runner2Procedure implements IntProcedure {
-           
-           public int single = 0;
-           public Throwable t = null;
-
-           public void clear() {
-               single = 0;
-               t = null;
-           }
-           
-        @Override
-        public void execute(ReadGraphImpl graph, int i) {
-            if(single == 0) single = i;
-            else single = -1;
-        }
-
-        @Override
-        public void finished(ReadGraphImpl graph) {
-            if(single == -1) single = 0;
-        }
-
-        @Override
-        public void exception(ReadGraphImpl graph, Throwable throwable) {
-            single = 0;
-            this.t = throwable;
-        }
-        
-        public int get() throws DatabaseException {
-            if(t != null) {
-                if(t instanceof DatabaseException) throw (DatabaseException)t;
-                else throw new DatabaseException(t);
-            }
-            return single;
-        }
-           
-       }
-       
-       static final Runner2Procedure runner2Procedure = new Runner2Procedure();
-       
-    public final static int runner2(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent) throws DatabaseException {
-
-        runner2Procedure.clear();
-
-        if(parent == null) {
-            Objects.computeForEach(graph, r1, r2, null, runner2Procedure);
-            return runner2Procedure.get();
-        }
-
-        QueryProcessor processor = graph.processor;
-
-        Objects entry = (Objects)processor.objectsMap.get(r1,r2);
-        if(entry == null) {
-
-            entry = new Objects(r1, r2);
-            entry.setPending();
-            entry.clearResult(processor.querySupport);
-            entry.putEntry(processor);
-
-            processor.performForEach(graph, entry, parent, null, runner2Procedure);
-            return runner2Procedure.get();
-
-        } else {
-
-            if(entry.isPending()) throw new IllegalStateException();
-
-            processor.performForEach(graph, entry, parent, null, runner2Procedure);
-            return runner2Procedure.get();
-
-        }
-
-    }
-       
-       @Override
-       public BinaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return provider.objectsMap.get(id);
-       }
-
-       @Override
-       public void putEntry(QueryProcessor provider) {
-               if(Development.DEVELOPMENT) {
-                       if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_PUT, Bindings.BOOLEAN)) {
-                               System.err.println("put " + this);
-                       }
-               }
-               provider.objectsMap.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.objectsMap.remove(id);
+               provider.cache.remove(this);
        }
 
-       final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) {
+       final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) throws DatabaseException {
 
                class AssertionMapProc implements IntProcedure {
 
@@ -215,10 +76,9 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        }
 
                        @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 = QueryCacheBase.resultAssertedStatements(graph, type, r2, entry, null);
                                } else {
                                        if (first) {
                                                IntArray ia = result;
@@ -228,7 +88,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                                }
                                                first = false;
                                        }
-                                       IntArray ia = stms.getResult();
+                                       IntArray ia = QueryCacheBase.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]);
                                        }
@@ -248,51 +108,39 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                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 Objects entry, final IntProcedure procedure) {
+       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];
                        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) {
 
@@ -307,13 +155,13 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                        } 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());
+                                               IntSet nextIs = null;
+                                               try {
+                                                       nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
+                                               } catch (DatabaseException e) {
+                                                       procedure.exception(graph, e);
                                                        return;
                                                }
-                                               IntSet nextIs = next.getResult();
 
                                                if(nextIs.contains(candidateS)) {
 
@@ -327,8 +175,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;                                         
 
@@ -340,13 +186,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);
 
                }
 
@@ -382,12 +223,12 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        };
 
        // Search for one statement
-       final public void computeFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, final RelationInfo ri, final IntProcedure procedure) {
+       final public void computeFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
                computeFunctionalIndex(graph, r1(), r2(), this, ri, procedure);
        }
 
        // 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) {
+       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) {
 
@@ -396,16 +237,15 @@ 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) {
 
                                graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
                                        @Override
-                                       public void execute(ReadGraphImpl graph, int i) {
-                                               if(entry != null) entry.addOrSetFunctional(i);
-                                               else procedure.execute(graph, i);
+                                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                           procedure.execute(graph, i);
                                        }
 
                                        @Override
@@ -420,18 +260,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);
 
                                
                        }
@@ -439,49 +274,46 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                } 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() {
 
                                boolean found = false;
 
                                @Override
-                               public void run(ReadGraphImpl graph) {
+                               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);
 
                                        }
 
                                }
 
                                @Override
-                               public void execute(ReadGraphImpl graph, final int pred) {
+                               public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
 
                                        if(found) return;
 
                                        if(pred == r2) {
 
                                                // 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) {
 
-                                                                       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);
 
                                                                }
@@ -493,7 +325,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                                        }
 
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                                procedure.exception(graph, t);
                                                        }
 
@@ -501,32 +333,29 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                        } else {
 
-                                               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) return;
 
                                                                if(result.contains(r2)) {
 
                                                                        // 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) {
 
-                                                                                               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);
 
                                                                                        }
@@ -538,7 +367,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                                                                }
 
                                                                                @Override
-                                                                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                                                        procedure.exception(graph, t);
                                                                                }
 
@@ -549,7 +378,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                                        }
 
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                                procedure.exception(graph, t);
                                                        }
 
@@ -560,7 +389,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                }
 
                                @Override
-                               public void finished(ReadGraphImpl graph) {
+                               public void finished(ReadGraphImpl graph) throws DatabaseException {
 
                                        dec(graph);
 
@@ -573,34 +402,30 @@ 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) {
+       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!
-               PrincipalTypes.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
+               QueryCache.runnerPrincipalTypes(graph, r1, null, 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 TripleIntProcedure() {
 
                                @Override
-                               public void execute(ReadGraphImpl graph, int s, int p, int o) {
-                                       if(entry != null) entry.addOrSet(o);
-                                       else procedure.execute(graph, o);
+                               public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
+                                       procedure.execute(graph, 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 {
                                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
                                        procedure.exception(graph, t);
                                        dec(graph);
@@ -609,16 +434,15 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        };
 
                        @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);
                        }
 
@@ -628,29 +452,26 @@ 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) {
+       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) {
+       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) {
 
                        graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
                                @Override
-                               public void execute(ReadGraphImpl graph, int i) {
-                                       if(entry != null) entry.addOrSet(i);
-                                       else procedure.execute(graph, i);
+                               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                       procedure.execute(graph, 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);
                                }
@@ -662,47 +483,44 @@ 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 {
 
                        // 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 pred) {
+                               public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
 
                                        if(pred == r2) {
 
                                                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) {
-                                                               if(entry != null) entry.addOrSet(i);
-                                                               else procedure.execute(graph, i);
+                                                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                                               procedure.execute(graph, 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);
                                                        }
@@ -713,31 +531,30 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                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(result.contains(r2)) {
 
                                                                        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) {
-                                                                                       if(entry != null) entry.addOrSet(i);
-                                                                                       else procedure.execute(graph, i);
+                                                                               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                                                                       procedure.execute(graph, 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 {
                                                                                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
                                                                                        procedure.exception(graph, t);
                                                                                        dec(graph);
@@ -752,7 +569,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                                        }
 
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                                procedure.exception(graph, t);
                                                                dec(graph);
                                                        }
@@ -764,7 +581,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                }
 
                                @Override
-                               public void finished(ReadGraphImpl graph) {
+                               public void finished(ReadGraphImpl graph) throws DatabaseException {
                                        dec(graph);
                                }
 
@@ -774,90 +591,38 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
        }
 
-       @Override
-       public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
+       public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
                computeForEach(graph, r1(), r2(), this, procedure);
+               return getResult();
        }
 
-       public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) {
+       public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure_) throws DatabaseException {
 
-               RelationInfo ri = RelationInfoQuery.queryEach(graph, r2, graph.processor, entry, null, ip);
-               graph.ensureLoaded(r1, r2);       
-               if(ri.isFunctional) {
-                       computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               } else {
-                       computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               }
+           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_);
 
        }
 
-       final static InternalProcedure<RelationInfo> ip = new InternalProcedure<RelationInfo>() {
-
-               @Override
-               public void execute(ReadGraphImpl graph, RelationInfo result) {
-               }
-
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
-
-       };
-
        @Override
        public String toString() {
                return "Objects[" + r1() + " - " + r2() + "]";
        }
 
-       final private void finish(ReadGraphImpl graph, IntProcedure procedure) {
-
-               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 void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
+       public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
 
                assert(isReady());
 
-               if(handleException(graph, procedure)) return;
+               if(handleException(graph, procedure)) return getResult();
 
                final IntArray value = (IntArray)getResult();
                if(value.data == null) {
@@ -867,32 +632,26 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                }
 
                procedure.finished(graph);
+               
+               return value;
 
        }
 
        @Override
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-
-               final Semaphore s = new Semaphore(0);
+       public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-               computeForEach(graph, provider, new IntProcedureAdapter() {
+               compute(graph, new IntProcedureAdapter() {
 
                        @Override
                        public void finished(ReadGraphImpl graph) {
-                               s.release();
                        }
 
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable t) {
-                               s.release();
                                new Error("Error in recompute.", t).printStackTrace();
                        }
 
-               }, true);
-
-               while(!s.tryAcquire()) {
-                       provider.resume(graph);
-               }
+               });
 
        }
 
@@ -906,4 +665,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);
+    }
+
 }