]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java
Use query caching previous to multi-query-thread to retain performance
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryCache.java
index ee65998d36d701d0149fc843139d8cd63d5e4c22..2fca451d66c14f97e6c70c31fdbf43d1969e0ce1 100644 (file)
@@ -19,6 +19,7 @@ import org.simantics.db.request.Read;
 
 public class QueryCache extends QueryCacheBase {
 
+    private static final boolean SINGLE = true;
     public QueryCache(QuerySupport querySupport, int threads) {
         super(querySupport, threads);
     }
@@ -53,6 +54,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,6 +75,12 @@ 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) {
@@ -97,6 +111,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,6 +132,12 @@ 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) {
@@ -141,6 +168,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,6 +189,12 @@ 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) {
@@ -185,6 +225,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,6 +246,12 @@ 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) {
@@ -229,6 +282,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,6 +303,12 @@ 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) {
@@ -273,6 +339,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,6 +360,12 @@ 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) {
@@ -317,6 +396,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,6 +417,12 @@ 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) {
@@ -361,6 +453,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,6 +474,12 @@ 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) {
@@ -405,6 +510,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,6 +531,12 @@ 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) {
@@ -449,6 +567,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,6 +588,12 @@ 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) {
@@ -486,6 +617,7 @@ public class QueryCache extends QueryCacheBase {
         }
         return existing;
     }
+
     
     void remove(ReadEntry entry) {
         synchronized(readEntryMap) {
@@ -496,6 +628,12 @@ public class QueryCache extends QueryCacheBase {
     public static Object runnerReadEntry(ReadGraphImpl graph, Read<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, final boolean needsToBlock) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+            if (SINGLE) {
+                ReadEntry e = cache.peekReadEntry(r);
+                if (e != null && e.isReady()) {
+                    return e.performFromCache(graph, procedure);
+                }
+            }
             return ReadEntry.computeForEach(graph, r, null, procedure);
         }
         ReadEntry entry = (ReadEntry)cache.getOrCreateReadEntry(graph, r, needsToBlock);
@@ -522,6 +660,13 @@ public class QueryCache extends QueryCacheBase {
           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;
@@ -546,6 +691,7 @@ public class QueryCache extends QueryCacheBase {
         }
         return existing;
     }
+
     
     void remove(AsyncReadEntry entry) {
         synchronized(asyncReadEntryMap) {
@@ -556,6 +702,12 @@ public class QueryCache extends QueryCacheBase {
     public static Object runnerAsyncReadEntry(ReadGraphImpl graph, AsyncRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, final 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);
@@ -582,6 +734,13 @@ public class QueryCache extends QueryCacheBase {
           return result;
         }
     }
+
+    
+    private AsyncReadEntry peekAsyncReadEntry(AsyncRead<?> r) {
+        synchronized(asyncReadEntryMap) {
+            return (AsyncReadEntry) asyncReadEntryMap.get(r);
+        }
+    }
     
     Types getOrCreateTypes(ReadGraphImpl graph, int r) throws DatabaseException {
         Types existing = null;
@@ -613,6 +772,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,6 +793,12 @@ 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) {
@@ -657,6 +829,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,6 +850,12 @@ 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) {
@@ -701,6 +886,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,6 +907,12 @@ 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) {
@@ -745,6 +943,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,6 +964,12 @@ 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) {
@@ -789,6 +1000,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,6 +1021,12 @@ 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) {
@@ -843,6 +1067,12 @@ 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) {
@@ -883,6 +1113,12 @@ 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) {
@@ -923,6 +1159,12 @@ 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) {
@@ -963,6 +1205,12 @@ 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) {
@@ -1003,6 +1251,12 @@ 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) {
@@ -1023,6 +1277,7 @@ public class QueryCache extends QueryCacheBase {
         if(existing.isPending()) waitPending(graph, existing);
         return existing;
     }
+
     
     void remove(ExternalReadEntry entry) {
         synchronized(externalReadEntryMap) {
@@ -1043,4 +1298,10 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    private ExternalReadEntry peekExternalReadEntry(ExternalRead<?> r) {
+        synchronized(externalReadEntryMap) {
+            return (ExternalReadEntry) externalReadEntryMap.get(r);
+        }
+    }
+    
 }