]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Objects.java
index 0b3e1be34d3e8f16a135df845e95ebdbe4fc7ccb..c84b63dc61a5d6304cf52c3478c8c91a7ca9f7e3 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,868 +35,655 @@ import org.simantics.utils.Development;
  * 
  */
 
-final public class Objects extends CollectionBinaryQuery<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) {
+public final class Objects extends CollectionBinaryQuery<IntProcedure> implements IntProcedure {
 
-                       entry = new Objects(r1, r2);
-                       entry.setPending();
-                       entry.clearResult(processor.querySupport);
-                       entry.putEntry(processor);
-
-                       processor.performForEach(graph, entry, parent, listener, procedure);
+    public Objects(final int r1, final int r2) {
+        super(r1, r2);
+    }
 
-               } else {
+    @Override
+    final public void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
+    }
 
-                       if(entry.isPending()) {
-                               synchronized(entry) {
-                                       if(entry.isPending()) {
-                                               processor.registerDependencies(graph, entry, parent, listener, procedure, false);
-                                               computeForEach(graph, r1, r2, null, procedure);
-                                               return;
-                                       }
-                               }
-                       }
+    final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) throws DatabaseException {
 
-                       processor.performForEach(graph, entry, parent, listener, procedure);
+        class AssertionMapProc implements IntProcedure {
 
-               }
+            boolean first = true;
 
-       }
+            private IntArray result;
 
-       static class Runner2Procedure implements IntProcedure {
-           
-           public int single = 0;
-           public Throwable t = null;
+            public void addStatement(int s, int p, int o) {
 
-           public void clear() {
-               single = 0;
-               t = null;
-           }
-           
-        @Override
-        public void execute(ReadGraphImpl graph, int i) {
-            if(single == 0) single = i;
-            else single = -1;
-        }
+                if(result.size() == 0) {
+                    result.add(s);
+                    result.add(p);
+                    result.add(o);
+                } else {
+                    for(int i = 0;i < result.sizeOrData ; i+=3) {
+                        int existingP = result.data[i+1];
+                        if(p == existingP) {
+                            int existingO = result.data[i+2];
+                            if(existingO == o) return;
+                        }
+                    }
+                    result.add(s);
+                    result.add(p);
+                    result.add(o);
+                }
 
-        @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 {
+            @Override
+            public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
+                if(result == null) {
+                    result = QueryCacheBase.resultAssertedStatements(graph, type, r2, entry, null);
+                } else {
+                    if (first) {
+                        IntArray ia = result;
+                        result = new IntArray();
+                        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]);
+                        }
+                        first = false;
+                    }
+                    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]);
+                    }
+                }
+            }
 
-            if(entry.isPending()) throw new IllegalStateException();
+            @Override
+            public void finished(ReadGraphImpl graph) {
+            }
 
-            processor.performForEach(graph, entry, parent, null, runner2Procedure);
-            return runner2Procedure.get();
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable throwable) {
+            }
 
         }
 
-    }
-       
-       @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);
-       }
-
-       final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) {
-
-               class AssertionMapProc implements IntProcedure {
-
-                       boolean first = true;
-
-                       private IntArray result;
-
-                       public void addStatement(int s, int p, int o) {
-
-                               if(result.size() == 0) {
-                                       result.add(s);
-                                       result.add(p);
-                                       result.add(o);
-                               } else {
-                                       for(int i = 0;i < result.sizeOrData ; i+=3) {
-                                               int existingP = result.data[i+1];
-                                               if(p == existingP) {
-                                                       int existingO = result.data[i+2];
-                                                       if(existingO == o) return;
-                                               }
-                                       }
-                                       result.add(s);
-                                       result.add(p);
-                                       result.add(o);
-                               }
-
-                       }
-
-                       @Override
-                       public void execute(ReadGraphImpl graph, int type) {
-                               AssertedStatements stms = AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, NOPT);
-                               if(result == null) {
-                                       result = stms.getResult();
-                               } else {
-                                       if (first) {
-                                               IntArray ia = result;
-                                               result = new IntArray();
-                                               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]);
-                                               }
-                                               first = false;
-                                       }
-                                       IntArray ia = stms.getResult();
-                                       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]);
-                                       }
-                               }
-                       }
-
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable throwable) {
-                       }
-
-               }
-
-               AssertionMapProc amp = new AssertionMapProc();
-
-               // This dependency could be cut
-               PrincipalTypes.queryEach(graph, r1, graph.processor, 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) {
-
-               IntArray map = getAssertionMap(graph, r1, r2, entry);
-               if(map == null) {
-                       if(entry != null) entry.finish(graph, procedure);
-                       else 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);
-                       }
-
-               } else if(size == 0) {
-
-                       if(entry != null) entry.finish(graph, procedure);
-                       else 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());
-                               return;
-                       }
-                       IntSet candidateIs = candidate.getResult();
-
-                       for(int i=3;i<map.size();i+=3) {
-
-                               int nextS = map.data[i];
-                               int nextO = map.data[i+2];
-
-                               if(nextS != candidateS) {
-
-                                       if(candidateIs.contains(nextS)) {
-
-                                               // Next is a super type of candidate => ignore next
-
-                                       } 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();
-
-                                               if(nextIs.contains(candidateS)) {
-
-                                                       // Candidate is a super type of next => next is the new candidate
-
-                                                       candidateS = nextS;
-                                                       candidateO = nextO;
-                                                       candidateIs = nextIs;
-
-                                               } else {
-
-                                                       // 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;                                         
+        AssertionMapProc amp = new AssertionMapProc();
 
-                                               }
+        // This dependency could be cut
+        QueryCache.runnerPrincipalTypes(graph, r1, entry, null, amp);
 
-                                       }
+        return amp.result;
 
-                               }
-
-                       }
-
-                       if(entry != null) {
-                               entry.addOrSetFunctional(candidateO);
-                               entry.finish(graph, procedure);
-                       } else {
-                               procedure.execute(graph, candidateO);
-                               procedure.finished(graph);
-                       }
-
-               }
-
-       }
-
-       final static InternalProcedure<IntSet> NOP = new InternalProcedure<IntSet>() {
-
-               @Override
-               public void execute(ReadGraphImpl graph, IntSet result) {
-               }
-
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
-
-       };
+    }
 
-       final static TripleIntProcedure NOPT = new TripleIntProcedure() {
+    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, parent);
+        if(map == null) {
+            procedure.finished(graph);
+            return;
+        }
 
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
+        int size = map.size();
+        if(size == 3) {
+            int value = map.data[2];
+            procedure.execute(graph, value);
+            procedure.finished(graph);
+        } else if(size == 0) {
+            procedure.finished(graph);
+        } else {
 
-               @Override
-               public void execute(ReadGraphImpl graph, int s, int p, int o) {
-               }
+            int candidateS = map.data[0];
+            int candidateO = map.data[2];
 
-               @Override
-               public void finished(ReadGraphImpl graph) {
-               }
+            IntSet candidateIs = null;
+            try {
+                candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null);
+            } catch (DatabaseException e) {
+                procedure.exception(graph, e);
+                return;
+            }
 
-       };
+            for(int i=3;i<map.size();i+=3) {
 
-       // Search for one statement
-       final public void computeFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, final RelationInfo ri, final IntProcedure procedure) {
-               computeFunctionalIndex(graph, r1(), r2(), this, ri, procedure);
-       }
+                int nextS = map.data[i];
+                int nextO = map.data[i+2];
 
-       // 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) {
+                if(nextS != candidateS) {
 
-               if(ri.isFinal) {
+                    if(candidateIs.contains(nextS)) {
 
-                       int result = graph.processor.querySupport.getFunctionalObject(r1, r2);
+                        // Next is a super type of candidate => ignore next
 
-                       if(result == 0) {
+                    } else {
 
-                               // Check for assertions
-                               forSingleAssertion(graph, r1, r2, entry, procedure);
+                        IntSet nextIs = null;
+                        try {
+                            nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
+                        } catch (DatabaseException e) {
+                            procedure.exception(graph, e);
+                            return;
+                        }
 
-                       } else if (result == -1) {
+                        if(nextIs.contains(candidateS)) {
 
-                               graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
+                            // Candidate is a super type of next => next is the new candidate
 
-                                       @Override
-                                       public void execute(ReadGraphImpl graph, int i) {
-                                               if(entry != null) entry.addOrSetFunctional(i);
-                                               else procedure.execute(graph, i);
-                                       }
+                            candidateS = nextS;
+                            candidateO = nextO;
+                            candidateIs = nextIs;
 
-                                       @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                               if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                                       }
+                        } else {
 
-                                       @Override
-                                       public void finished(ReadGraphImpl graph) {
-                                       }
+                            // candidate and next are unrelated => error
+                            ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions " + r1 + ", " + r2 + " " + map , r1);
+                            procedure.exception(graph, exception);
+                            return;
 
-                               });
+                        }
 
-                               // Check for assertions
-                               forSingleAssertion(graph, r1, r2, entry, 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, candidateO);
+            procedure.finished(graph);
 
-               } else {
+        }
 
-                       // Note! The dependency is intentionally cut!
-                       DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
+    }
 
-                               boolean found = false;
+    final static InternalProcedure<IntSet> NOP = new InternalProcedure<IntSet>() {
 
-                               @Override
-                               public void run(ReadGraphImpl graph) {
+        @Override
+        public void execute(ReadGraphImpl graph, IntSet result) {
+        }
 
-                                       if(found) {
-                                               if(entry != null) entry.finish(graph, procedure);
-                                               else procedure.finished(graph);
-                                       } else {
+        @Override
+        public void exception(ReadGraphImpl graph, Throwable throwable) {
+        }
 
-                                               // Check for assertions
-                                               forSingleAssertion(graph, r1, r2, entry, procedure);
+    };
 
-                                       }
+    final static TripleIntProcedure NOPT = new TripleIntProcedure() {
 
-                               }
 
-                               @Override
-                               public void execute(ReadGraphImpl graph, final int pred) {
+        @Override
+        public void exception(ReadGraphImpl graph, Throwable throwable) {
+        }
 
-                                       if(found) return;
+        @Override
+        public void execute(ReadGraphImpl graph, int s, int p, int o) {
+        }
 
-                                       if(pred == r2) {
+        @Override
+        public void finished(ReadGraphImpl graph) {
+        }
 
-                                               // Note! The dependency is intentionally cut!
-                                               DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+    };
 
-                                                       @Override
-                                                       public void execute(ReadGraphImpl graph, int i) {
+    // Search for one statement
+    final public void computeFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
+        computeFunctionalIndex(graph, r1(), r2(), this, ri, procedure);
+    }
 
-                                                               if(!found) {
+    // Search for one statement
+    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(entry != null) entry.addOrSetFunctional(i);
-                                                                       else procedure.execute(graph, i);
+        if(ri.isFinal) {
 
-                                                                       found = true;
+            int result = graph.processor.querySupport.getFunctionalObject(r1, r2);
 
-                                                               } else {
+            if(result == 0) {
 
-                                                                       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);
+                // Check for assertions
+                forSingleAssertion(graph, r1, r2, parent, procedure);
 
-                                                               }
+            } else if (result == -1) {
 
-                                                       }
+                graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
-                                                       @Override
-                                                       public void finished(ReadGraphImpl graph) {
-                                                       }
+                    @Override
+                    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                        procedure.execute(graph, i);
+                    }
 
-                                                       @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                                               procedure.exception(graph, t);
-                                                       }
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) {
+                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                    }
 
-                                               });
+                    @Override
+                    public void finished(ReadGraphImpl graph) {
+                    }
 
-                                       } else {
+                });
 
-                                               SuperRelations.queryEach(graph, pred, graph.processor, entry, null, new InternalProcedure<IntSet>() {
+                // Check for assertions
+                forSingleAssertion(graph, r1, r2, parent, procedure);
 
-                                                       @Override
-                                                       public void execute(ReadGraphImpl graph, IntSet result) {
+            } else {
 
-                                                               if(found) return;
+                // If functional relation was found there is no need to check assertions
+                procedure.execute(graph, result);
+                procedure.finished(graph);
 
-                                                               if(result.contains(r2)) {
+            }
 
-                                                                       // Note! The dependency is intentionally cut!
-                                                                       DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+        } else {
 
-                                                                               @Override
-                                                                               public void execute(ReadGraphImpl graph, int i) {
+            // Note! The dependency is intentionally cut!
+            IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null);
+            direct.forEach(graph, new SyncIntProcedure() {
 
-                                                                                       if(!found) {
+                /*
+                 * 0 = not found
+                 * 1 = found
+                 * 2 = exception
+                 */
+                int found = 0;
 
-                                                                                               if(entry != null) entry.addOrSetFunctional(i);
-                                                                                               else procedure.execute(graph, i);
+                @Override
+                public void run(ReadGraphImpl graph) throws DatabaseException {
 
-                                                                                               found = true;
+                    if(found == 1) {
 
-                                                                                       } else {
+                        procedure.finished(graph);
 
-                                                                                               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);
+                    } else if(found == 0) {
 
-                                                                                       }
+                        // Check for assertions
+                        forSingleAssertion(graph, r1, r2, parent, procedure);
 
-                                                                               }
+                    }
 
-                                                                               @Override
-                                                                               public void finished(ReadGraphImpl graph) {
-                                                                               }
+                }
 
-                                                                               @Override
-                                                                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                                                                       procedure.exception(graph, t);
-                                                                               }
+                @Override
+                public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
 
-                                                                       });
+                    if(found > 0)
+                        return;
 
-                                                               }
+                    if(pred == r2) {
 
-                                                       }
+                        // Note! The dependency is intentionally cut!
+                        QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
-                                                       @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                                               procedure.exception(graph, t);
-                                                       }
+                            @Override
+                            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
 
-                                               });
+                                if(found == 0) {
 
-                                       }
+                                    procedure.execute(graph, i);
+                                    found = 1;
 
-                               }
+                                } else {
 
-                               @Override
-                               public void finished(ReadGraphImpl graph) {
+                                    ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
+                                    procedure.exception(graph, exception);
+                                    found = 2;
 
-                                       dec(graph);
+                                }
 
-                               }
+                            }
 
-                       });
+                            @Override
+                            public void finished(ReadGraphImpl graph) {
+                            }
 
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
 
-               }
+                                procedure.exception(graph, t);
+                                found = 2;
 
-       }
+                            }
 
-       final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) {
+                        });
 
-               // Note! The dependency is intentionally cut!
-               PrincipalTypes.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
+                    } else {
 
-                       @Override
-                       public void run(ReadGraphImpl graph) {
+                        QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
 
-                               if(entry != null) entry.finish(graph, procedure);
-                               else procedure.finished(graph);
+                            @Override
+                            public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
 
-                       }
+                                if(found > 0)
+                                    return;
 
-                       TripleIntProcedure proc = new TripleIntProcedure() {
+                                if(result.contains(r2)) {
 
-                               @Override
-                               public void execute(ReadGraphImpl graph, int s, int p, int o) {
-                                       if(entry != null) entry.addOrSet(o);
-                                       else procedure.execute(graph, o);
-                               }
+                                    // Note! The dependency is intentionally cut!
+                                    QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
-                               @Override
-                               public void finished(ReadGraphImpl graph) {
-                                       dec(graph);
-                               }
+                                        @Override
+                                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
 
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                       if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                                       procedure.exception(graph, t);
-                                       dec(graph);
-                               }
+                                            if(found == 0) {
 
-                       };
+                                                procedure.execute(graph, i);
+                                                found = 1;
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, int type) {
+                                            } else {
 
-                               inc();
+                                                ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
+                                                procedure.exception(graph, exception);
+                                                found = 2;
 
-                               AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, proc);
+                                            }
 
-                       }
+                                        }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                               dec(graph);
-                       }
+                                        @Override
+                                        public void finished(ReadGraphImpl graph) {
+                                        }
 
-               });
+                                        @Override
+                                        public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                            procedure.exception(graph, t);
+                                            found = 2;
+                                        }
 
+                                    });
 
-       }
+                                }
 
-       final public static void computeNotFunctionalFinalIndex(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, RelationInfo ri, AsyncMultiProcedure<Resource> procedure) {
+                            }
 
-               throw new Error();
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                procedure.exception(graph, t);
+                                found = 2;
+                            }
 
-       }
+                        });
 
-       final public void computeNotFunctionalIndex(ReadGraphImpl graph, RelationInfo ri, final IntProcedure procedure) {
-               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) {
+                }
 
-               if(ri.isFinal) {
+                @Override
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
+                    dec(graph);
+                }
 
-                       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);
-                               }
 
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                       if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                                       procedure.exception(graph, t);
-                               }
+        }
 
-                               @Override
-                               public void finished(ReadGraphImpl graph) {
-                               }
+    }
 
-                       });
+    final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final IntProcedure procedure) throws DatabaseException {
 
-                       if(ri.isAsserted) {
-                               forAssertions(graph, r1, r2, entry, procedure);
-                       } else {
-                               if(entry != null) entry.finish(graph, procedure);
-                               else procedure.finished(graph);
-                       }
+        // Note! The dependency is intentionally cut!
+        QueryCache.runnerPrincipalTypes(graph, r1, null, null, new SyncIntProcedure() {
 
-               } else {
+            @Override
+            public void run(ReadGraphImpl graph) throws DatabaseException {
+                procedure.finished(graph);
+            }
 
-                       // Note! The dependency is intentionally cut!
-                       DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
+            TripleIntProcedure proc = new TripleIntProcedure() {
 
-                               @Override
-                               public void run(ReadGraphImpl graph) {
+                @Override
+                public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
+                    procedure.execute(graph, o);
+                }
 
-                                       forAssertions(graph, r1, r2, entry, procedure);
+                @Override
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
+                    dec(graph);
+                }
 
-                               }
+                @Override
+                public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                    if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                    procedure.exception(graph, t);
+                    dec(graph);
+                }
 
-                               @Override
-                               public void execute(ReadGraphImpl graph, final int pred) {
+            };
 
-                                       if(pred == r2) {
+            @Override
+            public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
 
-                                               inc();
+                inc();
+                QueryCache.runnerAssertedStatements(graph, type, r2, parent, null, proc);
 
-                                               // Note! The dependency is intentionally cut!
-                                               DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+            }
 
-                                                       @Override
-                                                       public void execute(ReadGraphImpl graph, int i) {
-                                                               if(entry != null) entry.addOrSet(i);
-                                                               else procedure.execute(graph, i);
-                                                       }
+            @Override
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
+                dec(graph);
+            }
 
-                                                       @Override
-                                                       public void finished(ReadGraphImpl graph) {
-                                                               dec(graph);
-                                                       }
+        });
 
-                                                       @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                                               procedure.exception(graph, t);
-                                                               dec(graph);
-                                                       }
 
-                                               });
+    }
 
-                                       } else {
+    final public static void computeNotFunctionalFinalIndex(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, RelationInfo ri, AsyncMultiProcedure<Resource> procedure) {
+        throw new Error();
+    }
 
-                                               inc();
+    final public void computeNotFunctionalIndex(ReadGraphImpl graph, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
+        computeNotFunctionalIndex(graph, r1(), r2(), this, ri, procedure);
+    }
 
-                                               SuperRelations.queryEach(graph, pred, graph.processor, entry, null, new InternalProcedure<IntSet>() {
+    final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
 
-                                                       @Override
-                                                       public void execute(ReadGraphImpl graph, IntSet result) {
+        if(ri.isFinal) {
 
-                                                               if(result.contains(r2)) {
+            graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
-                                                                       inc();
+                @Override
+                public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                    procedure.execute(graph, i);
+                }
 
-                                                                       // Note! The dependency is intentionally cut!
-                                                                       DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+                @Override
+                public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                    if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                    procedure.exception(graph, t);
+                }
 
-                                                                               @Override
-                                                                               public void execute(ReadGraphImpl graph, int i) {
-                                                                                       if(entry != null) entry.addOrSet(i);
-                                                                                       else procedure.execute(graph, i);
-                                                                               }
+                @Override
+                public void finished(ReadGraphImpl graph) {
+                }
 
-                                                                               @Override
-                                                                               public void finished(ReadGraphImpl graph) {
-                                                                                       dec(graph);
-                                                                               }
+            });
 
-                                                                               @Override
-                                                                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                                                                       if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                                                                                       procedure.exception(graph, t);
-                                                                                       dec(graph);
-                                                                               }
+            if(ri.isAsserted) {
+                forAssertions(graph, r1, r2, parent, procedure);
+            } else {
+                procedure.finished(graph);
+            }
 
-                                                                       });
+        } else {
 
-                                                               }
+            // Note! The dependency is intentionally cut!
+            IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null);
+            direct.forEach(graph, new SyncIntProcedure() {
 
-                                                               dec(graph);
+                @Override
+                public void run(ReadGraphImpl graph) throws DatabaseException {
+                    forAssertions(graph, r1, r2, parent, procedure);
+                }
 
-                                                       }
+                @Override
+                public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
 
-                                                       @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                                               procedure.exception(graph, t);
-                                                               dec(graph);
-                                                       }
+                    if(pred == r2) {
 
-                                               });
+                        // Note! The dependency is intentionally cut!
+                        QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
-                                       }
+                            @Override
+                            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                procedure.execute(graph, i);
+                            }
 
-                               }
+                            @Override
+                            public void finished(ReadGraphImpl graph) throws DatabaseException {
+                            }
 
-                               @Override
-                               public void finished(ReadGraphImpl graph) {
-                                       dec(graph);
-                               }
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                procedure.exception(graph, t);
+                            }
 
-                       });
+                        });
 
-               }
+                    } else {
 
-       }
+                        try {
 
-       @Override
-       public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
-               computeForEach(graph, r1(), r2(), this, procedure);
-       }
+                            IntSet result = QueryCache.resultSuperRelations(graph, pred, parent, null);
+                            if(result.contains(r2)) {
 
-       public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) {
+                                inc();
 
-               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);
-               }
+                                // Note! The dependency is intentionally cut!
+                                QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
-       }
+                                    @Override
+                                    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                        procedure.execute(graph, i);
+                                    }
 
-       final static InternalProcedure<RelationInfo> ip = new InternalProcedure<RelationInfo>() {
+                                    @Override
+                                    public void finished(ReadGraphImpl graph) throws DatabaseException {
+                                        dec(graph);
+                                    }
 
-               @Override
-               public void execute(ReadGraphImpl graph, RelationInfo result) {
-               }
+                                    @Override
+                                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                                        procedure.exception(graph, t);
+                                        dec(graph);
+                                    }
 
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
+                                });
 
-       };
+                            }
 
-       @Override
-       public String toString() {
-               return "Objects[" + r1() + " - " + r2() + "]";
-       }
+                        } catch (Throwable e) {
+                            procedure.exception(graph, e);
+                        }
 
-       final private void finish(ReadGraphImpl graph, IntProcedure procedure) {
+                    }
 
-               assert(assertPending());
+                }
 
-               synchronized(this) {
-                       setReady();
-               }
+                @Override
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
+                    dec(graph);
+                }
 
-               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);
+    }
 
-       }
+    public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
+        computeForEach(graph, r1(), r2(), this, procedure);
+        return getResult();
+    }
 
-       final public void addOrSet(int add) {
+    public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure_) throws DatabaseException {
 
-               assert(assertPending());
+        IntProcedure procedure = entry != null ? entry : procedure_;
 
-               IntArray value = (IntArray)getResult();
-               synchronized(value) {
-                       value.add(add);
-               }
+        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 public void addOrSetFunctional(int add) {
+    }
 
-               assert(isPending());
+    @Override
+    public String toString() {
+        return "Objects[" + r1() + " - " + r2() + "]";
+    }
 
-               IntArray value = (IntArray)getResult();
-               value.add(add);
+    @Override
+    public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
 
-       }
+        assert(isReady());
 
-       @Override
-       public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
+        if(handleException(graph, procedure)) return getResult();
 
-               assert(isReady());
+        final IntArray value = (IntArray)getResult();
+        if(value.data == null) {
+            if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
+        } else {
+            for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
+        }
 
-               if(handleException(graph, procedure)) return;
+        procedure.finished(graph);
 
-               final IntArray value = (IntArray)getResult();
-               if(value.data == null) {
-                       if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
-               } else {
-                       for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
-               }
+        return value;
 
-               procedure.finished(graph);
+    }
 
-       }
+    @Override
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-       @Override
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+        compute(graph, new IntProcedureAdapter() {
 
-               final Semaphore s = new Semaphore(0);
+            @Override
+            public void finished(ReadGraphImpl graph) {
+            }
 
-               computeForEach(graph, provider, new IntProcedureAdapter() {
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) {
+                new Error("Error in recompute.", t).printStackTrace();
+            }
 
-                       @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);
+    @Override
+    public int type() {
+        return RequestFlags.IMMEDIATE_UPDATE;
+    }
 
-               while(!s.tryAcquire()) {
-                       provider.resume(graph);
-               }
+    @Override
+    boolean isImmutable(ReadGraphImpl graph) {
+        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 int type() {
-               return RequestFlags.IMMEDIATE_UPDATE;
-       }
+    @Override
+    public void finished(ReadGraphImpl graph) throws DatabaseException {
+        setReady();
+    }
 
-       @Override
-       boolean isImmutable(ReadGraphImpl graph) {
-               return graph.processor.isImmutable(r1());
-       }
+    @Override
+    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
+    }
 
+    @Override
+    public void serializeValue(QuerySerializer serializer) {
+        IntArray is = getResult();
+        is.serialize(serializer);
+    }
+    
 }