]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java
Multiple simultaneous readers
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryCache.java
index 4be12da47d582b08cf0d6dfe16129d42650c7509..dbd0068c3f66100404eda02d5fbba9d70fdc8308 100644 (file)
@@ -47,7 +47,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, IntProcedure procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             Objects.computeForEach(graph, r1,r2, null, procedure);
             return;
@@ -89,7 +89,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, TripleIntProcedure procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             Statements.computeForEach(graph, r1,r2, null, procedure);
             return;
@@ -131,7 +131,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, IntProcedure procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             DirectObjects.computeForEach(graph, r1,r2, null, procedure);
             return;
@@ -173,7 +173,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             RelationInfoQuery.computeForEach(graph, r, null, procedure);
             return;
@@ -215,7 +215,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, InternalProcedure<Integer> procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             URIToResource.computeForEach(graph, id, null, procedure);
             return;
@@ -257,7 +257,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, InternalProcedure<byte[]> procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             ValueQuery.computeForEach(graph, r, null, procedure);
             return;
@@ -299,7 +299,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, IntProcedure procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             OrderedSet.computeForEach(graph, r, null, procedure);
             return;
@@ -341,7 +341,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, IntProcedure procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             PrincipalTypes.computeForEach(graph, r, null, procedure);
             return;
@@ -383,7 +383,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, InternalProcedure<IntSet> procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             DirectPredicates.computeForEach(graph, r, null, procedure);
             return;
@@ -425,7 +425,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, InternalProcedure<IntSet> procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             Predicates.computeForEach(graph, r, null, procedure);
             return;
@@ -467,7 +467,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, AsyncProcedure procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             ReadEntry.computeForEach(graph, r, null, procedure);
             return;
@@ -483,6 +483,48 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
+    AsyncReadEntry getOrCreateAsyncReadEntry(AsyncRead<?> r) throws DatabaseException {
+        AsyncReadEntry existing = null;
+        synchronized(asyncReadEntryMap) {
+            existing = (AsyncReadEntry)asyncReadEntryMap.get(r);
+            if(existing == null) {
+                existing = new AsyncReadEntry(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                asyncReadEntryMap.put(id(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(AsyncReadEntry entry) {
+        synchronized(asyncReadEntryMap) {
+            asyncReadEntryMap.remove(entry.request);
+        }
+    }
+    
+    public static void runnerAsyncReadEntry(ReadGraphImpl graph, AsyncRead<?> r, CacheEntry parent, ListenerBase listener, AsyncProcedure procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            AsyncReadEntry.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        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);
+        else {
+            AsyncReadEntry.computeForEach(graph, r, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
     Types getOrCreateTypes(int r) throws DatabaseException {
         Types existing = null;
         synchronized(typesMap) {
@@ -509,7 +551,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, InternalProcedure<IntSet> procedure) throws DatabaseException {
         if(parent == null && listener == null) {
             Types.computeForEach(graph, r, null, procedure);
             return;
@@ -525,15 +567,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    AssertedStatements getOrCreateAssertedStatements(int r1, int r2) throws DatabaseException {
-        AssertedStatements existing = null;
-        synchronized(assertedStatementsMap) {
-            existing = (AssertedStatements)assertedStatementsMap.get(r1,r2);
+    NamespaceIndex getOrCreateNamespaceIndex(String id) throws DatabaseException {
+        NamespaceIndex existing = null;
+        synchronized(namespaceIndexMap) {
+            existing = (NamespaceIndex)namespaceIndexMap.get(id);
             if(existing == null) {
-                existing = new AssertedStatements(r1,r2);
+                existing = new NamespaceIndex(id);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                assertedStatementsMap.put(keyR2(r1,r2), existing);
+                namespaceIndexMap.put(keyID(id), existing);
                 return existing;
             }
             if(existing.requiresComputation()) {
@@ -545,33 +587,37 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(AssertedStatements entry) {
-        synchronized(assertedStatementsMap) {
-            assertedStatementsMap.remove(entry.id);
+    void remove(NamespaceIndex entry) {
+        synchronized(namespaceIndexMap) {
+            namespaceIndexMap.remove(entry.id);
         }
     }
     
-    static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, TripleIntProcedure procedure) throws DatabaseException {
+    public static void runnerNamespaceIndex(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener, InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            NamespaceIndex.computeForEach(graph, id, null, procedure);
+            return;
+        }
         QueryCache cache  = graph.processor.cache;
-        if(procedure == null) procedure = emptyProcedureAssertedStatements;
-        AssertedStatements entry = (AssertedStatements)cache.getOrCreateAssertedStatements(r1,r2);
+        if(procedure == null) procedure = emptyProcedureNamespaceIndex;
+        NamespaceIndex entry = (NamespaceIndex)cache.getOrCreateNamespaceIndex(id);
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
         if(entry.isReady()) entry.performFromCache(graph, procedure);
         else {
-            entry.compute(graph, procedure);
+            NamespaceIndex.computeForEach(graph, id, entry, procedure);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
     
-    NamespaceIndex getOrCreateNamespaceIndex(String id) throws DatabaseException {
-        NamespaceIndex existing = null;
-        synchronized(namespaceIndexMap) {
-            existing = (NamespaceIndex)namespaceIndexMap.get(id);
+    AssertedStatements getOrCreateAssertedStatements(int r1, int r2) throws DatabaseException {
+        AssertedStatements existing = null;
+        synchronized(assertedStatementsMap) {
+            existing = (AssertedStatements)assertedStatementsMap.get(r1,r2);
             if(existing == null) {
-                existing = new NamespaceIndex(id);
+                existing = new AssertedStatements(r1,r2);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                namespaceIndexMap.put(keyID(id), existing);
+                assertedStatementsMap.put(keyR2(r1,r2), existing);
                 return existing;
             }
             if(existing.requiresComputation()) {
@@ -583,16 +629,16 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(NamespaceIndex entry) {
-        synchronized(namespaceIndexMap) {
-            namespaceIndexMap.remove(entry.id);
+    void remove(AssertedStatements entry) {
+        synchronized(assertedStatementsMap) {
+            assertedStatementsMap.remove(entry.id);
         }
     }
     
-    static void runnerNamespaceIndex(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener, InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
+    public static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, TripleIntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        if(procedure == null) procedure = emptyProcedureNamespaceIndex;
-        NamespaceIndex entry = (NamespaceIndex)cache.getOrCreateNamespaceIndex(id);
+        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);
         else {
@@ -627,7 +673,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, IntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(procedure == null) procedure = emptyProcedureAssertedPredicates;
         AssertedPredicates entry = (AssertedPredicates)cache.getOrCreateAssertedPredicates(r);
@@ -665,7 +711,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, IntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(procedure == null) procedure = emptyProcedureDirectSuperRelations;
         DirectSuperRelations entry = (DirectSuperRelations)cache.getOrCreateDirectSuperRelations(r);
@@ -703,7 +749,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(procedure == null) procedure = emptyProcedureSuperTypes;
         SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(r);
@@ -741,7 +787,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(procedure == null) procedure = emptyProcedureTypeHierarchy;
         TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(r);
@@ -779,7 +825,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(procedure == null) procedure = emptyProcedureSuperRelations;
         SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(r);
@@ -791,44 +837,6 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    AsyncReadEntry getOrCreateAsyncReadEntry(AsyncRead<?> r) throws DatabaseException {
-        AsyncReadEntry existing = null;
-        synchronized(asyncReadEntryMap) {
-            existing = (AsyncReadEntry)asyncReadEntryMap.get(r);
-            if(existing == null) {
-                existing = new AsyncReadEntry(r);
-                existing.clearResult(querySupport);
-                existing.setPending();
-                asyncReadEntryMap.put(id(r), existing);
-                return existing;
-            }
-            if(existing.requiresComputation()) {
-                existing.setPending();
-                return existing;
-            }
-        }
-        if(existing.isPending()) waitPending(existing);
-        return existing;
-    }
-    
-    void remove(AsyncReadEntry entry) {
-        synchronized(asyncReadEntryMap) {
-            asyncReadEntryMap.remove(entry.request);
-        }
-    }
-    
-    static void runnerAsyncReadEntry(ReadGraphImpl graph, AsyncRead<?> r, CacheEntry parent, ListenerBase listener, AsyncProcedure procedure) throws DatabaseException {
-        QueryCache cache  = graph.processor.cache;
-        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);
-        else {
-            entry.compute(graph, procedure);
-            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
-        }
-    }
-    
     MultiReadEntry getOrCreateMultiReadEntry(MultiRead<?> r) throws DatabaseException {
         MultiReadEntry existing = null;
         synchronized(multiReadEntryMap) {
@@ -855,7 +863,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, AsyncMultiProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(procedure == null) procedure = emptyProcedureMultiReadEntry;
         MultiReadEntry entry = (MultiReadEntry)cache.getOrCreateMultiReadEntry(r);
@@ -893,7 +901,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, AsyncMultiProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(procedure == null) procedure = emptyProcedureAsyncMultiReadEntry;
         AsyncMultiReadEntry entry = (AsyncMultiReadEntry)cache.getOrCreateAsyncMultiReadEntry(r);
@@ -931,7 +939,7 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    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, AsyncProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
         if(procedure == null) procedure = emptyProcedureExternalReadEntry;
         ExternalReadEntry entry = (ExternalReadEntry)cache.getOrCreateExternalReadEntry(r);