]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
First changes 11/1611/1
authorAntti Villberg <antti.villberg@semantum.fi>
Wed, 21 Mar 2018 07:39:58 +0000 (09:39 +0200)
committerAntti Villberg <antti.villberg@semantum.fi>
Wed, 21 Mar 2018 07:39:58 +0000 (09:39 +0200)
refs #6861

Change-Id: Ia28f96305e977927b14098f79dc3975a67bfd676

40 files changed:
bundles/org.simantics.db.common/src/org/simantics/db/common/primitiverequest/ForEachAssertedObject.java
bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncMultiRead.java
bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncRead2.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedPredicates.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedStatements.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncMultiReadEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQuery.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHash.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntryBase.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectPredicates.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/MultiReadEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/OrderedSet.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Predicates.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PrincipalTypes.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java [new file with mode: 0644]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryProcessor.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/RelationInfoQuery.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Statements.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/StringQuery.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperRelations.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQuery.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/QueryDebugImpl.java
bundles/org.simantics.db/src/org/simantics/db/request/AsyncMultiRead.java
bundles/org.simantics.db/src/org/simantics/db/request/AsyncRead.java
bundles/org.simantics.db/src/org/simantics/db/request/Request.java [deleted file]
bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/queries/QueryExecutor2.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestParentTest3.java

index a71fd1ba6f91552ef78794dc84faac7a21f183da..541aee94e10d31c50480f7a2508a3f432face183 100644 (file)
  *******************************************************************************/
 package org.simantics.db.common.primitiverequest;
 
+import java.util.Collection;
+
 import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
-import org.simantics.db.common.request.ResourceAsyncMultiRead2;
+import org.simantics.db.common.request.ResourceRead2;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.service.CollectionSupport;
+import org.simantics.utils.DataContainer;
 
-final public class ForEachAssertedObject extends ResourceAsyncMultiRead2<Resource> {
+final public class ForEachAssertedObject extends ResourceRead2<Collection<Resource>> {
 
     public ForEachAssertedObject(Resource subject, Resource relation) {
         super(subject, relation);
     }
 
        @Override
-       public void perform(AsyncReadGraph graph, AsyncMultiProcedure<Resource> procedure) {
-       graph.forEachAssertedObject(resource, resource2, procedure);
+       public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
+               CollectionSupport cs = graph.getService(CollectionSupport.class);
+               Collection<Resource> result = cs.createSet();
+               DataContainer<Throwable> throwable = new DataContainer<Throwable>(null);
+               graph.forEachAssertedObject(resource, resource2, new AsyncMultiProcedure<Resource>() {
+                       
+                       @Override
+                       public void finished(AsyncReadGraph graph) {
+                       }
+                       
+                       @Override
+                       public void execute(AsyncReadGraph graph, Resource r) {
+                               result.add(r);
+                       }
+                       
+                       @Override
+                       public void exception(AsyncReadGraph graph, Throwable t) {
+                               throwable.set(t);
+                       }
+               });
+               Throwable t = throwable.get();
+               if(t != null)
+                       if(t instanceof DatabaseException)
+                               throw (DatabaseException)t;
+                       else throw new DatabaseException(t);
+               return result;
        }
 
 }
index 4e51a09973c47a946e14ebc127fbb912c480bd30..4d15bd5f3398cbda58f2c82537c39ef9dae8081f 100644 (file)
@@ -22,11 +22,6 @@ public abstract class ResourceAsyncMultiRead<T> implements AsyncMultiRead<T> {
     public int hashCode() {
         return resource.hashCode();
     }
-    
-    @Override
-    final public int threadHash() {
-       return resource.getThreadHash();
-    }
 
     @Override
     public boolean equals(Object object) {
index ae40ad4acc90db9e8f6240ab5edc09d675a92f77..1b13285d86ef6fd3b7872ab7bdf1cec8cea2231d 100644 (file)
@@ -23,9 +23,8 @@ import org.simantics.db.procedure.SyncListener;
 import org.simantics.db.procedure.SyncProcedure;
 import org.simantics.db.request.AsyncRead;
 import org.simantics.db.request.ReadInterface;
-import org.simantics.db.request.Request;
 
-public abstract class ResourceAsyncRead2<T> implements AsyncRead<T>, Request, ReadInterface<T> {
+public abstract class ResourceAsyncRead2<T> implements AsyncRead<T>, ReadInterface<T> {
 
     final protected Resource resource;
     final protected Resource resource2;
index 31b0f3e90e977c9d7d496739e9f9cafdaae19ccf..5ac4886c88799a10e1e3fc0a832d958e02c4cd46 100644 (file)
@@ -22,8 +22,6 @@ import org.simantics.db.procedure.ListenerBase;
 
 final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure> {
        
-//     public ArrayList<IntProcedure> procs = null;
-       
     public AssertedPredicates(final int r) {
         super(r);
     }
@@ -34,7 +32,7 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
     
     final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
 
-        AssertedPredicates entry = (AssertedPredicates)provider.assertedPredicatesMap.get(r);
+        AssertedPredicates entry = (AssertedPredicates)provider.cache.assertedPredicatesMap.get(r);
         if(entry == null) {
                
                entry = new AssertedPredicates(r);
@@ -64,7 +62,7 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
     final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
         
        if(parent == null && listener == null) {
-               AssertedPredicates entry = (AssertedPredicates)provider.assertedPredicatesMap.get(r);
+               AssertedPredicates entry = (AssertedPredicates)provider.cache.assertedPredicatesMap.get(r);
                if(entry != null && entry.isReady()) { 
                        entry.performFromCache(graph, provider, procedure);
                        return;
@@ -77,17 +75,17 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
 
     @Override
     public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-        return provider.assertedPredicatesMap.get(id);
+        return provider.cache.assertedPredicatesMap.get(id);
     }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-        provider.assertedPredicatesMap.put(id, this);
+        provider.cache.assertedPredicatesMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-           provider.assertedPredicatesMap.remove(id);
+           provider.cache.assertedPredicatesMap.remove(id);
        }
        
        void computeInheritedAssertions(ReadGraphImpl graph, int type, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) {
index 6e7ca62e55c3cdb824177528e65808856e845928..24634292cbf66d5ad93ee034bb6d4e3ce6449f6e 100644 (file)
@@ -26,8 +26,6 @@ import org.simantics.db.request.RequestFlags;
 
 final public class AssertedStatements extends CollectionBinaryQuery<TripleIntProcedure> {
     
-//    public ArrayList<TripleIntProcedure> procs;
-    
     public AssertedStatements(final int r1, final int r2) {
         super(r1, r2);
     }
@@ -38,7 +36,7 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
     
     final static AssertedStatements runner(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final AssertedStatements cached, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
         
-        AssertedStatements entry = cached != null ? cached : (AssertedStatements)provider.assertedStatementsMap.get(id(r1,r2)); 
+        AssertedStatements entry = cached != null ? cached : (AssertedStatements)provider.cache.assertedStatementsMap.get(id(r1,r2)); 
         if(entry == null) {
                
                entry = new AssertedStatements(r1, r2);
@@ -75,7 +73,7 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
         assert(r1 != 0);
         assert(r2 != 0);
         
-        final AssertedStatements entry = (AssertedStatements)provider.assertedStatementsMap.get(id(r1,r2));
+        final AssertedStatements entry = (AssertedStatements)provider.cache.assertedStatementsMap.get(id(r1,r2));
         
         if(parent == null && !(listener != null)) {
             if(entry != null && entry.isReady()) { 
@@ -90,17 +88,17 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
     @Override
     public BinaryQuery<TripleIntProcedure> getEntry(QueryProcessor provider) {
-        return provider.assertedStatementsMap.get(id);
+        return provider.cache.assertedStatementsMap.get(id);
     }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-        provider.assertedStatementsMap.put(id, this);
+        provider.cache.assertedStatementsMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-           provider.assertedStatementsMap.remove(id);
+           provider.cache.assertedStatementsMap.remove(id);
        }
        
        void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) {
@@ -478,11 +476,11 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
     }
 
     @Override
-    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
+    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
         
        assert(isReady());
         
-       if(handleException(graph, procedure)) return;
+       if(handleException(graph, procedure)) return getResult();
        
         final IntArray value = (IntArray)getResult();
         for(int i=0;i<value.size();i+=3) {
@@ -491,6 +489,8 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
         procedure.finished(graph);
         
+        return value;
+        
     }
     
     @Override
index 8a8b18b7dda5641113207102d8c43f54c5608716..52965dd7cf127637140958bba7320cfe7c800f45 100644 (file)
@@ -155,7 +155,7 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                       processor.asyncMultiReadMap.remove(request);
+                       processor.cache.asyncMultiReadMap.remove(request);
                        }
 
                        @Override
@@ -174,7 +174,7 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
     }
 
        @Override
-       public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
+       public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
                
                final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
 
@@ -206,7 +206,7 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
 
         }
                
-               
+               return getResult();
                
        }
 
index 010f554463373ace1edf3ac21cef13a4cc17122a..c6091eb268c2268ef9413c4ddf70caefb0a807b8 100644 (file)
@@ -128,7 +128,7 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
 
                        @Override
                        public void removeEntry(QueryProcessor qp) {
-                       qp.asyncReadMap.remove(request);
+                       qp.cache.asyncReadMap.remove(request);
                        }
 
                        @Override
@@ -147,7 +147,7 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
         
     }
 
-       public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
+       public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
                
         AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
 
@@ -169,6 +169,8 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
             
         }
                
+           return getResult();
+           
        }
 
        @Override
index e7af8d04d683f15fd9677ad6573596cd40851ab5..357d3d1d450406cc800580a3bd5f28c6602c74b5 100644 (file)
@@ -72,13 +72,13 @@ abstract public class BinaryQuery<Procedure> extends CacheEntryBase implements Q
     }
     
     @Override
-    public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-        performFromCache(graph, (QueryProcessor)provider, (Procedure)procedure);
+    public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
+        return performFromCache(graph, (QueryProcessor)provider, (Procedure)procedure);
     }
 
     abstract public void recompute(ReadGraphImpl graph, QueryProcessor provider);
     abstract public void computeForEach(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure, boolean store);
-    abstract public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure);
+    abstract public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure);
     abstract public void putEntry(QueryProcessor provider);
     abstract public void removeEntry(QueryProcessor provider);
     abstract public BinaryQuery<Procedure> getEntry(QueryProcessor provider);
index f09ccc082c4a425fc5ab2b5cc25169f9bcea7712..3ac46276fe9d88f49f710eaa101ab8ef9210080b 100644 (file)
@@ -74,25 +74,14 @@ abstract public class BinaryQueryHash<Procedure> extends THash {
             throw new Error("Not possible!");
         }
 
-//        @Override
-//        public void reset() {
-//            throw new Error("Not possible!");
-//        }
-
                @Override
-               public void performFromCache(ReadGraphImpl graph, QueryProcessor provider,
+               public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider,
                                Procedure procedure) {
             throw new Error("Not possible!");
                }
 
-//             @Override
-//             public void performFromCache(int callerThread, Object provider,
-//                             Object procedure) {
-//            throw new Error("Not possible!");
-//             }
-
                @Override
-               public void performFromCache(ReadGraphImpl graph, Object provider,
+               public Object performFromCache(ReadGraphImpl graph, Object provider,
                                Object procedure) {
             throw new Error("Not possible!");
                }
index d65622dadd0a18ecafb1c15b3c7c22b15b345cb0..34eba4eb123969bf99ca0623e2a3fc28144dd6fc 100644 (file)
@@ -50,7 +50,7 @@ public abstract class CacheEntry {
     abstract boolean moreThanOneParent(QueryProcessor processor);
     abstract int parentCount(QueryProcessor processor);
     
-    abstract void performFromCache(ReadGraphImpl graph, Object provider, Object procedure);
+    abstract Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure);
     
     abstract boolean isImmutable(ReadGraphImpl graph) throws DatabaseException;
     
index 5776857d330ad98374d9b8c07d575ddb0118f7b2..800d19bdd1000e91315d05e1b939ccc7c5de8025 100644 (file)
@@ -14,6 +14,7 @@ package org.simantics.db.impl.query;
 import java.util.ArrayList;
 import java.util.Iterator;
 
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.DebugPolicy;
 import org.simantics.db.impl.graph.ReadGraphImpl;
@@ -454,4 +455,9 @@ abstract public class CacheEntryBase extends CacheEntry {
        return getQuery();
     }
     
+//    abstract public void addOrSet(AsyncReadGraph graph, Object item);
+//    abstract public void except(AsyncReadGraph graph, Throwable t);
+    
+    //abstract public void perform(AsyncReadGraph graph, Object query, Object procedure);
+    
 }
index 2c106476ef263cf36c1a23835d7bc5fbd24152e0..360e067fa28754aaee943b088e55ae25548b8166 100644 (file)
@@ -37,12 +37,12 @@ final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
 
        final static DirectObjects entry(final QueryProcessor provider, final int r1, final int r2) {
 
-               return (DirectObjects)provider.directObjectsMap.get(id(r1,r2));
+               return (DirectObjects)provider.cache.directObjectsMap.get(id(r1,r2));
 
        }
        
        final static Collection<DirectObjects> entries(final QueryProcessor processor, final int r1) {
-               DoubleKeyQueryHashMap<IntProcedure> hash = processor.directObjectsMap;
+               DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
                return hash.values(r1);
        }
 
@@ -50,7 +50,7 @@ final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
 
        QueryProcessor processor = graph.processor;
                
-               DirectObjects entry = (DirectObjects)processor.directObjectsMap.get(id(r1,r2));
+               DirectObjects entry = (DirectObjects)processor.cache.directObjectsMap.get(id(r1,r2));
                if(entry == null) {
                        
                entry = new DirectObjects(r1, r2);
@@ -93,17 +93,17 @@ final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
 
        @Override
        public BinaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return provider.directObjectsMap.get(id);
+               return provider.cache.directObjectsMap.get(id);
        }
 
        @Override
        public void putEntry(QueryProcessor provider) {
-               provider.directObjectsMap.put(id, this);
+               provider.cache.directObjectsMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.directObjectsMap.remove(id);
+               provider.cache.directObjectsMap.remove(id);
        }
 
        @Override
@@ -155,10 +155,11 @@ final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
        }
 
        @Override
-       public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
+       public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
 
                assert(isReady());
                computeForEach(graph, provider, procedure, false);
+               return null;
 
        }
 
index 606afa430c1e35e870aaed5e35abbb51612aa0ef..117448b5be8032157c1f696c7df23643f3de47c3 100644 (file)
@@ -26,13 +26,13 @@ final public class DirectPredicates extends CollectionUnaryQuery<IntProcedure> {
 
        final static DirectPredicates entry(final QueryProcessor provider, final int r) {
 
-               return (DirectPredicates)provider.directPredicatesMap.get(r);
+               return (DirectPredicates)provider.cache.directPredicatesMap.get(r);
 
        }
 
        final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
 
-               DirectPredicates entry = (DirectPredicates)provider.directPredicatesMap.get(r);
+               DirectPredicates entry = (DirectPredicates)provider.cache.directPredicatesMap.get(r);
                if(entry == null) {
 
                        entry = new DirectPredicates(r);
@@ -63,7 +63,7 @@ final public class DirectPredicates extends CollectionUnaryQuery<IntProcedure> {
        final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
 
                if(parent == null && listener == null) {
-                       DirectPredicates entry = (DirectPredicates)provider.directPredicatesMap.get(r);
+                       DirectPredicates entry = (DirectPredicates)provider.cache.directPredicatesMap.get(r);
                        if(entry != null && entry.isReady()) { 
                                entry.performFromCache(graph, provider, procedure);
                                return;
@@ -86,17 +86,17 @@ final public class DirectPredicates extends CollectionUnaryQuery<IntProcedure> {
        
        @Override
        public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return provider.directPredicatesMap.get(id);
+               return provider.cache.directPredicatesMap.get(id);
        }
 
        @Override
        public void putEntry(QueryProcessor provider) {
-               provider.directPredicatesMap.put(id, this);
+               provider.cache.directPredicatesMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.directPredicatesMap.remove(id);
+               provider.cache.directPredicatesMap.remove(id);
        }
 
        @Override
index 18d91dc0618643e092aa4d242f6a50545cbf6c6e..31fe0103b646a23ed634c59a6616176406ba1d3d 100644 (file)
@@ -22,8 +22,6 @@ import org.simantics.db.request.RequestFlags;
 final public class ExternalReadEntry<T> extends CacheEntryBase {
 
     final LinkedList<T> items = new LinkedList<T>();
-    
-//    public ArrayList<Procedure<T>> procs;
 
     protected ExternalRead<T> request;
 
@@ -125,7 +123,7 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                               processor.externalReadMap.remove(request);
+                               processor.cache.externalReadMap.remove(request);
                        }
 
                        @Override
@@ -143,7 +141,7 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
         
     }
 
-       public void performFromCache(Object procedure) {
+       public Object performFromCache(Object procedure) {
                
         Procedure<T> proc = (Procedure<T>)procedure;
 
@@ -156,6 +154,8 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
             proc.execute((T)getResult());
 
         }
+           
+           return getResult();
                
        }
 
@@ -166,8 +166,8 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
        }
 
     @Override
-    public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-        performFromCache(procedure);
+    public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
+        return performFromCache(procedure);
     }
     
     @Override
index d0bb436d74e88e994a66fa9e27474f5a31139bfd..1a1f88de6c8664adba1e3625499041380c2c1045 100644 (file)
@@ -141,7 +141,7 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                       processor.multiReadMap.remove(request);
+                       processor.cache.multiReadMap.remove(request);
                        }
 
                        @Override
@@ -195,7 +195,7 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
     }
     
        @Override
-       public void performFromCache(ReadGraphImpl graph, Object provider,
+       public Object performFromCache(ReadGraphImpl graph, Object provider,
                        Object procedure) {
                
                final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
@@ -227,7 +227,7 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
 
         }
                
-               
+               return null;
                
        }
        
index 5267280032fa67f51f188f624d7224061c076ad1..a0a8c19cbef996a68749fd8947c583f10b0a93ff 100644 (file)
@@ -25,8 +25,6 @@ import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.ListenerBase;
 
 final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectIntHashMap<String>>> {
-    
-//     public ArrayList<InternalProcedure<TObjectIntHashMap<String>>> procs = null;
        
     private NamespaceIndex(final String id) {
         super(id);
@@ -34,7 +32,7 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
     
     final static void runner(ReadGraphImpl graph, final String id, final QueryProcessor provider, NamespaceIndex cached, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
 
-       NamespaceIndex entry = cached != null ? cached : (NamespaceIndex)provider.namespaceIndexMap22.get(id); 
+       NamespaceIndex entry = cached != null ? cached : (NamespaceIndex)provider.cache.namespaceIndexMap22.get(id); 
         if(entry == null) {
                
                entry = new NamespaceIndex(id);
@@ -64,7 +62,7 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
     
     final public static void queryEach(ReadGraphImpl graph, final String id, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
 
-       final NamespaceIndex entry = (NamespaceIndex)provider.namespaceIndexMap22.get(id);
+       final NamespaceIndex entry = (NamespaceIndex)provider.cache.namespaceIndexMap22.get(id);
         
        if(parent == null && listener == null && entry != null && entry.isReady()) {
                entry.performFromCache(graph, provider, procedure);
@@ -77,17 +75,17 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
      
        @Override
        public NamespaceIndex getEntry(QueryProcessor provider) {
-        return provider.namespaceIndexMap22.get(id);
+        return provider.cache.namespaceIndexMap22.get(id);
        }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-        provider.namespaceIndexMap22.put(id, this);
+        provider.cache.namespaceIndexMap22.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.namespaceIndexMap22.remove(id);
+               provider.cache.namespaceIndexMap22.remove(id);
        }
        
        final private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
index 0b3e1be34d3e8f16a135df845e95ebdbe4fc7ccb..a35826341360c015e65578e71b3fd36690c43a3a 100644 (file)
@@ -49,11 +49,11 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        }
 
        final static Objects entry(final QueryProcessor provider, final int r1, final int r2) {
-               return (Objects)provider.objectsMap.get(r1,r2);
+               return (Objects)provider.cache.objectsMap.get(r1,r2);
        }
 
        final static Collection<Objects> entries(final QueryProcessor processor, final int r1) {
-               return processor.objectsMap.values(r1);
+               return processor.cache.objectsMap.values(r1);
        }
 
        public final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) {
@@ -65,7 +65,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                QueryProcessor processor = graph.processor;
 
-               Objects entry = (Objects)processor.objectsMap.get(r1,r2);
+               Objects entry = (Objects)processor.cache.objectsMap.get(r1,r2);
                if(entry == null) {
 
                        entry = new Objects(r1, r2);
@@ -143,7 +143,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
         QueryProcessor processor = graph.processor;
 
-        Objects entry = (Objects)processor.objectsMap.get(r1,r2);
+        Objects entry = (Objects)processor.cache.objectsMap.get(r1,r2);
         if(entry == null) {
 
             entry = new Objects(r1, r2);
@@ -167,7 +167,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        
        @Override
        public BinaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return provider.objectsMap.get(id);
+               return provider.cache.objectsMap.get(id);
        }
 
        @Override
@@ -177,12 +177,12 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                System.err.println("put " + this);
                        }
                }
-               provider.objectsMap.put(id, this);
+               provider.cache.objectsMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.objectsMap.remove(id);
+               provider.cache.objectsMap.remove(id);
        }
 
        final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) {
@@ -853,11 +853,11 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        }
 
        @Override
-       public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
+       public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
 
                assert(isReady());
 
-               if(handleException(graph, procedure)) return;
+               if(handleException(graph, procedure)) return getResult();
 
                final IntArray value = (IntArray)getResult();
                if(value.data == null) {
@@ -867,6 +867,8 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                }
 
                procedure.finished(graph);
+               
+               return value;
 
        }
 
index 65cfb007ce4e040f8014696dec1694ad58c678df..c693d2a91f0e38add060c1be564f97252e806a56 100644 (file)
@@ -21,21 +21,19 @@ import org.simantics.db.procedure.ListenerBase;
 
 final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
        
-//     public ArrayList<IntProcedure> procs = null;
-
        public OrderedSet(final int r) {
         super(r);
     }
 
     final static OrderedSet entry(final QueryProcessor provider, final int r) {
         
-        return (OrderedSet)provider.orderedSetMap.get(r);
+        return (OrderedSet)provider.cache.orderedSetMap.get(r);
 
     }
        
     final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, OrderedSet cached, final CacheEntry parent, ListenerBase listener, final IntProcedure procedure) {
         
-       OrderedSet entry = cached != null ? cached : (OrderedSet)provider.orderedSetMap.get(r); 
+       OrderedSet entry = cached != null ? cached : (OrderedSet)provider.cache.orderedSetMap.get(r); 
         if(entry == null) {
                
                entry = new OrderedSet(r);
@@ -67,7 +65,7 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
         
        assert(r != 0);
        
-       final OrderedSet entry = (OrderedSet)provider.orderedSetMap.get(r);
+       final OrderedSet entry = (OrderedSet)provider.cache.orderedSetMap.get(r);
        
        if(parent == null && !(listener != null)) {
                if(entry != null && entry.isReady()) { 
@@ -82,17 +80,17 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
 
     @Override
     public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-        return provider.orderedSetMap.get(id);
+        return provider.cache.orderedSetMap.get(id);
     }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-           provider.orderedSetMap.put(id, this);
+           provider.cache.orderedSetMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-        provider.orderedSetMap.remove(id);
+        provider.cache.orderedSetMap.remove(id);
        }
     
        private int current = 0;
index 960cdb6c02aba26fe709af0fac79e1c4c28412ae..312522007e4f53483490e08554fb4773dd86e64a 100644 (file)
@@ -21,8 +21,6 @@ import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
 
 final public class Predicates extends UnaryQuery<IntProcedure> {
-       
-//     public ArrayList<IntProcedure> procs;
 
        public Predicates(final int r) {
         super(r);
@@ -34,13 +32,13 @@ final public class Predicates extends UnaryQuery<IntProcedure> {
     
     final static Predicates entry(final QueryProcessor provider, final int r) {
         
-        return (Predicates)provider.predicatesMap.get(r);
+        return (Predicates)provider.cache.predicatesMap.get(r);
 
     }
     
     final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, Predicates cached, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
 
-       Predicates entry = cached != null ? cached : (Predicates)provider.predicatesMap.get(r); 
+       Predicates entry = cached != null ? cached : (Predicates)provider.cache.predicatesMap.get(r); 
         if(entry == null) {
                
                entry = new Predicates(r);
@@ -66,7 +64,7 @@ final public class Predicates extends UnaryQuery<IntProcedure> {
 
     final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
 
-       Predicates entry = (Predicates)provider.predicatesMap.get(r); 
+       Predicates entry = (Predicates)provider.cache.predicatesMap.get(r); 
         if(entry == null) {
                
                entry = new Predicates(r);
@@ -95,7 +93,7 @@ final public class Predicates extends UnaryQuery<IntProcedure> {
         
        assert(r != 0);
        
-       final Predicates entry = (Predicates)provider.predicatesMap.get(r);
+       final Predicates entry = (Predicates)provider.cache.predicatesMap.get(r);
        
        if(parent == null && listener == null) {
                if(entry != null && entry.isReady()) { 
@@ -111,7 +109,7 @@ final public class Predicates extends UnaryQuery<IntProcedure> {
     final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
         
        if(parent == null) {
-               final Predicates entry = (Predicates)provider.predicatesMap.get(r);
+               final Predicates entry = (Predicates)provider.cache.predicatesMap.get(r);
                if(entry != null && entry.isReady()) {
                        return (IntSet)entry.get(graph, provider, null);
                }
@@ -123,17 +121,17 @@ final public class Predicates extends UnaryQuery<IntProcedure> {
     
     @Override
     public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-        return provider.predicatesMap.get(id);
+        return provider.cache.predicatesMap.get(id);
     }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-           provider.predicatesMap.put(id, this);
+           provider.cache.predicatesMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-        provider.predicatesMap.remove(id);
+        provider.cache.predicatesMap.remove(id);
        }
     
     final private void forAssertions(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure procedure, final boolean store) {
index 25e2b6fe3d317e682d41ebf42f60c2f6a628a83d..60fe074115e44aceb77d4b92a5a7b17a50724c4a 100644 (file)
@@ -11,9 +11,6 @@
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.procedure.TIntProcedure;
-import gnu.trove.set.hash.TIntHashSet;
-
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.concurrent.Semaphore;
@@ -23,23 +20,24 @@ import org.simantics.db.impl.procedure.IntProcedureAdapter;
 import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.ListenerBase;
 
-final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
+import gnu.trove.procedure.TIntProcedure;
+import gnu.trove.set.hash.TIntHashSet;
 
-//     public ArrayList<IntProcedure> procs = null;
+final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
        private PrincipalTypes(final int resource) {
                super(resource);
        }
 
        final static PrincipalTypes entry(final QueryProcessor provider, final int r) {
-               return (PrincipalTypes)provider.principalTypesMap.get(r);
+               return (PrincipalTypes)provider.cache.principalTypesMap.get(r);
        }
 
        final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
 
        QueryProcessor processor = graph.processor;
                
-               PrincipalTypes entry = (PrincipalTypes)processor.principalTypesMap.get(r); 
+               PrincipalTypes entry = (PrincipalTypes)processor.cache.principalTypesMap.get(r); 
                if(entry == null) {
 
                        entry = new PrincipalTypes(r);
@@ -81,17 +79,17 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
        @Override
        public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return provider.principalTypesMap.get(id);
+               return provider.cache.principalTypesMap.get(id);
        }
 
        @Override
        public void putEntry(QueryProcessor provider) {
-               provider.principalTypesMap.put(id, this);
+               provider.cache.principalTypesMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.principalTypesMap.remove(id);
+               provider.cache.principalTypesMap.remove(id);
        }
 
        static class Koss {
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java
new file mode 100644 (file)
index 0000000..2adc7c7
--- /dev/null
@@ -0,0 +1,746 @@
+package org.simantics.db.impl.query;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.simantics.db.AsyncReadGraph;
+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.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.request.AsyncMultiRead;
+import org.simantics.db.request.AsyncRead;
+import org.simantics.db.request.ExternalRead;
+import org.simantics.db.request.MultiRead;
+import org.simantics.db.request.Read;
+
+import gnu.trove.map.hash.THashMap;
+
+public class QueryCache {
+
+       final public UnaryQueryHashMap<IntProcedure>                      directPredicatesMap;
+       final public UnaryQueryHashMap<IntProcedure>                      principalTypesMap;
+       final public THashMap<String, URIToResource>                      uriToResourceMap;
+       final public THashMap<String, NamespaceIndex>                     namespaceIndexMap22;
+       final public UnaryQueryHashMap<IntProcedure>                      projectsMap;
+       final public UnaryQueryHashMap<InternalProcedure<RelationInfo>>   relationInfoMap;
+       final public UnaryQueryHashMap<InternalProcedure<IntSet>>         superTypesMap;
+       final public UnaryQueryHashMap<InternalProcedure<IntSet>>         typeHierarchyMap;
+       final public UnaryQueryHashMap<InternalProcedure<IntSet>>         superRelationsMap;
+       final public UnaryQueryHashMap<InternalProcedure<IntSet>>         typesMap;
+       final public UnaryQueryHashMap<InternalProcedure<byte[]>>         valueMap;
+       final public DoubleKeyQueryHashMap<IntProcedure>                     directObjectsMap;
+       final public DoubleKeyQueryHashMap<IntProcedure>                     objectsMap;
+       final public UnaryQueryHashMap<IntProcedure>                      orderedSetMap;
+       final public UnaryQueryHashMap<IntProcedure>                      predicatesMap;
+       final public DoubleKeyQueryHashMap<TripleIntProcedure>               statementsMap;
+       final public UnaryQueryHashMap<IntProcedure>                      assertedPredicatesMap;
+       final public BinaryQueryHashMap<TripleIntProcedure>               assertedStatementsMap;
+       final public StableHashMap<ExternalRead, ExternalReadEntry>            externalReadMap; 
+       final public StableHashMap<AsyncRead, AsyncReadEntry>                  asyncReadMap; 
+       final public StableHashMap<Read, ReadEntry>                            readMap;
+       final public StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>        asyncMultiReadMap; 
+       final public StableHashMap<MultiRead, MultiReadEntry>                  multiReadMap; 
+
+       final THashMap<CacheEntry, ArrayList<ListenerEntry>>       listeners;
+
+       public QueryCache() {
+               directPredicatesMap = new UnaryQueryHashMap();
+               valueMap = new UnaryQueryHashMap();
+               principalTypesMap = new UnaryQueryHashMap();
+               uriToResourceMap = new THashMap<String, URIToResource>();
+               namespaceIndexMap22 = new THashMap<String, NamespaceIndex>();
+               projectsMap = new UnaryQueryHashMap();
+               relationInfoMap = new UnaryQueryHashMap();
+               typeHierarchyMap = new UnaryQueryHashMap();
+               superTypesMap = new UnaryQueryHashMap();
+               superRelationsMap = new UnaryQueryHashMap();
+               typesMap = new UnaryQueryHashMap();
+               objectsMap = new DoubleKeyQueryHashMap();
+               orderedSetMap = new UnaryQueryHashMap();
+               predicatesMap = new UnaryQueryHashMap();
+               statementsMap = new DoubleKeyQueryHashMap();
+               directObjectsMap = new DoubleKeyQueryHashMap();
+               assertedPredicatesMap = new UnaryQueryHashMap();
+               assertedStatementsMap = new BinaryQueryHashMap();
+               asyncReadMap = new StableHashMap<AsyncRead, AsyncReadEntry>(); 
+               readMap = new StableHashMap<Read, ReadEntry>();
+               asyncMultiReadMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>(); 
+               multiReadMap = new StableHashMap<MultiRead, MultiReadEntry>(); 
+               externalReadMap = new StableHashMap<ExternalRead, ExternalReadEntry>(); 
+               listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
+       }
+       
+       public int requestHash(Object object) {
+               try {
+                       return object.hashCode();
+               } catch (Throwable t) {
+                       Logger.defaultLogError(t);
+                       return 0;
+               }
+       }
+
+       private CacheEntryBase getCached(Object query, int hash) {
+               if (query instanceof AsyncRead)
+                       return asyncReadMap.get(query, hash);
+               else if (query instanceof Read)
+                       return readMap.get(query, hash);
+               else if (query instanceof ExternalRead)
+                       return externalReadMap.get(query, hash);
+               else if (query instanceof AsyncMultiRead)
+                       return asyncMultiReadMap.get(query, hash);
+               throw new IllegalStateException();
+       }
+       
+       private CacheEntryBase createEntry(QuerySupport support, Object query, int hash) {
+
+               CacheEntryBase result;
+               if (query instanceof AsyncRead) {
+                       AsyncReadEntry entry = new AsyncReadEntry((AsyncRead)query);
+                       asyncReadMap.put((AsyncRead)query, entry, hash);
+                       result = entry;
+               } else if (query instanceof Read) {
+                       ReadEntry entry = new ReadEntry((Read)query);
+                       readMap.put((Read)query, entry, hash);
+                       result = entry;
+               } else if (query instanceof ExternalRead) {
+                       ExternalReadEntry entry = new ExternalReadEntry((ExternalRead)query);
+                       externalReadMap.put((ExternalRead)query, entry, hash);
+                       result = entry;
+               } else if (query instanceof AsyncMultiRead) {
+                       AsyncMultiReadEntry entry = new AsyncMultiReadEntry((AsyncMultiRead)query);
+                       asyncMultiReadMap.put((AsyncMultiRead)query, entry, hash);
+                       result = entry;
+               } else {
+                       throw new IllegalStateException();
+               }
+               
+               result.setPending();
+               result.clearResult(support);
+               
+               return result;
+               
+       }
+       
+       public final <T> Object runQuery(final ReadGraphImpl graph, final Object query, final CacheEntry parent, final ListenerBase listener, final Object procedure) throws DatabaseException {
+
+               int hash = requestHash(query);
+
+               CacheEntryBase entry =  getCached(query, hash);
+
+               if(parent == null && listener == null) {
+                       if(entry != null && (entry.isReady() || entry.isExcepted())) {
+                               return entry.performFromCache(graph, this, procedure);
+                       } else {
+                               return performQuery(graph, query, entry, null, procedure);
+                       }
+               }
+
+               if(entry == null) {
+                       entry = createEntry(graph.processor.querySupport, query, hash);
+                       return performForEach(graph, query, entry, parent, listener, procedure, false);
+
+               } else {
+
+                       if(entry.isPending()) {
+                               synchronized(entry) {
+                                       if(entry.isPending()) {
+                                           throw new IllegalStateException();
+                                               //                      final AsyncBarrierImpl parentBarrier = graph.state.barrier;
+                                               //                      if(entry.procs == null) entry.procs = new ArrayList<AsyncProcedure<T>>();
+                                               //                        entry.procs.add(new AsyncProcedure<T>() {
+                                               //
+                                               //                                                      @Override
+                                               //                                                      public void execute(AsyncReadGraph graph, T result) {
+                                               //                                                              procedure.execute(graph, result);
+                                               //                                                              parentBarrier.dec(query);
+                                               //                                                      }
+                                               //
+                                               //                                                      @Override
+                                               //                                                      public void exception(AsyncReadGraph graph, Throwable throwable) {
+                                               //                                                              procedure.exception(graph, throwable);
+                                               //                                                              parentBarrier.dec(query);
+                                               //                                                      }
+                                               //                              
+                                               //                        });
+//                                             if(graph.parent != null || listener != null) {
+//                                                     registerDependencies(graph, entry, parent, listener, procedure, false);
+//                                             }
+//
+//                                             query.perform(graph, procedure);
+//
+//                                             return;
+
+                                       }
+                               }
+                       }
+
+                       if(entry.isReady()) { 
+                               Object result = entry.performFromCache(graph, this, procedure);
+                               registerDependencies(graph, entry, parent, listener, procedure, false);
+                               return result;
+                       } else {
+                               return performForEach(graph, query, entry, parent, listener, procedure, false);
+                       }
+
+               }
+
+       }
+
+       public <T> Object performQuery(ReadGraphImpl parentGraph, final Object query_, final CacheEntryBase entry_, ListenerEntry listenerEntry, Object procedure_) throws DatabaseException {
+
+               ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
+
+               if(query_ instanceof AsyncRead) {
+               
+                       AsyncRead<T> query = (AsyncRead<T>)query_;
+                       AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
+                       AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
+
+                       try {
+                               
+                               query.perform(queryGraph, new AsyncProcedure<T>() {
+       
+                                       @Override
+                                       public void execute(AsyncReadGraph returnGraph, T result) {
+                                               ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+                                               entry.addOrSet(parentGraph, result);
+                                               if(listenerEntry != null) {
+                                                       primeListenerEntry(listenerEntry, 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;
+
+               } else if (query_ instanceof Read) {
+                       
+                       Read query = (Read)query_;
+                       ReadEntry entry = (ReadEntry)entry_;
+
+                       entry.setPending();
+
+                       try {
+
+                               T result = (T)query.perform(queryGraph);
+                               entry.addOrSet(queryGraph, result);
+                               
+                               if(listenerEntry != null) primeListenerEntry(listenerEntry, result);
+
+                               return (T)entry.get(parentGraph, parentGraph.processor, procedure_);
+
+                       }  catch (Throwable t) {
+
+                               entry.except(t);
+                               return (T)entry.get(parentGraph, parentGraph.processor, procedure_);
+
+                       }
+                       
+               } else if (query_ instanceof ExternalRead) {
+                       
+                       ExternalRead query = (ExternalRead)query_;
+                       ExternalReadEntry entry = (ExternalReadEntry)entry_;
+                       Procedure<T> procedure = (Procedure<T>)procedure_;
+                       
+                       try {
+
+                               query.register(parentGraph, new Listener<T>() {
+
+                                       AtomicBoolean used = new AtomicBoolean(false);
+
+                                       @Override
+                                       public void execute(T result) {
+                                               
+                                               // Just for safety
+                                               if(entry.isDiscarded()) return;
+                                               if(entry.isExcepted()) entry.setPending();
+                                               
+                                               if(used.compareAndSet(false, true)) {
+                                                       entry.addOrSet(parentGraph.processor, result);
+                                                       procedure.execute(result);
+                                               } else {
+                                                       entry.queue(result);
+                                                       parentGraph.processor.updatePrimitive(query);
+                                               }
+                                               
+                                       }
+
+                                       @Override
+                                       public void exception(Throwable t) {
+                                               
+                                               entry.except(t);
+
+                                               if(used.compareAndSet(false, true)) {
+                                                       procedure.exception(t);
+                                               } else {
+//                                                     entry.queue(result);
+                                                       parentGraph.processor.updatePrimitive(query);
+                                               }
+                                               
+                                       }
+
+                                       @Override
+                                       public String toString() {
+                                               return procedure.toString();
+                                       }
+
+                                       @Override
+                                       public boolean isDisposed() {
+                                               return entry.isDiscarded() || !parentGraph.processor.isBound(entry);
+                                       }
+
+                               });
+                               
+                               return entry.getResult();
+
+                       } catch (Throwable t) {
+
+                               entry.except(t);
+                               procedure.exception(t);
+                               return entry.getResult();
+
+                       }
+
+               } else if (query_ instanceof AsyncMultiRead) {
+                       
+                       AsyncMultiRead query = (AsyncMultiRead)query_;
+                       AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
+                       AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
+
+                       try {
+
+                               query.perform(queryGraph, new AsyncMultiProcedure<T>() {
+
+                                       @Override
+                                       public void execute(AsyncReadGraph graph, T result) {
+                                               ReadGraphImpl impl = (ReadGraphImpl)graph;
+                                               entry.addOrSet(result);
+                                               try {
+                                                       procedure.execute(parentGraph, result);
+                                               } catch (Throwable t) {
+                                                       t.printStackTrace();
+                                               }
+                                       }
+
+                                       @Override
+                                       public void finished(AsyncReadGraph graph) {
+                                               ReadGraphImpl impl = (ReadGraphImpl)graph;
+                                               entry.finish(parentGraph);
+                                               try {
+                                                       procedure.finished(parentGraph);
+                                               } catch (Throwable t) {
+                                                       t.printStackTrace();
+                                               }
+                                       }
+
+                                       @Override
+                                       public void exception(AsyncReadGraph graph, Throwable t) {
+                                               ReadGraphImpl impl = (ReadGraphImpl)graph;
+                                               entry.except(parentGraph, t);
+                                               try {
+                                                       procedure.exception(parentGraph, t);
+                                               } catch (Throwable t2) {
+                                                       t2.printStackTrace();
+                                               }
+                                       }
+
+                               });
+                               
+                               return entry.getResult();
+
+                       } catch (Throwable t) {
+
+                               entry.except(t);
+                               try {
+                                       procedure.exception(parentGraph, t);
+                               } catch (Throwable t2) {
+                                       t2.printStackTrace();
+                               }
+                               
+                               return entry.getResult();
+                               
+                       }
+
+               } else {
+
+                       throw new IllegalStateException();
+
+               }
+
+       }
+       
+       public <T> Object performFromCache(ReadGraphImpl parentGraph, Object query, CacheEntryBase entry_, ListenerEntry listenerEntry, Object procedure_) throws DatabaseException {
+
+               Object result = entry_.performFromCache(parentGraph, this, procedure_);
+               if(listenerEntry != null) {
+                       primeListenerEntry(listenerEntry, result);
+               }
+               return result;
+
+//             if(query instanceof AsyncRead) {
+//                     
+//                     AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
+//
+//                     
+//                     return null;
+//
+//             } else if(query instanceof Read) {
+//                     
+//                     ReadEntry entry = (ReadEntry)entry_;
+//
+//                     T result = (T)entry.get(parentGraph, parentGraph.processor, procedure_); 
+//
+//                     if(listenerEntry != null) primeListenerEntry(listenerEntry, result);
+//
+//                     return result;
+//
+//             } else if(query instanceof ExternalRead) {
+//
+//                     ExternalReadEntry entry = (ExternalReadEntry)entry_;
+//                     Procedure<T> procedure = (Procedure<T>)procedure_;
+//
+//                     return entry.performFromCache(procedure);
+//                     
+//             } else if (query instanceof AsyncMultiRead) {
+//                     
+//                     AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
+//                     AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
+//
+//                     return entry.performFromCache(parentGraph, this, procedure);
+//                     
+//             } else {
+//                     
+//                     throw new IllegalStateException();
+//                     
+//             }
+
+       }
+       
+       public <T> Object performForEach(ReadGraphImpl parentGraph, final Object query, final CacheEntryBase entry, final CacheEntry parent, final ListenerBase base, final Object procedure,
+                       boolean inferredDependency) throws DatabaseException {
+
+//             if (DebugPolicy.PERFORM)
+//                     System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
+
+//             assert (!dirty);
+//             assert (!collecting);
+
+               assert(!entry.isDiscarded());
+
+               final ListenerEntry listenerEntry = registerDependencies(parentGraph, entry, parent, base, procedure, inferredDependency);
+
+               // FRESH, REFUTED, EXCEPTED go here 
+               if (!entry.isReady()) {
+
+                       entry.setPending();
+
+//                     size++;
+
+                       return performQuery(parentGraph, query, entry, listenerEntry, procedure);
+
+//                     misses++;
+
+               } else {
+                       
+                       return performFromCache(parentGraph, query, entry, listenerEntry, procedure);
+
+//                     parentBarrier.dec(query);
+
+//                     hits++;
+
+               }
+
+       }
+       
+       synchronized public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
+
+               if (parent != null && !inferred) {
+                       try {
+                               if(!child.isImmutable(graph))
+                                       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 ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) {
+
+               assert (entry != null);
+
+               if (base.isDisposed())
+                       return null;
+
+               return addListener(entry, base, procedure);
+
+       }
+
+       private 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>();
+
+               for (Object e : valueMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : directPredicatesMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : objectsMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : directObjectsMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : principalTypesMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : superRelationsMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : superTypesMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : typesMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : objectsMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : assertedStatementsMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : readMap.values()) {
+                       if(e instanceof CacheEntry) {
+                               result.add((CacheEntry) e);
+                       } else {
+                               System.err.println("e=" + e);
+                       }
+               }
+               for (Object e : asyncReadMap.values()) {
+                       if(e instanceof CacheEntry) {
+                               result.add((CacheEntry) e);
+                       } else {
+                               System.err.println("e=" + e);
+                       }
+               }
+               for (Object e : externalReadMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : orderedSetMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+
+               return result;
+
+       }
+
+       public int calculateCurrentSize() {
+               
+               int realSize = 0;
+               
+               realSize += directPredicatesMap.size();
+               realSize += principalTypesMap.size();
+               realSize += uriToResourceMap.size();
+               realSize += namespaceIndexMap22.size();
+               realSize += projectsMap.size();
+               
+               realSize += relationInfoMap.size();
+               realSize += superTypesMap.size();
+               realSize += typeHierarchyMap.size();
+               realSize += superRelationsMap.size();
+               realSize += typesMap.size();
+               
+               realSize += valueMap.size();
+               realSize += directObjectsMap.size();
+               realSize += objectsMap.size();
+               realSize += orderedSetMap.size();
+               realSize += predicatesMap.size();
+               
+               realSize += statementsMap.size();
+               realSize += assertedPredicatesMap.size();
+               realSize += assertedStatementsMap.size();
+               realSize += externalReadMap.size();
+               realSize += asyncReadMap.size();
+               
+               realSize += readMap.size();
+               realSize += asyncMultiReadMap.size();
+               realSize += multiReadMap.size();
+               
+               return realSize;
+               
+       }
+       
+       CacheCollectionResult allCaches(CacheCollectionResult result) {
+
+               int level = Integer.MAX_VALUE;
+               directPredicatesMap.values(level, result);
+               principalTypesMap.values(level, result);
+               for(CacheEntryBase e : uriToResourceMap.values())
+                       if(e.getLevel() <= level)
+                               result.add(e);
+               for(CacheEntryBase e : namespaceIndexMap22.values())
+                       if(e.getLevel() <= level)
+                               result.add(e);
+               projectsMap.values(level, result);
+               
+               relationInfoMap.values(level, result);
+               superTypesMap.values(level, result);
+               typeHierarchyMap.values(level, result);
+               superRelationsMap.values(level, result);
+               typesMap.values(level, result);
+
+               valueMap.values(level, result);
+               directObjectsMap.values(level, result);
+               objectsMap.values(level, result);
+               orderedSetMap.values(level, result);
+               predicatesMap.values(level, result);
+
+               statementsMap.values(level, result);
+               assertedPredicatesMap.values(level, result);
+               assertedStatementsMap.values(level, result);
+               externalReadMap.values(level, result);
+               asyncReadMap.values(level, result);
+               
+               readMap.values(level, result);
+               asyncMultiReadMap.values(level, result);
+               multiReadMap.values(level, result);
+
+               return result;
+               
+       }
+       
+       public void scanPending() {
+
+               ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
+
+               entries.addAll(directPredicatesMap.values());
+               entries.addAll(principalTypesMap.values());
+               entries.addAll(uriToResourceMap.values());
+               entries.addAll(namespaceIndexMap22.values());
+               entries.addAll(projectsMap.values());
+               entries.addAll(relationInfoMap.values());
+               entries.addAll(superTypesMap.values());
+               entries.addAll(superRelationsMap.values());
+               entries.addAll(typesMap.values());
+               entries.addAll(valueMap.values());
+               entries.addAll(directObjectsMap.values());
+               entries.addAll(objectsMap.values());
+               entries.addAll(orderedSetMap.values());
+               entries.addAll(predicatesMap.values());
+               entries.addAll(orderedSetMap.values());
+               entries.addAll(statementsMap.values());
+               //                      entries.addAll(assertedObjectsMap.values());
+               entries.addAll(assertedPredicatesMap.values());
+               entries.addAll(assertedStatementsMap.values());
+               entries.addAll(externalReadMap.values());
+               entries.addAll(asyncReadMap.values());
+               entries.addAll(externalReadMap.values());
+               entries.addAll(readMap.values());
+               entries.addAll(asyncMultiReadMap.values());
+               entries.addAll(multiReadMap.values());
+               entries.addAll(readMap.values());
+               System.out.println(entries.size() + " entries.");
+               for(Object e : entries) {
+                       if(e instanceof CacheEntry) {
+                               CacheEntry en = (CacheEntry)e;
+                               if(en.isPending()) System.out.println("pending " + e);
+                               if(en.isExcepted()) System.out.println("excepted " + e);
+                               if(en.isDiscarded()) System.out.println("discarded " + e);
+                               if(en.isRefuted()) System.out.println("refuted " + e);
+                               if(en.isFresh()) System.out.println("fresh " + e);
+                       } else {
+                               //System.out.println("Unknown object " + e);
+                       }
+               }
+       }
+       
+}
index 4842cf5a6ca44f6cbb543fad1cac8c4c5944e115..c9190193fe5587a26490be5350fc1fc553b57fef 100644 (file)
@@ -118,9 +118,8 @@ abstract public class QueryIdentityHash extends THash {
         }
 
         @Override
-        public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-            // TODO Auto-generated method stub
-            
+        public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
+               return null;
         }
 
         @Override
index ff15d301b58214a25d20bb663889d11430008ae2..cca9d681edd8f88893a65d819afee6b290cf5d0a 100644 (file)
@@ -94,31 +94,7 @@ import gnu.trove.set.hash.TIntHashSet;
 @SuppressWarnings({"rawtypes", "unchecked"})
 final public class QueryProcessor extends AbstractDisposable implements ReadGraphSupport {
 
-       final public UnaryQueryHashMap<IntProcedure>                      directPredicatesMap;
-       final public UnaryQueryHashMap<IntProcedure>                      principalTypesMap;
-       final public THashMap<String, URIToResource>                      uriToResourceMap;
-       final public THashMap<String, NamespaceIndex>                     namespaceIndexMap22;
-       final public UnaryQueryHashMap<IntProcedure>                      projectsMap;
-       final public UnaryQueryHashMap<InternalProcedure<RelationInfo>>   relationInfoMap;
-       final public UnaryQueryHashMap<InternalProcedure<IntSet>>         superTypesMap;
-       final public UnaryQueryHashMap<InternalProcedure<IntSet>>         typeHierarchyMap;
-       final public UnaryQueryHashMap<InternalProcedure<IntSet>>         superRelationsMap;
-       final public UnaryQueryHashMap<InternalProcedure<IntSet>>         typesMap;
-       final public UnaryQueryHashMap<InternalProcedure<byte[]>>         valueMap;
-       final public DoubleKeyQueryHashMap<IntProcedure>                     directObjectsMap;
-       final public DoubleKeyQueryHashMap<IntProcedure>                     objectsMap;
-       final public UnaryQueryHashMap<IntProcedure>                      orderedSetMap;
-       final public UnaryQueryHashMap<IntProcedure>                      predicatesMap;
-       final public DoubleKeyQueryHashMap<TripleIntProcedure>               statementsMap;
-       final public UnaryQueryHashMap<IntProcedure>                      assertedPredicatesMap;
-       final public BinaryQueryHashMap<TripleIntProcedure>               assertedStatementsMap;
-       final public StableHashMap<ExternalRead, ExternalReadEntry>            externalReadMap; 
-       final public StableHashMap<AsyncRead, AsyncReadEntry>                  asyncReadMap; 
-       final public StableHashMap<Read, ReadEntry>                            readMap;
-       final public StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>        asyncMultiReadMap; 
-       final public StableHashMap<MultiRead, MultiReadEntry>                  multiReadMap; 
-
-       final private THashMap<CacheEntry, ArrayList<ListenerEntry>>       listeners;
+       final public QueryCache cache = new QueryCache();
 
        public static int                                       indent                = 0;
 
@@ -365,31 +341,6 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                }
 
-               directPredicatesMap = new UnaryQueryHashMap();
-               valueMap = new UnaryQueryHashMap();
-               principalTypesMap = new UnaryQueryHashMap();
-               uriToResourceMap = new THashMap<String, URIToResource>();
-               namespaceIndexMap22 = new THashMap<String, NamespaceIndex>();
-               projectsMap = new UnaryQueryHashMap();
-               relationInfoMap = new UnaryQueryHashMap();
-               typeHierarchyMap = new UnaryQueryHashMap();
-               superTypesMap = new UnaryQueryHashMap();
-               superRelationsMap = new UnaryQueryHashMap();
-               typesMap = new UnaryQueryHashMap();
-               objectsMap = new DoubleKeyQueryHashMap();
-               orderedSetMap = new UnaryQueryHashMap();
-               predicatesMap = new UnaryQueryHashMap();
-               statementsMap = new DoubleKeyQueryHashMap();
-               directObjectsMap = new DoubleKeyQueryHashMap();
-               assertedPredicatesMap = new UnaryQueryHashMap();
-               assertedStatementsMap = new BinaryQueryHashMap();
-               asyncReadMap = new StableHashMap<AsyncRead, AsyncReadEntry>(); 
-               readMap = new StableHashMap<Read, ReadEntry>();
-               asyncMultiReadMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>(); 
-               multiReadMap = new StableHashMap<MultiRead, MultiReadEntry>(); 
-               externalReadMap = new StableHashMap<ExternalRead, ExternalReadEntry>(); 
-               listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
-
                // Now start threads
                for (int i = 0; i < THREADS; i++) {
                        executors[i].start();
@@ -625,201 +576,223 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
        public final <T> void runAsyncRead(final ReadGraphImpl graph, final AsyncRead<T> query, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure<T> procedure) {
 
-               int hash = requestHash(query);
-
-               AsyncReadEntry<T> entry = asyncReadMap.get(query, hash); 
-
-               if(parent == null && listener == null) {
-                       if(entry != null && (entry.isReady() || entry.isExcepted())) {
-                               System.out.println("ready " + query);
-                               entry.performFromCache(graph, this, procedure);
-//                             graph.state.barrier.dec(query);
-                               return;
-                       } else {
-                               query.perform(graph, procedure);
-//                             graph.state.barrier.dec(query);
-                               return;
-                       }
+               try {
+                       cache.runQuery(graph, query, parent, listener, procedure);
+               } catch (DatabaseException e) {
+                       throw new IllegalStateException(e);
                }
-
-               if(entry == null) {
-
-                       entry = new AsyncReadEntry<T>(query);
-                       entry.setPending();
-                       entry.clearResult(querySupport);
-                       asyncReadMap.put(query, entry, hash);
-
-                       performForEach(graph, query, entry, parent, listener, procedure, false);
-
-               } else {
-
-                       if(entry.isPending()) {
-                               synchronized(entry) {
-                                       if(entry.isPending()) {
-                                           throw new IllegalStateException();
-                                               //                      final AsyncBarrierImpl parentBarrier = graph.state.barrier;
-                                               //                      if(entry.procs == null) entry.procs = new ArrayList<AsyncProcedure<T>>();
-                                               //                        entry.procs.add(new AsyncProcedure<T>() {
-                                               //
-                                               //                                                      @Override
-                                               //                                                      public void execute(AsyncReadGraph graph, T result) {
-                                               //                                                              procedure.execute(graph, result);
-                                               //                                                              parentBarrier.dec(query);
-                                               //                                                      }
-                                               //
-                                               //                                                      @Override
-                                               //                                                      public void exception(AsyncReadGraph graph, Throwable throwable) {
-                                               //                                                              procedure.exception(graph, throwable);
-                                               //                                                              parentBarrier.dec(query);
-                                               //                                                      }
-                                               //                              
-                                               //                        });
-//                                             if(graph.parent != null || listener != null) {
-//                                                     registerDependencies(graph, entry, parent, listener, procedure, false);
-//                                             }
+               
+//             int hash = requestHash(query);
 //
-//                                             query.perform(graph, procedure);
+//             AsyncReadEntry<T> entry = asyncReadMap.get(query, hash); 
 //
-//                                             return;
-
-                                       }
-                               }
-                       }
-
-                       if(entry.isReady()) { 
-                               entry.performFromCache(graph, this, procedure);
-                               registerDependencies(graph, entry, parent, listener, procedure, false);
-                       } else {
-                               performForEach(graph, query, entry, parent, listener, procedure, false);
-                       }
-
-               }
+//             if(parent == null && listener == null) {
+//                     if(entry != null && (entry.isReady() || entry.isExcepted())) {
+//                             System.out.println("ready " + query);
+//                             entry.performFromCache(graph, this, procedure);
+////                           graph.state.barrier.dec(query);
+//                             return;
+//                     } else {
+//                             query.perform(graph, procedure);
+////                           graph.state.barrier.dec(query);
+//                             return;
+//                     }
+//             }
+//
+//             if(entry == null) {
+//
+//                     entry = new AsyncReadEntry<T>(query);
+//                     entry.setPending();
+//                     entry.clearResult(querySupport);
+//                     asyncReadMap.put(query, entry, hash);
+//
+//                     performForEach(graph, query, entry, parent, listener, procedure, false);
+//
+//             } else {
+//
+//                     if(entry.isPending()) {
+//                             synchronized(entry) {
+//                                     if(entry.isPending()) {
+//                                         throw new IllegalStateException();
+//                                             //                      final AsyncBarrierImpl parentBarrier = graph.state.barrier;
+//                                             //                      if(entry.procs == null) entry.procs = new ArrayList<AsyncProcedure<T>>();
+//                                             //                        entry.procs.add(new AsyncProcedure<T>() {
+//                                             //
+//                                             //                                                      @Override
+//                                             //                                                      public void execute(AsyncReadGraph graph, T result) {
+//                                             //                                                              procedure.execute(graph, result);
+//                                             //                                                              parentBarrier.dec(query);
+//                                             //                                                      }
+//                                             //
+//                                             //                                                      @Override
+//                                             //                                                      public void exception(AsyncReadGraph graph, Throwable throwable) {
+//                                             //                                                              procedure.exception(graph, throwable);
+//                                             //                                                              parentBarrier.dec(query);
+//                                             //                                                      }
+//                                             //                              
+//                                             //                        });
+////                                           if(graph.parent != null || listener != null) {
+////                                                   registerDependencies(graph, entry, parent, listener, procedure, false);
+////                                           }
+////
+////                                           query.perform(graph, procedure);
+////
+////                                           return;
+//
+//                                     }
+//                             }
+//                     }
+//
+//                     if(entry.isReady()) { 
+//                             entry.performFromCache(graph, this, procedure);
+//                             registerDependencies(graph, entry, parent, listener, procedure, false);
+//                     } else {
+//                             performForEach(graph, query, entry, parent, listener, procedure, false);
+//                     }
+//
+//             }
 
        }
 
 
-       final static <T> void runMultiRead(final ReadGraphImpl graph, MultiReadEntry cached, final MultiRead<T> query, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final AsyncMultiProcedure<T> procedure) {
-
-               MultiReadEntry entry = cached != null ? cached : provider.multiReadMap.get(query); 
-               if(entry == null) {
-
-                       entry = new MultiReadEntry(query);
-                       entry.setPending();
-                       entry.clearResult(provider.querySupport);
-
-                       provider.multiReadMap.put(query, entry);
-
-                       provider.performForEach(graph, query, entry, parent, listener, procedure, false);
-
-               } else {
-
-                       if(entry.isPending()) {
+       final <T> void runMultiRead(final ReadGraphImpl graph, MultiReadEntry cached, final MultiRead<T> query, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final AsyncMultiProcedure<T> procedure) {
 
-                               synchronized(entry) {
-
-                                       if(entry.isPending()) {
-                                           throw new IllegalStateException();
-
-//                                             if(entry.procs == null) entry.procs = new ArrayList<Pair<AsyncMultiProcedure<T>, AsyncBarrier>>();
-//                                             entry.procs.add(new Pair(procedure, parentBarrier));
-//                                             if(graph.parent != null || listener != null) {
-//                                                     provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                                             }
+               try {
+                       cache.runQuery(graph, query, parent, listener, procedure);
+               } catch (DatabaseException e) {
+                       throw new IllegalStateException(e);
+               }
 
-                                               // If this was synchronized we must wait here until completion
-                                               //                        if(graph.state.synchronizedExecution) {
-                                               //                            while(entry.isPending()) {
-                                               //                              graph.resumeTasks(graph.callerThread, null, null);
-                                               //                            }
-                                               //                        }
+//             MultiReadEntry entry = cached != null ? cached : provider.multiReadMap.get(query); 
+//             if(entry == null) {
 //
-//                                             return;
-
-                                       }
-                               }
-
-                               entry.performFromCache(graph, provider, procedure);
-//                             graph.state.barrier.dec(query);
-                               return;
-
-                       } else {
-
-                               provider.performForEach(graph, query, entry, parent, listener, procedure, false);
-
-                       }
-
-               }
+//                     entry = new MultiReadEntry(query);
+//                     entry.setPending();
+//                     entry.clearResult(provider.querySupport);
+//
+//                     provider.multiReadMap.put(query, entry);
+//
+//                     provider.performForEach(graph, query, entry, parent, listener, procedure, false);
+//
+//             } else {
+//
+//                     if(entry.isPending()) {
+//
+//                             synchronized(entry) {
+//
+//                                     if(entry.isPending()) {
+//                                         throw new IllegalStateException();
+//
+////                                           if(entry.procs == null) entry.procs = new ArrayList<Pair<AsyncMultiProcedure<T>, AsyncBarrier>>();
+////                                           entry.procs.add(new Pair(procedure, parentBarrier));
+////                                           if(graph.parent != null || listener != null) {
+////                                                   provider.registerDependencies(graph, entry, parent, listener, procedure, false);
+////                                           }
+//
+//                                             // If this was synchronized we must wait here until completion
+//                                             //                        if(graph.state.synchronizedExecution) {
+//                                             //                            while(entry.isPending()) {
+//                                             //                              graph.resumeTasks(graph.callerThread, null, null);
+//                                             //                            }
+//                                             //                        }
+////
+////                                           return;
+//
+//                                     }
+//                             }
+//
+//                             entry.performFromCache(graph, provider, procedure);
+////                           graph.state.barrier.dec(query);
+//                             return;
+//
+//                     } else {
+//
+//                             provider.performForEach(graph, query, entry, parent, listener, procedure, false);
+//
+//                     }
+//
+//             }
 
        }
 
        public final <T> void runAsyncMultiRead(final ReadGraphImpl graph, final AsyncMultiRead<T> query, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure) {
 
-               int hash = requestHash(query);
-
-               AsyncMultiReadEntry entry = asyncMultiReadMap.get(query, hash);
-
-               if(parent == null && listener == null) {
-                       if(entry != null && (entry.isReady() || entry.isExcepted())) {
-                               System.out.println("ready " + query);
-                               entry.performFromCache(graph, this, procedure);
-                               return;
-                       } else {
-                               query.perform(graph, procedure);
-                               return;
-                       }
+               
+               try {
+                       cache.runQuery(graph, query, parent, listener, procedure);
+               } catch (DatabaseException e) {
+                       throw new IllegalStateException(e);
                }
 
-               if(entry == null) {
-
-                       entry = new AsyncMultiReadEntry<T>(query); 
-                       entry.setPending();
-                       entry.clearResult(querySupport);
-
-                       asyncMultiReadMap.put(query, entry, hash);
 
-                       performForEach(graph, query, entry, parent, listener, procedure, false);
-
-               } else {
-
-                       if(entry.isPending()) {
-                           
-                               synchronized(entry) {
-                                       if(entry.isPending()) {
-                                           throw new IllegalStateException();
-//                                             if(entry.procs == null) entry.procs = new ArrayList<AsyncMultiProcedure<T>>();
-//                                             entry.procs.add(procedure);
-//                                             if(graph.parent != null || listener != null) {
-//                                                     registerDependencies(graph, entry, parent, listener, procedure, false);
-//                                             }
-//                                             return;
-                                       }
-                               }
-                       }
-
-                       performForEach(graph, query, entry, parent, listener, procedure, false);
-
-               }
+//             int hash = requestHash(query);
+//
+//             AsyncMultiReadEntry entry = asyncMultiReadMap.get(query, hash);
+//
+//             if(parent == null && listener == null) {
+//                     if(entry != null && (entry.isReady() || entry.isExcepted())) {
+//                             System.out.println("ready " + query);
+//                             entry.performFromCache(graph, this, procedure);
+//                             return;
+//                     } else {
+//                             query.perform(graph, procedure);
+//                             return;
+//                     }
+//             }
+//
+//             if(entry == null) {
+//
+//                     entry = new AsyncMultiReadEntry<T>(query); 
+//                     entry.setPending();
+//                     entry.clearResult(querySupport);
+//
+//                     asyncMultiReadMap.put(query, entry, hash);
+//
+//                     performForEach(graph, query, entry, parent, listener, procedure, false);
+//
+//             } else {
+//
+//                     if(entry.isPending()) {
+//                         
+//                             synchronized(entry) {
+//                                     if(entry.isPending()) {
+//                                         throw new IllegalStateException();
+////                                           if(entry.procs == null) entry.procs = new ArrayList<AsyncMultiProcedure<T>>();
+////                                           entry.procs.add(procedure);
+////                                           if(graph.parent != null || listener != null) {
+////                                                   registerDependencies(graph, entry, parent, listener, procedure, false);
+////                                           }
+////                                           return;
+//                                     }
+//                             }
+//                     }
+//
+//                     performForEach(graph, query, entry, parent, listener, procedure, false);
+//
+//             }
 
        }
 
-       final static <T> void runPrimitiveRead(ReadGraphImpl graph, ExternalReadEntry cached, final ExternalRead<T> query, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final Procedure<T> procedure) {
+       final <T> void runPrimitiveRead(ReadGraphImpl graph, ExternalReadEntry cached, final ExternalRead<T> query, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final Procedure<T> procedure) throws DatabaseException {
 
-               final ExternalReadEntry<T> entry = cached != null ? cached : provider.externalReadMap.get(query); 
-               if(entry == null) {
-                       provider.performForEach(graph, query, new ExternalReadEntry<T>(query), parent, listener, procedure, false);
-               } else {
-                       if(entry.isPending()) {
-                               synchronized(entry) {
-                                       if(entry.isPending()) {
-                                           throw new IllegalStateException();
-//                                             if(entry.procs == null) entry.procs = new ArrayList<Procedure<T>>();
-//                                             entry.procs.add(procedure);
-//                                             return;
-                                       }
-                               }
-                       }
-                       provider.performForEach(graph, query, entry, parent, listener, procedure, false);
-               }
+               cache.runQuery(graph, query, parent, listener, procedure);
+
+//             final ExternalReadEntry<T> entry = cached != null ? cached : provider.externalReadMap.get(query); 
+//             if(entry == null) {
+//                     provider.performForEach(graph, query, new ExternalReadEntry<T>(query), parent, listener, procedure, false);
+//             } else {
+//                     if(entry.isPending()) {
+//                             synchronized(entry) {
+//                                     if(entry.isPending()) {
+//                                         throw new IllegalStateException();
+////                                           if(entry.procs == null) entry.procs = new ArrayList<Procedure<T>>();
+////                                           entry.procs.add(procedure);
+////                                           return;
+//                                     }
+//                             }
+//                     }
+//                     provider.performForEach(graph, query, entry, parent, listener, procedure, false);
+//             }
 
        }
 
@@ -835,543 +808,549 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
     @Override
        public <T> T queryRead(final ReadGraphImpl graph, final Read<T> query, final CacheEntry parent, final AsyncProcedure<T> procedure, final ListenerBase listener) throws Throwable {
 
-               assert(query != null);
-
-               ReadEntry entry = readMap.get(query);
-
-               if(entry != null) {
-                       if(parent == null && (listener == null || listener.isDisposed()) && entry.isReady()) {
-                               return (T)entry.get(graph, this, procedure);
-                       } else if (entry.isPending()) {
-                           throw new IllegalStateException();
-                       }
-               }
-
-               if(entry == null) {
-
-                       entry = new ReadEntry(query);
-                       entry.setPending();
-                       entry.clearResult(querySupport);
-
-                       readMap.put(query, entry);
-
-                       return (T)performForEach(graph, query, entry, parent, listener, procedure, false);
-
-               } else {
-
-                       if(entry.isPending()) {
-                throw new IllegalStateException();
-                       } else {
-                               return (T)performForEach(graph, query, entry, parent, listener, procedure, false);
-                       }
-
-               }
-
-       }
-
-       public <T> void queryMultiRead(final ReadGraphImpl graph, final MultiRead<T> query, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure) {
-
-               assert(query != null);
-               assert(procedure != null);
-
-               final MultiReadEntry entry = multiReadMap.get(query);
-
-               if(parent == null && !(listener != null)) {
-                       if(entry != null && entry.isReady()) { 
-                               entry.performFromCache(graph, this, procedure);
-                               return;
-                       }
-               }
-
-               runMultiRead(graph, entry, query, parent, this, listener, procedure);
-
-       }
-
-       public <T> void queryPrimitiveRead(final ReadGraphImpl graph, final ExternalRead<T> query, final CacheEntry parent, final ListenerBase listener, final Procedure<T> procedure) {
-
-               assert(query != null);
-               assert(procedure != null);
-
-               final ExternalReadEntry entry = externalReadMap.get(query);
-
-               if(parent == null && !(listener != null)) {
-                       if(entry != null && entry.isReady()) { 
-                               entry.performFromCache(procedure);
-                               return;
-                       }
-               }
-
-               runPrimitiveRead(graph, entry, query, parent, this, listener, procedure);
-
-       }
-
-       public <T> void performForEach(ReadGraphImpl parentGraph, final AsyncRead<T> query, final AsyncReadEntry<T> entry, final CacheEntry parent, final ListenerBase base, final AsyncProcedure<T> procedure,
-                       boolean inferredDependency) {
-
-               if (DebugPolicy.PERFORM)
-                       System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-
-               assert (!dirty);
-               assert (!collecting);
-
-               assert(!entry.isDiscarded());
-
-               final ListenerEntry listenerEntry = registerDependencies(parentGraph, entry, parent, base, procedure, inferredDependency);
-
-               // FRESH, REFUTED, EXCEPTED go here 
-               if (!entry.isReady()) {
-
-                       entry.setPending();
+               return (T)cache.runQuery(graph, query, parent, listener, procedure);
 
-                       size++;
-
-                       try {
-
-                               final ReadGraphImpl finalParentGraph = parentGraph;
-
-                               query.perform(parentGraph.withParent(entry), new AsyncProcedure<T>() {
-
-                                       @Override
-                                       public void execute(AsyncReadGraph returnGraph, T result) {
-                                               ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
-                                               //AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
-                                               entry.addOrSet(finalParentGraph, result);
-                                               if(listenerEntry != null) {
-                                                       primeListenerEntry(listenerEntry, result);
-                                               }
-                                               try {
-                                                       procedure.execute(finalParentGraph, 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(finalParentGraph, t);
-                                               try {
-                                                       procedure.exception(finalParentGraph, 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);
-
-                       }
-
-                       misses++;
-
-               } else {
-
-                       entry.performFromCache(parentGraph, this, new AsyncProcedure<T>() {
-
-                               @Override
-                               public void exception(AsyncReadGraph graph, Throwable throwable) {
-                                       procedure.exception(graph, throwable);
-                               }
-
-                               @Override
-                               public void execute(AsyncReadGraph graph, T result) {
-                                       procedure.execute(graph, result);
-                                       if(listenerEntry != null) {
-                                               primeListenerEntry(listenerEntry, result);
-                                       }
-                               }
-
-                       });
-
-//                     parentBarrier.dec(query);
-
-                       hits++;
-
-               }
-
-               assert (!entry.isDiscarded());
-
-       }
-
-       public <T> T performForEach(final ReadGraphImpl graph, final Read<T> query, final ReadEntry<T> entry, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure<T> procedure,
-                       boolean inferredDependency) throws Throwable {
-
-               if (DebugPolicy.PERFORM)
-                       System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-
-               assert (!dirty);
-               assert (!collecting);
-
-               entry.assertNotDiscarded();
-
-               if(entry.isReady()) {
-
-                       // EXCEPTED goes here
-
-//                     if(procedure != null) entry.performFromCache(graph, this, procedure);
-//                     parentBarrier.dec(query);
-                       hits++;
-
-                       ListenerEntry listenerEntry = registerDependencies(graph, entry, parent, listener, procedure, inferredDependency);
-                       
-                       T result = (T)entry.get(graph, this, procedure); 
-
-                       if(listenerEntry != null) primeListenerEntry(listenerEntry, result);
-
-                       return result;
-
-               } else {
-
-                       // FRESH, REFUTED, PENDING go here
-
-                       entry.setPending();
-
-                       size++;
-                       misses++;
-
-               ListenerEntry listenerEntry = registerDependencies(graph, entry, parent, listener, procedure, inferredDependency);
-
-                       final ReadGraphImpl performGraph = graph.newSync(entry);
-
-                       try {
-
-                               if(Development.DEVELOPMENT)
-                                       Development.recordHistogram("run " + query);
-
-                               T result = query.perform(performGraph);
-                               entry.addOrSet(performGraph, result);
-                               
-                               if(listenerEntry != null) primeListenerEntry(listenerEntry, result);
-
-                               return (T)entry.get(graph, this, procedure);
-
-                       }  catch (Throwable t) {
-
-                               entry.except(t);
-                               return (T)entry.get(graph, this, procedure);
-
-                       }
-
-               } 
+//     assert(query != null);
+//
+//             ReadEntry entry = readMap.get(query);
+//
+//             if(entry != null) {
+//                     if(parent == null && (listener == null || listener.isDisposed()) && entry.isReady()) {
+//                             return (T)entry.get(graph, this, procedure);
+//                     } else if (entry.isPending()) {
+//                         throw new IllegalStateException();
+//                     }
+//             }
+//
+//             if(entry == null) {
+//
+//                     entry = new ReadEntry(query);
+//                     entry.setPending();
+//                     entry.clearResult(querySupport);
+//
+//                     readMap.put(query, entry);
+//
+//                     return (T)performForEach(graph, query, entry, parent, listener, procedure, false);
+//
+//             } else {
+//
+//                     if(entry.isPending()) {
+//                throw new IllegalStateException();
+//                     } else {
+//                             return (T)performForEach(graph, query, entry, parent, listener, procedure, false);
+//                     }
+//
+//             }
 
        }
 
-       public <T> void performForEach(final ReadGraphImpl graph, final MultiRead<T> query, final MultiReadEntry<T> entry, CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure,
-                       boolean inferredDependency) {
-
-               if (DebugPolicy.PERFORM)
-                       System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-
-               assert (!dirty);
-               assert (!collecting);
-
-               assert(!entry.isPending());
-               assert(!entry.isDiscarded());
-
-               // FRESH, REFUTED, EXCEPTED go here 
-               if (!entry.isReady()) {
-
-                       entry.setPending();
-                       entry.clearResult(querySupport);
-
-                       multiReadMap.put(query, entry);
-                       size++;
-
-                       final ReadGraphImpl newGraph = graph.newSync(entry);
-//                     newGraph.state.barrier.inc();
-
-                       try {
-
-                               query.perform(newGraph, new AsyncMultiProcedure<T>() {
+       public <T> void queryMultiRead(final ReadGraphImpl graph, final MultiRead<T> query, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure) throws DatabaseException {
 
-                                       @Override
-                                       public void execute(AsyncReadGraph graph, T result) {
-                                               entry.addOrSet(result);
-                                               try {
-                                                       procedure.execute(graph, result);
-                                               } catch (Throwable t) {
-                                                       t.printStackTrace();
-                                               }
-                                       }
-
-                                       @Override
-                                       public void finished(AsyncReadGraph graph) {
-                                               entry.finish(graph);
-                                               try {
-                                                       procedure.finished(graph);
-                                               } catch (Throwable t) {
-                                                       t.printStackTrace();
-                                               }
-//                                             newGraph.state.barrier.dec();
-//                                             parentBarrier.dec();
-                                       }
+               cache.runQuery(graph, query, parent, listener, procedure);
 
-                                       @Override
-                                       public void exception(AsyncReadGraph graph, Throwable t) {
-                                               entry.except(t);
-                                               try {
-                                                       procedure.exception(graph, t);
-                                               } catch (Throwable t2) {
-                                                       t2.printStackTrace();
-                                               }
-//                                             newGraph.state.barrier.dec();
-//                                             parentBarrier.dec();
-                                       }
-
-                               });
-
-                       } catch (DatabaseException e) {
-
-                               entry.except(e);
-                               try {
-                                       procedure.exception(graph, e);
-                               } catch (Throwable t2) {
-                                       t2.printStackTrace();
-                               }
-//                             newGraph.state.barrier.dec();
-//                             parentBarrier.dec();
-
-                       } catch (Throwable t) {
-
-                               DatabaseException e = new DatabaseException(t);
-
-                               entry.except(e);
-                               try {
-                                       procedure.exception(graph, e);
-                               } catch (Throwable t2) {
-                                       t2.printStackTrace();
-                               }
-//                             newGraph.state.barrier.dec();
-//                             parentBarrier.dec();
-
-                       }
-
-                       misses++;
-
-               } else {
-
-                       entry.performFromCache(graph, this, procedure);
-                       hits++;
-
-
-               }
-
-               assert (!entry.isDiscarded());
-
-               registerDependencies(graph, entry, parent, listener, procedure, inferredDependency);
+//             assert(query != null);
+//             assert(procedure != null);
+//
+//             final MultiReadEntry entry = multiReadMap.get(query);
+//
+//             if(parent == null && !(listener != null)) {
+//                     if(entry != null && entry.isReady()) { 
+//                             entry.performFromCache(graph, this, procedure);
+//                             return;
+//                     }
+//             }
+//
+//             runMultiRead(graph, entry, query, parent, this, listener, procedure);
 
        }
 
+       public <T> void queryPrimitiveRead(final ReadGraphImpl graph, final ExternalRead<T> query, final CacheEntry parent, final ListenerBase listener, final Procedure<T> procedure) throws DatabaseException {
 
-       public <T> void performForEach(final ReadGraphImpl callerGraph, AsyncMultiRead<T> query, final AsyncMultiReadEntry<T> entry, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure,
-                       boolean inferredDependency) {
-
-               if (DebugPolicy.PERFORM)
-                       System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-
-               assert (!dirty);
-               assert (!collecting);
-
-               try {
-
-                       assert(!entry.isDiscarded());
-
-                       // FRESH, REFUTED, EXCEPTED go here 
-                       if (!entry.isReady()) {
-
-                               size++;
-
-                               try {
-
-                                       ReadGraphImpl performGraph = callerGraph.withAsyncParent(entry);
-
-                                       query.perform(performGraph, new AsyncMultiProcedure<T>() {
-
-                                               @Override
-                                               public void execute(AsyncReadGraph graph, T result) {
-                                                       ReadGraphImpl impl = (ReadGraphImpl)graph;
-//                                                     ReadGraphImpl executeGraph = callerGraph.newAsync();
-                                                       entry.addOrSet(result);
-                                                       try {
-                                                               procedure.execute(callerGraph, result);
-                                                       } catch (Throwable t) {
-                                                               t.printStackTrace();
-                                                       }
-                                               }
-
-                                               @Override
-                                               public void finished(AsyncReadGraph graph) {
-                                                       ReadGraphImpl impl = (ReadGraphImpl)graph;
-//                                                     ReadGraphImpl executeGraph = callerGraph.newAsync();
-                                                       entry.finish(callerGraph);
-                                                       try {
-                                                               procedure.finished(callerGraph);
-                                                       } catch (Throwable t) {
-                                                               t.printStackTrace();
-                                                       }
-                                               }
-
-                                               @Override
-                                               public void exception(AsyncReadGraph graph, Throwable t) {
-                                                       ReadGraphImpl impl = (ReadGraphImpl)graph;
-//                                                     ReadGraphImpl executeGraph = callerGraph.newAsync();
-                                                       entry.except(callerGraph, t);
-                                                       try {
-                                                               procedure.exception(callerGraph, t);
-                                                       } catch (Throwable t2) {
-                                                               t2.printStackTrace();
-                                                       }
-                                               }
-
-                                       });
-
-                               } catch (Throwable t) {
-
-                                       entry.except(t);
-                                       try {
-                                               procedure.exception(callerGraph, t);
-                                       } catch (Throwable t2) {
-                                               t2.printStackTrace();
-                                       }
-
-                               }
-
-
-                               misses++;
+               cache.runQuery(graph, query, parent, listener, procedure);
 
-                       } else {
-
-                               entry.performFromCache(callerGraph, this, procedure);
-
-                               hits++;
-
-                       }
-
-                       assert (!entry.isDiscarded());
-
-                       registerDependencies(callerGraph, entry, parent, listener, procedure, inferredDependency);
-
-               } catch (Throwable t) {
-
-                       Logger.defaultLogError(t);
-
-               } finally {
-
-               }
+//             assert(query != null);
+//             assert(procedure != null);
+//
+//             final ExternalReadEntry entry = externalReadMap.get(query);
+//
+//             if(parent == null && !(listener != null)) {
+//                     if(entry != null && entry.isReady()) { 
+//                             entry.performFromCache(procedure);
+//                             return;
+//                     }
+//             }
+//
+//             runPrimitiveRead(graph, entry, query, parent, this, listener, procedure);
 
        }
 
-       public <T> void performForEach(ReadGraphImpl graph, final ExternalRead<T> query, final ExternalReadEntry<T> entry, final CacheEntry parent, final ListenerBase base, final Procedure<T> procedure,
-                       boolean inferredDependency) {
-
-               if (DebugPolicy.PERFORM)
-                       System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-
-               assert (!dirty);
-               assert (!collecting);
-
-               assert(!entry.isPending());
-               assert(!entry.isDiscarded());
-
-               registerDependencies(graph, entry, parent, base, procedure, inferredDependency);
-
-               // FRESH, REFUTED, EXCEPTED go here 
-               if (!entry.isReady()) {
-
-                       entry.setPending();
-                       entry.clearResult(querySupport);
-
-                       externalReadMap.put(query, entry);
-                       size++;
-
-                       try {
-
-                               query.register(graph, new Listener<T>() {
-
-                                       AtomicBoolean used = new AtomicBoolean(false);
-
-                                       @Override
-                                       public void execute(T result) {
-                                               
-                                               // Just for safety
-                                               if(entry.isDiscarded()) return;
-                                               if(entry.isExcepted()) entry.setPending();
-                                               
-                                               if(used.compareAndSet(false, true)) {
-                                                       entry.addOrSet(QueryProcessor.this, result);
-                                                       procedure.execute(result);
-                                               } else {
-                                                       entry.queue(result);
-                                                       updatePrimitive(query);
-                                               }
-                                               
-                                       }
+//     public <T> void performForEach(ReadGraphImpl parentGraph, final AsyncRead<T> query, final AsyncReadEntry<T> entry, final CacheEntry parent, final ListenerBase base, final AsyncProcedure<T> procedure,
+//                     boolean inferredDependency) {
+//
+//             if (DebugPolicy.PERFORM)
+//                     System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
+//
+//             assert (!dirty);
+//             assert (!collecting);
+//
+//             assert(!entry.isDiscarded());
+//
+//             final ListenerEntry listenerEntry = registerDependencies(parentGraph, entry, parent, base, procedure, inferredDependency);
+//
+//             // FRESH, REFUTED, EXCEPTED go here 
+//             if (!entry.isReady()) {
+//
+//                     entry.setPending();
+//
+//                     size++;
+//
+//                     try {
+//
+//                             final ReadGraphImpl finalParentGraph = parentGraph;
+//
+//                             query.perform(parentGraph.withParent(entry), new AsyncProcedure<T>() {
+//
+//                                     @Override
+//                                     public void execute(AsyncReadGraph returnGraph, T result) {
+//                                             ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+//                                             //AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
+//                                             entry.addOrSet(finalParentGraph, result);
+//                                             if(listenerEntry != null) {
+//                                                     primeListenerEntry(listenerEntry, result);
+//                                             }
+//                                             try {
+//                                                     procedure.execute(finalParentGraph, 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(finalParentGraph, t);
+//                                             try {
+//                                                     procedure.exception(finalParentGraph, 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);
+//
+//                     }
+//
+//                     misses++;
+//
+//             } else {
+//
+//                     entry.performFromCache(parentGraph, this, new AsyncProcedure<T>() {
+//
+//                             @Override
+//                             public void exception(AsyncReadGraph graph, Throwable throwable) {
+//                                     procedure.exception(graph, throwable);
+//                             }
+//
+//                             @Override
+//                             public void execute(AsyncReadGraph graph, T result) {
+//                                     procedure.execute(graph, result);
+//                                     if(listenerEntry != null) {
+//                                             primeListenerEntry(listenerEntry, result);
+//                                     }
+//                             }
+//
+//                     });
+//
+////                   parentBarrier.dec(query);
+//
+//                     hits++;
+//
+//             }
+//
+//             assert (!entry.isDiscarded());
+//
+//     }
 
-                                       @Override
-                                       public void exception(Throwable t) {
-                                               
-                                               entry.except(t);
+//     public <T> T performForEach(final ReadGraphImpl graph, final Read<T> query, final ReadEntry<T> entry, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure<T> procedure,
+//                     boolean inferredDependency) throws Throwable {
+//
+//             if (DebugPolicy.PERFORM)
+//                     System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
+//
+//             assert (!dirty);
+//             assert (!collecting);
+//
+//             entry.assertNotDiscarded();
+//
+//             if(entry.isReady()) {
+//
+//                     // EXCEPTED goes here
+//
+////                   if(procedure != null) entry.performFromCache(graph, this, procedure);
+////                   parentBarrier.dec(query);
+//                     hits++;
+//
+//                     ListenerEntry listenerEntry = registerDependencies(graph, entry, parent, listener, procedure, inferredDependency);
+//                     
+//                     T result = (T)entry.get(graph, this, procedure); 
+//
+//                     if(listenerEntry != null) primeListenerEntry(listenerEntry, result);
+//
+//                     return result;
+//
+//             } else {
+//
+//                     // FRESH, REFUTED, PENDING go here
+//
+//                     entry.setPending();
+//
+//                     size++;
+//                     misses++;
+//
+//             ListenerEntry listenerEntry = registerDependencies(graph, entry, parent, listener, procedure, inferredDependency);
+//
+//                     final ReadGraphImpl performGraph = graph.newSync(entry);
+//
+//                     try {
+//
+//                             if(Development.DEVELOPMENT)
+//                                     Development.recordHistogram("run " + query);
+//
+//                             T result = query.perform(performGraph);
+//                             entry.addOrSet(performGraph, result);
+//                             
+//                             if(listenerEntry != null) primeListenerEntry(listenerEntry, result);
+//
+//                             return (T)entry.get(graph, this, procedure);
+//
+//                     }  catch (Throwable t) {
+//
+//                             entry.except(t);
+//                             return (T)entry.get(graph, this, procedure);
+//
+//                     }
+//
+//             } 
+//
+//     }
 
-                                               if(used.compareAndSet(false, true)) {
-                                                       procedure.exception(t);
-                                               } else {
+//     public <T> void performForEach(final ReadGraphImpl graph, final MultiRead<T> query, final MultiReadEntry<T> entry, CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure,
+//                     boolean inferredDependency) {
+//
+//             if (DebugPolicy.PERFORM)
+//                     System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
+//
+//             assert (!dirty);
+//             assert (!collecting);
+//
+//             assert(!entry.isPending());
+//             assert(!entry.isDiscarded());
+//
+//             // FRESH, REFUTED, EXCEPTED go here 
+//             if (!entry.isReady()) {
+//
+//                     entry.setPending();
+//                     entry.clearResult();
+//
+//                     multiReadMap.put(query, entry);
+//                     size++;
+//
+//                     final ReadGraphImpl newGraph = graph.newSync(entry);
+////                   newGraph.state.barrier.inc();
+//
+//                     try {
+//
+//                             query.perform(newGraph, new AsyncMultiProcedure<T>() {
+//
+//                                     @Override
+//                                     public void execute(AsyncReadGraph graph, T result) {
+//                                             entry.addOrSet(result);
+//                                             try {
+//                                                     procedure.execute(graph, result);
+//                                             } catch (Throwable t) {
+//                                                     t.printStackTrace();
+//                                             }
+//                                     }
+//
+//                                     @Override
+//                                     public void finished(AsyncReadGraph graph) {
+//                                             entry.finish(graph);
+//                                             try {
+//                                                     procedure.finished(graph);
+//                                             } catch (Throwable t) {
+//                                                     t.printStackTrace();
+//                                             }
+////                                           newGraph.state.barrier.dec();
+////                                           parentBarrier.dec();
+//                                     }
+//
+//                                     @Override
+//                                     public void exception(AsyncReadGraph graph, Throwable t) {
+//                                             entry.except(t);
+//                                             try {
+//                                                     procedure.exception(graph, t);
+//                                             } catch (Throwable t2) {
+//                                                     t2.printStackTrace();
+//                                             }
+////                                           newGraph.state.barrier.dec();
+////                                           parentBarrier.dec();
+//                                     }
+//
+//                             });
+//
+//                     } catch (DatabaseException e) {
+//
+//                             entry.except(e);
+//                             try {
+//                                     procedure.exception(graph, e);
+//                             } catch (Throwable t2) {
+//                                     t2.printStackTrace();
+//                             }
+////                           newGraph.state.barrier.dec();
+////                           parentBarrier.dec();
+//
+//                     } catch (Throwable t) {
+//
+//                             DatabaseException e = new DatabaseException(t);
+//
+//                             entry.except(e);
+//                             try {
+//                                     procedure.exception(graph, e);
+//                             } catch (Throwable t2) {
+//                                     t2.printStackTrace();
+//                             }
+////                           newGraph.state.barrier.dec();
+////                           parentBarrier.dec();
+//
+//                     }
+//
+//                     misses++;
+//
+//             } else {
+//
+//                     entry.performFromCache(graph, this, procedure);
+//                     hits++;
+//
+//
+//             }
+//
+//             assert (!entry.isDiscarded());
+//
+//             registerDependencies(graph, entry, parent, listener, procedure, inferredDependency);
+//
+//     }
+//
+//
+//     public <T> void performForEach(final ReadGraphImpl callerGraph, AsyncMultiRead<T> query, final AsyncMultiReadEntry<T> entry, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure,
+//                     boolean inferredDependency) {
+//
+//             if (DebugPolicy.PERFORM)
+//                     System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
+//
+//             assert (!dirty);
+//             assert (!collecting);
+//
+//             try {
+//
+//                     assert(!entry.isDiscarded());
+//
+//                     // FRESH, REFUTED, EXCEPTED go here 
+//                     if (!entry.isReady()) {
+//
+//                             size++;
+//
+//                             try {
+//
+//                                     ReadGraphImpl performGraph = callerGraph.withAsyncParent(entry);
+//
+//                                     query.perform(performGraph, new AsyncMultiProcedure<T>() {
+//
+//                                             @Override
+//                                             public void execute(AsyncReadGraph graph, T result) {
+//                                                     ReadGraphImpl impl = (ReadGraphImpl)graph;
+////                                                   ReadGraphImpl executeGraph = callerGraph.newAsync();
+//                                                     entry.addOrSet(result);
+//                                                     try {
+//                                                             procedure.execute(callerGraph, result);
+//                                                     } catch (Throwable t) {
+//                                                             t.printStackTrace();
+//                                                     }
+//                                             }
+//
+//                                             @Override
+//                                             public void finished(AsyncReadGraph graph) {
+//                                                     ReadGraphImpl impl = (ReadGraphImpl)graph;
+////                                                   ReadGraphImpl executeGraph = callerGraph.newAsync();
+//                                                     entry.finish(callerGraph);
+//                                                     try {
+//                                                             procedure.finished(callerGraph);
+//                                                     } catch (Throwable t) {
+//                                                             t.printStackTrace();
+//                                                     }
+//                                             }
+//
+//                                             @Override
+//                                             public void exception(AsyncReadGraph graph, Throwable t) {
+//                                                     ReadGraphImpl impl = (ReadGraphImpl)graph;
+////                                                   ReadGraphImpl executeGraph = callerGraph.newAsync();
+//                                                     entry.except(callerGraph, t);
+//                                                     try {
+//                                                             procedure.exception(callerGraph, t);
+//                                                     } catch (Throwable t2) {
+//                                                             t2.printStackTrace();
+//                                                     }
+//                                             }
+//
+//                                     });
+//
+//                             } catch (Throwable t) {
+//
+//                                     entry.except(t);
+//                                     try {
+//                                             procedure.exception(callerGraph, t);
+//                                     } catch (Throwable t2) {
+//                                             t2.printStackTrace();
+//                                     }
+//
+//                             }
+//
+//
+//                             misses++;
+//
+//                     } else {
+//
+//                             entry.performFromCache(callerGraph, this, procedure);
+//
+//                             hits++;
+//
+//                     }
+//
+//                     assert (!entry.isDiscarded());
+//
+//                     registerDependencies(callerGraph, entry, parent, listener, procedure, inferredDependency);
+//
+//             } catch (Throwable t) {
+//
+//                     Logger.defaultLogError(t);
+//
+//             } finally {
+//
+//             }
+//
+//     }
+//
+//     public <T> void performForEach(ReadGraphImpl graph, final ExternalRead<T> query, final ExternalReadEntry<T> entry, final CacheEntry parent, final ListenerBase base, final Procedure<T> procedure,
+//                     boolean inferredDependency) {
+//
+//             if (DebugPolicy.PERFORM)
+//                     System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
+//
+//             assert (!dirty);
+//             assert (!collecting);
+//
+//             assert(!entry.isPending());
+//             assert(!entry.isDiscarded());
+//
+//             registerDependencies(graph, entry, parent, base, procedure, inferredDependency);
+//
+//             // FRESH, REFUTED, EXCEPTED go here 
+//             if (!entry.isReady()) {
+//
+//                     entry.setPending();
+//                     entry.clearResult(querySupport);
+//
+//                     externalReadMap.put(query, entry);
+//                     size++;
+//
+//                     try {
+//
+//                             query.register(graph, new Listener<T>() {
+//
+//                                     AtomicBoolean used = new AtomicBoolean(false);
+//
+//                                     @Override
+//                                     public void execute(T result) {
+//                                             
+//                                             // Just for safety
+//                                             if(entry.isDiscarded()) return;
+//                                             if(entry.isExcepted()) entry.setPending();
+//                                             
+//                                             if(used.compareAndSet(false, true)) {
+//                                                     entry.addOrSet(QueryProcessor.this, result);
+//                                                     procedure.execute(result);
+//                                             } else {
 //                                                     entry.queue(result);
-                                                       updatePrimitive(query);
-                                               }
-                                               
-                                       }
-
-                                       @Override
-                                       public String toString() {
-                                               return procedure.toString();
-                                       }
-
-                                       @Override
-                                       public boolean isDisposed() {
-                                               return entry.isDiscarded() || !isBound(entry);
-                                       }
-
-                               });
-
-                       } catch (Throwable t) {
-
-                               entry.except(t);
-                               procedure.exception(t);
-
-                       }
-
-                       misses++;
-
-               } else {
-
-                       entry.performFromCache(procedure);
-
-                       hits++;
-
-               }
-
-               assert (!entry.isDiscarded());
-
-       }
+//                                                     updatePrimitive(query);
+//                                             }
+//                                             
+//                                     }
+//
+//                                     @Override
+//                                     public void exception(Throwable t) {
+//                                             
+//                                             entry.except(t);
+//
+//                                             if(used.compareAndSet(false, true)) {
+//                                                     procedure.exception(t);
+//                                             } else {
+////                                                   entry.queue(result);
+//                                                     updatePrimitive(query);
+//                                             }
+//                                             
+//                                     }
+//
+//                                     @Override
+//                                     public String toString() {
+//                                             return procedure.toString();
+//                                     }
+//
+//                                     @Override
+//                                     public boolean isDisposed() {
+//                                             return entry.isDiscarded() || !isBound(entry);
+//                                     }
+//
+//                             });
+//
+//                     } catch (Throwable t) {
+//
+//                             entry.except(t);
+//                             procedure.exception(t);
+//
+//                     }
+//
+//                     misses++;
+//
+//             } else {
+//
+//                     entry.performFromCache(procedure);
+//
+//                     hits++;
+//
+//             }
+//
+//             assert (!entry.isDiscarded());
+//
+//     }
 
-       private boolean isBound(ExternalReadEntry<?> entry) {
+       boolean isBound(ExternalReadEntry<?> entry) {
                if(entry.hasParents()) return true;
                else if(hasListener(entry)) return true;
                else return false;
@@ -1690,101 +1669,12 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                }
 
                public Collection<CacheEntry> getRootList() {
-
-                       ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
-
-                       for (Object e : valueMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-                       for (Object e : directPredicatesMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-                       for (Object e : objectsMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-                       for (Object e : directObjectsMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-                       for (Object e : principalTypesMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-                       for (Object e : superRelationsMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-                       for (Object e : superTypesMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-                       for (Object e : typesMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-                       for (Object e : objectsMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-                       for (Object e : assertedStatementsMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-                       for (Object e : readMap.values()) {
-                               if(e instanceof CacheEntry) {
-                                       result.add((CacheEntry) e);
-                               } else {
-                                       System.err.println("e=" + e);
-                               }
-                       }
-                       for (Object e : asyncReadMap.values()) {
-                               if(e instanceof CacheEntry) {
-                                       result.add((CacheEntry) e);
-                               } else {
-                                       System.err.println("e=" + e);
-                               }
-                       }
-                       for (Object e : externalReadMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-                       for (Object e : orderedSetMap.values()) {
-                               result.add((CacheEntry) e);
-                       }
-
-                       return result;
-
+                       return cache.getRootList();
                }
 
                @Override
                public int calculateCurrentSize() {
-                       
-                       int realSize = 0;
-                       
-                       realSize += directPredicatesMap.size();
-                       realSize += principalTypesMap.size();
-                       realSize += uriToResourceMap.size();
-                       realSize += namespaceIndexMap22.size();
-                       realSize += projectsMap.size();
-                       
-                       realSize += relationInfoMap.size();
-                       realSize += superTypesMap.size();
-                       realSize += typeHierarchyMap.size();
-                       realSize += superRelationsMap.size();
-                       realSize += typesMap.size();
-                       
-                       realSize += valueMap.size();
-                       realSize += directObjectsMap.size();
-                       realSize += objectsMap.size();
-                       realSize += orderedSetMap.size();
-                       realSize += predicatesMap.size();
-                       
-                       realSize += statementsMap.size();
-                       realSize += assertedPredicatesMap.size();
-                       realSize += assertedStatementsMap.size();
-                       realSize += externalReadMap.size();
-                       realSize += asyncReadMap.size();
-                       
-                       realSize += readMap.size();
-                       realSize += asyncMultiReadMap.size();
-                       realSize += multiReadMap.size();
-                       
-                       size = realSize;
-                       
-                       return realSize;
-                       
+                       return cache.calculateCurrentSize();
                }
 
                @Override
@@ -1828,10 +1718,10 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                assert (entry != null);
                assert (procedure != null);
 
-               ArrayList<ListenerEntry> list = listeners.get(entry);
+               ArrayList<ListenerEntry> list = cache.listeners.get(entry);
                if (list == null) {
                        list = new ArrayList<ListenerEntry>(1);
-                       listeners.put(entry, list);
+                       cache.listeners.put(entry, list);
                }
 
                ListenerEntry result = new ListenerEntry(entry, base, procedure);
@@ -1862,22 +1752,22 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
        private void removeListener(ListenerEntry entry) {
                assert (entry != null);
-               ArrayList<ListenerEntry> list = listeners.get(entry.entry);
+               ArrayList<ListenerEntry> list = cache.listeners.get(entry.entry);
                if(list == null) return;
                boolean success = list.remove(entry);
                assert (success);
                if (list.isEmpty())
-                       listeners.remove(entry.entry);
+                       cache.listeners.remove(entry.entry);
        }
 
        private boolean hasListener(CacheEntry entry) {
-               if(listeners.get(entry) != null) return true;
+               if(cache.listeners.get(entry) != null) return true;
                return false;
        }
 
        boolean hasListenerAfterDisposing(CacheEntry entry) {
-               if(listeners.get(entry) != null) {
-                       ArrayList<ListenerEntry> entries = listeners.get(entry);
+               if(cache.listeners.get(entry) != null) {
+                       ArrayList<ListenerEntry> entries = cache.listeners.get(entry);
                        ArrayList<ListenerEntry> list = null;
                        for (ListenerEntry e : entries) {
                                if (e.base.isDisposed()) {
@@ -1891,7 +1781,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                                }
                        }
                        if (entries.isEmpty()) {
-                               listeners.remove(entry);
+                               cache.listeners.remove(entry);
                                return false;
                        }
                        return true;
@@ -1901,8 +1791,8 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
        List<ListenerEntry> getListenerEntries(CacheEntry entry) {
                hasListenerAfterDisposing(entry);
-               if(listeners.get(entry) != null)
-                       return listeners.get(entry);
+               if(cache.listeners.get(entry) != null)
+                       return cache.listeners.get(entry);
                else 
                        return Collections.emptyList();
        }
@@ -2420,7 +2310,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //             System.err.println(" => FOO " + type);
 
                if (hasListener) {
-                       ArrayList<ListenerEntry> entries = listeners.get(entry);
+                       ArrayList<ListenerEntry> entries = cache.listeners.get(entry);
                        if(entries != null) {
                                for (ListenerEntry le : entries) {
                                        scheduleListener(le);
@@ -2776,11 +2666,11 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        @Override
                        public boolean execute(Object arg0) {
 
-                               ExternalReadEntry query = (ExternalReadEntry)externalReadMap.get(arg0);
+                               ExternalReadEntry query = (ExternalReadEntry)cache.externalReadMap.get(arg0);
                                if (query != null) {
                                        boolean listening = update(graph, query);
                                        if (!listening && !query.hasParents()) {
-                                               externalReadMap.remove(arg0);
+                                               cache.externalReadMap.remove(arg0);
                                                query.discard();
                                        }
                                }
@@ -3003,15 +2893,15 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
        public Set<Long> getReferencedClusters() {
                HashSet<Long> result = new HashSet<Long>();
-               for (CacheEntry entry : objectsMap.values()) {
+               for (CacheEntry entry : cache.objectsMap.values()) {
                        Objects query = (Objects) entry.getQuery();
                        result.add(querySupport.getClusterId(query.r1()));
                }
-               for (CacheEntry entry : directPredicatesMap.values()) {
+               for (CacheEntry entry : cache.directPredicatesMap.values()) {
                        DirectPredicates query = (DirectPredicates) entry.getQuery();
                        result.add(querySupport.getClusterId(query.id));
                }
-               for (CacheEntry entry : valueMap.values()) {
+               for (CacheEntry entry : cache.valueMap.values()) {
                        ValueQuery query = (ValueQuery) entry.getQuery();
                        result.add(querySupport.getClusterId(query.id));
                }
@@ -3022,41 +2912,8 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
        }
 
        CacheCollectionResult allCaches(CacheCollectionResult result) {
-
-               int level = Integer.MAX_VALUE;
-               directPredicatesMap.values(level, result);
-               principalTypesMap.values(level, result);
-               for(CacheEntryBase e : uriToResourceMap.values())
-                       if(e.getLevel() <= level)
-                               result.add(e);
-               for(CacheEntryBase e : namespaceIndexMap22.values())
-                       if(e.getLevel() <= level)
-                               result.add(e);
-               projectsMap.values(level, result);
-               
-               relationInfoMap.values(level, result);
-               superTypesMap.values(level, result);
-               typeHierarchyMap.values(level, result);
-               superRelationsMap.values(level, result);
-               typesMap.values(level, result);
-
-               valueMap.values(level, result);
-               directObjectsMap.values(level, result);
-               objectsMap.values(level, result);
-               orderedSetMap.values(level, result);
-               predicatesMap.values(level, result);
-
-               statementsMap.values(level, result);
-               assertedPredicatesMap.values(level, result);
-               assertedStatementsMap.values(level, result);
-               externalReadMap.values(level, result);
-               asyncReadMap.values(level, result);
                
-               readMap.values(level, result);
-               asyncMultiReadMap.values(level, result);
-               multiReadMap.values(level, result);
-
-               return result;
+               return cache.allCaches(result);
 
        }
 
@@ -3083,7 +2940,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        public CacheEntryBase iterate(int level) {
                                if(iterator.hasNext()) {
                                        ExternalRead<?> request = iterator.next();
-                                       ExternalReadEntry entry = externalReadMap.get(request);
+                                       ExternalReadEntry entry = cache.externalReadMap.get(request);
                                        if (entry != null) return entry;
                                        else return iterate(level);
                                } else {
@@ -3103,7 +2960,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        public Collection<CacheEntry> getRootList() {
                                ArrayList<CacheEntry> result = new ArrayList<CacheEntry>(requests.size());
                                for (ExternalRead<?> request : requests) {
-                                       ExternalReadEntry entry = externalReadMap.get(request);
+                                       ExternalReadEntry entry = cache.externalReadMap.get(request);
                                        if (entry != null)
                                                result.add(entry);
                                }
@@ -3127,48 +2984,8 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
        }
 
        public void scanPending() {
-
-               ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
-
-               entries.addAll(directPredicatesMap.values());
-               entries.addAll(principalTypesMap.values());
-               entries.addAll(uriToResourceMap.values());
-               entries.addAll(namespaceIndexMap22.values());
-               entries.addAll(projectsMap.values());
-               entries.addAll(relationInfoMap.values());
-               entries.addAll(superTypesMap.values());
-               entries.addAll(superRelationsMap.values());
-               entries.addAll(typesMap.values());
-               entries.addAll(valueMap.values());
-               entries.addAll(directObjectsMap.values());
-               entries.addAll(objectsMap.values());
-               entries.addAll(orderedSetMap.values());
-               entries.addAll(predicatesMap.values());
-               entries.addAll(orderedSetMap.values());
-               entries.addAll(statementsMap.values());
-               //                      entries.addAll(assertedObjectsMap.values());
-               entries.addAll(assertedPredicatesMap.values());
-               entries.addAll(assertedStatementsMap.values());
-               entries.addAll(externalReadMap.values());
-               entries.addAll(asyncReadMap.values());
-               entries.addAll(externalReadMap.values());
-               entries.addAll(readMap.values());
-               entries.addAll(asyncMultiReadMap.values());
-               entries.addAll(multiReadMap.values());
-               entries.addAll(readMap.values());
-               System.out.println(entries.size() + " entries.");
-               for(Object e : entries) {
-                       if(e instanceof CacheEntry) {
-                               CacheEntry en = (CacheEntry)e;
-                               if(en.isPending()) System.out.println("pending " + e);
-                               if(en.isExcepted()) System.out.println("excepted " + e);
-                               if(en.isDiscarded()) System.out.println("discarded " + e);
-                               if(en.isRefuted()) System.out.println("refuted " + e);
-                               if(en.isFresh()) System.out.println("fresh " + e);
-                       } else {
-                               //System.out.println("Unknown object " + e);
-                       }
-               }
+               
+               cache.scanPending();
 
        }
 
@@ -5185,7 +5002,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                assert(graph != null);
                assert(request != null);
 
-               final ReadEntry entry = readMap.get(request);
+               final ReadEntry entry = cache.readMap.get(request);
                if(entry != null && entry.isReady()) {
                    return (T)entry.get(graph, this, null);
                } else {
@@ -5199,7 +5016,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
         assert(graph != null);
         assert(request != null);
 
-        final ExternalReadEntry<T> entry = externalReadMap.get(request);
+        final ExternalReadEntry<T> entry = cache.externalReadMap.get(request);
         if(entry != null && entry.isReady()) {
             if(entry.isExcepted()) {
                 Throwable t = (Throwable)entry.getResult();
@@ -5250,7 +5067,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                assert(graph != null);
                assert(request != null);
 
-               final AsyncReadEntry entry = asyncReadMap.get(request);
+               final AsyncReadEntry entry = cache.asyncReadMap.get(request);
                if(entry != null && entry.isReady()) {
                        if(entry.isExcepted()) {
                                procedure.exception(graph, (Throwable)entry.getResult());
@@ -5269,9 +5086,15 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                assert(request != null);
                assert(procedure != null);
 
-//             impl.state.barrier.inc(null, null);
+               try {
 
-               queryMultiRead(impl, request, parent, listener, procedure);
+                       queryMultiRead(impl, request, parent, listener, procedure);
+                       
+               } catch (DatabaseException e) {
+                       
+                       throw new IllegalStateException(e);
+                       
+               }
 
        }
 
@@ -5335,32 +5158,40 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                assert(request != null);
                assert(procedure != null);
 
-               queryPrimitiveRead(impl, request, parent, listener, new Procedure<T>() {
-
-                       @Override
-                       public void execute(T result) {
-                               try {
-                                       procedure.execute(result);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
+               try {
+               
+                       queryPrimitiveRead(impl, request, parent, listener, new Procedure<T>() {
+       
+                               @Override
+                               public void execute(T result) {
+                                       try {
+                                               procedure.execute(result);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
                                }
-                       }
-
-                       @Override
-                       public String toString() {
-                               return procedure.toString();
-                       }
-
-                       @Override
-                       public void exception(Throwable t) {
-                               try {
-                                       procedure.exception(t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
+       
+                               @Override
+                               public String toString() {
+                                       return procedure.toString();
                                }
-                       }
-
-               });
+       
+                               @Override
+                               public void exception(Throwable t) {
+                                       try {
+                                               procedure.exception(t);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
+                               }
+       
+                       });
+                       
+               } catch (DatabaseException e) {
+                       
+                       throw new IllegalStateException(e);
+                       
+               }
 
        }
 
index e4ff1ea874bb2c3b9f8e288d413eaedc49f51c51..edc32454929542ab76a0f4a51155559a52b22324 100644 (file)
@@ -97,7 +97,7 @@ final public class ReadEntry<T> extends CacheEntryBase {
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                               processor.readMap.remove(request);
+                               processor.cache.readMap.remove(request);
                        }
 
                        @Override
@@ -119,28 +119,28 @@ final public class ReadEntry<T> extends CacheEntryBase {
         
     }
     
-       public void performFromCache(ReadGraphImpl graph, Object provider,      Object procedure) {
+       public Object performFromCache(ReadGraphImpl graph, Object provider,    Object procedure) {
            
         AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
 
-           if(isExcepted()) {
-
-            try {
-                proc.exception(graph, (Throwable)getResult());
-            } catch (Throwable t) {
-                t.printStackTrace();
-            }
-               
-           } else {
-               
-            try {
-                proc.execute(graph, (T)getResult());
-            } catch (Throwable t) {
-                t.printStackTrace();
-            }
-
-           }
+        if(proc != null) {
+               if(isExcepted()) {
+                       try {
+                               proc.exception(graph, (Throwable)getResult());
+                       } catch (Throwable t) {
+                               t.printStackTrace();
+                       }
+               } else {
+                       try {
+                               proc.execute(graph, (T)getResult());
+                       } catch (Throwable t) {
+                               t.printStackTrace();
+                       }
+               }
+        }
                
+           return (T)getResult();
+           
        }
        
        @Override
index d32da5be54bfcf324f4555ff1740cb5cd258a3c6..f2081d35f767c9fced6b3814feb2393b0b68bca2 100644 (file)
@@ -11,7 +11,6 @@
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.ArrayList;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicBoolean;
 
@@ -23,8 +22,6 @@ import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
 
 final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<RelationInfo>> {
-    
-//     public ArrayList<InternalProcedure<RelationInfo>> procs = null;
 
        private RelationInfoQuery(final int resource) {
         super(resource);
@@ -32,7 +29,7 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
     
     final static RelationInfo runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<RelationInfo> procedure) {
 
-       RelationInfoQuery entry = (RelationInfoQuery)provider.relationInfoMap.get(r);
+       RelationInfoQuery entry = (RelationInfoQuery)provider.cache.relationInfoMap.get(r);
         if(entry == null) {
                
                entry = new RelationInfoQuery(r);
@@ -67,7 +64,7 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
     
     final public static RelationInfo queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<RelationInfo> procedure) {
         
-       RelationInfoQuery entry = (RelationInfoQuery)provider.relationInfoMap.get(r);
+       RelationInfoQuery entry = (RelationInfoQuery)provider.cache.relationInfoMap.get(r);
        if(entry != null && entry.isReady()) {
                entry.performFromCache(graph, provider, procedure);
                return entry.getResult();
@@ -80,7 +77,7 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
     final public static RelationInfoQuery probe(ReadGraphImpl graph, int resource) {
         
         final int thread = graph.thread(resource);
-       RelationInfoQuery entry = (RelationInfoQuery)graph.processor.relationInfoMap.get(resource);
+       RelationInfoQuery entry = (RelationInfoQuery)graph.processor.cache.relationInfoMap.get(resource);
        if(entry != null && entry.isReady()) {
                return entry;
        } else {
@@ -91,17 +88,17 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
 
     @Override
        public UnaryQuery<InternalProcedure<RelationInfo>> getEntry(QueryProcessor provider) {
-        return provider.relationInfoMap.get(id);
+        return provider.cache.relationInfoMap.get(id);
        }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-        provider.relationInfoMap.put(id, this);
+        provider.cache.relationInfoMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.relationInfoMap.remove(id);
+               provider.cache.relationInfoMap.remove(id);
        }
 
        private void computeAssertions(ReadGraphImpl graph, final boolean isFinal, final boolean isFunctional, final QueryProcessor queryProvider, final InternalProcedure<RelationInfo> proc) {
index 935b8115962df76e3672d5479521e5dba41378f3..d251a65c11b93dc4c70b3673d4b50518e912c5e5 100644 (file)
@@ -27,27 +27,25 @@ import org.simantics.db.request.RequestFlags;
 
 final public class Statements extends CollectionBinaryQuery<TripleIntProcedure> {
        
-//     public ArrayList<TripleIntProcedure> procs = null;
-       
     public Statements(final int r1, final int r2) {
         super(r1, r2);
     }
 
     final static Statements entry(final QueryProcessor processor, final int r1, final int r2) {
         
-        return (Statements)processor.statementsMap.get(id(r1,r2));
+        return (Statements)processor.cache.statementsMap.get(id(r1,r2));
 
     }
     
        final static Collection<Statements> entries(final QueryProcessor processor, final int r1) {
-               return processor.statementsMap.values(r1);
+               return processor.cache.statementsMap.values(r1);
        }
     
     final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
         
        QueryProcessor processor = graph.processor;
 
-        Statements entry = (Statements)processor.statementsMap.get(id(r1,r2));
+        Statements entry = (Statements)processor.cache.statementsMap.get(id(r1,r2));
         if(entry == null) {
                
                entry = new Statements(r1, r2);
@@ -93,17 +91,17 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
     @Override
     public BinaryQuery<TripleIntProcedure> getEntry(QueryProcessor provider) {
-        return provider.statementsMap.get(id);
+        return provider.cache.statementsMap.get(id);
     }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-           provider.statementsMap.put(id, this);
+           provider.cache.statementsMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-        provider.statementsMap.remove(id);
+        provider.cache.statementsMap.remove(id);
        }
        
     final static TripleIntProcedure NOPT = new TripleIntProcedure() {
@@ -842,19 +840,22 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
     }
     
     @Override
-    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
+    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
 
        assert(isReady());
 
-       if(handleException(graph, procedure)) return;
-       
         final IntArray value = (IntArray)getResult();
+
+       if(handleException(graph, procedure)) return value;
+       
         for(int i=0;i<value.size();i+=3) {
                procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
         }
 
         procedure.finished(graph);
         
+        return value;
+        
     }
     
     @Override
index 36d6acc5ae377e5c55e7f5019af705c48085cfe9..f879d6037877382c273d20d38da99845c2d0aa2c 100644 (file)
@@ -62,7 +62,7 @@ abstract public class StringQuery<Procedure> extends CacheEntryBase implements Q
     }
     
     @Override
-    public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
+    public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
        throw new Error("Not possible.");
     }
     
index e76f702e14f54ca8b1a379ba255ca8d4e69f229c..fc3cbebab6753460c3b396831845d11b13bdf937 100644 (file)
@@ -22,8 +22,6 @@ import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.ListenerBase;
 
 final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>> {
-       
-//     public ArrayList<InternalProcedure<IntSet>> procs = null;
     
     private SuperRelations(final int resource) {
         super(resource);
@@ -31,13 +29,13 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
     
     final static SuperRelations entry(final QueryProcessor provider, final int r) {
         
-        return (SuperRelations)provider.superRelationsMap.get(r);
+        return (SuperRelations)provider.cache.superRelationsMap.get(r);
 
     }
 
     final static IntSet runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
 
-        SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r);
+        SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r);
         if(entry == null) {
 
                entry = new SuperRelations(r);
@@ -60,7 +58,7 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
     
     final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws Throwable {
 
-        SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r);
+        SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r);
         if(entry == null) {
 
             entry = new SuperRelations(r);
@@ -84,7 +82,7 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
     final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
 
         if(parent == null && listener == null) {
-               SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r);
+               SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r);
                if(entry != null && entry.isReady()) {
                    entry.performFromCache(graph, provider, procedure);
                    return;
@@ -98,7 +96,7 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
     final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws Throwable {
 
         if(parent == null && listener == null) {
-            SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r);
+            SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r);
             if(entry != null && entry.isReady()) {
                 return (IntSet)entry.get(graph, provider, procedure);
             }
@@ -110,17 +108,17 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
     
        @Override
        public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.superRelationsMap.get(id);
+        return provider.cache.superRelationsMap.get(id);
        }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-        provider.superRelationsMap.put(id, this);
+        provider.cache.superRelationsMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.superRelationsMap.remove(id);
+               provider.cache.superRelationsMap.remove(id);
        }
        
        static int histoCounter = 0;
index 0489546c55d6caa5fdd50f5537bf321302db4f7f..1fce568707564691fe7420ef072af0c8f0b7924e 100644 (file)
@@ -20,8 +20,6 @@ import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.ListenerBase;
 
 final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
-    
-//     public ArrayList<InternalProcedure<IntSet>> procs = null;
        
     private SuperTypes(final int resource) {
         super(resource);
@@ -29,7 +27,7 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
     
     final static SuperTypes runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
 
-       SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);
+       SuperTypes entry = (SuperTypes)provider.cache.superTypesMap.get(r);
         if(entry == null) {
                
                entry = new SuperTypes(r);
@@ -64,7 +62,7 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
     final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
     
         if(parent == null && listener == null) {
-               SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);
+               SuperTypes entry = (SuperTypes)provider.cache.superTypesMap.get(r);
                if(entry != null && entry.isReady()) { 
                        entry.performFromCache(graph, provider, procedure);
                        return entry;
@@ -77,17 +75,17 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
        
        @Override
        public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.superTypesMap.get(id);
+        return provider.cache.superTypesMap.get(id);
        }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-        provider.superTypesMap.put(id, this);
+        provider.cache.superTypesMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.superTypesMap.remove(id);
+               provider.cache.superTypesMap.remove(id);
        }
 
        @Override
index 5c5d6cb093d1f3e3a90f10c30fb2560ff35b5bc5..6a96e11eaf65f857789740f77caa5e22ec48de91 100644 (file)
@@ -29,7 +29,7 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
     
     final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
 
-       TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r);
+       TypeHierarchy entry = (TypeHierarchy)provider.cache.typeHierarchyMap.get(r);
         if(entry == null) {
 
                entry = new TypeHierarchy(r);
@@ -60,7 +60,7 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
     final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
         
         if(parent == null && listener == null) {
-               TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r);
+               TypeHierarchy entry = (TypeHierarchy)provider.cache.typeHierarchyMap.get(r);
                if(entry != null && entry.isReady()) { 
                        entry.performFromCache(graph, provider, procedure);
                        return;
@@ -73,17 +73,17 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
        
        @Override
        public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.typeHierarchyMap.get(id);
+        return provider.cache.typeHierarchyMap.get(id);
        }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-        provider.typeHierarchyMap.put(id, this);
+        provider.cache.typeHierarchyMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.typeHierarchyMap.remove(id);
+               provider.cache.typeHierarchyMap.remove(id);
        }
 
        @Override
index 49cb5c4c61ac0fabb59d01cf326d96c7ba1ad0b1..0f49188506099bfb4a13c64faffb05daea1c02c3 100644 (file)
@@ -29,12 +29,12 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
     }
     
     final static Types entry(final QueryProcessor provider, final int r) {
-        return (Types)provider.typesMap.get(r);
+        return (Types)provider.cache.typesMap.get(r);
     }
     
    final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, Types cached, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
 
-       Types entry = cached != null ? cached : (Types)provider.typesMap.get(r); 
+       Types entry = cached != null ? cached : (Types)provider.cache.typesMap.get(r); 
         if(entry == null) {
                
                entry = new Types(r);
@@ -56,7 +56,7 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
     
     final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
 
-       Types entry = (Types)provider.typesMap.get(r); 
+       Types entry = (Types)provider.cache.typesMap.get(r); 
         if(entry == null) {
                
                entry = new Types(r);
@@ -79,7 +79,7 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
     
     final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
         
-       final Types entry = (Types)provider.typesMap.get(r);
+       final Types entry = (Types)provider.cache.typesMap.get(r);
        
        if(parent == null && listener == null) {
                if(entry != null && entry.isReady()) { 
@@ -95,7 +95,7 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
     final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
         
         if(parent == null) {
-            Types entry = (Types)provider.typesMap.get(r);
+            Types entry = (Types)provider.cache.typesMap.get(r);
             if(entry != null && entry.isReady()) {
                 return (IntSet)entry.get(graph, provider, null);
             }
@@ -107,17 +107,17 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
     
        @Override
        public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.typesMap.get(id);
+        return provider.cache.typesMap.get(id);
        }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-        provider.typesMap.put(id, this);
+        provider.cache.typesMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.typesMap.remove(id);
+               provider.cache.typesMap.remove(id);
        }
 
        @Override
index c466cb261cca975d10c16a70461eab15bb3f77b2..1098ed31398bb1ce8a88680416f1179ff10eec52 100644 (file)
@@ -30,14 +30,14 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
     }
     
     final static URIToResource entry(final QueryProcessor provider, final String id) {
-        return (URIToResource)provider.uriToResourceMap.get(id);
+        return (URIToResource)provider.cache.uriToResourceMap.get(id);
     }
 
     final static void runner(ReadGraphImpl graph, final String id, CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {
 
        QueryProcessor processor = graph.processor;
        
-        URIToResource entry = (URIToResource)processor.uriToResourceMap.get(id); 
+        URIToResource entry = (URIToResource)processor.cache.uriToResourceMap.get(id); 
         if(entry == null) {
 
                entry = new URIToResource(id);
@@ -81,17 +81,17 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
      
     @Override
     public URIToResource getEntry(QueryProcessor provider) {
-        return provider.uriToResourceMap.get(id);
+        return provider.cache.uriToResourceMap.get(id);
     }
     
     @Override
     public void putEntry(QueryProcessor provider) {
-        provider.uriToResourceMap.put(id, this);
+        provider.cache.uriToResourceMap.put(id, this);
     }
 
     @Override
     final public void removeEntry(QueryProcessor provider) {
-        provider.uriToResourceMap.remove(id);
+        provider.cache.uriToResourceMap.remove(id);
     }
 
     private void lookup(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure, final String namespace, final String name) {
index ee89bedf76f78de87234f5d83535ad2c66124cc6..ef60150702103b1f4532cab1e40443b6cb30fc36 100644 (file)
@@ -58,8 +58,8 @@ abstract public class UnaryQuery<Procedure> extends CacheEntryBase implements Qu
     }
     
     @Override
-    public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-        performFromCache(graph, (QueryProcessor)provider, (Procedure)procedure);
+    public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
+        return performFromCache(graph, (QueryProcessor)provider, (Procedure)procedure);
     }
     
     abstract public void recompute(ReadGraphImpl graph, QueryProcessor provider);
index d85595831851a5b87916d0edba978d2c67acef36..770a25cb93abc0e3fa75b2004efabc396da0fed2 100644 (file)
@@ -24,14 +24,14 @@ final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {
     }
     
     final static ValueQuery entry(final QueryProcessor provider, final int r) {
-        return (ValueQuery)provider.valueMap.get(r);
+        return (ValueQuery)provider.cache.valueMap.get(r);
     }
 
     final static byte[] runner(final ReadGraphImpl graph, final int r, CacheEntry parent, final ListenerBase listener, final InternalProcedure<byte[]> procedure) {
 
        QueryProcessor processor = graph.processor;
        
-       ValueQuery entry = (ValueQuery)processor.valueMap.get(r); 
+       ValueQuery entry = (ValueQuery)processor.cache.valueMap.get(r); 
         if(entry == null) {
                
                entry = new ValueQuery(r);
@@ -75,17 +75,17 @@ final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {
     
        @Override
        public UnaryQuery<InternalProcedure<byte[]>> getEntry(QueryProcessor provider) {
-        return provider.valueMap.get(id);
+        return provider.cache.valueMap.get(id);
        }
        
        @Override
        public void putEntry(QueryProcessor provider) {
-        provider.valueMap.put(id, this);
+        provider.cache.valueMap.put(id, this);
        }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.valueMap.remove(id);
+               provider.cache.valueMap.remove(id);
        }
        
                
index 7a642176841cf22ea9856df879811da90bf31224..93b3e363de22ae8be1da411703015a0f7c9ecc72 100644 (file)
@@ -22,7 +22,7 @@ public class QueryDebugImpl implements QueryDebug {
     @Override
     public Set<CacheEntry> getParents(AsyncRead request) {
         HashSet<CacheEntry> result = new HashSet<CacheEntry>();
-        CacheEntryBase entry = session.queryProvider2.asyncReadMap.get(request);
+        CacheEntryBase entry = session.queryProvider2.cache.asyncReadMap.get(request);
         if(entry != null) {
             for(CacheEntry parent : entry.getParents(session.queryProvider2)) result.add(parent);
         }
@@ -32,7 +32,7 @@ public class QueryDebugImpl implements QueryDebug {
     @Override
     public Set<CacheEntry> getParents(AsyncMultiRead request) {
         HashSet<CacheEntry> result = new HashSet<CacheEntry>();
-        CacheEntryBase entry = session.queryProvider2.asyncMultiReadMap.get(request);
+        CacheEntryBase entry = session.queryProvider2.cache.asyncMultiReadMap.get(request);
         if(entry != null) {
             for(CacheEntry parent : entry.getParents(session.queryProvider2)) result.add(parent);
         }
@@ -42,7 +42,7 @@ public class QueryDebugImpl implements QueryDebug {
     @Override
     public Set<CacheEntry> getParents(Read request) {
         HashSet<CacheEntry> result = new HashSet<CacheEntry>();
-        CacheEntryBase entry = session.queryProvider2.readMap.get(request);
+        CacheEntryBase entry = session.queryProvider2.cache.readMap.get(request);
         if(entry != null) {
             for(CacheEntry parent : entry.getParents(session.queryProvider2)) result.add(parent);
         }
@@ -52,7 +52,7 @@ public class QueryDebugImpl implements QueryDebug {
     @Override
     public Set<CacheEntry> getParents(MultiRead request) {
         HashSet<CacheEntry> result = new HashSet<CacheEntry>();
-        CacheEntryBase entry = session.queryProvider2.multiReadMap.get(request);
+        CacheEntryBase entry = session.queryProvider2.cache.multiReadMap.get(request);
         if(entry != null) {
             for(CacheEntry parent : entry.getParents(session.queryProvider2)) result.add(parent);
         }
index cb191f6471bee1fd3930eb518cdbdc64067f4b8e..a8e2f2b61d316e747a84fed61e6da7a54c1723bf 100644 (file)
@@ -50,7 +50,7 @@ import org.simantics.db.procedure.AsyncMultiProcedure;
  * @see AsyncMultiProcedure
  * @see Session
  */
-public interface AsyncMultiRead<Result> extends Request {
+public interface AsyncMultiRead<Result> {
 
     /**
      * When a <code>GraphRequest</code> is serviced by the database session
index a197a5f2169edeac7a501ee042d71b377be9f704..42a87600a863fbc67deb39e0d9ce66029da7335c 100644 (file)
@@ -50,7 +50,7 @@ import org.simantics.db.procedure.AsyncProcedure;
  * @see AsyncProcedure
  * @see Session
  */
-public interface AsyncRead<Result> extends Request {
+public interface AsyncRead<Result> {
 
     /**
      * When a <code>GraphRequest</code> is serviced by the database session
@@ -77,5 +77,6 @@ public interface AsyncRead<Result> extends Request {
      */
     void perform(AsyncReadGraph graph, AsyncProcedure<Result> procedure);
     int getFlags();
+    int threadHash();
     
  }
diff --git a/bundles/org.simantics.db/src/org/simantics/db/request/Request.java b/bundles/org.simantics.db/src/org/simantics/db/request/Request.java
deleted file mode 100644 (file)
index ef605a5..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-package org.simantics.db.request;
-
-public interface Request {
-
-       /*
-        * The integer value obtained from this method can be used to determine
-        * the evaluation thread of the request.
-        * 
-        * @return A non-negative integer value
-        * 
-        */
-       int threadHash();
-       
-}
index 9814925e3715daba6e0efcc16451d5d59a76b0e3..4753c0b3b79d9c254cf245ce71246f3e4862da9c 100644 (file)
@@ -47,11 +47,6 @@ public abstract class QueryExecutor2 extends ReadRequest implements AsyncListene
        public void execute(RequestProcessor processor) throws DatabaseException {
                
                AsyncMultiRead<Object> request = new AsyncMultiRead<Object>() {
-
-                       @Override
-                       public int threadHash() {
-                               return hashCode();
-                       }
                        
                        @Override
                        public void perform(AsyncReadGraph graph,
index 2bec7c21eb3666dedbf9a68a573daee4c77354c6..8181e08fb9d2bf1a4a9fc4646e099f30f9e54fdf 100644 (file)
@@ -33,11 +33,6 @@ public class RequestParentTest3 extends ExistingDatabaseTest {
                QueryDebug debug = session.getService(QueryDebug.class);
                
                class Request implements AsyncMultiRead<Object> {
-
-                       @Override
-                       public int threadHash() {
-                               return hashCode();
-                       }
                        
             @Override
             public void perform(AsyncReadGraph graph, AsyncMultiProcedure<Object> callback) {