]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryCacheBase.java
index 6abaf6b6518e7b9239dad4830aa9acadd65492c5..b0bca135042fbe99a5f07ffc3f4cab28628a164b 100644 (file)
@@ -3,21 +3,16 @@ package org.simantics.db.impl.query;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.concurrent.Semaphore;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ObjectResourceIdMap;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RelationInfo;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.impl.DebugPolicy;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.impl.query.QueryProcessor.SessionTask;
 import org.simantics.db.procedure.AsyncMultiProcedure;
 import org.simantics.db.procedure.AsyncProcedure;
-import org.simantics.db.procedure.Listener;
 import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.procedure.Procedure;
 import org.simantics.db.procedure.SyncMultiProcedure;
@@ -71,8 +66,6 @@ public class QueryCacheBase {
        protected final StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>        asyncMultiReadEntryMap; 
        protected final StableHashMap<ExternalRead, ExternalReadEntry>            externalReadEntryMap; 
 
-       final THashMap<CacheEntry, ArrayList<ListenerEntry>>                      listeners;
-
        public final QuerySupport                                                 querySupport;
 
        public QueryCacheBase(QuerySupport querySupport, int threads) {
@@ -105,94 +98,11 @@ public class QueryCacheBase {
                asyncMultiReadEntryMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>(); 
                multiReadEntryMap = new StableHashMap<MultiRead, MultiReadEntry>(); 
                externalReadEntryMap = new StableHashMap<ExternalRead, ExternalReadEntry>(); 
-               listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
        }
 
-//     public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
-//
-//             AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
-//             AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
-//
-//             ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
-//
-//             try {
-//                     
-//                     query.perform(queryGraph, new AsyncProcedure<T>() {
-//
-//                             @Override
-//                             public void execute(AsyncReadGraph returnGraph, T result) {
-//                                     ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
-//                                     entry.addOrSet(parentGraph, result);
-//                                     try {
-//                                             procedure.execute(parentGraph, result);
-//                                     } catch (Throwable t) {
-//                                             t.printStackTrace();
-//                                     }
-////                                   parentBarrier.dec(query);
-//                             }
-//
-//                             @Override
-//                             public void exception(AsyncReadGraph returnGraph, Throwable t) {
-//                                     ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
-////                                   AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
-//                                     entry.except(parentGraph, t);
-//                                     try {
-//                                             procedure.exception(parentGraph, t);
-//                                     } catch (Throwable t2) {
-//                                             t2.printStackTrace();
-//                                     }
-////                                   parentBarrier.dec(query);
-//                             }
-//
-//                             @Override
-//                             public String toString() {
-//                                     return procedure.toString();
-//                             }
-//
-//                     });
-//
-//             } catch (Throwable t) {
-//
-//                     entry.except(t);
-//                     try {
-//                             procedure.exception(parentGraph, t);
-//                     } catch (Throwable t2) {
-//                             t2.printStackTrace();
-//                     }
-////                   parentBarrier.dec(query);
-//
-//             }
-//             
-//             return null;
-//             
-//     }
-
-//     public <T> Object performQuery(ReadGraphImpl parentGraph, final Read<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
-//
-//             ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
-//
-//             ReadEntry entry = (ReadEntry)entry_;
-//
-//             try {
-//
-//                     T result = (T)query.perform(queryGraph);
-//                     entry.addOrSet(queryGraph, result);
-//
-//                     return (T)entry.get(parentGraph, procedure_);
-//
-//             }  catch (Throwable t) {
-//
-//                     entry.except(t);
-//                     return (T)entry.get(parentGraph, procedure_);
-//
-//             }
-//             
-//     }
-
-
        public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
 
-               ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
+               ReadGraphImpl queryGraph = parentGraph.withParent(entry_, null, false);
 
                AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
                AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
@@ -255,7 +165,7 @@ public class QueryCacheBase {
 
        public <T> Object performQuery(ReadGraphImpl parentGraph, final MultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
 
-               ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
+               ReadGraphImpl queryGraph = parentGraph.withParent(entry_, null, true);
 
                MultiReadEntry entry = (MultiReadEntry)entry_;
                SyncMultiProcedure<T> procedure = (SyncMultiProcedure<T>)procedure_;
@@ -316,76 +226,6 @@ public class QueryCacheBase {
                
        }
        
-       public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
-
-               if (parent != null && !inferred) {
-                       try {
-                               if(!child.isImmutable(graph)) {
-                                       synchronized(child) {
-                                               child.addParent(parent);
-                                       }
-                               }
-                       } catch (DatabaseException e) {
-                               Logger.defaultLogError(e);
-                       }
-                       if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent);
-               }
-
-               if (listener != null) {
-                       return registerListener(child, listener, procedure);
-               } else {
-                       return null;
-               }
-
-       }
-       
-       public synchronized ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) {
-
-               assert (entry != null);
-
-               if (base.isDisposed())
-                       return null;
-
-               return addListener(entry, base, procedure);
-
-       }
-
-       protected void primeListenerEntry(final ListenerEntry entry, final Object result) {
-               entry.setLastKnown(result);
-       }
-
-       private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) {
-
-               assert (entry != null);
-               assert (procedure != null);
-
-               ArrayList<ListenerEntry> list = listeners.get(entry);
-               if (list == null) {
-                       list = new ArrayList<ListenerEntry>(1);
-                       listeners.put(entry, list);
-               }
-
-               ListenerEntry result = new ListenerEntry(entry, base, procedure);
-               int currentIndex = list.indexOf(result);
-               // There was already a listener
-               if(currentIndex > -1) {
-                       ListenerEntry current = list.get(currentIndex);
-                       if(!current.base.isDisposed()) return null;
-                       list.set(currentIndex, result);
-               } else {
-                       list.add(result);
-               }
-
-               if(DebugPolicy.LISTENER) {
-                       new Exception().printStackTrace();
-                       System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
-               }
-
-               return result;
-
-       }
-       
-       
        public Collection<CacheEntry> getRootList() {
 
                ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
@@ -577,7 +417,7 @@ public class QueryCacheBase {
                int counter = 0;
                while(entry.isPending()) {
                        try {
-                           boolean performed = graph.performPending();
+                           boolean performed = false;//graph.performPending();
                            if(!performed) {
                                        Thread.sleep(1);
                                        counter++;
@@ -1061,10 +901,6 @@ public class QueryCacheBase {
                return wrap.get();
        }
 
-       public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
-               return (T)QueryCache.runnerReadEntry(graph, r, parent, listener, procedure, true);
-       }
-
        public static <T> T resultAsyncReadEntry(ReadGraphImpl graph, AsyncRead r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
                return (T)QueryCache.runnerAsyncReadEntry(graph, r, parent, listener, procedure, true);
        }
@@ -1140,5 +976,158 @@ public class QueryCacheBase {
        static boolean shouldCache(QueryProcessor processor, Object o) {
                return false;
        }
+       
+       AssertedPredicates getOrCreateAssertedPredicates(int r) {
+           AssertedPredicates entry = (AssertedPredicates)assertedPredicatesMap.get(r);
+           if(entry == null) {
+               entry = new AssertedPredicates(r);
+            assertedPredicatesMap.put(keyR(r), entry);
+           }
+           return entry;
+       }
 
+    AssertedStatements getOrCreateAssertedStatements(int r1, int r2) {
+        AssertedStatements entry = (AssertedStatements)assertedStatementsMap.get(r1, r2);
+        if(entry == null) {
+            entry = new AssertedStatements(r1, r2);
+            assertedStatementsMap.put(keyR2(r1, r2), entry);
+        }
+        return entry;
+    }
+
+    ChildMap getOrCreateChildMap(int r) {
+        ChildMap entry = (ChildMap)childMapMap.get(r);
+        if(entry == null) {
+            entry = new ChildMap(r);
+            childMapMap.put(keyR(r), entry);
+        }
+        return entry;
+    }
+
+    DirectObjects getOrCreateDirectObjects(int r1, int r2) {
+        DirectObjects entry = (DirectObjects)directObjectsMap.get(r1, r2);
+        if(entry == null) {
+            entry = new DirectObjects(r1, r2);
+            directObjectsMap.put(keyR2(r1, r2), entry);
+        }
+        return entry;
+    }
+    
+    DirectPredicates getOrCreateDirectPredicates(int r) {
+        DirectPredicates entry = (DirectPredicates)directPredicatesMap.get(r);
+        if(entry == null) {
+            entry = new DirectPredicates(r);
+            directPredicatesMap.put(keyR(r), entry);
+        }
+        return entry;
+    }
+
+    Objects getOrCreateObjects(int r1, int r2) {
+        Objects entry = (Objects)objectsMap.get(r1, r2);
+        if(entry == null) {
+            entry = new Objects(r1, r2);
+            objectsMap.put(keyR2(r1, r2), entry);
+        }
+        return entry;
+    }
+
+    OrderedSet getOrCreateOrderedSet(int r) {
+        OrderedSet entry = (OrderedSet)orderedSetMap.get(r);
+        if(entry == null) {
+            entry = new OrderedSet(r);
+            orderedSetMap.put(keyR(r), entry);
+        }
+        return entry;
+    }
+
+    Predicates getOrCreatePredicates(int r) {
+        Predicates entry = (Predicates)predicatesMap.get(r);
+        if(entry == null) {
+            entry = new Predicates(r);
+            predicatesMap.put(keyR(r), entry);
+        }
+        return entry;
+    }
+
+    PrincipalTypes getOrCreatePrincipalTypes(int r) {
+        PrincipalTypes entry = (PrincipalTypes)principalTypesMap.get(r);
+        if(entry == null) {
+            entry = new PrincipalTypes(r);
+            principalTypesMap.put(keyR(r), entry);
+        }
+        return entry;
+    }
+
+    RelationInfoQuery getOrCreateRelationInfoQuery(int r) {
+        RelationInfoQuery entry = (RelationInfoQuery)relationInfoQueryMap.get(r);
+        if(entry == null) {
+            entry = new RelationInfoQuery(r);
+            relationInfoQueryMap.put(keyR(r), entry);
+        }
+        return entry;
+    }
+
+    Statements getOrCreateStatements(int r1, int r2) {
+        Statements entry = (Statements)statementsMap.get(r1, r2);
+        if(entry == null) {
+            entry = new Statements(r1, r2);
+            statementsMap.put(keyR2(r1, r2), entry);
+        }
+        return entry;
+    }
+
+    SuperRelations getOrCreateSuperRelations(int r) {
+        SuperRelations entry = (SuperRelations)superRelationsMap.get(r);
+        if(entry == null) {
+            entry = new SuperRelations(r);
+            superRelationsMap.put(keyR(r), entry);
+        }
+        return entry;
+    }
+
+    SuperTypes getOrCreateSuperTypes(int r) {
+        SuperTypes entry = (SuperTypes)superTypesMap.get(r);
+        if(entry == null) {
+            entry = new SuperTypes(r);
+            superTypesMap.put(keyR(r), entry);
+        }
+        return entry;
+    }
+
+    TypeHierarchy getOrCreateTypeHierarchy(int r) {
+        TypeHierarchy entry = (TypeHierarchy)typeHierarchyMap.get(r);
+        if(entry == null) {
+            entry = new TypeHierarchy(r);
+            typeHierarchyMap.put(keyR(r), entry);
+        }
+        return entry;
+    }
+
+    Types getOrCreateTypes(int r) {
+        Types entry = (Types)typesMap.get(r);
+        if(entry == null) {
+            entry = new Types(r);
+            typesMap.put(keyR(r), entry);
+        }
+        return entry;
+    }
+
+    URIToResource getOrCreateURIToResource(String s) {
+        URIToResource entry = (URIToResource)uRIToResourceMap.get(s);
+        if(entry == null) {
+            entry = new URIToResource(s);
+            uRIToResourceMap.put(keyID(s), entry);
+        }
+        return entry;
+    }
+
+    ValueQuery getOrCreateValueQuery(int r) {
+        ValueQuery entry = (ValueQuery)valueQueryMap.get(r);
+        if(entry == null) {
+            entry = new ValueQuery(r);
+            valueQueryMap.put(keyR(r), entry);
+        }
+        return entry;
+    }
+    
 }
\ No newline at end of file