]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryCache.java
index 38ed21026f9ff2bbcbc70d7936f0aca30e7fc497..1467015512e38f81e6639fa8077f1808ec37865d 100644 (file)
@@ -16,8 +16,6 @@ import org.simantics.db.request.ExternalRead;
 import org.simantics.db.request.MultiRead;
 import org.simantics.db.request.Read;
 
-import gnu.trove.map.hash.TObjectIntHashMap;
-
 public class QueryCache extends QueryCacheBase {
 
     public QueryCache(QuerySupport querySupport, int threads) {
@@ -674,15 +672,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    AssertedStatements getOrCreateAssertedStatements(QueryProcessor processor, int r1, int r2) throws DatabaseException {
-        AssertedStatements existing = null;
-        synchronized(assertedStatementsMap) {
-            existing = (AssertedStatements)assertedStatementsMap.get(r1,r2);
+    TypeHierarchy getOrCreateTypeHierarchy(QueryProcessor processor, int r) throws DatabaseException {
+        TypeHierarchy existing = null;
+        synchronized(typeHierarchyMap) {
+            existing = (TypeHierarchy)typeHierarchyMap.get(r);
             if(existing == null) {
-                existing = new AssertedStatements(r1,r2);
+                existing = new TypeHierarchy(r);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                assertedStatementsMap.put(keyR2(r1,r2), existing);
+                typeHierarchyMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
@@ -695,34 +693,38 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(AssertedStatements entry) {
-        synchronized(assertedStatementsMap) {
-            assertedStatementsMap.remove(entry.id);
+    void remove(TypeHierarchy entry) {
+        synchronized(typeHierarchyMap) {
+            typeHierarchyMap.remove(entry.id);
         }
     }
     
-    public static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final TripleIntProcedure 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;
-        AssertedStatements entry = (AssertedStatements)cache.getOrCreateAssertedStatements(graph.processor, r1,r2);
-        TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedStatements;
+        if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+            TypeHierarchy.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(graph.processor, r);
+        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 {
           assert(entry.isPending());
-            entry.compute(graph, procedure_);
+            TypeHierarchy.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
     
-    AssertedPredicates getOrCreateAssertedPredicates(QueryProcessor processor, int r) throws DatabaseException {
-        AssertedPredicates existing = null;
-        synchronized(assertedPredicatesMap) {
-            existing = (AssertedPredicates)assertedPredicatesMap.get(r);
+    SuperTypes getOrCreateSuperTypes(QueryProcessor processor, int r) throws DatabaseException {
+        SuperTypes existing = null;
+        synchronized(superTypesMap) {
+            existing = (SuperTypes)superTypesMap.get(r);
             if(existing == null) {
-                existing = new AssertedPredicates(r);
+                existing = new SuperTypes(r);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                assertedPredicatesMap.put(keyR(r), existing);
+                superTypesMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
@@ -735,34 +737,38 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(AssertedPredicates entry) {
-        synchronized(assertedPredicatesMap) {
-            assertedPredicatesMap.remove(entry.id);
+    void remove(SuperTypes entry) {
+        synchronized(superTypesMap) {
+            superTypesMap.remove(entry.id);
         }
     }
     
-    public static void runnerAssertedPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure 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;
-        AssertedPredicates entry = (AssertedPredicates)cache.getOrCreateAssertedPredicates(graph.processor, r);
-        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedPredicates;
+        if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+            SuperTypes.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(graph.processor, r);
+        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 {
           assert(entry.isPending());
-            entry.compute(graph, procedure_);
+            SuperTypes.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
     
-    DirectSuperRelations getOrCreateDirectSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
-        DirectSuperRelations existing = null;
-        synchronized(directSuperRelationsMap) {
-            existing = (DirectSuperRelations)directSuperRelationsMap.get(r);
+    SuperRelations getOrCreateSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
+        SuperRelations existing = null;
+        synchronized(superRelationsMap) {
+            existing = (SuperRelations)superRelationsMap.get(r);
             if(existing == null) {
-                existing = new DirectSuperRelations(r);
+                existing = new SuperRelations(r);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                directSuperRelationsMap.put(keyR(r), existing);
+                superRelationsMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
@@ -775,34 +781,38 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(DirectSuperRelations entry) {
-        synchronized(directSuperRelationsMap) {
-            directSuperRelationsMap.remove(entry.id);
+    void remove(SuperRelations entry) {
+        synchronized(superRelationsMap) {
+            superRelationsMap.remove(entry.id);
         }
     }
     
-    public static void runnerDirectSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure 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;
-        DirectSuperRelations entry = (DirectSuperRelations)cache.getOrCreateDirectSuperRelations(graph.processor, r);
-        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectSuperRelations;
+        if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+            SuperRelations.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(graph.processor, r);
+        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 {
           assert(entry.isPending());
-            entry.compute(graph, procedure_);
+            SuperRelations.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
     
-    SuperTypes getOrCreateSuperTypes(QueryProcessor processor, int r) throws DatabaseException {
-        SuperTypes existing = null;
-        synchronized(superTypesMap) {
-            existing = (SuperTypes)superTypesMap.get(r);
+    AssertedPredicates getOrCreateAssertedPredicates(QueryProcessor processor, int r) throws DatabaseException {
+        AssertedPredicates existing = null;
+        synchronized(assertedPredicatesMap) {
+            existing = (AssertedPredicates)assertedPredicatesMap.get(r);
             if(existing == null) {
-                existing = new SuperTypes(r);
+                existing = new AssertedPredicates(r);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                superTypesMap.put(keyR(r), existing);
+                assertedPredicatesMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
@@ -815,16 +825,16 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(SuperTypes entry) {
-        synchronized(superTypesMap) {
-            superTypesMap.remove(entry.id);
+    void remove(AssertedPredicates entry) {
+        synchronized(assertedPredicatesMap) {
+            assertedPredicatesMap.remove(entry.id);
         }
     }
     
-    public static void runnerSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> 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;
-        SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(graph.processor, r);
-        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperTypes;
+        AssertedPredicates entry = (AssertedPredicates)cache.getOrCreateAssertedPredicates(graph.processor, r);
+        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedPredicates;
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
         if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
@@ -834,15 +844,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    TypeHierarchy getOrCreateTypeHierarchy(QueryProcessor processor, int r) throws DatabaseException {
-        TypeHierarchy existing = null;
-        synchronized(typeHierarchyMap) {
-            existing = (TypeHierarchy)typeHierarchyMap.get(r);
+    AssertedStatements getOrCreateAssertedStatements(QueryProcessor processor, int r1, int r2) throws DatabaseException {
+        AssertedStatements existing = null;
+        synchronized(assertedStatementsMap) {
+            existing = (AssertedStatements)assertedStatementsMap.get(r1,r2);
             if(existing == null) {
-                existing = new TypeHierarchy(r);
+                existing = new AssertedStatements(r1,r2);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                typeHierarchyMap.put(keyR(r), existing);
+                assertedStatementsMap.put(keyR2(r1,r2), existing);
                 size++;
                 return existing;
             }
@@ -855,16 +865,16 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(TypeHierarchy entry) {
-        synchronized(typeHierarchyMap) {
-            typeHierarchyMap.remove(entry.id);
+    void remove(AssertedStatements entry) {
+        synchronized(assertedStatementsMap) {
+            assertedStatementsMap.remove(entry.id);
         }
     }
     
-    public static void runnerTypeHierarchy(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> 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;
-        TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(graph.processor, r);
-        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureTypeHierarchy;
+        AssertedStatements entry = (AssertedStatements)cache.getOrCreateAssertedStatements(graph.processor, r1,r2);
+        TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedStatements;
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
         if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
@@ -874,15 +884,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    SuperRelations getOrCreateSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
-        SuperRelations existing = null;
-        synchronized(superRelationsMap) {
-            existing = (SuperRelations)superRelationsMap.get(r);
+    DirectSuperRelations getOrCreateDirectSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
+        DirectSuperRelations existing = null;
+        synchronized(directSuperRelationsMap) {
+            existing = (DirectSuperRelations)directSuperRelationsMap.get(r);
             if(existing == null) {
-                existing = new SuperRelations(r);
+                existing = new DirectSuperRelations(r);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                superRelationsMap.put(keyR(r), existing);
+                directSuperRelationsMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
@@ -895,16 +905,16 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(SuperRelations entry) {
-        synchronized(superRelationsMap) {
-            superRelationsMap.remove(entry.id);
+    void remove(DirectSuperRelations entry) {
+        synchronized(directSuperRelationsMap) {
+            directSuperRelationsMap.remove(entry.id);
         }
     }
     
-    public static void runnerSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> 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;
-        SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(graph.processor, r);
-        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperRelations;
+        DirectSuperRelations entry = (DirectSuperRelations)cache.getOrCreateDirectSuperRelations(graph.processor, r);
+        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectSuperRelations;
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
         if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {