]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java
Handle componentless parent nodes of UCs in synchronization
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryCache.java
index 86f759362bdd3a5ec216949d467ffd3a8b8749c3..06882a48943e711e502d725108ee2f0e4147f213 100644 (file)
@@ -19,6 +19,8 @@ import org.simantics.db.request.Read;
 
 public class QueryCache extends QueryCacheBase {
 
+    private static final boolean SINGLE = false;
+
     public QueryCache(QuerySupport querySupport, int threads) {
         super(querySupport, threads);
     }
@@ -29,18 +31,19 @@ public class QueryCache extends QueryCacheBase {
             existing = (Objects)objectsMap.get(r1,r2);
             if(existing == null) {
                 existing = new Objects(r1,r2);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 objectsMap.put(keyR2(r1,r2), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -53,6 +56,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                Objects e = cache.peekObjects(r1,r2);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             Objects.computeForEach(graph, r1,r2, null, procedure);
             return;
         }
@@ -67,24 +77,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private Objects peekObjects(int r1, int r2) {
+        synchronized(objectsMap) {
+            return (Objects) objectsMap.get(r1,r2);
+        }
+    }
+    
     Statements getOrCreateStatements(ReadGraphImpl graph, int r1, int r2) throws DatabaseException {
         Statements existing = null;
         synchronized(statementsMap) {
             existing = (Statements)statementsMap.get(r1,r2);
             if(existing == null) {
                 existing = new Statements(r1,r2);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 statementsMap.put(keyR2(r1,r2), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -97,6 +114,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                Statements e = cache.peekStatements(r1,r2);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             Statements.computeForEach(graph, r1,r2, null, procedure);
             return;
         }
@@ -111,24 +135,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private Statements peekStatements(int r1, int r2) {
+        synchronized(statementsMap) {
+            return (Statements) statementsMap.get(r1,r2);
+        }
+    }
+    
     DirectObjects getOrCreateDirectObjects(ReadGraphImpl graph, int r1, int r2) throws DatabaseException {
         DirectObjects existing = null;
         synchronized(directObjectsMap) {
             existing = (DirectObjects)directObjectsMap.get(r1,r2);
             if(existing == null) {
                 existing = new DirectObjects(r1,r2);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 directObjectsMap.put(keyR2(r1,r2), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -141,6 +172,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                DirectObjects e = cache.peekDirectObjects(r1,r2);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             DirectObjects.computeForEach(graph, r1,r2, null, procedure);
             return;
         }
@@ -155,24 +193,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private DirectObjects peekDirectObjects(int r1, int r2) {
+        synchronized(directObjectsMap) {
+            return (DirectObjects) directObjectsMap.get(r1,r2);
+        }
+    }
+    
     RelationInfoQuery getOrCreateRelationInfoQuery(ReadGraphImpl graph, int r) throws DatabaseException {
         RelationInfoQuery existing = null;
         synchronized(relationInfoQueryMap) {
             existing = (RelationInfoQuery)relationInfoQueryMap.get(r);
             if(existing == null) {
                 existing = new RelationInfoQuery(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 relationInfoQueryMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -185,6 +230,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                RelationInfoQuery e = cache.peekRelationInfoQuery(r);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             RelationInfoQuery.computeForEach(graph, r, null, procedure);
             return;
         }
@@ -199,24 +251,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private RelationInfoQuery peekRelationInfoQuery(int r) {
+        synchronized(relationInfoQueryMap) {
+            return (RelationInfoQuery) relationInfoQueryMap.get(r);
+        }
+    }
+    
     URIToResource getOrCreateURIToResource(ReadGraphImpl graph, String id) throws DatabaseException {
         URIToResource existing = null;
         synchronized(uRIToResourceMap) {
             existing = (URIToResource)uRIToResourceMap.get(id);
             if(existing == null) {
                 existing = new URIToResource(id);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 uRIToResourceMap.put(keyID(id), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -229,6 +288,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                URIToResource e = cache.peekURIToResource(id);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             URIToResource.computeForEach(graph, id, null, procedure);
             return;
         }
@@ -243,24 +309,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private URIToResource peekURIToResource(String id) {
+        synchronized(uRIToResourceMap) {
+            return (URIToResource) uRIToResourceMap.get(id);
+        }
+    }
+    
     ValueQuery getOrCreateValueQuery(ReadGraphImpl graph, int r) throws DatabaseException {
         ValueQuery existing = null;
         synchronized(valueQueryMap) {
             existing = (ValueQuery)valueQueryMap.get(r);
             if(existing == null) {
                 existing = new ValueQuery(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 valueQueryMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -273,6 +346,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                ValueQuery e = cache.peekValueQuery(r);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             ValueQuery.computeForEach(graph, r, null, procedure);
             return;
         }
@@ -287,24 +367,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private ValueQuery peekValueQuery(int r) {
+        synchronized(valueQueryMap) {
+            return (ValueQuery) valueQueryMap.get(r);
+        }
+    }
+    
     OrderedSet getOrCreateOrderedSet(ReadGraphImpl graph, int r) throws DatabaseException {
         OrderedSet existing = null;
         synchronized(orderedSetMap) {
             existing = (OrderedSet)orderedSetMap.get(r);
             if(existing == null) {
                 existing = new OrderedSet(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 orderedSetMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -317,6 +404,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                OrderedSet e = cache.peekOrderedSet(r);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             OrderedSet.computeForEach(graph, r, null, procedure);
             return;
         }
@@ -331,24 +425,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private OrderedSet peekOrderedSet(int r) {
+        synchronized(orderedSetMap) {
+            return (OrderedSet) orderedSetMap.get(r);
+        }
+    }
+    
     PrincipalTypes getOrCreatePrincipalTypes(ReadGraphImpl graph, int r) throws DatabaseException {
         PrincipalTypes existing = null;
         synchronized(principalTypesMap) {
             existing = (PrincipalTypes)principalTypesMap.get(r);
             if(existing == null) {
                 existing = new PrincipalTypes(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 principalTypesMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -361,6 +462,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                PrincipalTypes e = cache.peekPrincipalTypes(r);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             PrincipalTypes.computeForEach(graph, r, null, procedure);
             return;
         }
@@ -375,24 +483,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private PrincipalTypes peekPrincipalTypes(int r) {
+        synchronized(principalTypesMap) {
+            return (PrincipalTypes) principalTypesMap.get(r);
+        }
+    }
+    
     DirectPredicates getOrCreateDirectPredicates(ReadGraphImpl graph, int r) throws DatabaseException {
         DirectPredicates existing = null;
         synchronized(directPredicatesMap) {
             existing = (DirectPredicates)directPredicatesMap.get(r);
             if(existing == null) {
                 existing = new DirectPredicates(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 directPredicatesMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -405,6 +520,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                DirectPredicates e = cache.peekDirectPredicates(r);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             DirectPredicates.computeForEach(graph, r, null, procedure);
             return;
         }
@@ -419,24 +541,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private DirectPredicates peekDirectPredicates(int r) {
+        synchronized(directPredicatesMap) {
+            return (DirectPredicates) directPredicatesMap.get(r);
+        }
+    }
+    
     Predicates getOrCreatePredicates(ReadGraphImpl graph, int r) throws DatabaseException {
         Predicates existing = null;
         synchronized(predicatesMap) {
             existing = (Predicates)predicatesMap.get(r);
             if(existing == null) {
                 existing = new Predicates(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 predicatesMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -449,6 +578,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                Predicates e = cache.peekPredicates(r);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             Predicates.computeForEach(graph, r, null, procedure);
             return;
         }
@@ -463,114 +599,142 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private Predicates peekPredicates(int r) {
+        synchronized(predicatesMap) {
+            return (Predicates) predicatesMap.get(r);
+        }
+    }
+    
     ReadEntry getOrCreateReadEntry(ReadGraphImpl graph, Read<?> r, boolean needsToBlock) throws DatabaseException {
         ReadEntry existing = null;
         synchronized(readEntryMap) {
             existing = (ReadEntry)readEntryMap.get(r);
             if(existing == null) {
                 existing = new ReadEntry(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 readEntryMap.put(id(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
         if(existing.isPending()) {
-            if(needsToBlock) waitPending(graph, existing);
-            else return null;
+          if(needsToBlock)
+            waitPending(graph, existing);
+          else {
+            return null;
+          }
         }
         return existing;
     }
     
     void remove(ReadEntry entry) {
         synchronized(readEntryMap) {
-            readEntryMap.remove(entry.request);
+            readEntryMap.remove(entry.id);
         }
     }
     
-    public static Object runnerReadEntry(ReadGraphImpl graph, Read<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, final boolean needsToBlock) throws DatabaseException {
+    public static Object runnerReadEntry(ReadGraphImpl graph, Read<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, boolean needsToBlock) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
-            return ReadEntry.computeForEach(graph, r, null, procedure);
+            if (SINGLE) {
+                ReadEntry e = cache.peekReadEntry(r);
+                if (e != null && e.isReady()) {
+                    return e.performFromCache(graph, procedure);
+                }
+            }
+            return ReadEntry.computeForEach(graph, r, null, procedure, needsToBlock);
         }
         ReadEntry entry = (ReadEntry)cache.getOrCreateReadEntry(graph, r, needsToBlock);
         if(entry == null) {
-            graph.processor.schedule(new SessionTask(graph) {
-                @Override
-                public void run(int thread) {
-                    try {
-                        runnerReadEntry(graph, r, parent, listener, procedure, needsToBlock);
-                    } catch (DatabaseException e) {
-                        Logger.defaultLogError(e);
-                    }
-                }
-            });
-            return null;
+          graph.processor.schedule(new SessionTask(graph) {
+            @Override
+            public void run0(int thread) {
+              try {
+                runnerReadEntry(graph, r, parent, listener, procedure, needsToBlock);
+              } catch (DatabaseException e) {
+                Logger.defaultLogError(e);
+              }
+            }
+          });
+          return null;
         }
         AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureReadEntry;
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
         if(entry.isReady()) return entry.performFromCache(graph, procedure_);
         else {
           assert(entry.isPending());
-          Object result = ReadEntry.computeForEach(graph, r, entry, procedure_);
+          Object result = ReadEntry.computeForEach(graph, r, entry, procedure_, needsToBlock);
           if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
           return result;
         }
     }
     
+    private ReadEntry peekReadEntry(Read<?> r) {
+        synchronized(readEntryMap) {
+            return (ReadEntry) readEntryMap.get(r);
+        }
+    }
+    
     AsyncReadEntry getOrCreateAsyncReadEntry(ReadGraphImpl graph, AsyncRead<?> r, boolean needsToBlock) throws DatabaseException {
         AsyncReadEntry existing = null;
         synchronized(asyncReadEntryMap) {
             existing = (AsyncReadEntry)asyncReadEntryMap.get(r);
             if(existing == null) {
                 existing = new AsyncReadEntry(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 asyncReadEntryMap.put(id(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
         if(existing.isPending()) {
-            if(needsToBlock) waitPending(graph, existing);
-            else return null;
+          if(needsToBlock)
+            waitPending(graph, existing);
+          else {
+            return null;
+          }
         }
         return existing;
     }
     
     void remove(AsyncReadEntry entry) {
         synchronized(asyncReadEntryMap) {
-            asyncReadEntryMap.remove(entry.request);
+            asyncReadEntryMap.remove(entry.id);
         }
     }
     
-    public static Object runnerAsyncReadEntry(ReadGraphImpl graph, AsyncRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, final boolean needsToBlock) throws DatabaseException {
+    public static Object runnerAsyncReadEntry(ReadGraphImpl graph, AsyncRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, boolean needsToBlock) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+            if (SINGLE) {
+                AsyncReadEntry e = cache.peekAsyncReadEntry(r);
+                if (e != null && e.isReady()) {
+                    return e.performFromCache(graph, procedure);
+                }
+            }
             return AsyncReadEntry.computeForEach(graph, r, null, procedure, needsToBlock);
         }
         AsyncReadEntry entry = (AsyncReadEntry)cache.getOrCreateAsyncReadEntry(graph, r, needsToBlock);
         if(entry == null) {
-            graph.processor.schedule(new SessionTask(graph) {
-                @Override
-                public void run(int thread) {
-                    try {
-                        runnerAsyncReadEntry(graph, r, parent, listener, procedure, needsToBlock);
-                    } catch (DatabaseException e) {
-                        Logger.defaultLogError(e);
-                    }
-                }
-            });
-            return null;
+          graph.processor.schedule(new SessionTask(graph) {
+            @Override
+            public void run0(int thread) {
+              try {
+                runnerAsyncReadEntry(graph, r, parent, listener, procedure, needsToBlock);
+              } catch (DatabaseException e) {
+                Logger.defaultLogError(e);
+              }
+            }
+          });
+          return null;
         }
         AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureAsyncReadEntry;
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
@@ -583,24 +747,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private AsyncReadEntry peekAsyncReadEntry(AsyncRead<?> r) {
+        synchronized(asyncReadEntryMap) {
+            return (AsyncReadEntry) asyncReadEntryMap.get(r);
+        }
+    }
+    
     Types getOrCreateTypes(ReadGraphImpl graph, int r) throws DatabaseException {
         Types existing = null;
         synchronized(typesMap) {
             existing = (Types)typesMap.get(r);
             if(existing == null) {
                 existing = new Types(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 typesMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -613,6 +784,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                Types e = cache.peekTypes(r);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             Types.computeForEach(graph, r, null, procedure);
             return;
         }
@@ -627,24 +805,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private Types peekTypes(int r) {
+        synchronized(typesMap) {
+            return (Types) typesMap.get(r);
+        }
+    }
+    
     ChildMap getOrCreateChildMap(ReadGraphImpl graph, int r) throws DatabaseException {
         ChildMap existing = null;
         synchronized(childMapMap) {
             existing = (ChildMap)childMapMap.get(r);
             if(existing == null) {
                 existing = new ChildMap(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 childMapMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -657,6 +842,13 @@ public class QueryCache extends QueryCacheBase {
     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)) {
+            if (SINGLE) {
+                ChildMap e = cache.peekChildMap(r);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             ChildMap.computeForEach(graph, r, null, procedure);
             return;
         }
@@ -671,24 +863,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private ChildMap peekChildMap(int r) {
+        synchronized(childMapMap) {
+            return (ChildMap) childMapMap.get(r);
+        }
+    }
+    
     TypeHierarchy getOrCreateTypeHierarchy(ReadGraphImpl graph, int r) throws DatabaseException {
         TypeHierarchy existing = null;
         synchronized(typeHierarchyMap) {
             existing = (TypeHierarchy)typeHierarchyMap.get(r);
             if(existing == null) {
                 existing = new TypeHierarchy(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 typeHierarchyMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -701,6 +900,13 @@ public class QueryCache extends QueryCacheBase {
     public static void runnerTypeHierarchy(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)) {
+            if (SINGLE) {
+                TypeHierarchy e = cache.peekTypeHierarchy(r);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             TypeHierarchy.computeForEach(graph, r, null, procedure);
             return;
         }
@@ -715,24 +921,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private TypeHierarchy peekTypeHierarchy(int r) {
+        synchronized(typeHierarchyMap) {
+            return (TypeHierarchy) typeHierarchyMap.get(r);
+        }
+    }
+    
     SuperTypes getOrCreateSuperTypes(ReadGraphImpl graph, int r) throws DatabaseException {
         SuperTypes existing = null;
         synchronized(superTypesMap) {
             existing = (SuperTypes)superTypesMap.get(r);
             if(existing == null) {
                 existing = new SuperTypes(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 superTypesMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -745,6 +958,13 @@ public class QueryCache extends QueryCacheBase {
     public static void runnerSuperTypes(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)) {
+            if (SINGLE) {
+                SuperTypes e = cache.peekSuperTypes(r);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             SuperTypes.computeForEach(graph, r, null, procedure);
             return;
         }
@@ -759,24 +979,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private SuperTypes peekSuperTypes(int r) {
+        synchronized(superTypesMap) {
+            return (SuperTypes) superTypesMap.get(r);
+        }
+    }
+    
     SuperRelations getOrCreateSuperRelations(ReadGraphImpl graph, int r) throws DatabaseException {
         SuperRelations existing = null;
         synchronized(superRelationsMap) {
             existing = (SuperRelations)superRelationsMap.get(r);
             if(existing == null) {
                 existing = new SuperRelations(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 superRelationsMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -789,6 +1016,13 @@ public class QueryCache extends QueryCacheBase {
     public static void runnerSuperRelations(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)) {
+            if (SINGLE) {
+                SuperRelations e = cache.peekSuperRelations(r);
+                if (e != null && e.isReady()) {
+                    e.performFromCache(graph, procedure);
+                    return;
+                }
+            }
             SuperRelations.computeForEach(graph, r, null, procedure);
             return;
         }
@@ -803,24 +1037,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private SuperRelations peekSuperRelations(int r) {
+        synchronized(superRelationsMap) {
+            return (SuperRelations) superRelationsMap.get(r);
+        }
+    }
+    
     AssertedPredicates getOrCreateAssertedPredicates(ReadGraphImpl graph, int r) throws DatabaseException {
         AssertedPredicates existing = null;
         synchronized(assertedPredicatesMap) {
             existing = (AssertedPredicates)assertedPredicatesMap.get(r);
             if(existing == null) {
                 existing = new AssertedPredicates(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 assertedPredicatesMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -843,24 +1084,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private AssertedPredicates peekAssertedPredicates(int r) {
+        synchronized(assertedPredicatesMap) {
+            return (AssertedPredicates) assertedPredicatesMap.get(r);
+        }
+    }
+    
     AssertedStatements getOrCreateAssertedStatements(ReadGraphImpl graph, int r1, int r2) throws DatabaseException {
         AssertedStatements existing = null;
         synchronized(assertedStatementsMap) {
             existing = (AssertedStatements)assertedStatementsMap.get(r1,r2);
             if(existing == null) {
                 existing = new AssertedStatements(r1,r2);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 assertedStatementsMap.put(keyR2(r1,r2), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -883,24 +1131,31 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private AssertedStatements peekAssertedStatements(int r1, int r2) {
+        synchronized(assertedStatementsMap) {
+            return (AssertedStatements) assertedStatementsMap.get(r1,r2);
+        }
+    }
+    
     DirectSuperRelations getOrCreateDirectSuperRelations(ReadGraphImpl graph, int r) throws DatabaseException {
         DirectSuperRelations existing = null;
         synchronized(directSuperRelationsMap) {
             existing = (DirectSuperRelations)directSuperRelationsMap.get(r);
             if(existing == null) {
                 existing = new DirectSuperRelations(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 directSuperRelationsMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
@@ -923,30 +1178,37 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private DirectSuperRelations peekDirectSuperRelations(int r) {
+        synchronized(directSuperRelationsMap) {
+            return (DirectSuperRelations) directSuperRelationsMap.get(r);
+        }
+    }
+    
     MultiReadEntry getOrCreateMultiReadEntry(ReadGraphImpl graph, MultiRead<?> r) throws DatabaseException {
         MultiReadEntry existing = null;
         synchronized(multiReadEntryMap) {
             existing = (MultiReadEntry)multiReadEntryMap.get(r);
             if(existing == null) {
                 existing = new MultiReadEntry(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 multiReadEntryMap.put(id(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
     void remove(MultiReadEntry entry) {
         synchronized(multiReadEntryMap) {
-            multiReadEntryMap.remove(entry.request);
+            multiReadEntryMap.remove(entry.id);
         }
     }
     
@@ -963,30 +1225,37 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private MultiReadEntry peekMultiReadEntry(MultiRead<?> r) {
+        synchronized(multiReadEntryMap) {
+            return (MultiReadEntry) multiReadEntryMap.get(r);
+        }
+    }
+    
     AsyncMultiReadEntry getOrCreateAsyncMultiReadEntry(ReadGraphImpl graph, AsyncMultiRead<?> r) throws DatabaseException {
         AsyncMultiReadEntry existing = null;
         synchronized(asyncMultiReadEntryMap) {
             existing = (AsyncMultiReadEntry)asyncMultiReadEntryMap.get(r);
             if(existing == null) {
                 existing = new AsyncMultiReadEntry(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing.setPending(querySupport);
                 asyncMultiReadEntryMap.put(id(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
     void remove(AsyncMultiReadEntry entry) {
         synchronized(asyncMultiReadEntryMap) {
-            asyncMultiReadEntryMap.remove(entry.request);
+            asyncMultiReadEntryMap.remove(entry.id);
         }
     }
     
@@ -1003,30 +1272,37 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private AsyncMultiReadEntry peekAsyncMultiReadEntry(AsyncMultiRead<?> r) {
+        synchronized(asyncMultiReadEntryMap) {
+            return (AsyncMultiReadEntry) asyncMultiReadEntryMap.get(r);
+        }
+    }
+    
     ExternalReadEntry getOrCreateExternalReadEntry(ReadGraphImpl graph, ExternalRead<?> r) throws DatabaseException {
         ExternalReadEntry existing = null;
         synchronized(externalReadEntryMap) {
             existing = (ExternalReadEntry)externalReadEntryMap.get(r);
             if(existing == null) {
-                existing = new ExternalReadEntry(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
+                existing = new ExternalReadEntry(r, graph);
+                existing.setPending(querySupport);
                 externalReadEntryMap.put(id(r), existing);
                 size++;
                 return existing;
             }
             if(existing.requiresComputation()) {
-                existing.setPending();
+                existing.setPending(querySupport);
                 return existing;
             }
         }
-        if(existing.isPending()) waitPending(graph, existing);
+        if(existing.isPending()) {
+          waitPending(graph, existing);
+        }
         return existing;
     }
     
     void remove(ExternalReadEntry entry) {
         synchronized(externalReadEntryMap) {
-            externalReadEntryMap.remove(entry.request);
+            externalReadEntryMap.remove(entry.id);
         }
     }
     
@@ -1043,4 +1319,10 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private ExternalReadEntry peekExternalReadEntry(ExternalRead<?> r) {
+        synchronized(externalReadEntryMap) {
+            return (ExternalReadEntry) externalReadEntryMap.get(r);
+        }
+    }
+    
 }