]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java
Still working for multiple readers
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryCache.java
index 49e02dece32049884f7a3907e85f960f2bf27f64..3e7a25190df94fe225f803bea23215c770727417 100644 (file)
@@ -2,9 +2,11 @@ package org.simantics.db.impl.query;
 
 import org.simantics.db.ObjectResourceIdMap;
 import org.simantics.db.RelationInfo;
+import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
+import org.simantics.db.impl.query.QueryProcessor.SessionTask;
 import org.simantics.db.procedure.AsyncMultiProcedure;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.procedure.ListenerBase;
@@ -49,18 +51,19 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerObjects(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+    public static void runnerObjects(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r1,r2)) {
             Objects.computeForEach(graph, r1,r2, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureObjects;
         Objects entry = (Objects)cache.getOrCreateObjects(r1,r2);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureObjects;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            Objects.computeForEach(graph, r1,r2, entry, procedure);
+          assert(entry.isPending());
+            Objects.computeForEach(graph, r1,r2, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -92,18 +95,19 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, TripleIntProcedure procedure) throws DatabaseException {
+    public static void runnerStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r1,r2)) {
             Statements.computeForEach(graph, r1,r2, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureStatements;
         Statements entry = (Statements)cache.getOrCreateStatements(r1,r2);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureStatements;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            Statements.computeForEach(graph, r1,r2, entry, procedure);
+          assert(entry.isPending());
+            Statements.computeForEach(graph, r1,r2, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -135,18 +139,19 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerDirectObjects(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+    public static void runnerDirectObjects(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r1,r2)) {
             DirectObjects.computeForEach(graph, r1,r2, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureDirectObjects;
         DirectObjects entry = (DirectObjects)cache.getOrCreateDirectObjects(r1,r2);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectObjects;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            DirectObjects.computeForEach(graph, r1,r2, entry, procedure);
+          assert(entry.isPending());
+            DirectObjects.computeForEach(graph, r1,r2, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -178,18 +183,19 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
+    public static void runnerRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
             RelationInfoQuery.computeForEach(graph, r, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureRelationInfoQuery;
         RelationInfoQuery entry = (RelationInfoQuery)cache.getOrCreateRelationInfoQuery(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        InternalProcedure<RelationInfo> procedure_ = procedure != null ? procedure : emptyProcedureRelationInfoQuery;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            RelationInfoQuery.computeForEach(graph, r, entry, procedure);
+          assert(entry.isPending());
+            RelationInfoQuery.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -221,18 +227,19 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener, InternalProcedure<Integer> procedure) throws DatabaseException {
+    public static void runnerURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener, final InternalProcedure<Integer> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, id)) {
             URIToResource.computeForEach(graph, id, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureURIToResource;
         URIToResource entry = (URIToResource)cache.getOrCreateURIToResource(id);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        InternalProcedure<Integer> procedure_ = procedure != null ? procedure : emptyProcedureURIToResource;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            URIToResource.computeForEach(graph, id, entry, procedure);
+          assert(entry.isPending());
+            URIToResource.computeForEach(graph, id, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -264,18 +271,19 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<byte[]> procedure) throws DatabaseException {
+    public static void runnerValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<byte[]> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
             ValueQuery.computeForEach(graph, r, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureValueQuery;
         ValueQuery entry = (ValueQuery)cache.getOrCreateValueQuery(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        InternalProcedure<byte[]> procedure_ = procedure != null ? procedure : emptyProcedureValueQuery;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            ValueQuery.computeForEach(graph, r, entry, procedure);
+          assert(entry.isPending());
+            ValueQuery.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -307,18 +315,19 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerOrderedSet(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+    public static void runnerOrderedSet(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
             OrderedSet.computeForEach(graph, r, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureOrderedSet;
         OrderedSet entry = (OrderedSet)cache.getOrCreateOrderedSet(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureOrderedSet;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            OrderedSet.computeForEach(graph, r, entry, procedure);
+          assert(entry.isPending());
+            OrderedSet.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -350,18 +359,19 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerPrincipalTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+    public static void runnerPrincipalTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
             PrincipalTypes.computeForEach(graph, r, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedurePrincipalTypes;
         PrincipalTypes entry = (PrincipalTypes)cache.getOrCreatePrincipalTypes(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedurePrincipalTypes;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            PrincipalTypes.computeForEach(graph, r, entry, procedure);
+          assert(entry.isPending());
+            PrincipalTypes.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -393,18 +403,19 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+    public static void runnerDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
             DirectPredicates.computeForEach(graph, r, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureDirectPredicates;
         DirectPredicates entry = (DirectPredicates)cache.getOrCreateDirectPredicates(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureDirectPredicates;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            DirectPredicates.computeForEach(graph, r, entry, procedure);
+          assert(entry.isPending());
+            DirectPredicates.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -436,23 +447,24 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+    public static void runnerPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
             Predicates.computeForEach(graph, r, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedurePredicates;
         Predicates entry = (Predicates)cache.getOrCreatePredicates(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedurePredicates;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            Predicates.computeForEach(graph, r, entry, procedure);
+          assert(entry.isPending());
+            Predicates.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
     
-    ReadEntry getOrCreateReadEntry(Read<?> r) throws DatabaseException {
+    ReadEntry getOrCreateReadEntry(Read<?> r, boolean isSync) throws DatabaseException {
         ReadEntry existing = null;
         synchronized(readEntryMap) {
             existing = (ReadEntry)readEntryMap.get(r);
@@ -469,7 +481,10 @@ public class QueryCache extends QueryCacheBase {
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(existing);
+        if(existing.isPending()) {
+          if(isSync) waitPending(existing);
+          else return null;
+        }
         return existing;
     }
     
@@ -479,23 +494,38 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerReadEntry(ReadGraphImpl graph, Read<?> r, CacheEntry parent, ListenerBase listener, AsyncProcedure procedure) throws DatabaseException {
+    public static void runnerReadEntry(ReadGraphImpl graph, Read<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, boolean isSync) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
             ReadEntry.computeForEach(graph, r, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureReadEntry;
-        ReadEntry entry = (ReadEntry)cache.getOrCreateReadEntry(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        ReadEntry entry = (ReadEntry)cache.getOrCreateReadEntry(r, isSync);
+        if(entry == null) {
+            graph.processor.schedule(Integer.MIN_VALUE, new SessionTask(r, graph.processor.THREAD_MASK+1, -1) {
+                @Override
+                public void run(int thread) {
+                    try {
+                        assert(!isSync);
+                        runnerReadEntry(graph, r, parent, listener, procedure, isSync);
+                    } catch (DatabaseException e) {
+                        Logger.defaultLogError(e);
+                    }
+                }
+            });
+            return;
+        }
+        AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureReadEntry;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            ReadEntry.computeForEach(graph, r, entry, procedure);
+          assert(entry.isPending());
+            ReadEntry.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
     
-    AsyncReadEntry getOrCreateAsyncReadEntry(AsyncRead<?> r) throws DatabaseException {
+    AsyncReadEntry getOrCreateAsyncReadEntry(AsyncRead<?> r, boolean isSync) throws DatabaseException {
         AsyncReadEntry existing = null;
         synchronized(asyncReadEntryMap) {
             existing = (AsyncReadEntry)asyncReadEntryMap.get(r);
@@ -512,7 +542,10 @@ public class QueryCache extends QueryCacheBase {
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(existing);
+        if(existing.isPending()) {
+          if(isSync) waitPending(existing);
+          else return null;
+        }
         return existing;
     }
     
@@ -522,18 +555,33 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerAsyncReadEntry(ReadGraphImpl graph, AsyncRead<?> r, CacheEntry parent, ListenerBase listener, AsyncProcedure procedure) throws DatabaseException {
+    public static void runnerAsyncReadEntry(ReadGraphImpl graph, AsyncRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, boolean isSync) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
             AsyncReadEntry.computeForEach(graph, r, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureAsyncReadEntry;
-        AsyncReadEntry entry = (AsyncReadEntry)cache.getOrCreateAsyncReadEntry(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        AsyncReadEntry entry = (AsyncReadEntry)cache.getOrCreateAsyncReadEntry(r, isSync);
+        if(entry == null) {
+            graph.processor.schedule(Integer.MIN_VALUE, new SessionTask(r, graph.processor.THREAD_MASK+1, -1) {
+                @Override
+                public void run(int thread) {
+                    try {
+                        assert(!isSync);
+                        runnerAsyncReadEntry(graph, r, parent, listener, procedure, isSync);
+                    } catch (DatabaseException e) {
+                        Logger.defaultLogError(e);
+                    }
+                }
+            });
+            return;
+        }
+        AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureAsyncReadEntry;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            AsyncReadEntry.computeForEach(graph, r, entry, procedure);
+          assert(entry.isPending());
+            AsyncReadEntry.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -565,18 +613,19 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+    public static void runnerTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
             Types.computeForEach(graph, r, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureTypes;
         Types entry = (Types)cache.getOrCreateTypes(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureTypes;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            Types.computeForEach(graph, r, entry, procedure);
+          assert(entry.isPending());
+            Types.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -608,18 +657,19 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
+    public static void runnerChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
             ChildMap.computeForEach(graph, r, null, procedure);
             return;
         }
-        if(procedure == null) procedure = emptyProcedureChildMap;
         ChildMap entry = (ChildMap)cache.getOrCreateChildMap(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        InternalProcedure<ObjectResourceIdMap<String>> procedure_ = procedure != null ? procedure : emptyProcedureChildMap;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            ChildMap.computeForEach(graph, r, entry, procedure);
+          assert(entry.isPending());
+            ChildMap.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -651,14 +701,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, TripleIntProcedure procedure) throws DatabaseException {
+    public static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        if(procedure == null) procedure = emptyProcedureAssertedStatements;
         AssertedStatements entry = (AssertedStatements)cache.getOrCreateAssertedStatements(r1,r2);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedStatements;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            entry.compute(graph, procedure);
+          assert(entry.isPending());
+            entry.compute(graph, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -690,14 +741,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerAssertedPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+    public static void runnerAssertedPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        if(procedure == null) procedure = emptyProcedureAssertedPredicates;
         AssertedPredicates entry = (AssertedPredicates)cache.getOrCreateAssertedPredicates(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedPredicates;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            entry.compute(graph, procedure);
+          assert(entry.isPending());
+            entry.compute(graph, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -729,14 +781,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerDirectSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+    public static void runnerDirectSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        if(procedure == null) procedure = emptyProcedureDirectSuperRelations;
         DirectSuperRelations entry = (DirectSuperRelations)cache.getOrCreateDirectSuperRelations(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectSuperRelations;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            entry.compute(graph, procedure);
+          assert(entry.isPending());
+            entry.compute(graph, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -768,14 +821,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+    public static void runnerSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        if(procedure == null) procedure = emptyProcedureSuperTypes;
         SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperTypes;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            entry.compute(graph, procedure);
+          assert(entry.isPending());
+            entry.compute(graph, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -807,14 +861,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerTypeHierarchy(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+    public static void runnerTypeHierarchy(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        if(procedure == null) procedure = emptyProcedureTypeHierarchy;
         TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureTypeHierarchy;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            entry.compute(graph, procedure);
+          assert(entry.isPending());
+            entry.compute(graph, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -846,14 +901,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+    public static void runnerSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        if(procedure == null) procedure = emptyProcedureSuperRelations;
         SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperRelations;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            entry.compute(graph, procedure);
+          assert(entry.isPending());
+            entry.compute(graph, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -885,14 +941,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerMultiReadEntry(ReadGraphImpl graph, MultiRead<?> r, CacheEntry parent, ListenerBase listener, AsyncMultiProcedure procedure) throws DatabaseException {
+    public static void runnerMultiReadEntry(ReadGraphImpl graph, MultiRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncMultiProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        if(procedure == null) procedure = emptyProcedureMultiReadEntry;
         MultiReadEntry entry = (MultiReadEntry)cache.getOrCreateMultiReadEntry(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        AsyncMultiProcedure procedure_ = procedure != null ? procedure : emptyProcedureMultiReadEntry;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            entry.compute(graph, procedure);
+          assert(entry.isPending());
+            entry.compute(graph, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -924,14 +981,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerAsyncMultiReadEntry(ReadGraphImpl graph, AsyncMultiRead<?> r, CacheEntry parent, ListenerBase listener, AsyncMultiProcedure procedure) throws DatabaseException {
+    public static void runnerAsyncMultiReadEntry(ReadGraphImpl graph, AsyncMultiRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncMultiProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        if(procedure == null) procedure = emptyProcedureAsyncMultiReadEntry;
         AsyncMultiReadEntry entry = (AsyncMultiReadEntry)cache.getOrCreateAsyncMultiReadEntry(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        AsyncMultiProcedure procedure_ = procedure != null ? procedure : emptyProcedureAsyncMultiReadEntry;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            entry.compute(graph, procedure);
+          assert(entry.isPending());
+            entry.compute(graph, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
@@ -963,14 +1021,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    public static void runnerExternalReadEntry(ReadGraphImpl graph, ExternalRead<?> r, CacheEntry parent, ListenerBase listener, AsyncProcedure procedure) throws DatabaseException {
+    public static void runnerExternalReadEntry(ReadGraphImpl graph, ExternalRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        if(procedure == null) procedure = emptyProcedureExternalReadEntry;
         ExternalReadEntry entry = (ExternalReadEntry)cache.getOrCreateExternalReadEntry(r);
-        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
-        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureExternalReadEntry;
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
-            entry.compute(graph, procedure);
+          assert(entry.isPending());
+            entry.compute(graph, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }