]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Generate parts of db client query code 65/1665/1
authorAntti Villberg <antti.villberg@semantum.fi>
Thu, 29 Mar 2018 11:25:01 +0000 (14:25 +0300)
committerAntti Villberg <antti.villberg@semantum.fi>
Thu, 29 Mar 2018 11:25:01 +0000 (14:25 +0300)
refs #6961

Change-Id: I444c5b7e1357fb80e52f46b8668153a19af7c587

52 files changed:
bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphSupport.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/InternalProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/TripleIntProcedureAdapter.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/CodeGen.java [new file with mode: 0644]
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/DirectSuperRelations.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DoubleKeyQueryHashMap.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/IntProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntSet.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/PossibleSuperRelation.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/Query.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java [new file with mode: 0644]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCollectorImpl2.java
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/QuerySupport.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/SyncIntProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ThreadRunnable.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TripleIntProcedure.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/UnaryQueryHash.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.procore/src/fi/vtt/simantics/procore/internal/QuerySupportImpl.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplSocket.java

index 614a96df3c47d29ea45e249dfb10df9653aaedf4..3337e55b8455d88dd760efbfc19fe0df08e31a50 100644 (file)
@@ -1896,35 +1896,36 @@ public class ReadGraphImpl implements ReadGraph {
 
                assert (request != null);
 
-               if (parent != null) {
+               return processor.query(this, request, parent, null, null);
 
-                       try {
-                               return processor.queryRead(this, request, parent, null, null);
-                       } catch (Throwable e) {
-                               if(e instanceof DatabaseException) throw (DatabaseException)e;
-                               else throw new DatabaseException(e);
-                       }
-
-               } else {
-
-                       try {
-
-                               return processor.tryQuery(this, request);
-
-                       } catch (Throwable throwable) {
-
-                               //Logger.defaultLogError("Internal read request failure", throwable);
-
-                               if (throwable instanceof DatabaseException)
-                                       throw (DatabaseException) throwable;
-                               else
-                                       throw new DatabaseException(
-                                                       "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
-                                                       throwable);
-
-                       }
-
-               }
+//             if (parent != null) {
+//
+//                     try {
+//                     } catch (Throwable e) {
+//                             if(e instanceof DatabaseException) throw (DatabaseException)e;
+//                             else throw new DatabaseException(e);
+//                     }
+//
+//             } else {
+//
+//                     try {
+//
+//                             return processor.tryQuery(this, request);
+//
+//                     } catch (Throwable throwable) {
+//
+//                             //Logger.defaultLogError("Internal read request failure", throwable);
+//
+//                             if (throwable instanceof DatabaseException)
+//                                     throw (DatabaseException) throwable;
+//                             else
+//                                     throw new DatabaseException(
+//                                                     "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
+//                                                     throwable);
+//
+//                     }
+//
+//             }
                
        }
 
@@ -1947,42 +1948,44 @@ public class ReadGraphImpl implements ReadGraph {
 
                ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
 
-               if (parent != null || listener != null) {
-                       
-                       try {
-                return processor.queryRead(this, request, parent, procedure, listener);
-            } catch (Throwable e) {
-                if(e instanceof DatabaseException) throw (DatabaseException)e;
-                else throw new DatabaseException(e);
-            }
-
-               } else {
-
-                       try {
-
-                               T t = processor.tryQuery(this, request);
-                               if(procedure != null)
-                                       procedure.execute(this, t);
-
-                               return t;
-
-                       } catch (Throwable throwable) {
-
-                               Logger.defaultLogError("Internal read request failure", throwable);
-
-                               if(procedure != null)
-                                       procedure.exception(this, throwable);
-                               
-                               if (throwable instanceof DatabaseException)
-                                       throw (DatabaseException) throwable;
-                               else
-                                       throw new DatabaseException(
-                                                       "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
-                                                       throwable);
-
-                       }
+        return processor.query(this, request, parent, procedure, listener);
+        
 
-               }
+//             if (parent != null || listener != null) {
+//                     
+//                     try {
+//            } catch (Throwable e) {
+//                if(e instanceof DatabaseException) throw (DatabaseException)e;
+//                else throw new DatabaseException(e);
+//            }
+//
+//             } else {
+//
+//                     try {
+//
+//                             T t = processor.tryQuery(this, request);
+//                             if(procedure != null)
+//                                     procedure.execute(this, t);
+//
+//                             return t;
+//
+//                     } catch (Throwable throwable) {
+//
+//                             Logger.defaultLogError("Internal read request failure", throwable);
+//
+//                             if(procedure != null)
+//                                     procedure.exception(this, throwable);
+//                             
+//                             if (throwable instanceof DatabaseException)
+//                                     throw (DatabaseException) throwable;
+//                             else
+//                                     throw new DatabaseException(
+//                                                     "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
+//                                                     throwable);
+//
+//                     }
+//
+//             }
 
        }
 
@@ -2071,71 +2074,71 @@ public class ReadGraphImpl implements ReadGraph {
 
                assert (request != null);
 
-               // System.out.println("syncRequest " + request + " syncParent=" +
-               // syncParent);
-
                ListenerBase listener = getListenerBase(procedure);
 
-               if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
-
-//                     Object syncParent = request;
-
-//                     final ReadGraphImpl newGraph = newSync();
-
-                       final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
-                                       procedure, request);
-                       
-                       processor.query(this, request, parent, wrapper, listener);
-
-//                     newGraph.waitAsync(syncParent);
-                       
-                       Throwable e = wrapper.getException();
-                       if (e != null) {
-                               // The request was async - produce meaningful stack trace by
-                               // wrapping
-                               if (e instanceof DatabaseException)
-                                       throw (DatabaseException) e;
-                               else
-                                       throw new DatabaseException(e);
-                       }
-                       
-                       return wrapper.getResult();
-
-               } else {
-
-                       // System.out.println("direct call " + request );
-
-                       // Do not set the sync state.parent for external threads
-//                     Object syncParent = request;
-
-//                     final ReadGraphImpl newGraph = newSync();
-
-                       final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
-                                       procedure, request);
-
-                       try {
-
-                               processor.tryQuery(this, request, wrapper);
-
-                       } catch (Throwable t) {
-
-                               wrapper.exception(this, t);
-
-                       }
-
-                       Throwable e = wrapper.getException();
-                       if (e != null) {
-                               // The request was async - produce meaningful stack trace by
-                               // wrapping
-                               if (e instanceof DatabaseException)
-                                       throw (DatabaseException) e;
-                               else
-                                       throw new DatabaseException(e);
-                       }
-                       
-                       return wrapper.getResult();
+               final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
+                               procedure, request);
+               
+               processor.query(this, request, parent, wrapper, listener);
+               
+               return wrapper.getResult();
 
-               }
+//             if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
+//
+////                   Object syncParent = request;
+//
+////                   final ReadGraphImpl newGraph = newSync();
+//
+//
+////                   newGraph.waitAsync(syncParent);
+//                     
+//                     Throwable e = wrapper.getException();
+//                     if (e != null) {
+//                             // The request was async - produce meaningful stack trace by
+//                             // wrapping
+//                             if (e instanceof DatabaseException)
+//                                     throw (DatabaseException) e;
+//                             else
+//                                     throw new DatabaseException(e);
+//                     }
+//                     
+//                     return wrapper.getResult();
+//
+//             } else {
+//
+//                     // System.out.println("direct call " + request );
+//
+//                     // Do not set the sync state.parent for external threads
+////                   Object syncParent = request;
+//
+////                   final ReadGraphImpl newGraph = newSync();
+//
+//                     final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
+//                                     procedure, request);
+//
+//                     try {
+//
+//                             processor.tryQuery(this, request, wrapper);
+//
+//                     } catch (Throwable t) {
+//
+//                             wrapper.exception(this, t);
+//
+//                     }
+//
+//                     Throwable e = wrapper.getException();
+//                     if (e != null) {
+//                             // The request was async - produce meaningful stack trace by
+//                             // wrapping
+//                             if (e instanceof DatabaseException)
+//                                     throw (DatabaseException) e;
+//                             else
+//                                     throw new DatabaseException(e);
+//                     }
+//                     
+//                     return wrapper.getResult();
+//
+//             }
 
        }
 
@@ -2143,40 +2146,36 @@ public class ReadGraphImpl implements ReadGraph {
 
                assert (request != null);
 
-               // System.out.println("syncRequest " + request + " syncParent=" +
-               // syncParent);
-
                ListenerBase listener = getListenerBase(procedure);
 
-               if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
-
-//                     final ReadGraphImpl newGraph = newSync();
-
-                       final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
-                                       procedure, request);
-
-                       processor.query(this, request, parent, wrapper, listener);
-
-               } else {
+               final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
+                               procedure, request);
 
-                       try {
+               processor.query(this, request, parent, wrapper, listener);
 
-//                             final ReadGraphImpl newGraph = newSync();
-                               processor.tryQuery(this, request, procedure);
-//                             newGraph.waitAsync(null);
-                               waitAsyncProcedure(procedure);
-
-                       } catch (Throwable t) {
-                               if(Development.DEVELOPMENT) {
-                                       if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
-                                       t.printStackTrace();
-                                       }
-                               }
-                               procedure.exception(this, t);
-                               waitAsyncProcedure(procedure);
-                       }
-
-               }
+//             if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
+//
+//
+//             } else {
+//
+//                     try {
+//
+////                           final ReadGraphImpl newGraph = newSync();
+//                             processor.tryQuery(this, request, procedure);
+////                           newGraph.waitAsync(null);
+//                             waitAsyncProcedure(procedure);
+//
+//                     } catch (Throwable t) {
+//                             if(Development.DEVELOPMENT) {
+//                                     if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
+//                                     t.printStackTrace();
+//                                     }
+//                             }
+//                             procedure.exception(this, t);
+//                             waitAsyncProcedure(procedure);
+//                     }
+//
+//             }
 
        }
 
@@ -2545,55 +2544,58 @@ public class ReadGraphImpl implements ReadGraph {
                assert (request != null);
 
                ListenerBase listener = getListenerBase(procedure);
-
+               
                final DataContainer<Throwable> exception = new DataContainer<Throwable>();
                final DataContainer<T> result = new DataContainer<T>();
 
-               if (parent != null || listener != null) {
-
-//                     final ReadGraphImpl newGraph = newSync();
-                       
-                       processor.query(this, request, parent, new Procedure<T>() {
+               processor.query(this, request, parent, new Procedure<T>() {
 
-                               @Override
-                               public void exception(Throwable throwable) {
-                                       exception.set(throwable);
-                                       procedure.exception(throwable);
-                               }
-
-                               @Override
-                               public void execute(T t) {
-                                       result.set(t);
-                                       procedure.execute(t);
-                               }
-
-                       }, listener);
-
-//                     newGraph.waitAsync(request);
-
-               } else {
-
-                       try {
+                       @Override
+                       public void exception(Throwable throwable) {
+                               exception.set(throwable);
+                               procedure.exception(throwable);
+                       }
 
-                               T t = processor.tryQuery(this, request);
+                       @Override
+                       public void execute(T t) {
                                result.set(t);
                                procedure.execute(t);
+                       }
 
-                       } catch (Throwable t) {
+               }, listener);
+               
 
-                           if (t instanceof DatabaseException) {
-                               exception.set((DatabaseException)t);
-                               procedure.exception(exception.get());
-                           } else {
-                               exception.set(new DatabaseException(
-                                       "Unexpected exception in ReadGraph.syncRequest(Read)",
-                                       t));
-                               procedure.exception(exception.get());
-                           }
 
-                       }
-
-               }
+//             if (parent != null || listener != null) {
+//
+////                   final ReadGraphImpl newGraph = newSync();
+//                     
+//
+////                   newGraph.waitAsync(request);
+//
+//             } else {
+//
+//                     try {
+//
+//                             T t = processor.tryQuery(this, request);
+//                             result.set(t);
+//                             procedure.execute(t);
+//
+//                     } catch (Throwable t) {
+//
+//                         if (t instanceof DatabaseException) {
+//                             exception.set((DatabaseException)t);
+//                             procedure.exception(exception.get());
+//                         } else {
+//                             exception.set(new DatabaseException(
+//                                     "Unexpected exception in ReadGraph.syncRequest(Read)",
+//                                     t));
+//                             procedure.exception(exception.get());
+//                         }
+//
+//                     }
+//
+//             }
 
                Throwable t = exception.get();
                if (t != null) {
@@ -5406,11 +5408,11 @@ public class ReadGraphImpl implements ReadGraph {
                if (parent != null || listener != null) {
 
                        try {
-                               processor.queryRead(this, request, parent, procedure,
-                                               listener);
-                       } catch (Throwable e) {
+                               processor.query(this, request, parent, procedure,listener);
+                       } catch (DatabaseException e) {
+                               Logger.defaultLogError(e);
                                // This throwable has already been transferred to procedure at this point - do nothing about it
-                               //Logger.defaultLogError("Internal error ", e);
+                               //
                        }
                        
                } else {
@@ -5515,7 +5517,11 @@ public class ReadGraphImpl implements ReadGraph {
 
                if (parent != null || listener != null) {
 
-                       processor.query(this, request, parent, procedure, listener);
+                       try {
+                               processor.query(this, request, parent, procedure, listener);
+                       } catch (DatabaseException e) {
+                               Logger.defaultLogError(e);
+                       }
 
                } else {
 
index dea87d54ea35c01ccaf4fa482e285d01e4d33fca..4f8dfd2eed4b544952bf9efee417315bf6928405 100644 (file)
@@ -85,14 +85,14 @@ public interface ReadGraphSupport {
     void forHasValue(ReadGraphImpl graph, Resource subject, AsyncProcedure<Boolean> procedure);
     void forOrderedSet(ReadGraphImpl graph, Resource subject, AsyncMultiProcedure<Resource> procedure);
 
-    <T> T queryRead(ReadGraphImpl graph, Read<T> request, CacheEntry parent, AsyncProcedure<T> procedure, ListenerBase listener) throws Throwable;
+    <T> T query(ReadGraphImpl graph, Read<T> request, CacheEntry parent, AsyncProcedure<T> procedure, ListenerBase listener) throws DatabaseException;
     <T> void query(ReadGraphImpl graph, MultiRead<T> request, CacheEntry parent, AsyncMultiProcedure<T> procedure, ListenerBase listener);
-    <T> void query(ReadGraphImpl graph, AsyncRead<T> request, CacheEntry parent, AsyncProcedure<T> procedure, ListenerBase listener);
+    <T> void query(ReadGraphImpl graph, AsyncRead<T> request, CacheEntry parent, AsyncProcedure<T> procedure, ListenerBase listener) throws DatabaseException;
     <T> void query(ReadGraphImpl graph, AsyncMultiRead<T> request, CacheEntry parent, AsyncMultiProcedure<T> procedure, ListenerBase listener);
     <T> void query(ReadGraphImpl graph, ExternalRead<T> request, CacheEntry parent, Procedure<T> procedure, ListenerBase listener);
     
-    <T> T tryQuery(final ReadGraphImpl graph, final Read<T> request) throws DatabaseException;
-    <T> void tryQuery(final ReadGraphImpl graph, final AsyncRead<T> request, final AsyncProcedure<T> procedure);
+//    <T> T tryQuery(final ReadGraphImpl graph, final Read<T> request) throws DatabaseException;
+//    <T> void tryQuery(final ReadGraphImpl graph, final AsyncRead<T> request, final AsyncProcedure<T> procedure);
     
     VirtualGraph getProvider(Resource subject, Resource predicate, Resource object);
     VirtualGraph getProvider(Resource subject, Resource predicate);
index aa6425a8c11210d1cea94f01b4f37402ec96cd53..18345d632b73ef4ee0b9b6da137d540dd033218c 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.procedure;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
 
 
 public interface InternalProcedure<Result> {
     
-    void execute(ReadGraphImpl graph, Result result);
-    void exception(ReadGraphImpl graph, Throwable throwable);
+    void execute(ReadGraphImpl graph, Result result) throws DatabaseException;
+    void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException;
        
 }
index 44e7aa38c2c692d38aa587a82a3e56f0929dcf66..9bb96a83617bcbe65539ff92c27be1ddb5f870d9 100644 (file)
@@ -11,6 +11,7 @@
  *******************************************************************************/
 package org.simantics.db.impl.procedure;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.query.TripleIntProcedure;
 
@@ -19,10 +20,10 @@ abstract public class TripleIntProcedureAdapter implements TripleIntProcedure {
     public TripleIntProcedureAdapter() {
     }
     @Override
-    public void execute(ReadGraphImpl graph, int s, int p, int o) {
+    public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
     }
     @Override
-    public void finished(ReadGraphImpl graph) {
+    public void finished(ReadGraphImpl graph) throws DatabaseException {
     }
     
 }
index 98833067faf81050f879e2ec6b3594e7ec363426..8013c1a47d2c0684e0a5a2cf1a3084ab079fcde6 100644 (file)
@@ -11,9 +11,8 @@
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.concurrent.Semaphore;
-
 import org.simantics.db.RelationInfo;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.IntProcedureAdapter;
 import org.simantics.db.impl.procedure.InternalProcedure;
@@ -22,7 +21,7 @@ import org.simantics.db.procedure.ListenerBase;
 
 final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure> {
        
-    public AssertedPredicates(final int r) {
+    AssertedPredicates(final int r) {
         super(r);
     }
 
@@ -30,99 +29,44 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
         return new AssertedPredicates(r);
     }
     
-    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.cache.assertedPredicatesMap.get(r);
-        if(entry == null) {
-               
-               entry = new AssertedPredicates(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-               
-            if(!entry.isReady()) {
-               synchronized(entry) {
-                    if(!entry.isReady()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>();
-//                     entry.procs.add(procedure);
-//                     return;
-                    }
-               }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-
-    }
-    
-    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.cache.assertedPredicatesMap.get(r);
-               if(entry != null && entry.isReady()) { 
-                       entry.performFromCache(graph, provider, procedure);
-                       return;
-               }
-        }
-        
-        runner(graph, r, provider, parent, listener, procedure);
-         
-    }
-
-    @Override
-    public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-        return provider.cache.assertedPredicatesMap.get(id);
-    }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.assertedPredicatesMap.put(id, this);
-       }
+//    final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
+//        QueryCache.runnerAssertedPredicates(graph, r, parent, listener, procedure);
+//    }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-           provider.cache.assertedPredicatesMap.remove(id);
+           provider.cache.remove(this);
        }
        
-       void computeInheritedAssertions(ReadGraphImpl graph, int type, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) {
+       void computeInheritedAssertions(ReadGraphImpl graph, int type, final IntProcedure proc) throws DatabaseException {
+               
+               QueryProcessor processor = graph.processor;
 
-           DirectObjects.queryEach(graph, type, queryProvider.getInherits(), queryProvider, this, null, new SyncIntProcedure() {
+               QueryCache.runnerDirectObjects(graph, type, processor.getInherits(), this, null, new SyncIntProcedure() {
 
             @Override
             public void run(ReadGraphImpl graph) {
-                
-//                finish(graph, queryProvider);
-//                     proc.finished(graph);
-                
             }
 
             @Override
-            public void execute(ReadGraphImpl graph,int inh) {
+            public void execute(ReadGraphImpl graph,int inh) throws DatabaseException {
 
                inc();
                 
-                AssertedPredicates.queryEach(graph, inh, queryProvider, AssertedPredicates.this, null, new IntProcedure() {
+                QueryCache.runnerAssertedPredicates(graph, inh, AssertedPredicates.this, null, new IntProcedure() {
 
                     @Override
                     public void execute(ReadGraphImpl graph, int ass) {
-                            
                        addOrSet(ass);
-//                     proc.execute(graph, ass);
-                        
                     }
 
                     @Override
-                    public void finished(ReadGraphImpl graph) {
+                    public void finished(ReadGraphImpl graph) throws DatabaseException {
                         dec(graph);
                     }
                                
                                @Override
                                public void exception(ReadGraphImpl graph, Throwable t) {
-//                                     proc.exception(graph, t);
                     }
                     
                 });
@@ -130,10 +74,8 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
-               
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
-                
             }
             
         });
@@ -141,22 +83,23 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
        }
 
     @Override
-    public Object computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) {
+    public Object compute(ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
 
-        computeInheritedAssertions(graph, id, queryProvider, proc, store);
+       QueryProcessor processor = graph.processor;
+       
+        computeInheritedAssertions(graph, id, proc);
 
-       DirectObjects.queryEach(graph, id, queryProvider.getAsserts(), queryProvider, this, null, new IntProcedure() {
+       QueryCache.runnerDirectObjects(graph, id, processor.getAsserts(), this, null, new IntProcedure() {
 
             @Override
-            public void execute(ReadGraphImpl graph, final int ass) {
+            public void execute(ReadGraphImpl graph, final int ass) throws DatabaseException {
                 
-                DirectObjects.queryEach(graph, ass, queryProvider.getHasPredicate(), queryProvider, AssertedPredicates.this, null, new IntProcedure() {
+               QueryCache.runnerDirectObjects(graph, ass, processor.getHasPredicate(), AssertedPredicates.this, null, new IntProcedure() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, final int pred) {
+                    public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
 
                        addOrSetHiding(graph, pred, AssertedPredicates.this);
-//                     proc.execute(graph, pred);
                         return;
                         
                     }
@@ -185,9 +128,9 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
             
         });
        
-        finish(graph, queryProvider);
+        finish(graph, processor);
 
-       performFromCache(graph, queryProvider, proc);
+       performFromCache(graph, proc);
         
         return getResult();
         
@@ -202,31 +145,10 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
         
        assert(!isReady());
 
-//        ArrayList<IntProcedure> p = null;
-
         synchronized(this) {
-
                setReady();
-//            p = procs;
-//            procs = null; 
-        
         }
 
-//        if(p != null) {
-//             IntArray v = (IntArray)getResult();
-//             if(v.data == null) {
-//                 if(v.sizeOrData != IntArray.NO_DATA) {
-//                     for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);
-//                 }
-//             } else {
-//                 for(IntProcedure proc : p) {
-//                     for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);
-//                 }
-//             }
-//             
-//             for(IntProcedure proc : p) proc.finished(graph);
-//        }
-
     }
 
     synchronized private void addOrSet(int add) {
@@ -262,16 +184,16 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
 
        };
 
-       synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) {
+       synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) throws DatabaseException {
 
        assert(isPending());
        
         IntArray value = (IntArray)getResult(); 
-               RelationInfo ri = RelationInfoQuery.queryEach(graph, add, graph.processor, parent, null, ip);
+               RelationInfo ri = QueryCacheBase.resultRelationInfoQuery(graph, add, parent, null, ip);
                if(ri.isFunctional) {
                        // Replace existing functional predicate if found
                        try {
-                               IntSet supers = SuperRelations.queryEach2(graph, add, graph.processor, parent, null, ip2);
+                               IntSet supers = QueryCache.resultSuperRelations(graph, add, parent, null, ip2); 
                        if(value.data == null) {
                            if(value.sizeOrData != IntArray.NO_DATA) {
                                if(supers.contains(value.sizeOrData)) {
@@ -299,7 +221,7 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
     }
     
     @Override
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
+    public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
 
        assert(isReady());
        
@@ -319,30 +241,21 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-        computeForEach(graph, provider, new IntProcedureAdapter() {
+        compute(graph, new IntProcedureAdapter() {
 
             @Override
             public void finished(ReadGraphImpl graph) {
-                s.release();
             }
 
             @Override
             public void exception(ReadGraphImpl graph, Throwable t) {
-               s.release();
                 new Error("Error in recompute.", t).printStackTrace();
             }
 
-        }, true);
-
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+        });
         
     }
     
-    
 }
index d6d6fc08ad373bc9579de74bf55f4cdc760e1e76..a9ce7c6f8c7334926e2c132b05a21bd941e9592d 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.simantics.db.RelationInfo;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
@@ -33,81 +33,28 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
         return new AssertedStatements(r1, r2);
     }
     
-    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.cache.assertedStatementsMap.get(id(r1,r2)); 
-        if(entry == null) {
-               
-               entry = new AssertedStatements(r1, r2);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-            return entry;
-            
-        } else {
-               
-            if(entry.isPending()) {
-                synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<TripleIntProcedure>();
-//                        entry.procs.add(procedure);
-//                        provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                        return entry;
-                    }
-                }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-        
-        return entry;
-        
-    }
-    
-    final public static AssertedStatements queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
-        
-        assert(r1 != 0);
-        assert(r2 != 0);
-        
-        final AssertedStatements entry = (AssertedStatements)provider.cache.assertedStatementsMap.get(id(r1,r2));
-        
-        if(parent == null && !(listener != null)) {
-            if(entry != null && entry.isReady()) { 
-                entry.performFromCache(graph, provider, procedure);
-                return entry;
-            }
-        }
-
-        return runner(graph, r1, r2, provider, entry, parent, listener, procedure);
-         
-    }
-
-    @Override
-    public BinaryQuery<TripleIntProcedure> getEntry(QueryProcessor provider) {
-        return provider.cache.assertedStatementsMap.get(id);
-    }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.assertedStatementsMap.put(id, this);
-       }
+//    final public static AssertedStatements queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException {
+//        
+//        assert(r1 != 0);
+//        assert(r2 != 0);
+//
+//        return QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, procedure);
+//         
+//    }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-           provider.cache.assertedStatementsMap.remove(id);
+           provider.cache.remove(this);
        }
        
-       void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) {
+       static void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final AssertedStatements entry, final TripleIntProcedure proc) throws DatabaseException {
            
-//        final AtomicBoolean found = new AtomicBoolean(0);
+        QueryProcessor processor = graph.processor;
 
-           DirectObjects.queryEach(graph, type, queryProvider.getInherits(), queryProvider, this, null, new SyncIntProcedure() {
+        QueryCache.runnerDirectObjects(graph, type, processor.getInherits(), entry, null, new SyncIntProcedure() {
 
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
 
 //                if(ri.isFunctional && found.get() == 1) {
 //
@@ -118,29 +65,29 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 //                    
 //                }
 
-                finish(graph, queryProvider);
+                finish(graph, entry);
                 proc.finished(graph);
                 
             }
 
             @Override
-            public void execute(ReadGraphImpl graph, int inh) {
+            public void execute(ReadGraphImpl graph, int inh) throws DatabaseException {
                 
 //                if(ri.isFunctional && found.get() == 1) return;
 
                 inc();
                 
-                AssertedStatements.queryEach(graph, inh, predicate, queryProvider, AssertedStatements.this, null, new TripleIntProcedureAdapter() {
+                QueryCache.runnerAssertedStatements(graph, inh, predicate, entry, null, new TripleIntProcedureAdapter() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, int s, int p, int o) {
+                    public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
 
 //                        if(ri.isFunctional) {
 //                            
 ////                            if(found.get() == 1) return;
 //
 //                            if(found.compareAndSet(0, o)) {
-                                if(addOrSet(s,p,o))
+                                if(addOrSet(s,p,o, entry))
                                        proc.execute(graph, s, p, o);
 //                            }
 //                            // If this was a duplicate, we can ignore this
@@ -162,12 +109,12 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                     }
 
                     @Override
-                    public void finished(ReadGraphImpl graph) {
+                    public void finished(ReadGraphImpl graph) throws DatabaseException {
                         dec(graph);
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        proc.exception(graph, t);
                                        dec(graph);
                     }
@@ -177,7 +124,7 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
             
@@ -185,47 +132,62 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
        }
 
-    void computeLocalAssertions(ReadGraphImpl graph, final int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) {
+    static void computeLocalAssertions(ReadGraphImpl graph, final int type, final int predicate, final RelationInfo ri, final AssertedStatements entry, final TripleIntProcedure proc) throws DatabaseException {
         
+        
+    }
+
+       @Override
+       public Object compute(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
+               computeForEach(graph, r1(), r2(), this, procedure);
+               return getResult();
+       }
+
+    public static void computeForEach(ReadGraphImpl graph, final int type, final int predicate, final AssertedStatements entry, final TripleIntProcedure procedure) throws DatabaseException {
+
+       RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, predicate, entry, null, QueryCache.emptyRelationInfoProcedure);
+       
         final AtomicInteger found = new AtomicInteger(0);
 
-        DirectObjects.queryEach(graph, type, queryProvider.getAsserts(), queryProvider, this, null, new SyncIntProcedure() {
+        QueryProcessor processor = graph.processor;
+        
+        QueryCache.runnerDirectObjects(graph, type, processor.getAsserts(), entry, null, new SyncIntProcedure() {
 
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
                 
                 if(ri.isFunctional && found.get() > 1) {
 
                     ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one asserted statement.");
-                    except(exception);
-                    proc.exception(graph, exception);
+                    except(exception, entry);
+                    procedure.exception(graph, exception);
                     return;
                     
                 }
 
                 if(ri.isFunctional && found.get() == 1) {
                                         
-                    finish(graph, queryProvider);
-                    proc.finished(graph);
+                    finish(graph, entry);
+                    procedure.finished(graph);
                     return;
                     
                 }
 
-                computeInheritedAssertions(graph, type, predicate, ri, queryProvider, proc);
+                computeInheritedAssertions(graph, type, predicate, ri, entry, procedure);
                 
             }
 
             @Override
-            public void execute(ReadGraphImpl graph, final int ass) {
+            public void execute(ReadGraphImpl graph, final int ass) throws DatabaseException {
                 
                 if(ri.isFunctional && found.get() > 1) return;
                 
                 inc();
                 
-                DirectObjects.queryEach(graph, ass, queryProvider.getHasPredicate(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+                QueryCache.runnerDirectObjects(graph, ass, processor.getHasPredicate(), entry, null, new IntProcedure() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, final int pred) {
+                    public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
                         
                         if(ri.isFunctional) {
                             
@@ -233,18 +195,18 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                             inc();
 
-                            DirectObjects.queryEach(graph, ass, queryProvider.getHasObject(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+                            QueryCache.runnerDirectObjects(graph, ass, processor.getHasObject(), entry, null, new IntProcedure() {
 
                                 @Override
-                                public void execute(ReadGraphImpl graph, final int object) {
+                                public void execute(ReadGraphImpl graph, final int object) throws DatabaseException {
                                     
                                     if(found.get() > 1) return;
 
                                     if(pred == predicate) {
 
                                        if(found.getAndIncrement() == 0) {
-                                               if(addOrSet(type, pred, object))
-                                                       proc.execute(graph, type, pred, object);
+                                               if(addOrSet(type, pred, object, entry))
+                                                       procedure.execute(graph, type, pred, object);
                                        }
                                        
                                         return;
@@ -255,10 +217,10 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                                         inc();
 
-                                        SuperRelations.queryEach(graph, pred, queryProvider, AssertedStatements.this, null, new InternalProcedure<IntSet>() {
+                                        QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
                                             
                                             @Override
-                                            public void execute(ReadGraphImpl graph, IntSet result) {
+                                            public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
                                                 
                                                 if(found.get() > 1) {
                                                     dec(graph);
@@ -268,8 +230,8 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                                 if(result.contains(predicate)) {
                     
                                                     if(found.getAndIncrement() == 0) {
-                                                               if(addOrSet(type, pred, object))
-                                                                       proc.execute(graph, type, pred, object);
+                                                               if(addOrSet(type, pred, object, entry))
+                                                                       procedure.execute(graph, type, pred, object);
                                                        }
                                                     
                                                 }
@@ -279,9 +241,9 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                             }
                                                        
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                            
-                                                               proc.exception(graph, t);
+                                                               procedure.exception(graph, t);
                                                 dec(graph);
                                                                
                                             }
@@ -293,13 +255,13 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                 }
                                 
                                 @Override
-                                public void finished(ReadGraphImpl graph) {
+                                public void finished(ReadGraphImpl graph) throws DatabaseException {
                                     dec(graph);
                                 }
                                        
                                        @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                               proc.exception(graph, t);
+                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                               procedure.exception(graph, t);
                                     dec(graph);
                                 }
 
@@ -309,15 +271,15 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                             inc();
 
-                            DirectObjects.queryEach(graph, ass, queryProvider.getHasObject(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+                            QueryCache.runnerDirectObjects(graph, ass, processor.getHasObject(), entry, null, new IntProcedure() {
 
                                 @Override
-                                public void execute(ReadGraphImpl graph, final int object) {
+                                public void execute(ReadGraphImpl graph, final int object) throws DatabaseException {
                                     
                                     if(pred == predicate) {
 
-                                        addOrSet(type, pred, object);
-                                        proc.execute(graph, type, pred, object);
+                                        addOrSet(type, pred, object, entry);
+                                        procedure.execute(graph, type, pred, object);
                                         return;
                                         
                                     }
@@ -326,15 +288,15 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                                         inc();
 
-                                        SuperRelations.queryEach(graph, pred, queryProvider, AssertedStatements.this, null, new InternalProcedure<IntSet>() {
+                                        QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
                                             
                                             @Override
-                                            public void execute(ReadGraphImpl graph, IntSet result) {
+                                            public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
                                                 
                                                 if(result.contains(predicate)) {
                     
-                                                    addOrSet(type, pred, object);
-                                                    proc.execute(graph, type, pred, object);
+                                                    addOrSet(type, pred, object, entry);
+                                                    procedure.execute(graph, type, pred, object);
 
                                                 }
                                                 
@@ -343,8 +305,8 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                             }
                                                        
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                                               proc.exception(graph, t);
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                                               procedure.exception(graph, t);
                                                 dec(graph);
                                             }
  
@@ -355,13 +317,13 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                 }
                                 
                                 @Override
-                                public void finished(ReadGraphImpl graph) {
+                                public void finished(ReadGraphImpl graph) throws DatabaseException {
                                     dec(graph);
                                 }
                                        
                                        @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                               proc.exception(graph, t);
+                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                               procedure.exception(graph, t);
                                     dec(graph);
                                 }
 
@@ -372,13 +334,13 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                     }
 
                     @Override
-                    public void finished(ReadGraphImpl graph) {
+                    public void finished(ReadGraphImpl graph) throws DatabaseException {
                         dec(graph);
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                       proc.exception(graph, t);
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                       procedure.exception(graph, t);
                         dec(graph);
                     }
 
@@ -387,41 +349,23 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
             
         });
         
-    }
-
-       @Override
-    public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final TripleIntProcedure procedure, boolean store) {
-        
-        RelationInfoQuery.queryEach(graph, r2(), provider, this, null, new InternalProcedure<RelationInfo>() {
-
-            @Override
-            public void execute(ReadGraphImpl graph, RelationInfo ri) {
-                
-                computeLocalAssertions(graph, r1(), r2(), ri, provider, procedure);
-                
-            }
-                       
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               procedure.exception(graph, t);
-            }
-        });
-        
     }
     
     @Override
     public String toString() {
-       return "AssertedStatements2[" + r1() + " - " + r2() + "]";
+       return "AssertedStatements[" + r1() + " - " + r2() + "]";
     }
 
     private boolean addOrSet(int s, int p, int o) {
+       
+       if(!isPending())
+               System.err.println("asd");
         
        assert(isPending());
         
@@ -447,35 +391,38 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
         
     }
     
-    final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
-        
-       assert(isPending());
-
-//        ArrayList<TripleIntProcedure> p = null;
-
-        synchronized(this) {
-               
-               setReady();
-//            p = procs;
-//            procs = null; 
-        
-        }
+    static boolean addOrSet(int s, int p, int o, AssertedStatements entry) {
+       if(entry != null) {
+               return entry.addOrSet(s, p, o);
+       } else {
+               return true;
+       }
+    }
+    
+    static void finish(ReadGraphImpl graph, AssertedStatements entry) {
         
-//        if(p != null) {
-//             final IntArray value = (IntArray)getResult();
-//             for(TripleIntProcedure proc : p) {
-//                     for(int i=0;i<value.size();i+=3) {
-//                             proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
-//                     }
-//             }
-//
-//             for(TripleIntProcedure proc : p) proc.finished(graph);
-//        }
+       if(!entry.isPending())
+               System.err.println("asd");
+       
+       assert(entry.isPending());
+       if(entry != null) {
+               synchronized(entry) {
+                       entry.setReady();
+               }
+       }
 
     }
+    
+    static void except(Throwable t, AssertedStatements entry) {
+       if(entry != null) {
+               synchronized(entry) {
+                       entry.except(t);
+               }
+       }
+    }
 
     @Override
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
+    public Object performFromCache(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
         
        assert(isReady());
         
@@ -493,15 +440,12 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        final Semaphore s = new Semaphore(0);
-
-        computeForEach(graph, provider, new TripleIntProcedureAdapter() {
+        compute(graph, new TripleIntProcedureAdapter() {
 
             @Override
             public void finished(ReadGraphImpl graph) {
-                s.release();
             }
                        
                        @Override
@@ -509,13 +453,7 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                throw new Error("Error in recompute", t);
             }
     
-        }, true);
-        
-        try {
-            s.acquire();
-        } catch (InterruptedException e) {
-            throw new Error(e);
-        }
+        });
         
     }
     
index a2a7aded168da11f23e65011de56fe42194cf8b6..ba08114c6a8261330846c5411ab1902a7f73a9ca 100644 (file)
@@ -14,19 +14,17 @@ package org.simantics.db.impl.query;
 import java.util.ArrayList;
 
 import org.simantics.db.AsyncReadGraph;
-import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.procedure.AsyncMultiProcedure;
 import org.simantics.db.request.AsyncMultiRead;
 import org.simantics.db.request.RequestFlags;
 
-final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
-
-//    public ArrayList<AsyncMultiProcedure<T>> procs = null;
+final public class AsyncMultiReadEntry<T> extends CacheEntryBase<AsyncMultiProcedure<T>> {
 
     protected AsyncMultiRead<T> request;
     
-    public AsyncMultiReadEntry(AsyncMultiRead<T> request) {
+    AsyncMultiReadEntry(AsyncMultiRead<T> request) {
        this.request = request;
     }
     
@@ -49,55 +47,22 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
     
     final synchronized public void finish(AsyncReadGraph graph) {
        
-//     new Exception("finish " + this).printStackTrace();
-       
-       if(!isPending()) {
-               System.err.println("aff");
-       }
-       
        assert(isPending());
 
-//        ArrayList<AsyncMultiProcedure<T>> p = null;
-
         synchronized(this) {
-
                setReady();
-//            p = procs;
-//            procs = null; 
-        
         }
         
-//        if(p != null) {
-//             ArrayList<T> v = (ArrayList<T>)getResult();
-//             if(v != null) {
-//                 for(AsyncMultiProcedure<T> proc : p) {
-//                     for(T t : v) proc.execute(graph, t);
-//                 }
-//             }
-//             
-//             for(AsyncMultiProcedure<T>  proc : p) proc.finished(graph);
-//        }
-        
     }
 
     final synchronized public void except(AsyncReadGraph graph, Throwable t) {
 
        assert(isPending());
 
-//        ArrayList<AsyncMultiProcedure<T>> p = null;
-
         synchronized(this) {
-
                except(t);
-//            p = procs;
-//            procs = null; 
-        
         }
         
-//        if(p != null) {
-//             for(AsyncMultiProcedure<T>  proc : p) proc.exception(graph, t);
-//        }
-        
     }
 
     @SuppressWarnings("unchecked")
@@ -121,42 +86,31 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
         return new Query() {
 
                        @Override
-                       public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-                               
-                               QueryProcessor qp = (QueryProcessor)provider;
-
-                               final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); 
+                       public void recompute(ReadGraphImpl graph) {
 
-                               try {
+                               request.perform(graph , new AsyncMultiProcedure<T>() {
 
-                                   request.perform(parentGraph , new AsyncMultiProcedure<T>() {
+                                       @Override
+                                       public void execute(AsyncReadGraph graph, T result) {
+                                               addOrSet(result);
+                                       }
 
-                        @Override
-                        public void execute(AsyncReadGraph graph, T result) {
-                            addOrSet(result);
-                        }
-                        
-                        public void finished(AsyncReadGraph graph) {
-                               finish(graph);
-                        };
-                                       
-                                       @Override
-                                       public void exception(AsyncReadGraph graph, Throwable t) {
-                            except(t);
-                           }
+                                       public void finished(AsyncReadGraph graph) {
+                                               finish(graph);
+                                       };
 
-                    });
+                                       @Override
+                                       public void exception(AsyncReadGraph graph, Throwable t) {
+                                               except(t);
+                                       }
 
-                               } catch (Throwable t) {
-                    except(t);
-                    if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                }
+                               });
                                
                        }
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                       processor.cache.asyncMultiReadMap.remove(request);
+                       processor.cache.remove(AsyncMultiReadEntry.this);
                        }
 
                        @Override
@@ -176,9 +130,7 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
 
        @SuppressWarnings("unchecked")
        @Override
-       public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-               
-               final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
+       public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure<T> proc) {
 
         if(isExcepted()) {
 
@@ -211,16 +163,6 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
                return getResult();
                
        }
-
-//     @Override
-//     public void performFromCache(int callerThread, Object provider,
-//                     Object procedure) {
-//
-//         QueryProvider2 queryProvider = (QueryProvider2)provider;
-//         ReadGraphImpl graph = ReadGraphImpl.forFromCache(callerThread, null, new ReadGraphSupportImpl(null, queryProvider, null));
-//             performFromCache(graph, provider, procedure);
-//             
-//     }
        
        @Override
        public String toString() {
@@ -228,4 +170,9 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
                else return request.toString() + statusOrException;
        }
 
+       @Override
+       public Object compute(ReadGraphImpl graph, AsyncMultiProcedure<T> procedure) throws DatabaseException {
+       return graph.processor.cache.performQuery(graph, request, this, procedure);
+       }
+
 }
index 2410dfad7cf3b7c1e1c4bd26eca52e53a45ffd6a..767a82bd5644e8759a282b8820c49b570a8fdd60 100644 (file)
 package org.simantics.db.impl.query;
 
 import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.DebugPolicy;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.request.AsyncRead;
 
-final public class AsyncReadEntry<T> extends CacheEntryBase {
+final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
 
     protected AsyncRead<T> request;
 
-    public AsyncReadEntry(AsyncRead<T> request) {
+    AsyncReadEntry(AsyncRead<T> request) {
        this.request = request;
        if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
     }
@@ -39,31 +40,17 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
     @Override
     public void discard() {
        super.discard();
-       //request = null;
        setResult(null);
     }
     
     final public void addOrSet(AsyncReadGraph graph, Object item) {
 
        assert(isPending());
-        
-//        ArrayList<AsyncProcedure<T>> p = null;
 
         synchronized(this) {
-               
             setResult(item);
                setReady();
-//            p = procs;
-//            procs = null;
-            
         }
-
-//        if(p != null)
-//            for(AsyncProcedure<T> proc : p) {
-//             proc.execute(graph, (T)item);
-////                proc.first.execute(graph, (T)item);
-////                proc.second.dec();
-//            }
         
     }
     
@@ -71,21 +58,10 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
     public void except(AsyncReadGraph graph, Throwable t) {
        
        assert(isPending());
-        
-//        ArrayList<AsyncProcedure<T>> p = null;
 
         synchronized(this) {
-               
             except(t);
-////            p = procs;
-//            procs = null;
-            
         }
-
-//        if(p != null)
-//            for(AsyncProcedure<T> proc : p) {
-//             proc.exception(graph, t);
-//            }
        
     }
     
@@ -96,15 +72,11 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
         return new Query() {
 
                        @Override
-                       public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-                               
-                               QueryProcessor qp = (QueryProcessor)provider;
-
-                               final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); 
+                       public void recompute(ReadGraphImpl graph) {
 
                                try {
 
-                                   request.perform(parentGraph , new AsyncProcedure<T>() {
+                                   request.perform(graph , new AsyncProcedure<T>() {
 
                         @Override
                         public void execute(AsyncReadGraph graph, T result) {
@@ -126,7 +98,7 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
 
                        @Override
                        public void removeEntry(QueryProcessor qp) {
-                       qp.cache.asyncReadMap.remove(request);
+                       qp.cache.remove(AsyncReadEntry.this);
                        }
 
                        @Override
@@ -145,15 +117,9 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
         
     }
 
-<<<<<<< HEAD
-       @SuppressWarnings("unchecked")
-       public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-=======
-       public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
->>>>>>> refs/heads/db-threads
+    @Override
+       public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
                
-        AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
-
            if(isExcepted()) {
             
             try {
@@ -176,6 +142,11 @@ final public class AsyncReadEntry<T> extends CacheEntryBase {
            
        }
 
+    @Override
+    public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+       return graph.processor.cache.performQuery(graph, request, this, procedure);
+    }
+    
        @Override
        public String toString() {
                if(isDiscarded()) return "DISCARDED " + request.toString();
index 9437ffe379f6eda5be21ab5410e46566442b50d0..3d7e14c289e5650594f86e08ab12ab2f2cbf5bf0 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.request.RequestFlags;
 
 
-abstract public class BinaryQuery<Procedure> extends CacheEntryBase implements Query {
+abstract public class BinaryQuery<Procedure> extends CacheEntryBase<Procedure> implements Query {
 
        final public long id;
 
@@ -66,22 +67,21 @@ abstract public class BinaryQuery<Procedure> extends CacheEntryBase implements Q
         return this;
     }
     
-    @Override
-    public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-        recompute(graph, (QueryProcessor)provider);
-    }
-    
-    @SuppressWarnings("unchecked")
-    @Override
-    public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-        return performFromCache(graph, (QueryProcessor)provider, (Procedure)procedure);
-    }
+//    @Override
+//    public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) throws DatabaseException {
+//        recompute(graph, (QueryProcessor)provider);
+//    }
+//    
+//    @SuppressWarnings("unchecked")
+//    @Override
+//    public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) throws DatabaseException {
+//        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 Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure);
-    abstract public void putEntry(QueryProcessor provider);
+    //abstract public void recompute(ReadGraphImpl graph, QueryProcessor provider) throws DatabaseException ;
+    //abstract public void compute(ReadGraphImpl graph, Procedure procedure) throws DatabaseException ;
+    //abstract public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure) throws DatabaseException;
+    //abstract public void putEntry(QueryProcessor provider);
     abstract public void removeEntry(QueryProcessor provider);
-    abstract public BinaryQuery<Procedure> getEntry(QueryProcessor provider);
 
 }
index 3ac46276fe9d88f49f710eaa101ab8ef9210080b..833eafc4c41dc0b3b4072391866bb8c51fd48ec0 100644 (file)
@@ -15,6 +15,7 @@ import gnu.trove.impl.hash.THash;
 
 import java.lang.reflect.Array;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
 
@@ -34,55 +35,28 @@ abstract public class BinaryQueryHash<Procedure> extends THash {
 
     protected final BinaryQuery<Procedure> REMOVED = new BinaryQuery<Procedure>(-1, -1) {
 
-        @Override
-        public void computeForEach(ReadGraphImpl graph, QueryProcessor provider, Object procedure, boolean store) {
-            throw new Error("Not possible!");
-        }
-
-        @Override
-        public void putEntry(QueryProcessor provider) {
-            throw new Error("Not possible!");
-        }
-
-        @Override
-        public BinaryQuery<Procedure> getEntry(QueryProcessor provider) {
-            throw new Error("Not possible!");
-        }
-
-        @Override
-        public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-            throw new Error("Not possible!");
-        }
-
         @Override
         public void removeEntry(QueryProcessor provider) {
             throw new Error("Not possible!");
         }
 
-//        @Override
-//        public ICacheEntry2 cachedEntry(Object provider) {
-//            throw new Error("Not possible!");
-//        }
-//
-//        @Override
-//        public void computeForEach(int callerThread, Object provider, ICacheEntry2 parent, Object procedure) {
-//            throw new Error("Not possible!");
-//        }
-
         @Override
         public int type() {
             throw new Error("Not possible!");
         }
 
                @Override
-               public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider,
-                               Procedure procedure) {
+               public Object compute(ReadGraphImpl graph, Procedure procedure) throws DatabaseException {
+            throw new Error("Not possible!");
+               }
+
+               @Override
+               Object performFromCache(ReadGraphImpl graph, Procedure procedure) throws DatabaseException {
             throw new Error("Not possible!");
                }
 
                @Override
-               public Object performFromCache(ReadGraphImpl graph, Object provider,
-                               Object procedure) {
+               public void recompute(ReadGraphImpl graph) throws DatabaseException {
             throw new Error("Not possible!");
                }
         
index 34eba4eb123969bf99ca0623e2a3fc28144dd6fc..8489256225da47bfa52c2aa29940ffbba386e24b 100644 (file)
@@ -16,7 +16,7 @@ import org.simantics.db.impl.graph.ReadGraphImpl;
 
 
 
-public abstract class CacheEntry {
+public abstract class CacheEntry<Procedure> {
        
     final public static int HAS_BEEN_BOUND = 1;
 
@@ -32,25 +32,26 @@ public abstract class CacheEntry {
     abstract void setPending();
     abstract void discard();
     abstract void except(Throwable t);
-    abstract void setResult(Object result);
     abstract void clearResult(QuerySupport support);
 
     abstract void prepareRecompute(QuerySupport querySupport);
     
     abstract public Object getOriginalRequest();
     abstract Query getQuery();
-    abstract <T> T getResult();
 
     abstract CacheEntry pruneFirstParents();
     abstract void removeParent(CacheEntry entry);
     abstract void addParent(CacheEntry entry);
     abstract boolean hasParents();
-    abstract Iterable<CacheEntry> getParents(QueryProcessor processor);
+    abstract Iterable<CacheEntry<?>> getParents(QueryProcessor processor);
     abstract CacheEntry getFirstParent(QueryProcessor processor);
     abstract boolean moreThanOneParent(QueryProcessor processor);
     abstract int parentCount(QueryProcessor processor);
     
-    abstract Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure);
+    abstract <T> T getResult();
+    abstract void setResult(Object result);
+    
+    abstract Object performFromCache(ReadGraphImpl graph, Procedure procedure) throws DatabaseException;
     
     abstract boolean isImmutable(ReadGraphImpl graph) throws DatabaseException;
     
index 4070e060d0090e96969520ce2a0dad0ab7647df0..b17c066f380b5548562985d967cff6d33f5ed378 100644 (file)
@@ -14,15 +14,17 @@ package org.simantics.db.impl.query;
 import java.util.ArrayList;
 import java.util.Iterator;
 
-import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.RelationInfo;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.DebugPolicy;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
+import org.simantics.db.impl.query.QueryProcessor.Runner2Procedure;
+import org.simantics.db.procedure.ListenerBase;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-abstract public class CacheEntryBase extends CacheEntry {
+abstract public class CacheEntryBase<Procedure> extends CacheEntry<Procedure> {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(CacheEntryBase.class);
     
@@ -35,11 +37,11 @@ abstract public class CacheEntryBase extends CacheEntry {
        
     final public static CacheEntryBase[] NONE = new CacheEntryBase[0];
 
-       static private Object NO_RESULT = new Object();
+       static Object NO_RESULT = new Object();
        static protected Object INVALID_RESULT = new Object();
        
-       // Just created
-    static protected Object FRESH = new Object() { public String toString() { return "CREATED"; }};
+//     // Just created
+//    static protected Object FRESH = new Object() { public String toString() { return "CREATED"; }};
     // Result is computed - no exception
     static protected Object READY = new Object() { public String toString() { return "READY"; }};
     // Computation is under way
@@ -47,12 +49,12 @@ abstract public class CacheEntryBase extends CacheEntry {
     // Entry is discarded and is waiting for garbage collect
     static protected Object DISCARDED = new Object() { public String toString() { return "DISCARDED"; }};
     // The result has been invalidated
-    static protected Object REFUTED = new Object() { public String toString() { return "REFUTED"; }};
+    static protected Object REQUIRES_COMPUTATION = new Object() { public String toString() { return "REFUTED"; }};
     // The computation has excepted - the exception is in the result
     static protected Object EXCEPTED = new Object() { public String toString() { return "EXCEPTED"; }};
 
     // This indicates the status of the entry
-    public Object statusOrException = FRESH;
+    public Object statusOrException = REQUIRES_COMPUTATION;
     
     private CacheEntry p1 = null;
     private Object p2OrParents = null;
@@ -71,7 +73,7 @@ abstract public class CacheEntryBase extends CacheEntry {
     private Object result = NO_RESULT;
     
     final public boolean isFresh() {
-       return FRESH == statusOrException;
+       return REQUIRES_COMPUTATION == statusOrException;
     }
 
     public void setReady() {
@@ -97,12 +99,12 @@ abstract public class CacheEntryBase extends CacheEntry {
     @Override
     final public void refute() {
        if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: refuted " + this);
-       statusOrException = REFUTED;
+       statusOrException = REQUIRES_COMPUTATION;
     }
     
     @Override
     final public boolean isRefuted() {
-        return REFUTED == statusOrException;
+        return REQUIRES_COMPUTATION == statusOrException;
     }
 
     @Override
@@ -140,6 +142,10 @@ abstract public class CacheEntryBase extends CacheEntry {
         return PENDING == statusOrException;
     }
     
+    final public boolean requiresComputation() {
+       return REQUIRES_COMPUTATION == statusOrException;
+    }
+    
     final public boolean assertPending() {
        boolean result = isPending();
        if(!result) {
@@ -320,9 +326,9 @@ abstract public class CacheEntryBase extends CacheEntry {
     }
     
     @Override
-       final public Iterable<CacheEntry> getParents(QueryProcessor processor) {
+       final public Iterable<CacheEntry<?>> getParents(QueryProcessor processor) {
 
-               ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
+               ArrayList<CacheEntry<?>> result = new ArrayList<CacheEntry<?>>();
                if(p1 != null) result.add(p1);
                if(p2OrParents != null) {
                if(p2OrParents instanceof QueryIdentityHashSet) {
@@ -362,8 +368,7 @@ abstract public class CacheEntryBase extends CacheEntry {
        
     }
     
-    protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry> result) {
-       
+    protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {
     }
     
     protected String internalError() {
@@ -371,7 +376,7 @@ abstract public class CacheEntryBase extends CacheEntry {
     }
 
     
-    protected boolean handleException(ReadGraphImpl graph, IntProcedure procedure) {
+    protected boolean handleException(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
        if(isExcepted()) {
                procedure.exception(graph, (Throwable)getResult());
                return true;
@@ -380,7 +385,7 @@ abstract public class CacheEntryBase extends CacheEntry {
        }
     }
     
-    protected boolean handleException(ReadGraphImpl graph, TripleIntProcedure procedure) {
+    protected boolean handleException(ReadGraphImpl graph, TripleIntProcedure procedure) throws DatabaseException {
        if(isExcepted()) {
                procedure.exception(graph, (Throwable)getResult());
                return true;
@@ -389,7 +394,7 @@ abstract public class CacheEntryBase extends CacheEntry {
        }
     }
 
-    protected <T> boolean handleException(ReadGraphImpl graph, InternalProcedure<T> procedure) {
+    protected <T> boolean handleException(ReadGraphImpl graph, InternalProcedure<T> procedure) throws DatabaseException {
        if(isExcepted()) {
                procedure.exception(graph, (Throwable)getResult());
                return true;
@@ -426,10 +431,6 @@ abstract public class CacheEntryBase extends CacheEntry {
                clearResult(querySupport);
     }
     
-    /*
-     * 
-     * 
-     */
     @Override
     int getGCStatus() {
        return GCStatus;
@@ -455,10 +456,10 @@ abstract public class CacheEntryBase extends CacheEntry {
        // This is the original request for all built-in queries
        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);
+
+       public CacheEntryBase() {
+       }
+       
+    abstract public Object compute(ReadGraphImpl graph, Procedure procedure) throws DatabaseException;
     
 }
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CodeGen.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CodeGen.java
new file mode 100644 (file)
index 0000000..3bceb12
--- /dev/null
@@ -0,0 +1,189 @@
+package org.simantics.db.impl.query;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLDecoder;
+
+import org.simantics.utils.FileUtils;
+
+public class CodeGen {
+       
+       int indent = 4;
+       
+
+       
+       String[] signatureR1RelationInfo = { "int r", "r", "keyR", "long", "InternalProcedure<RelationInfo>", "entry.id" };
+       String[] signatureR1Bytes = { "int r", "r", "keyR", "long", "InternalProcedure<byte[]>", "entry.id" };
+       String[] signatureR1IntSet = { "int r", "r", "keyR", "long", "InternalProcedure<IntSet>", "entry.id" };
+       String[] signatureR1IP = { "int r", "r", "keyR", "long", "IntProcedure", "entry.id" };
+       String[] signatureR2IP = { "int r1, int r2", "r1,r2", "keyR2", "long", "IntProcedure", "entry.id" };
+       String[] signatureR2TIP = { "int r1, int r2", "r1,r2", "keyR2", "long", "TripleIntProcedure", "entry.id" };
+       String[] signatureID1 = { "String id", "id", "keyID", "String", "InternalProcedure<Integer>", "entry.id" };
+       String[] signatureID2 = { "String id", "id", "keyID", "String", "InternalProcedure<TObjectIntHashMap<String>>", "entry.id" };
+       String[] signatureRead = { "Read<?> r", "r", "id", "long", "AsyncProcedure", "entry.request" };
+       String[] signatureAsyncRead = { "AsyncRead<?> r", "r", "id", "long", "AsyncProcedure", "entry.request" };
+       String[] signatureMultiRead = { "MultiRead<?> r", "r", "id", "long", "AsyncMultiProcedure", "entry.request" };
+       String[] signatureAsyncMultiRead = { "AsyncMultiRead<?> r", "r", "id", "long", "AsyncMultiProcedure", "entry.request" };
+       String[] signatureExternalRead = { "ExternalRead<?> r", "r", "id", "long", "AsyncProcedure", "entry.request" };
+
+       private void line(StringBuilder content, String line) {
+               for(int i=0;i<indent;i++)
+                       content.append(" ");
+               content.append(line);
+               content.append("\n");
+       }
+       
+       public void generateQuery(StringBuilder content, String clazz, String[] signature, boolean runnerShortcut) {
+               generateGetOrCreate(content, clazz, signature);
+               generateRemove(content, clazz, signature);
+               generateRunner(content, clazz, signature, runnerShortcut);
+       }
+       
+       public void generateRunner(StringBuilder content, String clazz, String[] signature, boolean shortcut) {
+               
+               line(content, "static void runner" + clazz + "(ReadGraphImpl graph, " + signature[0] + ", CacheEntry parent, ListenerBase listener, " + signature[4] + " procedure) throws DatabaseException {");
+               if(shortcut) {
+                       line(content, "    if(parent == null && listener == null) {");
+                       line(content, "        " + clazz + ".computeForEach(graph, " + signature[1] + ", null, procedure);");
+                       line(content, "        return;");
+                       line(content, "    }");
+               }
+               line(content, "    QueryCache cache  = graph.processor.cache;");
+               line(content, "    if(procedure == null) procedure = emptyProcedure" + clazz + ";");
+               line(content, "    " + clazz + " entry = (" + clazz + ")cache.getOrCreate" + clazz + "(" + signature[1] + ");");
+               line(content, "    ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);");
+               line(content, "    if(entry.isReady()) entry.performFromCache(graph, procedure);");
+               line(content, "    else {");
+               if(shortcut) line(content, "        " + clazz + ".computeForEach(graph, " + signature[1] + ", entry, procedure);");
+               else line(content, "        entry.compute(graph, procedure);"); 
+               line(content, "        if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());");
+               line(content, "    }");
+               line(content, "}");
+               line(content, "");
+
+       }
+       
+       public void generateRemove(StringBuilder content, String clazz, String[] signature) {
+
+               String lower = Character.toLowerCase(clazz.charAt(0)) + clazz.substring(1);
+
+               line(content, "void remove(" + clazz + " entry) {");
+               line(content, "    synchronized(" + lower + "Map) {");
+               line(content, "        " + lower + "Map.remove(" + signature[5] + ");");
+               line(content, "    }");
+               line(content, "}");
+               line(content, "");
+
+       }
+
+       public void generateGetOrCreate(StringBuilder content, String clazz, String[] signature) {
+               
+               String lower = Character.toLowerCase(clazz.charAt(0)) + clazz.substring(1);
+               
+               line(content, "" + clazz + " getOrCreate" + clazz + "(" + signature[0] + ") throws DatabaseException {");
+               line(content, "    " + clazz + " existing = null;");
+               line(content, "    synchronized(" + lower + "Map) {");
+               line(content, "        existing = (" + clazz + ")" + lower + "Map.get(" + signature[1] + ");");
+               line(content, "        if(existing == null) {");
+               line(content, "            existing = new " + clazz + "(" + signature[1] + ");");
+               line(content, "            existing.clearResult(querySupport);");
+               line(content, "            existing.setPending();");
+               line(content, "            " + lower + "Map.put(" + signature[2] + "(" + signature[1] + "), existing);");
+               line(content, "            return existing;");
+               line(content, "        }");
+               line(content, "        if(existing.requiresComputation()) {");
+               line(content, "            existing.setPending();");
+               line(content, "            return existing;");
+               line(content, "        }");
+               line(content, "    }");
+               line(content, "    if(existing.isPending()) waitPending(existing);");
+               line(content, "    return existing;");
+               line(content, "}");
+               line(content, "");
+               
+       }
+       
+       public void generate() {
+
+        URL classLocation = CodeGen.class.getResource(".");
+        if (classLocation != null) {
+            if (classLocation.getProtocol().equals("file")) {
+                try {
+                    URL resource = new URL(classLocation, ".");
+                    File path = new File(URLDecoder.decode(resource.getPath(), "UTF-8"));
+                    String target = path.getAbsolutePath().replace("\\", "/");
+                    target = target.replace("/bin/", "/src/") + "/QueryCache.java";
+                    System.err.println("target=" + target);
+                    File source = new File(target);
+                    StringBuilder content = new StringBuilder();
+                    content.append("package org.simantics.db.impl.query;\n");
+                    content.append("\n");
+                    content.append("import org.simantics.db.RelationInfo;\n");
+                    content.append("import org.simantics.db.exception.DatabaseException;\n");
+                    content.append("import org.simantics.db.impl.graph.ReadGraphImpl;\n");
+                    content.append("import org.simantics.db.impl.procedure.InternalProcedure;\n");
+                    content.append("import org.simantics.db.procedure.AsyncMultiProcedure;\n");
+                    content.append("import org.simantics.db.procedure.AsyncProcedure;\n");
+                    content.append("import org.simantics.db.procedure.ListenerBase;\n");
+                    content.append("import org.simantics.db.request.AsyncMultiRead;\n");
+                    content.append("import org.simantics.db.request.AsyncRead;\n");
+                    content.append("import org.simantics.db.request.ExternalRead;\n");
+                    content.append("import org.simantics.db.request.MultiRead;\n");
+                    content.append("import org.simantics.db.request.Read;\n");
+                    content.append("\n");
+                    content.append("import gnu.trove.map.hash.TObjectIntHashMap;\n");
+                    content.append("\n");
+                    
+                    content.append("public class QueryCache extends QueryCacheBase {\n");                    
+                    content.append("\n");
+                    line(content,"public QueryCache(QuerySupport querySupport, int threads) {");
+                    line(content,"    super(querySupport, threads);");
+                    line(content,"}");
+                    content.append("\n");
+                    
+                    generateQuery(content, "Objects", signatureR2IP, true);
+                    generateQuery(content, "Statements", signatureR2TIP, true);
+                    generateQuery(content, "DirectObjects", signatureR2IP, true);
+                    generateQuery(content, "RelationInfoQuery", signatureR1RelationInfo, true);
+                    generateQuery(content, "URIToResource", signatureID1, true);
+                    generateQuery(content, "ValueQuery", signatureR1Bytes, true);
+                    generateQuery(content, "OrderedSet", signatureR1IP, true);
+                    generateQuery(content, "PrincipalTypes", signatureR1IP, true);
+                    generateQuery(content, "DirectPredicates", signatureR1IntSet, true);
+                    generateQuery(content, "Predicates", signatureR1IntSet, true);
+                    generateQuery(content, "ReadEntry", signatureRead, true);
+                    generateQuery(content, "Types", signatureR1IntSet, true);
+
+                    generateQuery(content, "AssertedStatements", signatureR2TIP, false);
+                    generateQuery(content, "NamespaceIndex", signatureID2, false);
+                    generateQuery(content, "AssertedPredicates", signatureR1IP, false);
+                    generateQuery(content, "DirectSuperRelations", signatureR1IP, false);
+                    generateQuery(content, "SuperTypes", signatureR1IntSet, false);
+                    generateQuery(content, "TypeHierarchy", signatureR1IntSet, false);
+                    generateQuery(content, "SuperRelations", signatureR1IntSet, false);
+                    generateQuery(content, "AsyncReadEntry", signatureAsyncRead, false);
+                    generateQuery(content, "MultiReadEntry", signatureMultiRead, false);
+                    generateQuery(content, "AsyncMultiReadEntry", signatureAsyncMultiRead, false);
+                    generateQuery(content, "ExternalReadEntry", signatureExternalRead, false);
+                    content.append("}\n");
+                    FileUtils.writeFile(source, content.toString().getBytes());
+                } catch (MalformedURLException e) {
+                    e.printStackTrace();
+                } catch (UnsupportedEncodingException e) {
+                    e.printStackTrace();
+                } catch (IOException e) {
+                                       e.printStackTrace();
+                               }
+            }
+        }
+               
+       }
+       
+       public static void main(String[] args) {
+               new CodeGen().generate();
+       }
+       
+}
index 360e067fa28754aaee943b088e55ae25548b8166..c38a50b561fa643538a4f9bfd0d4dd6a09391a95 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.Collection;
-import java.util.concurrent.Semaphore;
-
 import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
 
 final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
 
-       private DirectObjects(final int r1, final int r2) {
+       DirectObjects(final int r1, final int r2) {
                super(r1, r2);
        }
 
@@ -30,88 +28,36 @@ final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
                return RequestFlags.INVALIDATE;
        }
        
-    @Override
-    public void clearResult(QuerySupport support) {
-       setResult(INVALID_RESULT);
-    }
-
-       final static DirectObjects entry(final QueryProcessor provider, final int r1, final int 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.cache.directObjectsMap;
-               return hash.values(r1);
-       }
-
-       final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-
-       QueryProcessor processor = graph.processor;
-               
-               DirectObjects entry = (DirectObjects)processor.cache.directObjectsMap.get(id(r1,r2));
-               if(entry == null) {
-                       
-               entry = new DirectObjects(r1, r2);
-               entry.setPending();
-               entry.clearResult(processor.querySupport);
-               entry.putEntry(processor);
-               
-                       processor.performForEach(graph, entry, parent, listener, procedure);
-                       
-               } else {
-                       
-            if(entry.isPending()) {
-                synchronized(entry) {
-                    if(entry.isPending()) {
-                        processor.registerDependencies(graph, entry, parent, listener, procedure, false);
-                                               entry.computeForEach(graph, processor, procedure, true);
-                        return;
-                    }
-                }
-            }
-               
-                       processor.performForEach(graph, entry, parent, listener, procedure);
-                       
-               }
-
-       }
-
-       final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-
-        assert(r1 != 0);
-        assert(r2 != 0);
-        
-        if(parent == null && listener == null) {
-               DirectObjects.computeForEach(graph, r1, r2, null, procedure);
-        } else {
-               runner(graph, r1, r2, parent, listener, procedure);
-        }
-
-       }
-
-       @Override
-       public BinaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return provider.cache.directObjectsMap.get(id);
-       }
-
-       @Override
-       public void putEntry(QueryProcessor provider) {
-               provider.cache.directObjectsMap.put(id, this);
-       }
+//    @Override
+//    public void clearResult(QuerySupport support) {
+//     setResult(INVALID_RESULT);
+//    }
+
+//     final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
+//
+//        assert(r1 != 0);
+//        assert(r2 != 0);
+//        
+//        if(parent == null && listener == null) {
+//             DirectObjects.computeForEach(graph, r1, r2, null, procedure);
+//        } else {
+//             QueryCache.runnerDirectObjects(graph, r1, r2, parent, listener, procedure);
+//        }
+//
+//     }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.cache.directObjectsMap.remove(id);
+               provider.cache.remove(this);
        }
 
        @Override
-       public void computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure procedure, final boolean store) {
+       public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
                computeForEach(graph, r1(), r2(), this, procedure);
+               return getResult();
        }
 
-       static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) {
+       static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) throws DatabaseException {
 
                QueryProcessor processor = graph.processor;
                
@@ -120,8 +66,9 @@ final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
                processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
                        @Override
-                       public void execute(ReadGraphImpl graph, int i) {
+                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                procedure.execute(graph, i);
+                               if(entry != null) entry.add(i);
                        }
 
                        @Override
@@ -150,29 +97,46 @@ final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
                statusOrException = READY;
        }
        
+       final public void add(int add) {
+
+               assert(isPending());
+
+               IntArray value = (IntArray)getResult();
+               value.add(add);
+
+       }
+
        final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
                setReady();
        }
 
        @Override
-       public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
+       public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
 
                assert(isReady());
-               computeForEach(graph, provider, procedure, false);
-               return null;
+
+               if(handleException(graph, procedure)) return getResult();
+
+               final IntArray value = (IntArray)getResult();
+               if(value.data == null) {
+                       if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
+               } else {
+                       for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
+               }
+
+               procedure.finished(graph);
+               
+               return value;
 
        }
 
        @Override
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-
-        final Semaphore s = new Semaphore(0);
+       public void recompute(ReadGraphImpl graph) throws DatabaseException {
                
-               computeForEach(graph, provider, new IntProcedure() {
+               compute(graph, new IntProcedure() {
 
                        @Override
                        public void finished(ReadGraphImpl graph) {
-                               s.release();
                        }
 
                        @Override
@@ -184,11 +148,7 @@ final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
                        public void execute(ReadGraphImpl graphd, int i) {
                        }
 
-               }, true);
-
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+               });
 
        }
 
index 117448b5be8032157c1f696c7df23643f3de47c3..2d15b6b804031e8934640a526794fe349daf7c9e 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.concurrent.Semaphore;
-
 import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
-import org.simantics.db.impl.procedure.IntProcedureAdapter;
-import org.simantics.db.procedure.ListenerBase;
+import org.simantics.db.impl.procedure.InternalProcedure;
 
-final public class DirectPredicates extends CollectionUnaryQuery<IntProcedure> {
+final public class DirectPredicates extends CollectionUnaryQuery<InternalProcedure<IntSet>> {
 
-       private DirectPredicates(final int resource) {
+       DirectPredicates(final int resource) {
                super(resource);
        }
-
-       final static DirectPredicates entry(final QueryProcessor provider, final int 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.cache.directPredicatesMap.get(r);
-               if(entry == null) {
-
-                       entry = new DirectPredicates(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-                       
-                       provider.performForEach(graph, entry, parent, listener, procedure);
-                       
-               } else {
-                       
-                       if(!entry.isReady()) {
-                               synchronized(entry) {
-                                       if(!entry.isReady()) {
-                                               provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-                                               entry.computeForEach(graph, provider, procedure, true);
-                                               return;
-                                       }
-                               }
-                       }
-                       
-                       provider.performForEach(graph, entry, parent, listener, procedure);
-                       
-               }
-
-       }
-
-       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.cache.directPredicatesMap.get(r);
-                       if(entry != null && entry.isReady()) { 
-                               entry.performFromCache(graph, provider, procedure);
-                               return;
-                       } else {
-                           computeForEach(graph, r, null, procedure, false);
-                           return;
-                       }
-               }
-
-               runner(graph, r, provider, parent, listener, procedure);
-
-       }
-
        
        @Override
        public void clearResult(QuerySupport support) {
@@ -84,31 +28,21 @@ final public class DirectPredicates extends CollectionUnaryQuery<IntProcedure> {
                setResult(INVALID_RESULT);
        }
        
-       @Override
-       public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return provider.cache.directPredicatesMap.get(id);
-       }
-
-       @Override
-       public void putEntry(QueryProcessor provider) {
-               provider.cache.directPredicatesMap.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.cache.directPredicatesMap.remove(id);
+               provider.cache.remove(this);
        }
 
        @Override
-       public Object computeForEach(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure, boolean store) {
-           return computeForEach(graph, id, this, procedure, store);
+       public Object compute(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
+           return computeForEach(graph, id, this, procedure);
        }
        
-       public static Object computeForEach(ReadGraphImpl graph, int id, final DirectPredicates entry, final IntProcedure procedure, final boolean store) {
+       public static Object computeForEach(ReadGraphImpl graph, int id, final DirectPredicates entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
 
                graph.processor.querySupport.ensureLoaded(graph, id);
                
-               final IntArray list = new IntArray();
+               final IntSet list = new IntSet();
 
                graph.processor.querySupport.getPredicates(graph, id, new IntProcedure() {
 
@@ -128,16 +62,12 @@ final public class DirectPredicates extends CollectionUnaryQuery<IntProcedure> {
 
                });
 
-               if(entry != null)
-                   entry.finish(graph, graph.processor);
-
-               if(list.data == null) {
-                       if(list.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, list.sizeOrData);
-               } else {
-                       for(int i = 0;i < list.sizeOrData ; i++) procedure.execute(graph, list.data[i]);
+               if(entry != null) {
+                   entry.setResult(list);
+                   entry.setReady();
                }
-
-               procedure.finished(graph);
+               
+               procedure.execute(graph, list);
                
                return list;
 
@@ -152,45 +82,37 @@ final public class DirectPredicates extends CollectionUnaryQuery<IntProcedure> {
        public void setReady() {
                statusOrException = READY;
        }
-       
-       final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
-
-               setReady();
-
-       }
 
        @Override
-       public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
+       public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
 
                assert(isReady());
 
-               return computeForEach(graph, provider, procedure, false);
+       if(handleException(graph, procedure)) return EXCEPTED;
+       
+       IntSet result = getResult();
+       
+        procedure.execute(graph, result);
+        
+        return result;
 
        }
 
        @Override
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+       public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-               final Semaphore s = new Semaphore(0);
-
-               computeForEach(graph, provider, new IntProcedureAdapter() {
+               compute(graph, new InternalProcedure<IntSet>() {
 
                        @Override
-                       public void finished(ReadGraphImpl graph) {
-                               s.release();
+                       public void execute(ReadGraphImpl graph, IntSet set) {
                        }
 
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable t) {
-                               s.release();
                                new Error("Error in recompute.", t).printStackTrace();
                        }
 
-               }, true);
-
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+               });
 
        }
 
index c11e9f262118ceca8fdb0f2f4c7070f23c293c38..3932f7da6031bc4b083b53d327e6128aa2438864 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.procedure.TIntProcedure;
-import gnu.trove.set.hash.TIntHashSet;
-
-import java.util.ArrayList;
+import java.util.concurrent.Semaphore;
 
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
-import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.procedure.ListenerBase;
+import org.simantics.db.impl.procedure.IntProcedureAdapter;
 
-final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
+import gnu.trove.procedure.TIntProcedure;
+import gnu.trove.set.hash.TIntHashSet;
 
-       public ArrayList<InternalProcedure<IntSet>> procs = null;
+final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
 
-       private DirectSuperRelations(final int resource) {
+       DirectSuperRelations(final int resource) {
                super(resource);
        }
 
-       final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-
-               new DirectSuperRelations(r).computeForEach(graph, provider, procedure, false);
-
-       }
-
-       @Override
-       public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return null;
-       }
-
-       @Override
-       public void putEntry(QueryProcessor provider) {
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
+               provider.cache.remove(this);
        }
 
        class Koss {
@@ -70,15 +55,6 @@ final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
 
                }
 
-               //        public int[] toArray() {
-                       //            
-                       //            int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
-                       //            result[set.size()] = single;
-                       //            return result;
-                       //            
-                       //        }
-               //        
-
                public void forEach(TIntProcedure proc) {
                        if(single > 0) proc.execute(single);
                        if(set != null) set.forEach(proc);
@@ -87,20 +63,22 @@ final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
        }
 
        @Override
-       public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
+       public Object compute(final ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
 
-               provider.querySupport.ensureLoaded(graph, id);
+               QueryProcessor processor = graph.processor;
                
-               int single = provider.querySupport.getSingleSuperrelation(id);
+               processor.querySupport.ensureLoaded(graph, id);
+               
+               int single = processor.querySupport.getSingleSuperrelation(id);
                if(single > 0) {
                        procedure.execute(graph, single);
                        procedure.finished(graph);
                        return single;
                }
 
-               final int subrelationOf = provider.getSubrelationOf();
+               final int subrelationOf = processor.getSubrelationOf();
 
-               final IntSet result = new IntSet(provider.querySupport);
+               final IntSet result = new IntSet(processor.querySupport);
 
                final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
                        @Override
@@ -128,7 +106,7 @@ final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
 
                final DirectProcedure directProc = new DirectProcedure();
 
-               provider.querySupport.getObjects(graph, id, subrelationOf, directProc);
+               processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
 
                int size = directProc.size();
 
@@ -148,7 +126,11 @@ final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
                                @Override
                                public boolean execute(int arg0) {
 
-                                       procedure.execute(graph, arg0);
+                                       try {
+                                               procedure.execute(graph, arg0);
+                                       } catch (DatabaseException e) {
+                                               Logger.defaultLogError(e);
+                                       }
                                        return true;
 
                                }
@@ -166,17 +148,34 @@ final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
 
        @Override
        public String toString() {
-               return "SuperRelations2[" + id + "]";
+               return "DirectSuperRelations[" + id + "]";
        }
 
        @Override
-       public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
-               throw new UnsupportedOperationException();
+       public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
+
+               assert(isReady());
+
+               return compute(graph, procedure);
+               
        }
 
        @Override
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+       public void recompute(ReadGraphImpl graph) throws DatabaseException {
+
+               compute(graph, new IntProcedureAdapter() {
+
+                       @Override
+                       public void finished(ReadGraphImpl graph) {
+                       }
+
+                       @Override
+                       public void exception(ReadGraphImpl graph, Throwable t) {
+                               new Error("Error in recompute.", t).printStackTrace();
+                       }
 
+               });
+               
        }
 
 }
index f1aa887bba51ac1f425e7de97e74869fb7a528c0..d59f5beb80a3e54e2246785fc997a0bb3d9ab105 100644 (file)
@@ -193,9 +193,9 @@ public class DoubleKeyQueryHashMap<Procedure> extends DoubleKeyQueryHash<Procedu
        
     }
 
-    final public ArrayList<CacheEntry> values() {
+    final public <T extends BinaryQuery> ArrayList<T> values() {
 
-       ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
+       ArrayList<T> result = new ArrayList<T>();
        
         for (int i = _set.length; i-- > 0;) {
             if(_set[i] != null && _set[i] != REMOVED) {
index 31fe0103b646a23ed634c59a6616176406ba1d3d..7fb8598b7c36087f074ea71425b8c4c2434fc0d1 100644 (file)
@@ -14,12 +14,14 @@ package org.simantics.db.impl.query;
 import java.util.ArrayList;
 import java.util.LinkedList;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.procedure.Procedure;
 import org.simantics.db.request.ExternalRead;
 import org.simantics.db.request.RequestFlags;
 
-final public class ExternalReadEntry<T> extends CacheEntryBase {
+final public class ExternalReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
 
     final LinkedList<T> items = new LinkedList<T>();
 
@@ -93,37 +95,27 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
         return new Query() {
 
                        @Override
-                       public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-
-                           final QueryProcessor qp = (QueryProcessor)provider;
-                           synchronized(items) {
-
-                               if(entry.isExcepted()) {
-                                       
-                                       // Exception persists
-                                       
-                               } else {
-                               
-                                       // Update
-                                       if(!items.isEmpty()) {
-                                               setResult(items.removeFirst());
-                                       }
-                                       // Reschedule
-                                           if(!items.isEmpty()) {
-                                               qp.updatePrimitive(request);
-                                           }
-                                           
-                                           setReady();
-                                           
-                               }
-                               
-                           }
+                       public void recompute(ReadGraphImpl graph) {
+
+                               synchronized(items) {
+
+
+                                       // Update
+                                       if(!items.isEmpty()) {
+                                               setResult(items.removeFirst());
+                                       }
+                                       // Reschedule
+                                       if(!items.isEmpty()) {
+                                               graph.processor.updatePrimitive(request);
+                                       }
+
+                               }
                                
                        }
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                               processor.cache.externalReadMap.remove(request);
+                               processor.cache.remove(ExternalReadEntry.this);
                        }
 
                        @Override
@@ -141,38 +133,29 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
         
     }
 
-       public Object performFromCache(Object procedure) {
-               
-        Procedure<T> proc = (Procedure<T>)procedure;
+       @Override
+       public String toString() {
+               if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
+               else return request.toString() + " " + + System.identityHashCode(this);
+       }
+
+    @Override
+    public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
+       
+        AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
 
            if(isExcepted()) {
             
-            proc.exception((Throwable)getResult());
+            proc.exception(graph, (Throwable)getResult());
             
         } else {
             
-            proc.execute((T)getResult());
+            proc.execute(graph, (T)getResult());
 
         }
            
            return getResult();
-               
-       }
-
-       @Override
-       public String toString() {
-               if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
-               else return request.toString() + " " + + System.identityHashCode(this);
-       }
-
-    @Override
-    public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-        return performFromCache(procedure);
-    }
-    
-    @Override
-    public void setReady() {
-       super.setReady();
+       
     }
     
     @Override
@@ -180,4 +163,9 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
        // Do nothing - the state is already set and cannot be recomputed on demand
     }
 
+       @Override
+       public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+       return graph.processor.cache.performQuery(graph, request, this, procedure);
+       }
+
 }
index 0a9ccad900745d8af16d98fb8a321bae5bf18f98..4a7bbdb28acf9e52a4e68149a51c4c0add5c3c3e 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
 
 public interface IntProcedure {
 
-    void execute(ReadGraphImpl graph, int i);
-    void finished(ReadGraphImpl graph);
-    void exception(ReadGraphImpl graph, Throwable throwable);
+    void execute(ReadGraphImpl graph, int i) throws DatabaseException;
+    void finished(ReadGraphImpl graph) throws DatabaseException;
+    void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException;
     
 }
index 7c32b8e593e19ea7757355d3c1768c0757abe7c1..fdea8d2132e49c78ace27d26015ff8b5800084cd 100644 (file)
@@ -18,9 +18,12 @@ import java.util.Collection;
 import java.util.Iterator;
 import java.util.Set;
 
+import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.ResourceSet;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.ResourceImpl;
+import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.support.ResourceSupport;
 
 
@@ -286,5 +289,17 @@ final public class IntSet implements ResourceSet {
             }
         }
     }
+    
+    public void forEach(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
+        if(data != null) {
+            for(int i=0;i<sizeOrData;++i)
+               procedure.execute(graph, data[i]);
+        }
+        else if(sizeOrData == NO_DATA) {
+        } else {
+               procedure.execute(graph, sizeOrData);
+        }
+        procedure.finished(graph);
+    }
 
 }
\ No newline at end of file
index 1a1f88de6c8664adba1e3625499041380c2c1045..8d1003b801525d26a436061d8355f3a47b539527 100644 (file)
@@ -15,6 +15,7 @@ import java.util.ArrayList;
 
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.query.QueryProcessor.AsyncBarrier;
 import org.simantics.db.procedure.AsyncMultiProcedure;
@@ -22,13 +23,11 @@ import org.simantics.db.request.MultiRead;
 import org.simantics.db.request.RequestFlags;
 import org.simantics.utils.datastructures.Pair;
 
-final public class MultiReadEntry<T> extends CacheEntryBase {
-
-//    public ArrayList<Pair<AsyncMultiProcedure<T>, AsyncBarrier>> procs;
+final public class MultiReadEntry<T> extends CacheEntryBase<AsyncMultiProcedure<T>> {
 
     protected MultiRead<T> request;
     
-    public MultiReadEntry(MultiRead<T> request) {
+    MultiReadEntry(MultiRead<T> request) {
        this.request = request;
     }
 
@@ -56,27 +55,9 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
         ArrayList<Pair<AsyncMultiProcedure<T>, AsyncBarrier>> p = null;
 
         synchronized(this) {
-
                setReady();
-               
-//            p = procs;
-//            procs = null; 
-        
         }
         
-//        if(p != null) {
-//             ArrayList<T> v = (ArrayList<T>)getResult();
-//             if(v != null) {
-//                     for(Pair<AsyncMultiProcedure<T>, AsyncBarrier> pair : p) {
-//                             for(T t : v) pair.first.execute(graph, t);
-//                     }
-//             }
-//             for(Pair<AsyncMultiProcedure<T>, AsyncBarrier> pair : p) {
-//                     pair.first.finished(graph);
-//                     pair.second.dec();
-//             }
-//        }
-        
     }
 
     @Override
@@ -99,41 +80,30 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
         return new Query() {
 
                        @Override
-                       public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
+                       public void recompute(ReadGraphImpl graph) {
                                
-                               QueryProcessor qp = (QueryProcessor)provider;
-
-                               final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); 
-//                parentGraph.state.barrier.inc();
-
                                try {
 
-                                   request.perform(parentGraph , new AsyncMultiProcedure<T>() {
+                                   request.perform(graph , new AsyncMultiProcedure<T>() {
 
                         @Override
                         public void execute(AsyncReadGraph graph, T result) {
                             addOrSet(result);
-//                            parentGraph.state.barrier.dec();
                         }
                         
                         public void finished(AsyncReadGraph graph) {
                                finish(graph);
-//                            parentGraph.state.barrier.dec();
                         };
                                        
                                        @Override
                                        public void exception(AsyncReadGraph graph, Throwable t) {
                             except(t);
-//                            parentGraph.state.barrier.dec();
                            }
 
                     });
 
-//                                     parentGraph.waitAsync(request);
-
                                } catch (Throwable t) {
                     except(t);
-//                    parentGraph.state.barrier.dec();
                     if(DebugException.DEBUG) new DebugException(t).printStackTrace();
                 }
                                
@@ -141,7 +111,7 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                       processor.cache.multiReadMap.remove(request);
+                       processor.cache.remove(MultiReadEntry.this);
                        }
 
                        @Override
@@ -195,11 +165,8 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
     }
     
        @Override
-       public Object performFromCache(ReadGraphImpl graph, Object provider,
-                       Object procedure) {
+       public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure<T> proc) {
                
-               final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
-
         if(isExcepted()) {
             
             try {
@@ -237,4 +204,9 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
                else return request.toString() + statusOrException;
        }
 
+       @Override
+       public Object compute(ReadGraphImpl graph, AsyncMultiProcedure<T> procedure) throws DatabaseException {
+       return graph.processor.cache.performQuery(graph, request, this, procedure);
+       }
+
 }
index a0a8c19cbef996a68749fd8947c583f10b0a93ff..e55e99c6117bd1bf296c6f90a7b31f558ea7be51 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.map.hash.TObjectIntHashMap;
-
-import java.util.concurrent.Semaphore;
-
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.serialization.Serializer;
 import org.simantics.databoard.util.URIStringUtils;
 import org.simantics.db.common.WriteBindings;
 import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.procedure.ListenerBase;
+
+import gnu.trove.map.hash.TObjectIntHashMap;
 
 final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectIntHashMap<String>>> {
        
-    private NamespaceIndex(final String id) {
+    NamespaceIndex(final String id) {
         super(id);
     }
     
-    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.cache.namespaceIndexMap22.get(id); 
-        if(entry == null) {
-               
-               entry = new NamespaceIndex(id);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-
-            if(entry.isPending()) {
-               synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<TObjectIntHashMap<String>>>(); 
-//                     entry.procs.add(procedure);
-//                        provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                     return;
-                    }
-               }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-        
-    }
-    
-    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.cache.namespaceIndexMap22.get(id);
-        
-       if(parent == null && listener == null && entry != null && entry.isReady()) {
-               entry.performFromCache(graph, provider, procedure);
-               return;
-       }
-       
-        runner(graph, id, provider, entry, parent, listener, procedure);
-        
-    }
-     
-       @Override
-       public NamespaceIndex getEntry(QueryProcessor provider) {
-        return provider.cache.namespaceIndexMap22.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.namespaceIndexMap22.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.cache.namespaceIndexMap22.remove(id);
+               provider.cache.remove(this);
        }
        
-       final private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
+       final private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
                
                if(root == 0) {
             add2(graph, null);
@@ -102,10 +47,10 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
         
         final TObjectIntHashMap<String> result = new TObjectIntHashMap<String>();
         
-        Objects.runner(graph, root, consistsOf, graph.parent, null, new SyncIntProcedure() {
+        QueryCache.runnerObjects(graph, root, consistsOf, graph.parent, null, new SyncIntProcedure() {
                
                @Override
-                       public void run(ReadGraphImpl graph) {
+                       public void run(ReadGraphImpl graph) throws DatabaseException {
                        
                        if(isPending()) { 
                     add2(graph, result);
@@ -118,30 +63,28 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
                        }
 
                        @Override
-                       public void finished(ReadGraphImpl graph) {
-                           
+                       public void finished(ReadGraphImpl graph) throws DatabaseException {
                                dec(graph);
-                               
                        }
 
                @Override
-               public void execute(ReadGraphImpl graph, final int obj) {
+               public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
                        
                        //System.out.println(id + " => " + obj);
 
                        inc();
                        
-                       Objects.runner(graph, obj, hasName, graph.parent, null, new IntProcedure() {
+                       QueryCache.runnerObjects(graph, obj, hasName, graph.parent, null, new IntProcedure() {
                        
                        @Override
-                       public void execute(ReadGraphImpl graph, int i) {
+                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
 
                                inc();
 
-                               ValueQuery.queryEach(graph, i, NamespaceIndex.this, null, new InternalProcedure<byte[]>() {
+                               QueryCache.runnerValueQuery(graph, i, NamespaceIndex.this, null, new InternalProcedure<byte[]>() {
                                        
                                        @Override
-                                       public void execute(ReadGraphImpl graph, byte[] value) {
+                                       public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException {
                                                
                                                if(value != null) {
 
@@ -168,7 +111,7 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
                                        }
                                        
                                        @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                        except(t);
                                                dec(graph);
                             }
@@ -178,12 +121,12 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
                        }
                        
                        @Override
-                       public void finished(ReadGraphImpl graph) {
+                       public void finished(ReadGraphImpl graph) throws DatabaseException {
                                dec(graph);
                        }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                except(t);
                         dec(graph);
                     }
@@ -197,7 +140,9 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
     }
 
     @Override
-       public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
+       public Object compute(ReadGraphImpl graph, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
+       
+       QueryProcessor processor = graph.processor;
        
 //     System.err.println("NamespaceIndex " + id);
        
@@ -206,10 +151,10 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
         } else {
             final String[] parts = URIStringUtils.splitURI(id);
             if(parts != null) {
-                NamespaceIndex.queryEach(graph, parts[0], processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {
+                QueryCache.runnerNamespaceIndex(graph, parts[0], this, null, new InternalProcedure<TObjectIntHashMap<String>>() {
     
                     @Override
-                    public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
+                    public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) throws DatabaseException {
     
                         if(index != null) {
                             index(graph, processor, index.get(parts[1]), procedure);
@@ -222,7 +167,7 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
                     }
     
                     @Override
-                    public void exception(ReadGraphImpl graph, Throwable t) {
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                         if(DebugException.DEBUG) new DebugException(t).printStackTrace();
                         except(t);
                         procedure.exception(graph, t);
@@ -237,6 +182,8 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
 
         }
         
+        return getResult();
+        
     }
 
     @Override
@@ -278,26 +225,27 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
     }
     
     @Override
-    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<TObjectIntHashMap<String>> procedure) {
+    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
         
         assert(isReady());
         
-       if(handleException(graph, procedure)) return;
+       if(handleException(graph, procedure)) return (Throwable)statusOrException;
         
-        procedure.execute(graph, (TObjectIntHashMap<String>)getResult());
+       TObjectIntHashMap<String> result = (TObjectIntHashMap<String>)getResult();
+       
+        procedure.execute(graph, result);
+        
+        return result;
         
     }
     
     @Override
-    public synchronized void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        computeForEach(graph, provider, new InternalProcedure<TObjectIntHashMap<String>>() {
+        compute(graph, new InternalProcedure<TObjectIntHashMap<String>>() {
 
             @Override
             public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
-                s.release();
             }
                        
                        @Override
@@ -308,10 +256,6 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
 
         });
         
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
-        
     }
     
 }
index a35826341360c015e65578e71b3fd36690c43a3a..d95f3160a6774a53dc566a1b1d15054224b1a93d 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.Collection;
-import java.util.concurrent.Semaphore;
-
-import org.simantics.databoard.Bindings;
-import org.simantics.db.DevelopmentKeys;
 import org.simantics.db.RelationInfo;
 import org.simantics.db.Resource;
 import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.IntProcedureAdapter;
 import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.AsyncMultiProcedure;
-import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
-import org.simantics.utils.Development;
+
+import gnu.trove.procedure.TIntProcedure;
 
 /*
  * Size analysis:
@@ -48,144 +44,12 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                super(r1, r2);
        }
 
-       final static Objects entry(final QueryProcessor provider, final int r1, final int r2) {
-               return (Objects)provider.cache.objectsMap.get(r1,r2);
-       }
-
-       final static Collection<Objects> entries(final QueryProcessor processor, final int 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) {
-
-               if(parent == null && listener == null) {
-                       Objects.computeForEach(graph, r1, r2, null, procedure);
-                       return;
-               }
-
-               QueryProcessor processor = graph.processor;
-
-               Objects entry = (Objects)processor.cache.objectsMap.get(r1,r2);
-               if(entry == null) {
-
-                       entry = new Objects(r1, r2);
-                       entry.setPending();
-                       entry.clearResult(processor.querySupport);
-                       entry.putEntry(processor);
-
-                       processor.performForEach(graph, entry, parent, listener, procedure);
-
-               } else {
-
-                       if(entry.isPending()) {
-                               synchronized(entry) {
-                                       if(entry.isPending()) {
-                                               processor.registerDependencies(graph, entry, parent, listener, procedure, false);
-                                               computeForEach(graph, r1, r2, null, procedure);
-                                               return;
-                                       }
-                               }
-                       }
-
-                       processor.performForEach(graph, entry, parent, listener, procedure);
-
-               }
-
-       }
-
-       static class Runner2Procedure implements IntProcedure {
-           
-           public int single = 0;
-           public Throwable t = null;
-
-           public void clear() {
-               single = 0;
-               t = null;
-           }
-           
-        @Override
-        public void execute(ReadGraphImpl graph, int i) {
-            if(single == 0) single = i;
-            else single = -1;
-        }
-
-        @Override
-        public void finished(ReadGraphImpl graph) {
-            if(single == -1) single = 0;
-        }
-
-        @Override
-        public void exception(ReadGraphImpl graph, Throwable throwable) {
-            single = 0;
-            this.t = throwable;
-        }
-        
-        public int get() throws DatabaseException {
-            if(t != null) {
-                if(t instanceof DatabaseException) throw (DatabaseException)t;
-                else throw new DatabaseException(t);
-            }
-            return single;
-        }
-           
-       }
-       
-       static final Runner2Procedure runner2Procedure = new Runner2Procedure();
-       
-    public final static int runner2(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent) throws DatabaseException {
-
-        runner2Procedure.clear();
-
-        if(parent == null) {
-            Objects.computeForEach(graph, r1, r2, null, runner2Procedure);
-            return runner2Procedure.get();
-        }
-
-        QueryProcessor processor = graph.processor;
-
-        Objects entry = (Objects)processor.cache.objectsMap.get(r1,r2);
-        if(entry == null) {
-
-            entry = new Objects(r1, r2);
-            entry.setPending();
-            entry.clearResult(processor.querySupport);
-            entry.putEntry(processor);
-
-            processor.performForEach(graph, entry, parent, null, runner2Procedure);
-            return runner2Procedure.get();
-
-        } else {
-
-            if(entry.isPending()) throw new IllegalStateException();
-
-            processor.performForEach(graph, entry, parent, null, runner2Procedure);
-            return runner2Procedure.get();
-
-        }
-
-    }
-       
-       @Override
-       public BinaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return provider.cache.objectsMap.get(id);
-       }
-
-       @Override
-       public void putEntry(QueryProcessor provider) {
-               if(Development.DEVELOPMENT) {
-                       if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_PUT, Bindings.BOOLEAN)) {
-                               System.err.println("put " + this);
-                       }
-               }
-               provider.cache.objectsMap.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.cache.objectsMap.remove(id);
+               provider.cache.remove(this);
        }
 
-       final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) {
+       final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) throws DatabaseException {
 
                class AssertionMapProc implements IntProcedure {
 
@@ -215,10 +79,9 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        }
 
                        @Override
-                       public void execute(ReadGraphImpl graph, int type) {
-                               AssertedStatements stms = AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, NOPT);
+                       public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
                                if(result == null) {
-                                       result = stms.getResult();
+                                       result = QueryCacheBase.resultAssertedStatements(graph, type, r2, entry, null, NOPT);
                                } else {
                                        if (first) {
                                                IntArray ia = result;
@@ -228,7 +91,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                                }
                                                first = false;
                                        }
-                                       IntArray ia = stms.getResult();
+                                       IntArray ia = QueryCacheBase.resultAssertedStatements(graph, type, r2, entry, null, NOPT);
                                        if(ia.data != null) {
                                                for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
                                        }
@@ -248,13 +111,13 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                AssertionMapProc amp = new AssertionMapProc();
 
                // This dependency could be cut
-               PrincipalTypes.queryEach(graph, r1, graph.processor, entry, null, amp);
+               QueryCache.runnerPrincipalTypes(graph, r1, entry, null, amp);
 
                return amp.result;
 
        }
 
-       final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) {
+       final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException {
 
                IntArray map = getAssertionMap(graph, r1, r2, entry);
                if(map == null) {
@@ -286,13 +149,14 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        int candidateS = map.data[0];
                        int candidateO = map.data[2];
 
-                       SuperTypes candidate = SuperTypes.queryEach(graph, candidateS, graph.processor, entry, null, NOP);
-                       if(candidate.isExcepted()) {
-                               if(entry != null) entry.except((Throwable)candidate.getResult());
-                               procedure.exception(graph, (Throwable)candidate.getResult());
+                       IntSet candidateIs = null;
+                       try {
+                               candidateIs = QueryCache.resultSuperTypes(graph, candidateS, entry, null, NOP);
+                       } catch (DatabaseException e) {
+                               if(entry != null) entry.except(e);
+                               procedure.exception(graph, e);
                                return;
                        }
-                       IntSet candidateIs = candidate.getResult();
 
                        for(int i=3;i<map.size();i+=3) {
 
@@ -307,13 +171,22 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                        } else {
 
-                                               SuperTypes next = SuperTypes.queryEach(graph, nextS, graph.processor, entry, null, NOP);
-                                               if(next.isExcepted()) {
-                                                       if(entry != null) entry.except((Throwable)next.getResult());
-                                                       procedure.exception(graph, (Throwable)next.getResult());
+                                               IntSet nextIs = null;
+                                               try {
+                                                       nextIs = QueryCache.resultSuperTypes(graph, nextS, entry, null, NOP);
+                                               } catch (DatabaseException e) {
+                                                       if(entry != null) entry.except(e);
+                                                       procedure.exception(graph, e);
                                                        return;
                                                }
-                                               IntSet nextIs = next.getResult();
+                                               
+//                                             SuperTypes next = SuperTypes.queryEach(graph, nextS, graph.processor, entry, null, NOP);
+//                                             if(next.isExcepted()) {
+//                                                     if(entry != null) entry.except((Throwable)next.getResult());
+//                                                     procedure.exception(graph, (Throwable)next.getResult());
+//                                                     return;
+//                                             }
+//                                             IntSet nextIs = next.getResult();
 
                                                if(nextIs.contains(candidateS)) {
 
@@ -382,12 +255,12 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        };
 
        // Search for one statement
-       final public void computeFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, final RelationInfo ri, final IntProcedure procedure) {
+       final public void computeFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
                computeFunctionalIndex(graph, r1(), r2(), this, ri, procedure);
        }
 
        // Search for one statement
-       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final RelationInfo ri, final IntProcedure procedure) {
+       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
 
                if(ri.isFinal) {
 
@@ -403,7 +276,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
                                        @Override
-                                       public void execute(ReadGraphImpl graph, int i) {
+                                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                                if(entry != null) entry.addOrSetFunctional(i);
                                                else procedure.execute(graph, i);
                                        }
@@ -439,12 +312,13 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                } else {
 
                        // Note! The dependency is intentionally cut!
-                       DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
+            IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null, QueryCache.emptyProcedureDirectPredicates);
+            direct.forEach(graph, new SyncIntProcedure() {
 
                                boolean found = false;
 
                                @Override
-                               public void run(ReadGraphImpl graph) {
+                               public void run(ReadGraphImpl graph) throws DatabaseException {
 
                                        if(found) {
                                                if(entry != null) entry.finish(graph, procedure);
@@ -459,17 +333,17 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                }
 
                                @Override
-                               public void execute(ReadGraphImpl graph, final int pred) {
+                               public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
 
                                        if(found) return;
 
                                        if(pred == r2) {
 
                                                // Note! The dependency is intentionally cut!
-                                               DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+                                               QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
                                                        @Override
-                                                       public void execute(ReadGraphImpl graph, int i) {
+                                                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
 
                                                                if(!found) {
 
@@ -493,7 +367,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                                        }
 
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                                procedure.exception(graph, t);
                                                        }
 
@@ -501,20 +375,20 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                        } else {
 
-                                               SuperRelations.queryEach(graph, pred, graph.processor, entry, null, new InternalProcedure<IntSet>() {
+                                               QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
 
                                                        @Override
-                                                       public void execute(ReadGraphImpl graph, IntSet result) {
+                                                       public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
 
                                                                if(found) return;
 
                                                                if(result.contains(r2)) {
 
                                                                        // Note! The dependency is intentionally cut!
-                                                                       DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+                                                                       QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
                                                                                @Override
-                                                                               public void execute(ReadGraphImpl graph, int i) {
+                                                                               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
 
                                                                                        if(!found) {
 
@@ -538,7 +412,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                                                                }
 
                                                                                @Override
-                                                                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                                                        procedure.exception(graph, t);
                                                                                }
 
@@ -549,7 +423,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                                        }
 
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                                procedure.exception(graph, t);
                                                        }
 
@@ -560,7 +434,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                }
 
                                @Override
-                               public void finished(ReadGraphImpl graph) {
+                               public void finished(ReadGraphImpl graph) throws DatabaseException {
 
                                        dec(graph);
 
@@ -573,13 +447,13 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
        }
 
-       final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) {
+       final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException {
 
                // Note! The dependency is intentionally cut!
-               PrincipalTypes.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
+               QueryCache.runnerPrincipalTypes(graph, r1, null, null, new SyncIntProcedure() {
 
                        @Override
-                       public void run(ReadGraphImpl graph) {
+                       public void run(ReadGraphImpl graph) throws DatabaseException {
 
                                if(entry != null) entry.finish(graph, procedure);
                                else procedure.finished(graph);
@@ -589,18 +463,18 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        TripleIntProcedure proc = new TripleIntProcedure() {
 
                                @Override
-                               public void execute(ReadGraphImpl graph, int s, int p, int o) {
+                               public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
                                        if(entry != null) entry.addOrSet(o);
                                        else procedure.execute(graph, o);
                                }
 
                                @Override
-                               public void finished(ReadGraphImpl graph) {
+                               public void finished(ReadGraphImpl graph) throws DatabaseException {
                                        dec(graph);
                                }
 
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
                                        procedure.exception(graph, t);
                                        dec(graph);
@@ -609,16 +483,15 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        };
 
                        @Override
-                       public void execute(ReadGraphImpl graph, int type) {
+                       public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
 
                                inc();
-
-                               AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, proc);
+                               QueryCache.runnerAssertedStatements(graph, type, r2, entry, null, proc);
 
                        }
 
                        @Override
-                       public void finished(ReadGraphImpl graph) {
+                       public void finished(ReadGraphImpl graph) throws DatabaseException {
                                dec(graph);
                        }
 
@@ -633,24 +506,24 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
        }
 
-       final public void computeNotFunctionalIndex(ReadGraphImpl graph, RelationInfo ri, final IntProcedure procedure) {
+       final public void computeNotFunctionalIndex(ReadGraphImpl graph, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
                computeNotFunctionalIndex(graph, r1(), r2(), this, ri, procedure);
        }
 
-       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, RelationInfo ri, final IntProcedure procedure) {
+       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
 
                if(ri.isFinal) {
 
                        graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
                                @Override
-                               public void execute(ReadGraphImpl graph, int i) {
+                               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                        if(entry != null) entry.addOrSet(i);
                                        else procedure.execute(graph, i);
                                }
 
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
                                        procedure.exception(graph, t);
                                }
@@ -671,38 +544,39 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                } else {
 
                        // Note! The dependency is intentionally cut!
-                       DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
+            IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null, QueryCache.emptyProcedureDirectPredicates);
+            direct.forEach(graph, new SyncIntProcedure() {
 
                                @Override
-                               public void run(ReadGraphImpl graph) {
+                               public void run(ReadGraphImpl graph) throws DatabaseException {
 
                                        forAssertions(graph, r1, r2, entry, procedure);
 
                                }
 
                                @Override
-                               public void execute(ReadGraphImpl graph, final int pred) {
+                               public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
 
                                        if(pred == r2) {
 
                                                inc();
 
                                                // Note! The dependency is intentionally cut!
-                                               DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+                                               QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
                                                        @Override
-                                                       public void execute(ReadGraphImpl graph, int i) {
+                                                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                                                if(entry != null) entry.addOrSet(i);
                                                                else procedure.execute(graph, i);
                                                        }
 
                                                        @Override
-                                                       public void finished(ReadGraphImpl graph) {
+                                                       public void finished(ReadGraphImpl graph) throws DatabaseException {
                                                                dec(graph);
                                                        }
 
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                                procedure.exception(graph, t);
                                                                dec(graph);
                                                        }
@@ -713,31 +587,31 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                inc();
 
-                                               SuperRelations.queryEach(graph, pred, graph.processor, entry, null, new InternalProcedure<IntSet>() {
+                                               QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
 
                                                        @Override
-                                                       public void execute(ReadGraphImpl graph, IntSet result) {
+                                                       public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
 
                                                                if(result.contains(r2)) {
 
                                                                        inc();
 
                                                                        // Note! The dependency is intentionally cut!
-                                                                       DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+                                                                       QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
                                                                                @Override
-                                                                               public void execute(ReadGraphImpl graph, int i) {
+                                                                               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                                                                        if(entry != null) entry.addOrSet(i);
                                                                                        else procedure.execute(graph, i);
                                                                                }
 
                                                                                @Override
-                                                                               public void finished(ReadGraphImpl graph) {
+                                                                               public void finished(ReadGraphImpl graph) throws DatabaseException {
                                                                                        dec(graph);
                                                                                }
 
                                                                                @Override
-                                                                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                                                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
                                                                                        procedure.exception(graph, t);
                                                                                        dec(graph);
@@ -752,7 +626,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                                        }
 
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                                procedure.exception(graph, t);
                                                                dec(graph);
                                                        }
@@ -764,7 +638,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                }
 
                                @Override
-                               public void finished(ReadGraphImpl graph) {
+                               public void finished(ReadGraphImpl graph) throws DatabaseException {
                                        dec(graph);
                                }
 
@@ -775,13 +649,14 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        }
 
        @Override
-       public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
+       public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
                computeForEach(graph, r1(), r2(), this, procedure);
+               return getResult();
        }
 
-       public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) {
+       public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException {
 
-               RelationInfo ri = RelationInfoQuery.queryEach(graph, r2, graph.processor, entry, null, ip);
+               RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null, ip);
                graph.ensureLoaded(r1, r2);       
                if(ri.isFunctional) {
                        computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
@@ -808,7 +683,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                return "Objects[" + r1() + " - " + r2() + "]";
        }
 
-       final private void finish(ReadGraphImpl graph, IntProcedure procedure) {
+       final private void finish(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
 
                assert(assertPending());
 
@@ -853,7 +728,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        }
 
        @Override
-       public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
+       public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
 
                assert(isReady());
 
@@ -873,28 +748,20 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        }
 
        @Override
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-
-               final Semaphore s = new Semaphore(0);
+       public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-               computeForEach(graph, provider, new IntProcedureAdapter() {
+               compute(graph, new IntProcedureAdapter() {
 
                        @Override
                        public void finished(ReadGraphImpl graph) {
-                               s.release();
                        }
 
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable t) {
-                               s.release();
                                new Error("Error in recompute.", t).printStackTrace();
                        }
 
-               }, true);
-
-               while(!s.tryAcquire()) {
-                       provider.resume(graph);
-               }
+               });
 
        }
 
index c693d2a91f0e38add060c1be564f97252e806a56..38c6be6e1f30fc60e6b0a06dae1d5542c2f9b671 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.ArrayList;
-import java.util.concurrent.Semaphore;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.IntProcedureAdapter;
+import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.ListenerBase;
 
 final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
@@ -25,93 +26,32 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
         super(r);
     }
 
-    final static OrderedSet entry(final QueryProcessor provider, final int 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.cache.orderedSetMap.get(r); 
-        if(entry == null) {
-               
-               entry = new OrderedSet(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-
-            if(entry.isPending()) {
-               synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>();
-//                     entry.procs.add(procedure);
-//                        provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                     return;
-                    }
-               }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-
-    }
-    
-    final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-        
-       assert(r != 0);
-       
-       final OrderedSet entry = (OrderedSet)provider.cache.orderedSetMap.get(r);
-       
-       if(parent == null && !(listener != null)) {
-               if(entry != null && entry.isReady()) { 
-                       entry.performFromCache(graph, provider, procedure);
-                       return;
-               }
-        }
-
-        runner(graph, r, provider, entry, parent, listener, procedure);
-         
-    }
-
-    @Override
-    public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-        return provider.cache.orderedSetMap.get(id);
-    }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-           provider.cache.orderedSetMap.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-        provider.cache.orderedSetMap.remove(id);
+        provider.cache.remove(this);
        }
-    
-       private int current = 0;
 
-       private boolean nextElement(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
+       private static int nextElement(ReadGraphImpl graph, int current, int orderedSet, OrderedSet entry, final IntProcedure procedure) throws DatabaseException {
            
-               provider.querySupport.ensureLoaded(graph, current);             
+               QueryProcessor processor = graph.processor;
+               
+               processor.querySupport.ensureLoaded(graph, current);
+               
+               AtomicInteger res = new AtomicInteger(0);
                
-               boolean found = provider.querySupport.getObjects(graph, current, id, new IntProcedure() {
+               boolean found = processor.querySupport.getObjects(graph, current, orderedSet, new IntProcedure() {
 
                        @Override
-                       public void execute(ReadGraphImpl graph, int i) {
-                               if(i != id) {
-                                       addOrSet(i);
+                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                               if(i != orderedSet) {
+                                       if(entry != null) entry.addOrSet(i);
                                        procedure.execute(graph, i);
                                }
-                               current = i;
+                               res.set(i);
                        }
 
                        @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
+                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                if(DebugException.DEBUG) new DebugException(t).printStackTrace();
                                procedure.exception(graph, t);
                        }
@@ -122,31 +62,33 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
 
                });
 
-               if(!found) current = id;
-
-               if(current == id) {
-                       finish(graph, provider);
+               if(res.get() == orderedSet) {
+                       if(entry != null) entry.finish(graph, processor);
                        procedure.finished(graph);
-                       return false;
-               } else {
-                       return true;
                }
+               
+               return res.get();
 
        }
        
     @Override
     public void clearResult(QuerySupport support) {
-        current = id;
         setResult(new IntArray());
     }
        
     @Override
-    public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
-
-        while(nextElement(graph, provider, procedure, store));
-        
+    public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
+       computeForEach(graph, id, this, procedure);
         return getResult();
-        
+    }
+
+    static void computeForEach(ReadGraphImpl graph, int orderedSet, final OrderedSet entry, final IntProcedure procedure) throws DatabaseException {
+
+       int current = nextElement(graph, orderedSet, orderedSet, entry, procedure);
+       while(current != orderedSet) {
+               current = nextElement(graph, current, orderedSet, entry, procedure);
+       }
+
     }
     
     @Override
@@ -158,30 +100,10 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
         
        assert(isPending());
 
-//        ArrayList<IntProcedure> p = null;
-
         synchronized(this) {
-
                setReady();
-//            p = procs;
-//            procs = null; 
-        
         }
         
-//        if(p != null) {
-//             IntArray v = (IntArray)getResult();
-//             if(v.data == null) {
-//                 if(v.sizeOrData != IntArray.NO_DATA) {
-//                     for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);
-//                 }
-//             } else {
-//                 for(IntProcedure proc : p) {
-//                     for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);
-//                 }
-//             }
-//             for(IntProcedure proc : p) proc.finished(graph);
-//        }
-
     }
 
     final public void addOrSet(int add) {
@@ -194,7 +116,7 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
     }
 
     @Override
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
+    public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
        
        assert(isReady());
 
@@ -214,15 +136,12 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        computeForEach(graph, provider, new IntProcedureAdapter() {
+        compute(graph, new IntProcedureAdapter() {
 
             @Override
             public void finished(ReadGraphImpl graph) {
-                s.release();
             }
                        
                        @Override
@@ -230,11 +149,7 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
                                throw new Error("Error in recompute.", t);
             }
 
-        }, true);
-        
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+        });
         
     }
     
index 51c6c68d3da238342828090d5a93cca1a0cb2548..50e14d962f879df12cc62beb3154ade31e58db02 100644 (file)
@@ -15,6 +15,8 @@ import gnu.trove.procedure.TIntProcedure;
 import gnu.trove.set.hash.TIntHashSet;
 
 import org.simantics.db.Resource;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.procedure.ListenerBase;
@@ -36,15 +38,6 @@ final public class PossibleSuperRelation extends UnaryQuery<IntProcedure> {
 
        }
 
-       @Override
-       public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return null;
-       }
-
-       @Override
-       public void putEntry(QueryProcessor provider) {
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
        }
@@ -65,22 +58,11 @@ final public class PossibleSuperRelation extends UnaryQuery<IntProcedure> {
                }
 
                public int size() {
-
                        if(single == 0) return 0;
                        if(set == null) return 1;
                        return set.size() + 1;
-
                }
 
-               //        public int[] toArray() {
-                       //            
-                       //            int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
-                       //            result[set.size()] = single;
-                       //            return result;
-                       //            
-                       //        }
-               //        
-
                public void forEach(TIntProcedure proc) {
                        if(single > 0) proc.execute(single);
                        if(set != null) set.forEach(proc);
@@ -89,19 +71,21 @@ final public class PossibleSuperRelation extends UnaryQuery<IntProcedure> {
        }
 
        @Override
-       public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
+       public Object compute(final ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
 
-               provider.querySupport.ensureLoaded(graph, id);
-               int single = provider.querySupport.getSingleSuperrelation(id);
+               QueryProcessor processor = graph.processor;
+               
+               processor.querySupport.ensureLoaded(graph, id);
+               int single = processor.querySupport.getSingleSuperrelation(id);
                if(single > 0) {
                        procedure.execute(graph, single);
                        procedure.finished(graph);
                        return single;
                }
 
-               final int subrelationOf = provider.getSubrelationOf();
+               final int subrelationOf = processor.getSubrelationOf();
 
-               final IntSet result = new IntSet(provider.querySupport);
+               final IntSet result = new IntSet(processor.querySupport);
 
                final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
                        @Override
@@ -129,7 +113,7 @@ final public class PossibleSuperRelation extends UnaryQuery<IntProcedure> {
 
                final DirectProcedure directProc = new DirectProcedure();
 
-               provider.querySupport.getObjects(graph, id, subrelationOf, directProc);
+               processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
 
                int size = directProc.size();
 
@@ -149,7 +133,11 @@ final public class PossibleSuperRelation extends UnaryQuery<IntProcedure> {
                                @Override
                                public boolean execute(int arg0) {
 
-                                       procedure.execute(graph, arg0);
+                                       try {
+                                               procedure.execute(graph, arg0);
+                                       } catch (DatabaseException e) {
+                                               Logger.defaultLogError(e);
+                                       }
                                        return true;
 
                                }
@@ -166,17 +154,16 @@ final public class PossibleSuperRelation extends UnaryQuery<IntProcedure> {
 
        @Override
        public String toString() {
-               return "SuperRelations2[" + id + "]";
+               return "PossibleSuperRelation[" + id + "]";
        }
 
        @Override
-       public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
+       public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) {
                throw new UnsupportedOperationException();
        }
 
        @Override
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-
+       public void recompute(ReadGraphImpl graph) {
        }
 
 }
index 312522007e4f53483490e08554fb4773dd86e64a..3841e1d14640777706ce30421c00d2bcce2d43ea 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.procedure.TIntProcedure;
-
-import java.util.concurrent.Semaphore;
-
 import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
 
-final public class Predicates extends UnaryQuery<IntProcedure> {
-
-       public Predicates(final int r) {
-        super(r);
-    }
-
-    public static Predicates newInstance(final int r) {
-        return new Predicates(r);
-    }
-    
-    final static Predicates entry(final QueryProcessor provider, final int 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.cache.predicatesMap.get(r); 
-        if(entry == null) {
-               
-               entry = new Predicates(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-               
-            if(entry.isPending()) {
-               synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-                    }
-               }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-
-    }
-
-    final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
-
-       Predicates entry = (Predicates)provider.cache.predicatesMap.get(r); 
-        if(entry == null) {
-               
-               entry = new Predicates(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
-            
-        } else {
-               
-            if(entry.isPending()) {
-               synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-                    }
-               }
-            }
-            return (IntSet)provider.performForEach(graph, entry, parent, null, null);
-            
-        }
-
-    }
-    
-    final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-        
-       assert(r != 0);
-       
-       final Predicates entry = (Predicates)provider.cache.predicatesMap.get(r);
-       
-       if(parent == null && listener == null) {
-               if(entry != null && entry.isReady()) { 
-                       entry.performFromCache(graph, provider, procedure);
-                       return;
-               }
-        }
-
-        runner(graph, r, provider, entry, parent, listener, procedure);
-         
-    }
+import gnu.trove.procedure.TIntProcedure;
 
-    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.cache.predicatesMap.get(r);
-               if(entry != null && entry.isReady()) {
-                       return (IntSet)entry.get(graph, provider, null);
-               }
-        }
+final public class Predicates extends UnaryQuery<InternalProcedure<IntSet>> {
 
-        return runner2(graph, r, provider, parent);
-         
-    }
-    
-    @Override
-    public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-        return provider.cache.predicatesMap.get(id);
+       Predicates(final int r) {
+        super(r);
     }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-           provider.cache.predicatesMap.put(id, this);
-       }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-        provider.cache.predicatesMap.remove(id);
+        provider.cache.remove(this);
        }
     
-    final private void forAssertions(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure procedure, final boolean store) {
+    final static private void forAssertions(ReadGraphImpl graph, int r, Predicates entry, final IntSet set) throws DatabaseException {
 
-        PrincipalTypes.queryEach(graph, id, queryProvider, store ? Predicates.this : null, null, new SyncIntProcedure() {
+       QueryCache.runnerPrincipalTypes(graph, r, entry, null, new SyncIntProcedure() {
             
             @Override
-            public void run(ReadGraphImpl graph) {
-                
-                finish(graph, queryProvider);
-                procedure.finished(graph);
-                
+            public void run(ReadGraphImpl graph) throws DatabaseException {
             }
             
             IntProcedure proc = new IntProcedure() {
 
                 @Override
-                public void execute(ReadGraphImpl graph, int i) {
-                    if(addOrSet(queryProvider, i))
-                       procedure.execute(graph, i);
+                public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                       set.add(i);
                 }
 
                 @Override
-                public void finished(ReadGraphImpl graph) {
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
                     dec(graph);
                 }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                     if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                                       procedure.exception(graph, t);
                    }
 
             }; 
 
             @Override
-            public void execute(ReadGraphImpl graph, int type) {
+            public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
 
                inc();
-                
-                AssertedPredicates.queryEach(graph, type, queryProvider, store ? Predicates.this : null, null, proc);
+                QueryCache.runnerAssertedPredicates(graph, type, entry, null, proc);
                 
             }
             
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);       
             }
             
@@ -186,84 +77,67 @@ final public class Predicates extends UnaryQuery<IntProcedure> {
 
     }
 
-    @Override
-    public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
-
-               DirectPredicates.queryEach(graph, id, provider, store ? Predicates.this : null, null, new IntProcedure() {
-
-                       @Override
-                       public void execute(ReadGraphImpl graph, final int pred) {
-
-                               if(addOrSet(provider, pred))
-                                       procedure.execute(graph, pred);
-
-                       }
-
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-
-                               forAssertions(graph, provider, procedure, store);
-
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               procedure.exception(graph, t);
-                       }
+       @Override
+       public Object compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+               computeForEach(graph, id, this, procedure);
+               return getResult();
+       }
 
-               });
+       public static void computeForEach(ReadGraphImpl graph, final int r, final Predicates entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+       
+       
+       IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null, procedure);
+               IntSet result = new IntSet();
+               direct.forEach(new TIntProcedure() {
+                       @Override
+                       public boolean execute(int value) {
+                               result.add(value);
+                               return true;
+                       }
+               });
+               
+       forAssertions(graph, r, entry, result);
+       
+//             DirectPredicates.queryEach(graph, r, processor, entry, null, new IntProcedure() {
+//
+//                     @Override
+//                     public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
+//                             result.add(pred);
+//                     }
+//
+//                     @Override
+//                     public void finished(ReadGraphImpl graph) throws DatabaseException {
+//                             
+//
+//                     }
+//
+//                     @Override
+//                     public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+//                             procedure.exception(graph, t);
+//                     }
+//
+//             });
+               
+               if(entry != null) {
+                       entry.setResult(result);
+                       entry.setReady();
+               }
                
-               return getResult();
+               procedure.execute(graph, result);
         
     }
     
     @Override
     public String toString() {
-       return "Predicates2[" + id + "]";
+       return "Predicates[" + id + "]";
     }
 
     final public void finish(final ReadGraphImpl graph, QueryProcessor provider) {
         
-//        ArrayList<IntProcedure> p = null;
-
         synchronized(this) {
-
                setReady();
-//            p = procs;
-//            procs = null; 
-        
         }
-        
-//        if(p != null) {
-//        
-//             final ArrayList<IntProcedure> finalP = p;
-//
-//             IntSet v = (IntSet)getResult();
-//             v.forEach(new TIntProcedure() {
-//
-//                     @Override
-//                     public boolean execute(int arg0) {
-//                             for(IntProcedure proc : finalP) proc.execute(graph, arg0);
-//                             return true;
-//                     }
-//
-//             });
-//
-//             for(IntProcedure proc : p) proc.finished(graph);
-//        
-//        }
-
-    }
-
-    synchronized private boolean addOrSet(QueryProcessor processor, int add) {
 
-       if(!isPending()) {
-               setResult(new IntSet(null));
-       }
-       
-       IntSet value = (IntSet)getResult();
-        return value.add(add);
-        
     }
 
     @Override
@@ -272,55 +146,35 @@ final public class Predicates extends UnaryQuery<IntProcedure> {
     }
     
     @Override
-    public Object performFromCache(final ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
+    public Object performFromCache(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
        
        assert(isReady());
 
        if(handleException(graph, procedure)) return EXCEPTED;
        
-        IntSet v = getResult();
-        if(procedure != null) {
-               v.forEach(new TIntProcedure() {
-
-                       @Override
-                       public boolean execute(int arg0) {
-                               procedure.execute(graph, arg0);                                         
-                               return true;
-                       }
-               });
-            procedure.finished(graph);
-        }
+       IntSet result = getResult();
+       
+       procedure.execute(graph, result);
         
-        return v;
+        return result;
         
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        computeForEach(graph, provider, new IntProcedure() {
+        compute(graph, new InternalProcedure<IntSet>() {
 
-            @Override
-            public void finished(ReadGraphImpl graph) {
-                s.release();
-            }
-                       
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable t) {
                                throw new Error("Error in recompute.", t);
             }
 
                        @Override
-                       public void execute(ReadGraphImpl graph, int i) {
+                       public void execute(ReadGraphImpl graph, IntSet i) {
                        }
 
-        }, true);
-        
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+        });
         
     }
     
index 60fe074115e44aceb77d4b92a5a7b17a50724c4a..81c0efc20737d50e862f0d000bdf445488b871f5 100644 (file)
@@ -15,6 +15,8 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.concurrent.Semaphore;
 
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.IntProcedureAdapter;
 import org.simantics.db.impl.procedure.InternalProcedure;
@@ -25,71 +27,13 @@ import gnu.trove.set.hash.TIntHashSet;
 
 final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
-       private PrincipalTypes(final int resource) {
+       PrincipalTypes(final int resource) {
                super(resource);
        }
 
-       final static PrincipalTypes entry(final QueryProcessor provider, final int 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.cache.principalTypesMap.get(r); 
-               if(entry == null) {
-
-                       entry = new PrincipalTypes(r);
-               entry.setPending();
-               entry.clearResult(processor.querySupport);
-               entry.putEntry(processor);
-                       
-               processor.performForEach(graph, entry, parent, listener, procedure);
-                       
-               } else {
-                       
-            if(entry.isPending()) {
-                synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-//                                             if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>(1);
-//                                             entry.procs.add(procedure);
-//                                             processor.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                                             return;
-                                       }
-                               }
-                       }
-            processor.performForEach(graph, entry, parent, listener, procedure);
-               }
-
-       }
-
-       final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-
-               assert(r != 0);
-
-        if(parent == null && listener == null) {
-               PrincipalTypes.computeForEach(graph, r, null, graph.processor, procedure);
-        } else {
-               runner(graph, r, parent, listener, procedure);
-        }
-
-       }
-
-       @Override
-       public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return provider.cache.principalTypesMap.get(id);
-       }
-
-       @Override
-       public void putEntry(QueryProcessor provider) {
-               provider.cache.principalTypesMap.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.cache.principalTypesMap.remove(id);
+               provider.cache.remove(this);
        }
 
        static class Koss {
@@ -132,13 +76,13 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
        }
 
        @Override
-       public Object computeForEach(final ReadGraphImpl procedureGraph, final QueryProcessor provider, final IntProcedure proc, final boolean store) {
-
-               return computeForEach(procedureGraph, id, this, provider, proc);
-               
+       public Object compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
+               return computeForEach(graph, id, this, proc);
        }
        
-       public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) {
+       public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException {
+
+               QueryProcessor provider = graph.processor;
                
                provider.querySupport.ensureLoaded(graph, id);
                assert(id != 0);
@@ -169,7 +113,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                        }
 
                        @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
+                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                proc.exception(graph, t);
                        }
 
@@ -187,7 +131,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                        }
 
                        @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
+                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                proc.exception(graph, t);
                        }
 
@@ -230,6 +174,15 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                        
                        @Override
                        public boolean execute(final int arg0) {
+                               try {
+                                       return execute0(arg0);
+                               } catch (DatabaseException e) {
+                                       Logger.defaultLogError(e);
+                               }
+                               return false;
+                       }
+                       
+                       public boolean execute0(final int arg0) throws DatabaseException {
 
                                // No self-loops!
                                if(arg0 == id) {
@@ -257,7 +210,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                                        return true;
                                }
 
-                               PrincipalTypes.queryEach(graph, arg0, provider, entry, null, new IntProcedure() {
+                               QueryCache.runnerPrincipalTypes(graph, arg0, entry, null, new IntProcedure() {
 
                                        @Override
                                        public void execute(ReadGraphImpl graph, int i) {
@@ -267,7 +220,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                                        }
 
                                        @Override
-                                       public void finished(ReadGraphImpl graph) {
+                                       public void finished(ReadGraphImpl graph) throws DatabaseException {
 
                                                int current = (++finishes);
                                                if(current == indirect.size()) {
@@ -294,7 +247,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                                        }
 
                                        @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                proc.exception(graph, t);
                                        }
 
@@ -310,7 +263,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
        }
 
-       private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) {
+       private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) throws DatabaseException {
 
                if(entry != null) {
                        for(int i : material) {
@@ -333,7 +286,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
        }
 
-       private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes entry, final IntProcedure proc) {
+       private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException {
 
                //        if((counter++ % 500) == 0) System.out.println("PT " + counter + " mat = " + material.length);
 
@@ -352,10 +305,10 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                }
                final int nextIndex = index;
 
-               SuperTypes.queryEach(graph, type, provider, entry, null, new InternalProcedure<IntSet>() {
+               QueryCache.runnerSuperTypes(graph, type, entry, null, new InternalProcedure<IntSet>() {
 
                        @Override
-                       public void execute(ReadGraphImpl graph, IntSet supers) {
+                       public void execute(ReadGraphImpl graph, IntSet supers) throws DatabaseException {
 
                                synchronized(rejects) {
 
@@ -376,7 +329,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                        }
 
                        @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
+                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                proc.exception(graph, t);
                        }
 
@@ -398,41 +351,14 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
        final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
 
                assert(isPending());
-
-//             ArrayList<IntProcedure> p = null;
-
                synchronized(this) {
-
                        setReady();
-//                     p = procs;
-//                     procs = null;
-
                }
 
-//             if(p != null) {
-//
-//                     IntArray v = (IntArray)getResult();
-//                     if(v != null) {
-//                             if(v.data == null) {
-//                                     if(v.sizeOrData != IntArray.NO_DATA) {
-//                                             for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);
-//                                     }
-//                             } else {
-//                                     for(IntProcedure proc : p) {
-//                                             for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);
-//                                     }
-//                             }
-//                     }
-//
-//                     for(IntProcedure proc : p) proc.finished(graph);
-//
-//             }
-
-
        }
 
        @Override
-       public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
+       public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
 
                assert(isReady());
 
@@ -452,28 +378,20 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
        }
 
        @Override
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-
-               final Semaphore s = new Semaphore(0);
+       public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-               computeForEach(graph, provider, new IntProcedureAdapter() {
+               compute(graph, new IntProcedureAdapter() {
 
                        @Override
                        public void finished(ReadGraphImpl graph) {
-                               s.release();
                        }
 
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable t) {
-                               s.release();
                                new Error("Error in recompute.", t).printStackTrace();
                        }
 
-               }, true);
-
-       while(!s.tryAcquire()) {
-               provider.resume(graph);
-       }
+               });
 
        }
 
@@ -484,8 +402,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
     }
     
     @Override
-    protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry> result) {
-//             for(Objects o : Objects.entries(processor, id)) result.add(o);
+    protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {
     }
 
 }
index 29ba90e11cd250fc0aa2b02376faa804e208669a..3c22da5c92818e22a1a2383d69d8e05e759e7d98 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
-
-
 /*
  * The following methods need to be properly implemented
  * - equals()
  * - hashCode()  
  */
 public interface Query {
-
        
-       void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry);
+       void recompute(ReadGraphImpl graph) throws DatabaseException;
     void removeEntry(QueryProcessor processor);
     int type(); 
     
index 2adc7c78bff6fc0836d25ac373f0829987ca7ff1..4be12da47d582b08cf0d6dfe16129d42650c7509 100644 (file)
 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);
-                       }
-               }
-       }
-       
+import gnu.trove.map.hash.TObjectIntHashMap;
+
+public class QueryCache extends QueryCacheBase {
+
+    public QueryCache(QuerySupport querySupport, int threads) {
+        super(querySupport, threads);
+    }
+
+    Objects getOrCreateObjects(int r1, int r2) throws DatabaseException {
+        Objects existing = null;
+        synchronized(objectsMap) {
+            existing = (Objects)objectsMap.get(r1,r2);
+            if(existing == null) {
+                existing = new Objects(r1,r2);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                objectsMap.put(keyR2(r1,r2), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(Objects entry) {
+        synchronized(objectsMap) {
+            objectsMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerObjects(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            Objects.computeForEach(graph, r1,r2, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureObjects;
+        Objects entry = (Objects)cache.getOrCreateObjects(r1,r2);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            Objects.computeForEach(graph, r1,r2, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    Statements getOrCreateStatements(int r1, int r2) throws DatabaseException {
+        Statements existing = null;
+        synchronized(statementsMap) {
+            existing = (Statements)statementsMap.get(r1,r2);
+            if(existing == null) {
+                existing = new Statements(r1,r2);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                statementsMap.put(keyR2(r1,r2), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(Statements entry) {
+        synchronized(statementsMap) {
+            statementsMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, TripleIntProcedure procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            Statements.computeForEach(graph, r1,r2, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureStatements;
+        Statements entry = (Statements)cache.getOrCreateStatements(r1,r2);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            Statements.computeForEach(graph, r1,r2, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    DirectObjects getOrCreateDirectObjects(int r1, int r2) throws DatabaseException {
+        DirectObjects existing = null;
+        synchronized(directObjectsMap) {
+            existing = (DirectObjects)directObjectsMap.get(r1,r2);
+            if(existing == null) {
+                existing = new DirectObjects(r1,r2);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                directObjectsMap.put(keyR2(r1,r2), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(DirectObjects entry) {
+        synchronized(directObjectsMap) {
+            directObjectsMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerDirectObjects(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            DirectObjects.computeForEach(graph, r1,r2, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureDirectObjects;
+        DirectObjects entry = (DirectObjects)cache.getOrCreateDirectObjects(r1,r2);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            DirectObjects.computeForEach(graph, r1,r2, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    RelationInfoQuery getOrCreateRelationInfoQuery(int r) throws DatabaseException {
+        RelationInfoQuery existing = null;
+        synchronized(relationInfoQueryMap) {
+            existing = (RelationInfoQuery)relationInfoQueryMap.get(r);
+            if(existing == null) {
+                existing = new RelationInfoQuery(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                relationInfoQueryMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(RelationInfoQuery entry) {
+        synchronized(relationInfoQueryMap) {
+            relationInfoQueryMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            RelationInfoQuery.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureRelationInfoQuery;
+        RelationInfoQuery entry = (RelationInfoQuery)cache.getOrCreateRelationInfoQuery(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            RelationInfoQuery.computeForEach(graph, r, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    URIToResource getOrCreateURIToResource(String id) throws DatabaseException {
+        URIToResource existing = null;
+        synchronized(uRIToResourceMap) {
+            existing = (URIToResource)uRIToResourceMap.get(id);
+            if(existing == null) {
+                existing = new URIToResource(id);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                uRIToResourceMap.put(keyID(id), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(URIToResource entry) {
+        synchronized(uRIToResourceMap) {
+            uRIToResourceMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener, InternalProcedure<Integer> procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            URIToResource.computeForEach(graph, id, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureURIToResource;
+        URIToResource entry = (URIToResource)cache.getOrCreateURIToResource(id);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            URIToResource.computeForEach(graph, id, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    ValueQuery getOrCreateValueQuery(int r) throws DatabaseException {
+        ValueQuery existing = null;
+        synchronized(valueQueryMap) {
+            existing = (ValueQuery)valueQueryMap.get(r);
+            if(existing == null) {
+                existing = new ValueQuery(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                valueQueryMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(ValueQuery entry) {
+        synchronized(valueQueryMap) {
+            valueQueryMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<byte[]> procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            ValueQuery.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureValueQuery;
+        ValueQuery entry = (ValueQuery)cache.getOrCreateValueQuery(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            ValueQuery.computeForEach(graph, r, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    OrderedSet getOrCreateOrderedSet(int r) throws DatabaseException {
+        OrderedSet existing = null;
+        synchronized(orderedSetMap) {
+            existing = (OrderedSet)orderedSetMap.get(r);
+            if(existing == null) {
+                existing = new OrderedSet(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                orderedSetMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(OrderedSet entry) {
+        synchronized(orderedSetMap) {
+            orderedSetMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerOrderedSet(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            OrderedSet.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureOrderedSet;
+        OrderedSet entry = (OrderedSet)cache.getOrCreateOrderedSet(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            OrderedSet.computeForEach(graph, r, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    PrincipalTypes getOrCreatePrincipalTypes(int r) throws DatabaseException {
+        PrincipalTypes existing = null;
+        synchronized(principalTypesMap) {
+            existing = (PrincipalTypes)principalTypesMap.get(r);
+            if(existing == null) {
+                existing = new PrincipalTypes(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                principalTypesMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(PrincipalTypes entry) {
+        synchronized(principalTypesMap) {
+            principalTypesMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerPrincipalTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            PrincipalTypes.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedurePrincipalTypes;
+        PrincipalTypes entry = (PrincipalTypes)cache.getOrCreatePrincipalTypes(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            PrincipalTypes.computeForEach(graph, r, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    DirectPredicates getOrCreateDirectPredicates(int r) throws DatabaseException {
+        DirectPredicates existing = null;
+        synchronized(directPredicatesMap) {
+            existing = (DirectPredicates)directPredicatesMap.get(r);
+            if(existing == null) {
+                existing = new DirectPredicates(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                directPredicatesMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(DirectPredicates entry) {
+        synchronized(directPredicatesMap) {
+            directPredicatesMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            DirectPredicates.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureDirectPredicates;
+        DirectPredicates entry = (DirectPredicates)cache.getOrCreateDirectPredicates(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            DirectPredicates.computeForEach(graph, r, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    Predicates getOrCreatePredicates(int r) throws DatabaseException {
+        Predicates existing = null;
+        synchronized(predicatesMap) {
+            existing = (Predicates)predicatesMap.get(r);
+            if(existing == null) {
+                existing = new Predicates(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                predicatesMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(Predicates entry) {
+        synchronized(predicatesMap) {
+            predicatesMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            Predicates.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedurePredicates;
+        Predicates entry = (Predicates)cache.getOrCreatePredicates(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            Predicates.computeForEach(graph, r, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    ReadEntry getOrCreateReadEntry(Read<?> r) throws DatabaseException {
+        ReadEntry existing = null;
+        synchronized(readEntryMap) {
+            existing = (ReadEntry)readEntryMap.get(r);
+            if(existing == null) {
+                existing = new ReadEntry(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                readEntryMap.put(id(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(ReadEntry entry) {
+        synchronized(readEntryMap) {
+            readEntryMap.remove(entry.request);
+        }
+    }
+    
+    static void runnerReadEntry(ReadGraphImpl graph, Read<?> r, CacheEntry parent, ListenerBase listener, AsyncProcedure procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            ReadEntry.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureReadEntry;
+        ReadEntry entry = (ReadEntry)cache.getOrCreateReadEntry(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            ReadEntry.computeForEach(graph, r, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    Types getOrCreateTypes(int r) throws DatabaseException {
+        Types existing = null;
+        synchronized(typesMap) {
+            existing = (Types)typesMap.get(r);
+            if(existing == null) {
+                existing = new Types(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                typesMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(Types entry) {
+        synchronized(typesMap) {
+            typesMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+        if(parent == null && listener == null) {
+            Types.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureTypes;
+        Types entry = (Types)cache.getOrCreateTypes(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            Types.computeForEach(graph, r, entry, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    AssertedStatements getOrCreateAssertedStatements(int r1, int r2) throws DatabaseException {
+        AssertedStatements existing = null;
+        synchronized(assertedStatementsMap) {
+            existing = (AssertedStatements)assertedStatementsMap.get(r1,r2);
+            if(existing == null) {
+                existing = new AssertedStatements(r1,r2);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                assertedStatementsMap.put(keyR2(r1,r2), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(AssertedStatements entry) {
+        synchronized(assertedStatementsMap) {
+            assertedStatementsMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, TripleIntProcedure procedure) throws DatabaseException {
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureAssertedStatements;
+        AssertedStatements entry = (AssertedStatements)cache.getOrCreateAssertedStatements(r1,r2);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            entry.compute(graph, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    NamespaceIndex getOrCreateNamespaceIndex(String id) throws DatabaseException {
+        NamespaceIndex existing = null;
+        synchronized(namespaceIndexMap) {
+            existing = (NamespaceIndex)namespaceIndexMap.get(id);
+            if(existing == null) {
+                existing = new NamespaceIndex(id);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                namespaceIndexMap.put(keyID(id), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(NamespaceIndex entry) {
+        synchronized(namespaceIndexMap) {
+            namespaceIndexMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerNamespaceIndex(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener, InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureNamespaceIndex;
+        NamespaceIndex entry = (NamespaceIndex)cache.getOrCreateNamespaceIndex(id);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            entry.compute(graph, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    AssertedPredicates getOrCreateAssertedPredicates(int r) throws DatabaseException {
+        AssertedPredicates existing = null;
+        synchronized(assertedPredicatesMap) {
+            existing = (AssertedPredicates)assertedPredicatesMap.get(r);
+            if(existing == null) {
+                existing = new AssertedPredicates(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                assertedPredicatesMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(AssertedPredicates entry) {
+        synchronized(assertedPredicatesMap) {
+            assertedPredicatesMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerAssertedPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureAssertedPredicates;
+        AssertedPredicates entry = (AssertedPredicates)cache.getOrCreateAssertedPredicates(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            entry.compute(graph, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    DirectSuperRelations getOrCreateDirectSuperRelations(int r) throws DatabaseException {
+        DirectSuperRelations existing = null;
+        synchronized(directSuperRelationsMap) {
+            existing = (DirectSuperRelations)directSuperRelationsMap.get(r);
+            if(existing == null) {
+                existing = new DirectSuperRelations(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                directSuperRelationsMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(DirectSuperRelations entry) {
+        synchronized(directSuperRelationsMap) {
+            directSuperRelationsMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerDirectSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, IntProcedure procedure) throws DatabaseException {
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureDirectSuperRelations;
+        DirectSuperRelations entry = (DirectSuperRelations)cache.getOrCreateDirectSuperRelations(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            entry.compute(graph, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    SuperTypes getOrCreateSuperTypes(int r) throws DatabaseException {
+        SuperTypes existing = null;
+        synchronized(superTypesMap) {
+            existing = (SuperTypes)superTypesMap.get(r);
+            if(existing == null) {
+                existing = new SuperTypes(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                superTypesMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(SuperTypes entry) {
+        synchronized(superTypesMap) {
+            superTypesMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureSuperTypes;
+        SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            entry.compute(graph, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    TypeHierarchy getOrCreateTypeHierarchy(int r) throws DatabaseException {
+        TypeHierarchy existing = null;
+        synchronized(typeHierarchyMap) {
+            existing = (TypeHierarchy)typeHierarchyMap.get(r);
+            if(existing == null) {
+                existing = new TypeHierarchy(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                typeHierarchyMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(TypeHierarchy entry) {
+        synchronized(typeHierarchyMap) {
+            typeHierarchyMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerTypeHierarchy(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureTypeHierarchy;
+        TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            entry.compute(graph, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    SuperRelations getOrCreateSuperRelations(int r) throws DatabaseException {
+        SuperRelations existing = null;
+        synchronized(superRelationsMap) {
+            existing = (SuperRelations)superRelationsMap.get(r);
+            if(existing == null) {
+                existing = new SuperRelations(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                superRelationsMap.put(keyR(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(SuperRelations entry) {
+        synchronized(superRelationsMap) {
+            superRelationsMap.remove(entry.id);
+        }
+    }
+    
+    static void runnerSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureSuperRelations;
+        SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            entry.compute(graph, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    AsyncReadEntry getOrCreateAsyncReadEntry(AsyncRead<?> r) throws DatabaseException {
+        AsyncReadEntry existing = null;
+        synchronized(asyncReadEntryMap) {
+            existing = (AsyncReadEntry)asyncReadEntryMap.get(r);
+            if(existing == null) {
+                existing = new AsyncReadEntry(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                asyncReadEntryMap.put(id(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(AsyncReadEntry entry) {
+        synchronized(asyncReadEntryMap) {
+            asyncReadEntryMap.remove(entry.request);
+        }
+    }
+    
+    static void runnerAsyncReadEntry(ReadGraphImpl graph, AsyncRead<?> r, CacheEntry parent, ListenerBase listener, AsyncProcedure procedure) throws DatabaseException {
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureAsyncReadEntry;
+        AsyncReadEntry entry = (AsyncReadEntry)cache.getOrCreateAsyncReadEntry(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            entry.compute(graph, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    MultiReadEntry getOrCreateMultiReadEntry(MultiRead<?> r) throws DatabaseException {
+        MultiReadEntry existing = null;
+        synchronized(multiReadEntryMap) {
+            existing = (MultiReadEntry)multiReadEntryMap.get(r);
+            if(existing == null) {
+                existing = new MultiReadEntry(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                multiReadEntryMap.put(id(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(MultiReadEntry entry) {
+        synchronized(multiReadEntryMap) {
+            multiReadEntryMap.remove(entry.request);
+        }
+    }
+    
+    static void runnerMultiReadEntry(ReadGraphImpl graph, MultiRead<?> r, CacheEntry parent, ListenerBase listener, AsyncMultiProcedure procedure) throws DatabaseException {
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureMultiReadEntry;
+        MultiReadEntry entry = (MultiReadEntry)cache.getOrCreateMultiReadEntry(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            entry.compute(graph, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    AsyncMultiReadEntry getOrCreateAsyncMultiReadEntry(AsyncMultiRead<?> r) throws DatabaseException {
+        AsyncMultiReadEntry existing = null;
+        synchronized(asyncMultiReadEntryMap) {
+            existing = (AsyncMultiReadEntry)asyncMultiReadEntryMap.get(r);
+            if(existing == null) {
+                existing = new AsyncMultiReadEntry(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                asyncMultiReadEntryMap.put(id(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(AsyncMultiReadEntry entry) {
+        synchronized(asyncMultiReadEntryMap) {
+            asyncMultiReadEntryMap.remove(entry.request);
+        }
+    }
+    
+    static void runnerAsyncMultiReadEntry(ReadGraphImpl graph, AsyncMultiRead<?> r, CacheEntry parent, ListenerBase listener, AsyncMultiProcedure procedure) throws DatabaseException {
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureAsyncMultiReadEntry;
+        AsyncMultiReadEntry entry = (AsyncMultiReadEntry)cache.getOrCreateAsyncMultiReadEntry(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            entry.compute(graph, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
+    ExternalReadEntry getOrCreateExternalReadEntry(ExternalRead<?> r) throws DatabaseException {
+        ExternalReadEntry existing = null;
+        synchronized(externalReadEntryMap) {
+            existing = (ExternalReadEntry)externalReadEntryMap.get(r);
+            if(existing == null) {
+                existing = new ExternalReadEntry(r);
+                existing.clearResult(querySupport);
+                existing.setPending();
+                externalReadEntryMap.put(id(r), existing);
+                return existing;
+            }
+            if(existing.requiresComputation()) {
+                existing.setPending();
+                return existing;
+            }
+        }
+        if(existing.isPending()) waitPending(existing);
+        return existing;
+    }
+    
+    void remove(ExternalReadEntry entry) {
+        synchronized(externalReadEntryMap) {
+            externalReadEntryMap.remove(entry.request);
+        }
+    }
+    
+    static void runnerExternalReadEntry(ReadGraphImpl graph, ExternalRead<?> r, CacheEntry parent, ListenerBase listener, AsyncProcedure procedure) throws DatabaseException {
+        QueryCache cache  = graph.processor.cache;
+        if(procedure == null) procedure = emptyProcedureExternalReadEntry;
+        ExternalReadEntry entry = (ExternalReadEntry)cache.getOrCreateExternalReadEntry(r);
+        ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);
+        if(entry.isReady()) entry.performFromCache(graph, procedure);
+        else {
+            entry.compute(graph, procedure);
+            if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+        }
+    }
+    
 }
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java
new file mode 100644 (file)
index 0000000..4056f06
--- /dev/null
@@ -0,0 +1,1089 @@
+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.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;
+import gnu.trove.map.hash.TObjectIntHashMap;
+
+public class QueryCacheBase {
+
+       // Statistics
+       final int THREADS;
+       final public int  THREAD_MASK;
+       int                                             hits                  = 0;
+       int                                             misses                = 0;
+       int                                             updates               = 0;
+       public int                                              size                  = 0;
+       
+       volatile public boolean dirty = false;
+       public boolean                                         collecting            = false;
+
+       final protected THashMap<String, URIToResource>                      uRIToResourceMap;
+       final protected THashMap<String, NamespaceIndex>                     namespaceIndexMap;
+       final protected DoubleKeyQueryHashMap<IntProcedure>                     objectsMap;
+       final protected DoubleKeyQueryHashMap<TripleIntProcedure>               assertedStatementsMap;
+       final protected DoubleKeyQueryHashMap<IntProcedure>                     directObjectsMap;
+       final protected DoubleKeyQueryHashMap<TripleIntProcedure>               statementsMap;
+       final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         typesMap;
+       final protected UnaryQueryHashMap<IntProcedure>                      principalTypesMap;
+       final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         predicatesMap;
+       final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         superTypesMap;
+       final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         typeHierarchyMap;
+       final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         superRelationsMap;
+       
+       final protected UnaryQueryHashMap<IntProcedure>                      orderedSetMap;
+       final protected UnaryQueryHashMap<IntProcedure>                      assertedPredicatesMap;
+       final protected UnaryQueryHashMap<InternalProcedure<IntSet>>         directPredicatesMap;
+       final protected UnaryQueryHashMap<IntProcedure>                      directSuperRelationsMap;
+       
+       final protected UnaryQueryHashMap<InternalProcedure<RelationInfo>>   relationInfoQueryMap;
+       final protected UnaryQueryHashMap<InternalProcedure<byte[]>>         valueQueryMap;
+       
+       final protected StableHashMap<AsyncRead, AsyncReadEntry>                  asyncReadEntryMap; 
+       final protected StableHashMap<Read, ReadEntry>                            readEntryMap;
+       final protected StableHashMap<MultiRead, MultiReadEntry>                  multiReadEntryMap; 
+       final protected StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>        asyncMultiReadEntryMap; 
+       final protected StableHashMap<ExternalRead, ExternalReadEntry>            externalReadEntryMap; 
+
+       final THashMap<CacheEntry, ArrayList<ListenerEntry>>       listeners;
+
+       final public QuerySupport                               querySupport;
+
+       public QueryCacheBase(QuerySupport querySupport, int threads) {
+               
+               THREADS = threads;
+               THREAD_MASK = threads - 1;
+
+               this.querySupport = querySupport;
+               directPredicatesMap = new UnaryQueryHashMap();
+               directSuperRelationsMap = new UnaryQueryHashMap();
+               valueQueryMap = new UnaryQueryHashMap();
+               principalTypesMap = new UnaryQueryHashMap();
+               uRIToResourceMap = new THashMap<String, URIToResource>();
+               namespaceIndexMap = new THashMap<String, NamespaceIndex>();
+               relationInfoQueryMap = 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 DoubleKeyQueryHashMap();
+               asyncReadEntryMap = new StableHashMap<AsyncRead, AsyncReadEntry>(); 
+               readEntryMap = new StableHashMap<Read, ReadEntry>();
+               asyncMultiReadEntryMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>(); 
+               multiReadEntryMap = new StableHashMap<MultiRead, MultiReadEntry>(); 
+               externalReadEntryMap = new StableHashMap<ExternalRead, ExternalReadEntry>(); 
+               listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
+       }
+       
+       public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
+
+               AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
+               AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
+
+               ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
+
+               try {
+                       
+                       query.perform(queryGraph, new AsyncProcedure<T>() {
+
+                               @Override
+                               public void execute(AsyncReadGraph returnGraph, T result) {
+                                       ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+                                       entry.addOrSet(parentGraph, result);
+                                       try {
+                                               procedure.execute(parentGraph, result);
+                                       } catch (Throwable t) {
+                                               t.printStackTrace();
+                                       }
+//                                     parentBarrier.dec(query);
+                               }
+
+                               @Override
+                               public void exception(AsyncReadGraph returnGraph, Throwable t) {
+                                       ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+//                                     AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
+                                       entry.except(parentGraph, t);
+                                       try {
+                                               procedure.exception(parentGraph, t);
+                                       } catch (Throwable t2) {
+                                               t2.printStackTrace();
+                                       }
+//                                     parentBarrier.dec(query);
+                               }
+
+                               @Override
+                               public String toString() {
+                                       return procedure.toString();
+                               }
+
+                       });
+
+               } catch (Throwable t) {
+
+                       entry.except(t);
+                       try {
+                               procedure.exception(parentGraph, t);
+                       } catch (Throwable t2) {
+                               t2.printStackTrace();
+                       }
+//                     parentBarrier.dec(query);
+
+               }
+               
+               return null;
+               
+       }
+
+//     public <T> Object performQuery(ReadGraphImpl parentGraph, final Read<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
+//
+//             ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
+//
+//             ReadEntry entry = (ReadEntry)entry_;
+//
+//             try {
+//
+//                     T result = (T)query.perform(queryGraph);
+//                     entry.addOrSet(queryGraph, result);
+//
+//                     return (T)entry.get(parentGraph, procedure_);
+//
+//             }  catch (Throwable t) {
+//
+//                     entry.except(t);
+//                     return (T)entry.get(parentGraph, procedure_);
+//
+//             }
+//             
+//     }
+
+       public <T> Object performQuery(ReadGraphImpl parentGraph, final ExternalRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
+
+               ExternalReadEntry entry = (ExternalReadEntry)entry_;
+               AsyncProcedure<T> procedure = (AsyncProcedure<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(parentGraph, 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(parentGraph, 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(parentGraph, t);
+                       return entry.getResult();
+
+               }
+               
+       }
+
+       public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
+
+               ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
+
+               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();
+                       
+               }
+               
+       }
+
+       public <T> Object performQuery(ReadGraphImpl parentGraph, final MultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
+
+               ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
+
+               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();
+                       
+               }
+               
+       }
+       
+       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);
+
+       }
+
+       protected void primeListenerEntry(final ListenerEntry entry, final Object result) {
+               entry.setLastKnown(result);
+       }
+
+       private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) {
+
+               assert (entry != null);
+               assert (procedure != null);
+
+               ArrayList<ListenerEntry> list = listeners.get(entry);
+               if (list == null) {
+                       list = new ArrayList<ListenerEntry>(1);
+                       listeners.put(entry, list);
+               }
+
+               ListenerEntry result = new ListenerEntry(entry, base, procedure);
+               int currentIndex = list.indexOf(result);
+               // There was already a listener
+               if(currentIndex > -1) {
+                       ListenerEntry current = list.get(currentIndex);
+                       if(!current.base.isDisposed()) return null;
+                       list.set(currentIndex, result);
+               } else {
+                       list.add(result);
+               }
+
+               if(DebugPolicy.LISTENER) {
+                       new Exception().printStackTrace();
+                       System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
+               }
+
+               return result;
+
+       }
+       
+       
+       public Collection<CacheEntry> getRootList() {
+
+               ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
+
+               for (Object e : valueQueryMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : directPredicatesMap.values()) {
+                       result.add((CacheEntry) e);
+               }
+               for (Object e : directSuperRelationsMap.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 : readEntryMap.values()) {
+                       if(e instanceof CacheEntry) {
+                               result.add((CacheEntry) e);
+                       } else {
+                               System.err.println("e=" + e);
+                       }
+               }
+               for (Object e : asyncReadEntryMap.values()) {
+                       if(e instanceof CacheEntry) {
+                               result.add((CacheEntry) e);
+                       } else {
+                               System.err.println("e=" + e);
+                       }
+               }
+               for (Object e : externalReadEntryMap.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 += directSuperRelationsMap.size();
+               realSize += principalTypesMap.size();
+               realSize += uRIToResourceMap.size();
+               realSize += namespaceIndexMap.size();
+               
+               realSize += relationInfoQueryMap.size();
+               realSize += superTypesMap.size();
+               realSize += typeHierarchyMap.size();
+               realSize += superRelationsMap.size();
+               realSize += typesMap.size();
+               
+               realSize += valueQueryMap.size();
+               realSize += directObjectsMap.size();
+               realSize += objectsMap.size();
+               realSize += orderedSetMap.size();
+               realSize += predicatesMap.size();
+               
+               realSize += statementsMap.size();
+               realSize += assertedPredicatesMap.size();
+               realSize += assertedStatementsMap.size();
+               realSize += externalReadEntryMap.size();
+               realSize += asyncReadEntryMap.size();
+               
+               realSize += readEntryMap.size();
+               realSize += asyncMultiReadEntryMap.size();
+               realSize += multiReadEntryMap.size();
+               
+               return realSize;
+               
+       }
+       
+       CacheCollectionResult allCaches(CacheCollectionResult result) {
+
+               int level = Integer.MAX_VALUE;
+               directPredicatesMap.values(level, result);
+               directSuperRelationsMap.values(level, result);
+               principalTypesMap.values(level, result);
+               for(CacheEntryBase e : uRIToResourceMap.values())
+                       if(e.getLevel() <= level)
+                               result.add(e);
+               for(CacheEntryBase e : namespaceIndexMap.values())
+                       if(e.getLevel() <= level)
+                               result.add(e);
+               
+               relationInfoQueryMap.values(level, result);
+               superTypesMap.values(level, result);
+               typeHierarchyMap.values(level, result);
+               superRelationsMap.values(level, result);
+               typesMap.values(level, result);
+
+               valueQueryMap.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);
+               externalReadEntryMap.values(level, result);
+               asyncReadEntryMap.values(level, result);
+               
+               readEntryMap.values(level, result);
+               asyncMultiReadEntryMap.values(level, result);
+               multiReadEntryMap.values(level, result);
+
+               return result;
+               
+       }
+       
+       public void scanPending() {
+
+               ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
+
+               entries.addAll(directPredicatesMap.values());
+               entries.addAll(directSuperRelationsMap.values());
+               entries.addAll(principalTypesMap.values());
+               entries.addAll(uRIToResourceMap.values());
+               entries.addAll(namespaceIndexMap.values());
+               entries.addAll(relationInfoQueryMap.values());
+               entries.addAll(superTypesMap.values());
+               entries.addAll(superRelationsMap.values());
+               entries.addAll(typesMap.values());
+               entries.addAll(valueQueryMap.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(externalReadEntryMap.values());
+               entries.addAll(asyncReadEntryMap.values());
+               entries.addAll(externalReadEntryMap.values());
+               entries.addAll(readEntryMap.values());
+               entries.addAll(asyncMultiReadEntryMap.values());
+               entries.addAll(multiReadEntryMap.values());
+               entries.addAll(readEntryMap.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);
+                       }
+               }
+       }
+       
+       public static void waitPending(CacheEntry entry) throws DatabaseException {
+               
+               int counter = 0;
+               while(entry.isPending()) {
+                       try {
+                               Thread.sleep(1);
+                               counter++;
+                               if(counter > 1000) {
+                                       CacheEntryBase base = ((CacheEntryBase)entry);
+//                                     if(base.created != null) {
+//                                             System.err.println("created:");
+//                                             base.created.printStackTrace();
+//                                     }
+//                                     if(base.performed != null) {
+//                                             System.err.println("performed:");
+//                                             base.performed.printStackTrace();
+//                                     }
+//                                     if(base.ready != null) {
+//                                             System.err.println("ready:");
+//                                             base.ready.printStackTrace();
+//                                     }
+                                       System.err.println("asd");
+                                       //base.getQuery().recompute(null, null, entry);
+                               }
+                       } catch (InterruptedException e) {
+                       }
+               }
+
+       }
+       
+       //////////////////////////////////////
+       
+       static public Collection<Objects> entriesObjects(QueryProcessor processor, int r1) {
+               synchronized(processor.cache.objectsMap) {
+                       return processor.cache.objectsMap.values(r1);
+               }
+       }
+       
+       static public Collection<Objects> entriesObjects(QueryProcessor processor) {
+               synchronized(processor.cache.objectsMap) {
+                       return processor.cache.objectsMap.values();
+               }
+       }
+       
+       static public Collection<CacheEntry> entriesDirectPredicates(QueryProcessor processor) {
+               synchronized(processor.cache.directPredicatesMap) {
+                       return processor.cache.directPredicatesMap.values();
+               }
+       }
+
+       final static Collection<DirectObjects> entriesDirectObjects(final QueryProcessor processor, final int r1) {
+               DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
+               return hash.values(r1);
+       }
+       
+       final static Collection<Statements> entriesStatements(final QueryProcessor processor, final int r1) {
+               return processor.cache.statementsMap.values(r1);
+       }
+
+       final static Types entryTypes(final QueryProcessor processor, final int r) {
+               return (Types)processor.cache.typesMap.get(r);
+       }
+
+       final static PrincipalTypes entryPrincipalTypes(final QueryProcessor processor, final int r) {
+               return (PrincipalTypes)processor.cache.principalTypesMap.get(r);
+       }
+
+       final static OrderedSet entryOrderedSet(final QueryProcessor processor, final int r) {
+               return (OrderedSet)processor.cache.orderedSetMap.get(r);
+       }
+       
+       final static ValueQuery entryValueQuery(final QueryProcessor processor, final int r) {
+               return (ValueQuery)processor.cache.valueQueryMap.get(r);
+       }
+
+       final static DirectPredicates entryDirectPredicates(final QueryProcessor processor, final int r) {
+               return (DirectPredicates)processor.cache.directPredicatesMap.get(r);
+       }
+
+       public final static ReadEntry entryRead(final QueryProcessor processor, final Read request) {
+               return (ReadEntry)processor.cache.readEntryMap.get(request);
+       }
+
+       public final static MultiReadEntry entryMultiRead(final QueryProcessor processor, final MultiRead request) {
+               return (MultiReadEntry)processor.cache.multiReadEntryMap.get(request);
+       }
+
+       public final static AsyncReadEntry entryAsyncRead(final QueryProcessor processor, final AsyncRead request) {
+               return (AsyncReadEntry)processor.cache.asyncReadEntryMap.get(request);
+       }
+
+       public final static AsyncMultiReadEntry entryAsyncMultiRead(final QueryProcessor processor, final AsyncMultiRead request) {
+               return (AsyncMultiReadEntry)processor.cache.asyncMultiReadEntryMap.get(request);
+       }
+
+       final protected static long keyR2(long r1, long r2) {
+        long result = (r1<<32) | (r2 & 0xffffffffL); 
+        return result;
+    }
+       
+       final protected static <T> T id(T o) {
+               return o;
+       }
+
+    final protected static int keyR(int r) {
+       return r;
+    }
+
+    final protected static String keyID(String id) {
+       return id;
+    }
+       
+    protected static InternalProcedure<IntSet> emptyIntSetProcedure = new InternalProcedure<IntSet>() {
+
+               @Override
+               public void execute(ReadGraphImpl graph, IntSet result) {
+               }
+
+               @Override
+               public void exception(ReadGraphImpl graph, Throwable throwable) {
+               }
+               
+       }; 
+
+    protected static InternalProcedure<byte[]> emptyBytesProcedure = new InternalProcedure<byte[]>() {
+
+               @Override
+               public void execute(ReadGraphImpl graph, byte[] bytes) {
+               }
+
+               @Override
+               public void exception(ReadGraphImpl graph, Throwable throwable) {
+               }
+               
+       }; 
+
+    protected static InternalProcedure<Integer> emptyIntegerProcedure = new InternalProcedure<Integer>() {
+
+               @Override
+               public void execute(ReadGraphImpl graph, Integer i) {
+               }
+
+               @Override
+               public void exception(ReadGraphImpl graph, Throwable throwable) {
+               }
+               
+       }; 
+
+
+    protected static InternalProcedure<TObjectIntHashMap<String>> emptyNamespaceProcedure = new InternalProcedure<TObjectIntHashMap<String>>() {
+
+               @Override
+               public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> i) {
+               }
+
+               @Override
+               public void exception(ReadGraphImpl graph, Throwable throwable) {
+               }
+               
+       }; 
+
+
+    protected static InternalProcedure<RelationInfo> emptyRelationInfoProcedure = new InternalProcedure<RelationInfo>() {
+
+               @Override
+               public void execute(ReadGraphImpl graph, RelationInfo i) {
+               }
+
+               @Override
+               public void exception(ReadGraphImpl graph, Throwable throwable) {
+               }
+               
+       }; 
+
+       
+       protected static IntProcedure emptyIntProcedure = new IntProcedure() {
+               
+               @Override
+               public void finished(ReadGraphImpl graph) {
+               }
+               
+               @Override
+               public void execute(ReadGraphImpl graph, int i) {
+               }
+               
+               @Override
+               public void exception(ReadGraphImpl graph, Throwable throwable) {
+               }
+       }; 
+       
+       protected static TripleIntProcedure emptyTripleIntProcedure = new TripleIntProcedure() {
+
+               @Override
+               public void execute(ReadGraphImpl graph, int s, int p, int o) {
+               }
+
+               @Override
+               public void finished(ReadGraphImpl graph) {
+               }
+
+               @Override
+               public void exception(ReadGraphImpl graph, Throwable throwable) {
+               }
+               
+       }; 
+
+    protected static AsyncProcedure<Object> emptyAsyncProcedure = new AsyncProcedure<Object>() {
+
+               @Override
+               public void execute(AsyncReadGraph graph, Object result) {
+               }
+
+               @Override
+               public void exception(AsyncReadGraph graph, Throwable throwable) {
+               }
+               
+       }; 
+       
+    protected static AsyncMultiProcedure<Object> emptyAsyncMultiProcedure = new AsyncMultiProcedure<Object>() {
+
+               @Override
+               public void execute(AsyncReadGraph graph, Object result) {
+               }
+
+               @Override
+               public void finished(AsyncReadGraph graph) {
+               }
+
+               @Override
+               public void exception(AsyncReadGraph graph, Throwable throwable) {
+               }
+
+               
+       }; 
+
+       protected static InternalProcedure<IntSet> emptyProcedureTypes = emptyIntSetProcedure;
+    protected static InternalProcedure<IntSet> emptyProcedureSuperTypes = emptyIntSetProcedure;
+    protected static InternalProcedure<IntSet> emptyProcedureTypeHierarchy = emptyIntSetProcedure;
+    protected static InternalProcedure<IntSet> emptyProcedureSuperRelations = emptyIntSetProcedure;
+    protected static InternalProcedure<IntSet> emptyProcedurePredicates = emptyIntSetProcedure;
+    protected static InternalProcedure<IntSet> emptyProcedureDirectPredicates = emptyIntSetProcedure;
+
+    protected static IntProcedure emptyProcedureObjects = emptyIntProcedure;
+    protected static IntProcedure emptyProcedureDirectObjects = emptyIntProcedure;
+    protected static IntProcedure emptyProcedurePrincipalTypes = emptyIntProcedure;
+    protected static IntProcedure emptyProcedureDirectSuperRelations = emptyIntProcedure;
+    protected static IntProcedure emptyProcedureAssertedPredicates = emptyIntProcedure;
+    protected static IntProcedure emptyProcedureOrderedSet = emptyIntProcedure;
+    
+    protected static TripleIntProcedure emptyProcedureStatements = emptyTripleIntProcedure;
+    protected static TripleIntProcedure emptyProcedureAssertedStatements = emptyTripleIntProcedure;
+
+    protected static InternalProcedure<byte[]> emptyProcedureValueQuery = emptyBytesProcedure;
+    
+    protected static InternalProcedure<Integer> emptyProcedureURIToResource = emptyIntegerProcedure;
+    protected static InternalProcedure<TObjectIntHashMap<String>> emptyProcedureNamespaceIndex = emptyNamespaceProcedure;
+    protected static InternalProcedure<RelationInfo> emptyProcedureRelationInfoQuery = emptyRelationInfoProcedure;
+
+    protected static AsyncProcedure emptyProcedureReadEntry = emptyAsyncProcedure;
+    protected static AsyncProcedure emptyProcedureAsyncReadEntry = emptyAsyncProcedure;
+    protected static AsyncMultiProcedure emptyProcedureMultiReadEntry = emptyAsyncMultiProcedure;
+    protected static AsyncMultiProcedure emptyProcedureAsyncMultiReadEntry = emptyAsyncMultiProcedure;
+    protected static AsyncProcedure emptyProcedureExternalReadEntry = emptyAsyncProcedure;
+       
+    static class AsyncProcedureWrapper<T> implements AsyncProcedure<T> {
+       
+       private AsyncProcedure<T> procedure;
+       private T result = null;
+       private Throwable throwable = null;
+       
+       AsyncProcedureWrapper(AsyncProcedure<T> procedure) {
+               this.procedure = procedure;
+       }
+
+               @Override
+               public void execute(AsyncReadGraph graph, T result) {
+                       if(procedure != null) procedure.execute(graph, result);
+                       this.result = result;
+               }
+
+               @Override
+               public void exception(AsyncReadGraph graph, Throwable throwable) {
+                       if(procedure != null) procedure.exception(graph, throwable);
+                       this.throwable = throwable;
+               }
+               
+               public T get() throws DatabaseException {
+                       if(throwable != null) {
+                               if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
+                               else throw new DatabaseException(throwable);
+                       } else {
+                               return result;
+                       }
+               }
+               
+    }
+
+    
+    static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
+       
+       private InternalProcedure<T> procedure;
+       private T result = null;
+       private Throwable throwable = null;
+       
+       InternalProcedureWrapper(InternalProcedure<T> procedure) {
+               this.procedure = procedure;
+       }
+
+               @Override
+               public void execute(ReadGraphImpl graph, T result) throws DatabaseException {
+                       if(procedure != null) procedure.execute(graph, result);
+                       this.result = result;
+               }
+
+               @Override
+               public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+                       if(procedure != null) procedure.exception(graph, throwable);
+                       this.throwable = throwable;
+               }
+               
+               public T get() throws DatabaseException {
+                       if(throwable != null) {
+                               if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
+                               else throw new DatabaseException(throwable);
+                       } else {
+                               return result;
+                       }
+               }
+               
+    }
+    
+    static class IntSetWrapper implements IntProcedure {
+       
+       private IntProcedure procedure;
+       private IntSet result = new IntSet();
+       private Throwable throwable = null;
+       
+       IntSetWrapper(IntProcedure procedure) {
+               this.procedure = procedure;
+       }
+
+               @Override
+               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                       if(procedure != null) procedure.execute(graph, i);
+                       result.add(i);
+               }
+
+               @Override
+               public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+                       if(procedure != null) procedure.exception(graph, throwable);
+                       this.throwable = throwable;
+               }
+               
+               @Override
+               public void finished(ReadGraphImpl graph) throws DatabaseException {
+                       if(procedure != null) procedure.finished(graph);
+               }
+               
+               public IntSet get() throws DatabaseException {
+                       if(throwable != null) {
+                               if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
+                               else throw new DatabaseException(throwable);
+                       } else {
+                               return result;
+                       }
+               }
+
+    }
+
+    static class TripleIntProcedureWrapper implements TripleIntProcedure {
+       
+       private TripleIntProcedure procedure;
+       private IntArray result = new IntArray();
+       private Throwable throwable = null;
+       
+       TripleIntProcedureWrapper(TripleIntProcedure procedure) {
+               this.procedure = procedure;
+       }
+
+               @Override
+               public void execute(ReadGraphImpl graph, int i1, int i2, int i3) throws DatabaseException {
+                       if(procedure != null) procedure.execute(graph, i1, i2, i3);
+                       result.add(i1);
+                       result.add(i2);
+                       result.add(i3);
+               }
+
+               @Override
+               public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+                       if(procedure != null) procedure.exception(graph, throwable);
+                       this.throwable = throwable;
+               }
+               
+               @Override
+               public void finished(ReadGraphImpl graph) throws DatabaseException {
+                       if(procedure != null) procedure.finished(graph);
+               }
+               
+               public IntArray get() throws DatabaseException {
+                       if(throwable != null) {
+                               if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
+                               else throw new DatabaseException(throwable);
+                       } else {
+                               return result;
+                       }
+               }
+
+    }
+
+    public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
+       AsyncProcedureWrapper<T> wrap = new AsyncProcedureWrapper<>(procedure);
+        QueryCache.runnerReadEntry(graph, r, parent, listener, wrap);
+        return wrap.get();
+    }
+
+    public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<byte[]> procedure) throws DatabaseException {
+       InternalProcedureWrapper<byte[]> wrap = new InternalProcedureWrapper<>(procedure);
+        QueryCache.runnerValueQuery(graph, r, parent, listener, wrap);
+        return wrap.get();
+    }
+
+    public static RelationInfo resultRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
+       InternalProcedureWrapper<RelationInfo> wrap = new InternalProcedureWrapper<>(procedure);
+        QueryCache.runnerRelationInfoQuery(graph, r, parent, listener, wrap);
+        return wrap.get();
+    }
+
+    public static IntSet resultSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+       InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
+        QueryCache.runnerSuperRelations(graph, r, parent, listener, wrap);
+        return wrap.get();
+    }
+
+    public static IntSet resultSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+       InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
+        QueryCache.runnerSuperTypes(graph, r, parent, listener, wrap);
+        return wrap.get();
+    }
+    
+    public static IntSet resultTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+       InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
+        QueryCache.runnerTypes(graph, r, parent, listener, wrap);
+        return wrap.get();
+    }
+
+    public static IntSet resultPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+       InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
+        QueryCache.runnerPredicates(graph, r, parent, listener, wrap);
+        return wrap.get();
+    }
+
+    public static IntSet resultDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
+       InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
+        QueryCache.runnerDirectPredicates(graph, r, parent, listener, wrap);
+        return wrap.get();
+    }
+
+    public static IntArray resultAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, TripleIntProcedure procedure) throws DatabaseException {
+       TripleIntProcedureWrapper wrap = new TripleIntProcedureWrapper(procedure);
+        QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, wrap);
+        return wrap.get();
+    }
+
+}
+
index 0c33321731053d96c7f8c5bcf6c47deac66ba809..84273b81d63cf6921be763840b2c0cae172ba934 100644 (file)
@@ -25,7 +25,7 @@ class QueryCollectorImpl2 implements QueryProcessor.QueryCollector {
                this.support = support;
        }
 
-       private boolean findCollectables(CacheEntry entry, Map<CacheEntry, Boolean> collectables, ArrayList<CacheEntry> result) {
+       private boolean findCollectables(CacheEntry<?> entry, Map<CacheEntry, Boolean> collectables, ArrayList<CacheEntry> result) {
 
                if (entry.isDiscarded()) {
                        if(DebugPolicy.COLLECT && DebugPolicy.VERBOSE)
@@ -129,7 +129,7 @@ class QueryCollectorImpl2 implements QueryProcessor.QueryCollector {
 
                // Prune discarded parents
                ArrayList<CacheEntry> removals = new ArrayList<CacheEntry>();
-               for (CacheEntry entry : support.allCaches().toCollection()) {
+               for (CacheEntry<?> entry : support.allCaches().toCollection()) {
                        for(CacheEntry p : entry.getParents(queryProcessor)) {
                                if(p.isDiscarded()) removals.add(p);
                        }
@@ -157,7 +157,7 @@ class QueryCollectorImpl2 implements QueryProcessor.QueryCollector {
                        if(DebugPolicy.COLLECT)
                                new DebugException("checking the need for collecting queries (current=" + current + " , lastKnownFixedSize=" + lastKnownFixedSize + " max free=" + 0 + ")").printStackTrace();
 
-                       QueryProcessor.collecting = true;
+                       queryProcessor.cache.collecting = true;
 
                        long start = System.nanoTime();
 
@@ -175,8 +175,7 @@ class QueryCollectorImpl2 implements QueryProcessor.QueryCollector {
                        t.printStackTrace();
                }
 
-               QueryProcessor.collecting = false;
-
+               queryProcessor.cache.collecting = false;
 
        }
 
index c9190193fe5587a26490be5350fc1fc553b57fef..6db4726ac4de7eb859327b873f17dfdc6cddfaed 100644 (file)
@@ -13,6 +13,7 @@ package org.simantics.db.impl.query;
 
 import gnu.trove.impl.hash.THash;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
 
@@ -69,12 +70,6 @@ abstract public class QueryIdentityHash extends THash {
             return null;
         }
 
-        @Override
-        public <T> T getResult() {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
         @Override
         public boolean hasParents() {
             // TODO Auto-generated method stub
@@ -117,11 +112,6 @@ abstract public class QueryIdentityHash extends THash {
             return false;
         }
 
-        @Override
-        public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-               return null;
-        }
-
         @Override
         public void refute() {
             // TODO Auto-generated method stub
@@ -225,6 +215,18 @@ abstract public class QueryIdentityHash extends THash {
                public Object getOriginalRequest() {
                        throw new UnsupportedOperationException();
                }
+
+               @Override
+               Object performFromCache(ReadGraphImpl graph, Object procedure) throws DatabaseException {
+                       // TODO Auto-generated method stub
+                       return null;
+               }
+
+               @Override
+               public Object getResult() {
+                       // TODO Auto-generated method stub
+                       return null;
+               }
         
     };
 
index cca9d681edd8f88893a65d819afee6b290cf5d0a..a2fff0515b1975f98484ca969005ff40b5c32113 100644 (file)
@@ -65,7 +65,6 @@ import org.simantics.db.procedure.AsyncMultiListener;
 import org.simantics.db.procedure.AsyncMultiProcedure;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.procedure.AsyncSetListener;
-import org.simantics.db.procedure.Listener;
 import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.procedure.MultiProcedure;
 import org.simantics.db.procedure.Procedure;
@@ -84,7 +83,6 @@ import org.simantics.utils.datastructures.Pair;
 import org.simantics.utils.datastructures.collections.CollectionUtils;
 import org.simantics.utils.datastructures.disposable.AbstractDisposable;
 
-import gnu.trove.map.hash.THashMap;
 import gnu.trove.procedure.TIntProcedure;
 import gnu.trove.procedure.TLongProcedure;
 import gnu.trove.procedure.TObjectProcedure;
@@ -94,23 +92,13 @@ import gnu.trove.set.hash.TIntHashSet;
 @SuppressWarnings({"rawtypes", "unchecked"})
 final public class QueryProcessor extends AbstractDisposable implements ReadGraphSupport {
 
-       final public QueryCache cache = new QueryCache();
-
        public static int                                       indent                = 0;
 
-       public int                                              size                  = 0;
-       
        
        // Garbage collection
        
        public int                                              boundQueries          = 0;
 
-       // Statistics
-       private int                                             hits                  = 0;
-
-       private int                                             misses                = 0;
-
-       private int                                             updates               = 0;
 
        final private int                                       functionalRelation;
 
@@ -150,10 +138,10 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
        private boolean                                         updating              = false;
 
-       static public boolean                                         collecting            = false;
 
        private boolean                                         firingListeners       = false;
 
+       final public QueryCache                                 cache;
        final public QuerySupport                               querySupport;
        final public Session                                    session;
        final public ResourceSupport                            resourceSupport;
@@ -233,8 +221,8 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
 
        final int THREADS;
-
        final public int  THREAD_MASK;
+       
        final public static ThreadGroup QueryThreadGroup = new ThreadGroup("Query Thread Group"); 
 
        public static abstract class SessionTask {
@@ -299,6 +287,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                THREAD_MASK = threads - 1;
 
                querySupport = core;
+               cache = new QueryCache(core, threads);
                session = querySupport.getSession();
                resourceSupport = querySupport.getSupport();
                querySupportLock = core.getLock();
@@ -522,48 +511,53 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
        public void forResource(ReadGraphImpl graph, final String id, CacheEntry parent, final InternalProcedure<Integer> procedure) {
 
-               URIToResource.queryEach(graph, id, parent, null, new InternalProcedure<Integer>() {
+               try {
+                       
+                       QueryCache.runnerURIToResource(graph, id, parent, null, new InternalProcedure<Integer>() {
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, Integer result) {
+                               @Override
+                               public void execute(ReadGraphImpl graph, Integer result) throws DatabaseException {
 
-                               if (result != null && result != 0) {
-                                       procedure.execute(graph, result);
-                                       return;
-                               }
+                                       if (result != null && result != 0) {
+                                               procedure.execute(graph, result);
+                                               return;
+                                       }
 
-                               // Fall back to using the fixed builtins.
-                               result = querySupport.getBuiltin(id);
-                               if (result != 0) {
-                                       procedure.execute(graph, result);
-                                       return;
-                               } 
+                                       // Fall back to using the fixed builtins.
+                                       result = querySupport.getBuiltin(id);
+                                       if (result != 0) {
+                                               procedure.execute(graph, result);
+                                               return;
+                                       
 
-                               try {
-                                       result = querySupport.getRandomAccessReference(id);
-                               } catch (ResourceNotFoundException e) {
-                                       procedure.exception(graph, e);
-                                       return;
-                               }
+                                       try {
+                                               result = querySupport.getRandomAccessReference(id);
+                                       } catch (ResourceNotFoundException e) {
+                                               procedure.exception(graph, e);
+                                               return;
+                                       }
 
-                               if (result != 0) {
-                                       procedure.execute(graph, result);
-                               } else {
-                                       procedure.exception(graph, new ResourceNotFoundException(id));
-                               }
+                                       if (result != 0) {
+                                               procedure.execute(graph, result);
+                                       } else {
+                                               procedure.exception(graph, new ResourceNotFoundException(id));
+                                       }
 
-                       }
+                               }
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               procedure.exception(graph, t);
-                       }
+                               @Override
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                       procedure.exception(graph, t);
+                               }
 
-               });
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
-       public void forBuiltin(ReadGraphImpl graph, final String id, CacheEntry parent, final InternalProcedure<Integer> procedure) {
+       public void forBuiltin(ReadGraphImpl graph, final String id, CacheEntry parent, final InternalProcedure<Integer> procedure) throws DatabaseException {
 
                Integer result = querySupport.getBuiltin(id);
                if (result != 0) {
@@ -574,932 +568,122 @@ 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) {
+       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) {
 
                try {
-                       cache.runQuery(graph, query, parent, listener, procedure);
+                       QueryCache.runnerMultiReadEntry(graph, query, parent, listener, procedure);
                } catch (DatabaseException e) {
                        throw new IllegalStateException(e);
                }
-               
-//             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;
-//                     }
-//             }
-//
-//             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);
-//                     }
-//
-//             }
 
        }
 
+       public final <T> void runAsyncMultiRead(final ReadGraphImpl graph, final AsyncMultiRead<T> query, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure) {
 
-       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) {
-
+               
                try {
-                       cache.runQuery(graph, query, parent, listener, procedure);
+                       QueryCache.runnerAsyncMultiReadEntry(graph, query, parent, listener, procedure);
                } catch (DatabaseException e) {
                        throw new IllegalStateException(e);
                }
 
-//             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()) {
-//
-//                             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);
-//
-//                     }
-//
-//             }
+       }
 
+       final <T> void runPrimitiveRead(ReadGraphImpl graph, ExternalReadEntry cached, final ExternalRead<T> query, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final AsyncProcedure<T> procedure) throws DatabaseException {
+               QueryCache.runnerExternalReadEntry(graph, query, parent, listener, procedure);
        }
 
-       public final <T> void runAsyncMultiRead(final ReadGraphImpl graph, final AsyncMultiRead<T> query, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure) {
+    @Override
+       public <T> T query(final ReadGraphImpl graph, final Read<T> query, final CacheEntry parent, final AsyncProcedure<T> procedure, final ListenerBase listener) throws DatabaseException {
+       
+       return QueryCache.resultReadEntry(graph, query, parent, listener, procedure);
 
-               
-               try {
-                       cache.runQuery(graph, query, parent, listener, procedure);
-               } catch (DatabaseException e) {
-                       throw new IllegalStateException(e);
-               }
+       }
 
+       public <T> void queryMultiRead(final ReadGraphImpl graph, final MultiRead<T> query, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure) throws DatabaseException {
 
-//             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);
-//
-//             }
+               QueryCache.runnerMultiReadEntry(graph, query, parent, listener, 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 {
+       public <T> void queryPrimitiveRead(final ReadGraphImpl graph, final ExternalRead<T> query, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure<T> procedure) throws DatabaseException {
 
-               cache.runQuery(graph, query, parent, listener, procedure);
+               QueryCache.runnerExternalReadEntry(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);
-//             }
+       }
 
+       boolean isBound(ExternalReadEntry<?> entry) {
+               if(entry.hasParents()) return true;
+               else if(hasListener(entry)) return true;
+               else return false;
        }
 
-       public int requestHash(Object object) {
-               try {
-                       return object.hashCode();
-               } catch (Throwable t) {
-                       Logger.defaultLogError(t);
-                       return 0;
+       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;
                }
+
        }
+
        
-    @Override
-       public <T> T queryRead(final ReadGraphImpl graph, final Read<T> query, final CacheEntry parent, final AsyncProcedure<T> procedure, final ListenerBase listener) throws Throwable {
+       static class Dummy implements InternalProcedure<Object>, IntProcedure {
 
-               return (T)cache.runQuery(graph, query, parent, listener, procedure);
+               @Override
+               public void execute(ReadGraphImpl graph, int i) {
+               }
 
-//     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);
-//                     }
-//
-//             }
+               @Override
+               public void finished(ReadGraphImpl graph) {
+               }
+
+               @Override
+               public void execute(ReadGraphImpl graph, Object result) {
+               }
 
+               @Override
+               public void exception(ReadGraphImpl graph, Throwable throwable) {
+               }
+               
        }
+       
+       private static final Dummy dummy = new Dummy();
 
-       public <T> void queryMultiRead(final ReadGraphImpl graph, final MultiRead<T> query, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure) throws DatabaseException {
+       /*
+    public <Procedure> Object performForEach2(ReadGraphImpl graph, UnaryQuery<Procedure> query, CacheEntry parent, ListenerBase listener, Procedure procedure) throws Throwable {
 
-               cache.runQuery(graph, query, parent, listener, procedure);
+        if (DebugPolicy.PERFORM)
+            System.out.println("PE[ " + (query.hashCode() &  THREAD_MASK) + "] " + query);
 
-//             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);
+        assert (!dirty);
+        assert (!collecting);
 
-       }
+        assert(query.assertNotDiscarded());
 
-       public <T> void queryPrimitiveRead(final ReadGraphImpl graph, final ExternalRead<T> query, final CacheEntry parent, final ListenerBase listener, final Procedure<T> procedure) throws DatabaseException {
+        registerDependencies(graph, query, parent, listener, procedure, false);
 
-               cache.runQuery(graph, query, parent, listener, procedure);
+        // FRESH, REFUTED, EXCEPTED go here 
+        if (!query.isReady()) {
 
-//             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);
+            size++;
+            misses++;
 
-       }
+            query.computeForEach(graph, this, (Procedure)dummy, true);
+            return query.get(graph, this, null);
 
-//     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());
-//
-//     }
-
-//     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);
-//
-//                     }
-//
-//             } 
-//
-//     }
-
-//     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 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());
-//
-//     }
-
-       boolean isBound(ExternalReadEntry<?> entry) {
-               if(entry.hasParents()) return true;
-               else if(hasListener(entry)) return true;
-               else return false;
-       }
-
-       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 <Procedure> void performForEach(ReadGraphImpl graph, BinaryQuery<Procedure> query, CacheEntry parent, ListenerBase listener, Procedure procedure) {
-
-               if (DebugPolicy.PERFORM)
-                       System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-
-               assert (!dirty);
-               assert (!collecting);
-
-               try {
-
-                       registerDependencies(graph, query, parent, listener, procedure, false);
-
-                       // FRESH, REFUTED, EXCEPTED go here 
-                       if (!query.isReady()) {
-
-                               boolean fresh = query.isFresh();
-
-                               if(fresh) {
-                                       size++;
-                               }
-
-                               query.computeForEach(graph, this, procedure, true);
-
-                               misses++;
-
-                       } else {
-
-                               query.performFromCache(graph, this, procedure);
-
-                               hits++;
-
-                       }
-
-               } catch (Throwable t) {
-
-                       Logger.defaultLogError(t);
-
-               }
-       }
-
-       public <Procedure> Object performForEach(ReadGraphImpl graph, UnaryQuery<Procedure> query, CacheEntry parent, ListenerBase listener, Procedure procedure) {
-
-               if (DebugPolicy.PERFORM)
-                       System.out.println("PE[ " + (query.hashCode() &  THREAD_MASK) + "] " + query);
-
-               assert (!dirty);
-               assert (!collecting);
-
-               try {
-
-                       assert(query.assertNotDiscarded());
-
-                       registerDependencies(graph, query, parent, listener, procedure, false);
-
-                       // FRESH, REFUTED, EXCEPTED go here 
-                       if (!query.isReady()) {
-
-                               size++;
-                               misses++;
-
-                               return query.computeForEach(graph, this, procedure, true);
-
-
-                       } else {
-
-                               hits++;
-
-                               return query.performFromCache(graph, this, procedure);
-
-                       }
-
-               } catch (Throwable t) {
-
-                       Logger.defaultLogError(t);
-                       return null;
-
-               } 
-
-       }
-       
-       static class Dummy implements InternalProcedure<Object>, IntProcedure {
-
-               @Override
-               public void execute(ReadGraphImpl graph, int i) {
-               }
-
-               @Override
-               public void finished(ReadGraphImpl graph) {
-               }
-
-               @Override
-               public void execute(ReadGraphImpl graph, Object result) {
-               }
-
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
-               
-       }
-       
-       private static final Dummy dummy = new Dummy();
-
-    public <Procedure> Object performForEach2(ReadGraphImpl graph, UnaryQuery<Procedure> query, CacheEntry parent, ListenerBase listener, Procedure procedure) throws Throwable {
-
-        if (DebugPolicy.PERFORM)
-            System.out.println("PE[ " + (query.hashCode() &  THREAD_MASK) + "] " + query);
-
-        assert (!dirty);
-        assert (!collecting);
-
-        assert(query.assertNotDiscarded());
-
-        registerDependencies(graph, query, parent, listener, procedure, false);
-
-        // FRESH, REFUTED, EXCEPTED go here 
-        if (!query.isReady()) {
-
-            size++;
-            misses++;
-
-            query.computeForEach(graph, this, (Procedure)dummy, true);
-            return query.get(graph, this, null);
-
-        } else {
+        } else {
 
             hits++;
 
@@ -1508,50 +692,8 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
         }
 
     }
+       */
        
-       public <Procedure> void performForEach(ReadGraphImpl graph, StringQuery<Procedure> query, CacheEntry parent, final ListenerBase listener, Procedure procedure) {
-
-               if (DebugPolicy.PERFORM)
-                       System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-
-               assert (!dirty);
-               assert (!collecting);
-
-               try {
-
-                       if(query.isDiscarded()) {
-                               System.err.println("aff");
-                       }
-                       assert(!query.isDiscarded());
-
-                       // FRESH, REFUTED, EXCEPTED go here 
-                       if (!query.isReady()) {
-
-                               query.computeForEach(graph.withAsyncParent(query), this, procedure);
-
-                               size++;
-                               misses++;
-
-                       } else {
-
-                               query.performFromCache(graph, this, procedure);
-
-                               hits++;
-
-                       }
-
-                       assert (!query.isDiscarded());
-
-                       registerDependencies(graph, query, parent, listener, procedure, false);
-
-               } catch (Throwable t) {
-
-                       t.printStackTrace();
-                       Logger.defaultLogError(t);
-
-               }
-
-       }
 
        interface QueryCollectorSupport {
                public CacheCollectionResult allCaches();
@@ -1679,7 +821,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                @Override
                public int getCurrentSize() {
-                       return size;
+                       return cache.size;
                }
 
        }
@@ -1689,7 +831,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
        private QueryCollector collector = new QueryCollectorImpl(this, collectorSupport);
 
     public int querySize() {
-        return size;
+        return cache.size;
     }
 
        public void gc(int youngTarget, int allowedTimeInMs) {
@@ -1797,7 +939,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        return Collections.emptyList();
        }
 
-       void processListenerReport(CacheEntry entry, Map<CacheEntry, Set<ListenerBase>> workarea) {
+       void processListenerReport(CacheEntry<?> entry, Map<CacheEntry, Set<ListenerBase>> workarea) {
 
                if(!workarea.containsKey(entry)) {
 
@@ -1968,7 +1110,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        int unboundCounter = 0;
                        int unknownCounter = 0;
 
-                       for(CacheEntry entry : workarea.keySet()) {
+                       for(CacheEntry<?> entry : workarea.keySet()) {
 
                                //System.err.println("process " + entry);
 
@@ -2176,8 +1318,8 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                query.removeEntry(this);
 
-               updates++;
-               size--;
+               cache.updates++;
+               cache.size--;
 
                if((entry.getGCStatus() & CacheEntry.HAS_BEEN_BOUND) != 0)
                        boundQueries--;
@@ -2245,7 +1387,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        }
                }
 
-               updates++;
+               cache.updates++;
 
                if (Development.DEVELOPMENT) {
                        if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
@@ -2394,7 +1536,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        
                        ReadGraphImpl parentGraph = graph.withParent(entry);
 
-                       query.recompute(parentGraph, this, entry);
+                       query.recompute(parentGraph);
 
                        if(entry.isExcepted()) return ListenerEntry.NO_VALUE;
 
@@ -2445,7 +1587,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
        public void performScheduledUpdates(WriteGraphImpl graph) {
 
                assert (!updating);
-               assert (!collecting);
+               assert (!cache.collecting);
                assert (!firingListeners);
 
                firingListeners = true;
@@ -2493,7 +1635,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                                        try {
                                                if(DebugPolicy.LISTENER)
                                                        System.out.println("Firing " + listenerEntry.procedure + " for " + listenerEntry.entry);
-                                               entry.performFromCache(graph, this, listenerEntry.procedure);
+                                               entry.performFromCache(graph, listenerEntry.procedure);
                                        } catch (Throwable t) {
                                                t.printStackTrace();
                                        }
@@ -2517,7 +1659,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
         */
        public boolean update(final ReadGraphImpl graph, final CacheEntry entry) {
 
-               assert (!collecting);
+               assert (!cache.collecting);
                assert (!updating);
                updating = true;
 
@@ -2585,8 +1727,6 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
        }
 
-       volatile public boolean dirty = false;
-
        private ObjectUpdateSet scheduledObjectUpdates = new ObjectUpdateSet();
        private ValueUpdateSet scheduledValueUpdates = new ValueUpdateSet();
        private ValueUpdateSet scheduledInvalidates = new ValueUpdateSet();
@@ -2596,7 +1736,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
        public void performDirtyUpdates(final ReadGraphImpl graph) {
 
-               dirty = false;
+               cache.dirty = false;
                lastInvalidate = 0;
 
                if (Development.DEVELOPMENT) {
@@ -2613,14 +1753,14 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        final int subject = (int)(arg0 >>> 32);
                        final int predicate = (int)(arg0 & 0xffffffff);
 
-                       for(Objects o : Objects.entries(QueryProcessor.this, subject)) update(graph, o);
-                       for(DirectObjects o : DirectObjects.entries(QueryProcessor.this, subject)) update(graph, o);
-                       for(Statements o : Statements.entries(QueryProcessor.this, subject)) update(graph, o);
+                       for(Objects o : QueryCache.entriesObjects(QueryProcessor.this, subject)) update(graph, o);
+                       for(DirectObjects o : QueryCache.entriesDirectObjects(QueryProcessor.this, subject)) update(graph, o);
+                       for(Statements o : QueryCache.entriesStatements(QueryProcessor.this, subject)) update(graph, o);
 
                        if(predicate == instanceOf || predicate == inherits || predicate == subrelationOf) {
-                               PrincipalTypes principalTypes = PrincipalTypes.entry(QueryProcessor.this, subject);
+                               PrincipalTypes principalTypes = QueryCache.entryPrincipalTypes(QueryProcessor.this, subject);
                                if(principalTypes != null) update(graph, principalTypes);
-                               Types types = Types.entry(QueryProcessor.this, subject);
+                               Types types = QueryCache.entryTypes(QueryProcessor.this, subject);
                                if(types != null) update(graph, types);
                        }
 
@@ -2629,9 +1769,9 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                                if(superRelations != null) update(graph, superRelations);
                        }
 
-                       DirectPredicates dp = DirectPredicates.entry(QueryProcessor.this, subject);
+                       DirectPredicates dp = QueryCache.entryDirectPredicates(QueryProcessor.this, subject);
                        if(dp != null) update(graph, dp);
-                       OrderedSet os = OrderedSet.entry(QueryProcessor.this, predicate);
+                       OrderedSet os = QueryCache.entryOrderedSet(QueryProcessor.this, predicate);
                        if(os != null) update(graph, os);
 
                        scheduledObjectUpdates.clear();
@@ -2644,7 +1784,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                        int arg0 = scheduledValueUpdates.getFirst();
 
-                       ValueQuery valueQuery = ValueQuery.entry(QueryProcessor.this, arg0);
+                       ValueQuery valueQuery = QueryCache.entryValueQuery(QueryProcessor.this, arg0);
                        if(valueQuery != null) update(graph, valueQuery);
 
                        scheduledValueUpdates.clear();
@@ -2666,11 +1806,11 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        @Override
                        public boolean execute(Object arg0) {
 
-                               ExternalReadEntry query = (ExternalReadEntry)cache.externalReadMap.get(arg0);
+                               ExternalReadEntry query = (ExternalReadEntry)cache.externalReadEntryMap.get(arg0);
                                if (query != null) {
                                        boolean listening = update(graph, query);
                                        if (!listening && !query.hasParents()) {
-                                               cache.externalReadMap.remove(arg0);
+                                               cache.externalReadEntryMap.remove(arg0);
                                                query.discard();
                                        }
                                }
@@ -2683,7 +1823,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                        @Override
                        public boolean execute(int arg0) {
-                               ValueQuery valueQuery = ValueQuery.entry(QueryProcessor.this, arg0);
+                               ValueQuery valueQuery = QueryCache.entryValueQuery(QueryProcessor.this, arg0);
                                if(valueQuery != null) update(graph, valueQuery);
                                return true;
                        }
@@ -2695,12 +1835,12 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        @Override
                        public boolean execute(int resource) {
                                
-                               ValueQuery valueQuery = ValueQuery.entry(QueryProcessor.this, resource);
+                               ValueQuery valueQuery = QueryCache.entryValueQuery(QueryProcessor.this, resource);
                                if(valueQuery != null) update(graph, valueQuery);
                                
-                               PrincipalTypes principalTypes = PrincipalTypes.entry(QueryProcessor.this, resource);
+                               PrincipalTypes principalTypes = QueryCache.entryPrincipalTypes(QueryProcessor.this, resource);
                                if(principalTypes != null) update(graph, principalTypes);
-                               Types types = Types.entry(QueryProcessor.this, resource);
+                               Types types = QueryCache.entryTypes(QueryProcessor.this, resource);
                                if(types != null) update(graph, types);
 
                                SuperRelations superRelations = SuperRelations.entry(QueryProcessor.this, resource);
@@ -2722,9 +1862,9 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                                final int predicate = (int)(arg0 & 0xffffffff);
 
                                if(predicate == instanceOf || predicate == inherits || predicate == subrelationOf) {
-                                       PrincipalTypes principalTypes = PrincipalTypes.entry(QueryProcessor.this, subject);
+                                       PrincipalTypes principalTypes = QueryCache.entryPrincipalTypes(QueryProcessor.this, subject);
                                        if(principalTypes != null) update(graph, principalTypes);
-                                       Types types = Types.entry(QueryProcessor.this, subject);
+                                       Types types = QueryCache.entryTypes(QueryProcessor.this, subject);
                                        if(types != null) update(graph, types);
                                }
 
@@ -2747,11 +1887,11 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        @Override
                        public boolean execute(final int subject) {
 
-                               for(Objects o : Objects.entries(QueryProcessor.this, subject)) update(graph, o);
-                               for(DirectObjects o : DirectObjects.entries(QueryProcessor.this, subject)) update(graph, o);
-                               for(Statements o : Statements.entries(QueryProcessor.this, subject)) update(graph, o);
+                               for(Objects o : QueryCache.entriesObjects(QueryProcessor.this, subject)) update(graph, o);
+                               for(DirectObjects o : QueryCache.entriesDirectObjects(QueryProcessor.this, subject)) update(graph, o);
+                               for(Statements o : QueryCache.entriesStatements(QueryProcessor.this, subject)) update(graph, o);
 
-                               DirectPredicates entry = DirectPredicates.entry(QueryProcessor.this, subject);
+                               DirectPredicates entry = QueryCache.entryDirectPredicates(QueryProcessor.this, subject);
                                if(entry != null) update(graph, entry);
 
                                return true;
@@ -2765,7 +1905,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        @Override
                        public boolean execute(int orderedSet) {
 
-                               OrderedSet entry = OrderedSet.entry(QueryProcessor.this, orderedSet);
+                               OrderedSet entry = QueryCache.entryOrderedSet(QueryProcessor.this, orderedSet);
                                if(entry != null) update(graph, entry);
 
                                return true;
@@ -2794,12 +1934,12 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
        public void updateValue(final int resource) {
                scheduledValueUpdates.add(resource);
-               dirty = true;
+               cache.dirty = true;
        }
 
        public void updateStatements(final int resource, final int predicate) {
                scheduledObjectUpdates.add((((long)resource) << 32) + predicate);
-               dirty = true;
+               cache.dirty = true;
        }
        
        private int lastInvalidate = 0;
@@ -2808,7 +1948,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                if(lastInvalidate == resource) return;
                scheduledValueUpdates.add(resource);
                lastInvalidate = resource;
-               dirty = true;
+               cache.dirty = true;
        }
 
        public void updatePrimitive(final ExternalRead primitive) {
@@ -2824,7 +1964,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
        @Override
        public synchronized String toString() {
-               return "QueryProvider [size = " + size + ", hits = " + hits + " misses = " + misses + ", updates = " + updates + "]";
+               return "QueryProvider [size = " + cache.size + ", hits = " + cache.hits + " misses = " + cache.misses + ", updates = " + cache.updates + "]";
        }
 
        @Override
@@ -2880,28 +2020,28 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
        }
 
        public int getHits() {
-               return hits;
+               return cache.hits;
        }
 
        public int getMisses() {
-               return misses;
+               return cache.misses;
        }
 
        public int getSize() {
-               return size;
+               return cache.size;
        }
 
        public Set<Long> getReferencedClusters() {
                HashSet<Long> result = new HashSet<Long>();
-               for (CacheEntry entry : cache.objectsMap.values()) {
+               for (CacheEntry entry : QueryCache.entriesObjects(this)) {
                        Objects query = (Objects) entry.getQuery();
                        result.add(querySupport.getClusterId(query.r1()));
                }
-               for (CacheEntry entry : cache.directPredicatesMap.values()) {
+               for (CacheEntry entry : QueryCache.entriesDirectPredicates(this)) {
                        DirectPredicates query = (DirectPredicates) entry.getQuery();
                        result.add(querySupport.getClusterId(query.id));
                }
-               for (CacheEntry entry : cache.valueMap.values()) {
+               for (CacheEntry entry : cache.valueQueryMap.values()) {
                        ValueQuery query = (ValueQuery) entry.getQuery();
                        result.add(querySupport.getClusterId(query.id));
                }
@@ -2926,7 +2066,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
        public int clean() {
                collector.collect(0, Integer.MAX_VALUE);
-               return size;
+               return cache.size;
        }
 
        public void clean(final Collection<ExternalRead<?>> requests) {
@@ -2940,7 +2080,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        public CacheEntryBase iterate(int level) {
                                if(iterator.hasNext()) {
                                        ExternalRead<?> request = iterator.next();
-                                       ExternalReadEntry entry = cache.externalReadMap.get(request);
+                                       ExternalReadEntry entry = cache.externalReadEntryMap.get(request);
                                        if (entry != null) return entry;
                                        else return iterate(level);
                                } else {
@@ -2960,7 +2100,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 = cache.externalReadMap.get(request);
+                                       ExternalReadEntry entry = cache.externalReadEntryMap.get(request);
                                        if (entry != null)
                                                result.add(entry);
                                }
@@ -2968,7 +2108,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        }
                        @Override
                        public int getCurrentSize() {
-                               return size;
+                               return cache.size;
                        }
                        @Override
                        public int calculateCurrentSize() {
@@ -3026,162 +2166,166 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                builtinValues.put(b.Double, Double.class);
                builtinValues.put(b.Float, Float.class);
                builtinValues.put(b.Long, Long.class);
-               builtinValues.put(b.Integer, Integer.class);
-               builtinValues.put(b.Byte, Byte.class);
-               builtinValues.put(b.Boolean, Boolean.class);
-
-               builtinValues.put(b.StringArray, String[].class);
-               builtinValues.put(b.DoubleArray, double[].class);
-               builtinValues.put(b.FloatArray, float[].class);
-               builtinValues.put(b.LongArray, long[].class);
-               builtinValues.put(b.IntegerArray, int[].class);
-               builtinValues.put(b.ByteArray, byte[].class);
-               builtinValues.put(b.BooleanArray, boolean[].class);
-
-       }
-
-//     public ReadGraphSupportImpl(final QueryProcessor provider2) {
-//
-//             if (null == provider2) {
-//                     this.processor = null;
-//                     support = null;
-//                     return;
-//             }
-//             this.processor = provider2;
-//             support = provider2.getCore();
-//             initBuiltinValues();
-//
-//     }
-
-//     final static public ReadGraphSupportImpl basedOn(ReadGraphSupportImpl impl) {
-//             return new ReadGraphSupportImpl(impl.processor);
-//     }
-
-       @Override
-       final public Session getSession() {
-               return session;
-       }
-       
-       final public ResourceSupport getResourceSupport() {
-               return resourceSupport;
-       }
-
-       @Override
-       final public void forEachPredicate(final ReadGraphImpl impl, final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
-
-               assert(subject != null);
-               assert(procedure != null);
-
-               final ListenerBase listener = getListenerBase(procedure);
-
-               IntProcedure ip = new IntProcedure() {
-
-                       AtomicBoolean first = new AtomicBoolean(true);
-
-                       @Override
-                       public void execute(ReadGraphImpl graph, int i) {
-                               try {
-                                       if(first.get()) {
-                                               procedure.execute(graph, querySupport.getResource(i));
-                                       } else {
-                                               procedure.execute(impl.newRestart(graph), querySupport.getResource(i));
-                                       }
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
-                       }
-
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                               try {
-                                       if(first.compareAndSet(true, false)) {
-                                               procedure.finished(graph);
-//                                             impl.state.barrier.dec(this);
-                                       } else {
-                                               procedure.finished(impl.newRestart(graph));
-                                       }
-
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       if(first.compareAndSet(true, false)) {
-                                               procedure.exception(graph, t);
-//                                             impl.state.barrier.dec(this);
-                                       } else {
-                                               procedure.exception(impl.newRestart(graph), t);
-                                       }
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
-                       }
+               builtinValues.put(b.Integer, Integer.class);
+               builtinValues.put(b.Byte, Byte.class);
+               builtinValues.put(b.Boolean, Boolean.class);
 
-               };
+               builtinValues.put(b.StringArray, String[].class);
+               builtinValues.put(b.DoubleArray, double[].class);
+               builtinValues.put(b.FloatArray, float[].class);
+               builtinValues.put(b.LongArray, long[].class);
+               builtinValues.put(b.IntegerArray, int[].class);
+               builtinValues.put(b.ByteArray, byte[].class);
+               builtinValues.put(b.BooleanArray, boolean[].class);
 
-               int sId = querySupport.getId(subject);
+       }
 
-//             if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#Predicates#" + sId);
-//             else impl.state.barrier.inc(null, null);
+//     public ReadGraphSupportImpl(final QueryProcessor provider2) {
+//
+//             if (null == provider2) {
+//                     this.processor = null;
+//                     support = null;
+//                     return;
+//             }
+//             this.processor = provider2;
+//             support = provider2.getCore();
+//             initBuiltinValues();
+//
+//     }
 
-               Predicates.queryEach(impl, sId, this, impl.parent, listener, ip);
+//     final static public ReadGraphSupportImpl basedOn(ReadGraphSupportImpl impl) {
+//             return new ReadGraphSupportImpl(impl.processor);
+//     }
 
+       @Override
+       final public Session getSession() {
+               return session;
+       }
+       
+       final public ResourceSupport getResourceSupport() {
+               return resourceSupport;
        }
 
        @Override
-       final public void forEachPredicate(final ReadGraphImpl impl, final Resource subject, final MultiProcedure<Resource> procedure) {
-
-               assert(subject != null);
-               assert(procedure != null);
-
-               final ListenerBase listener = getListenerBase(procedure);
-
-//             impl.state.barrier.inc();
+       final public void forEachPredicate(final ReadGraphImpl impl, final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
 
-               Predicates.queryEach(impl, querySupport.getId(subject), this, impl.parent, listener, new IntProcedure() {
+               throw new UnsupportedOperationException();
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, int i) {
-                               try {
-                                       procedure.execute(querySupport.getResource(i));
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
-                       }
+//             assert(subject != null);
+//             assert(procedure != null);
+//
+//             final ListenerBase listener = getListenerBase(procedure);
+//
+//             IntProcedure ip = new IntProcedure() {
+//
+//                     AtomicBoolean first = new AtomicBoolean(true);
+//
+//                     @Override
+//                     public void execute(ReadGraphImpl graph, int i) {
+//                             try {
+//                                     if(first.get()) {
+//                                             procedure.execute(graph, querySupport.getResource(i));
+//                                     } else {
+//                                             procedure.execute(impl.newRestart(graph), querySupport.getResource(i));
+//                                     }
+//                             } catch (Throwable t2) {
+//                                     Logger.defaultLogError(t2);
+//                             }
+//                     }
+//
+//                     @Override
+//                     public void finished(ReadGraphImpl graph) {
+//                             try {
+//                                     if(first.compareAndSet(true, false)) {
+//                                             procedure.finished(graph);
+////                                           impl.state.barrier.dec(this);
+//                                     } else {
+//                                             procedure.finished(impl.newRestart(graph));
+//                                     }
+//
+//                             } catch (Throwable t2) {
+//                                     Logger.defaultLogError(t2);
+//                             }
+//                     }
+//
+//                     @Override
+//                     public void exception(ReadGraphImpl graph, Throwable t) {
+//                             try {
+//                                     if(first.compareAndSet(true, false)) {
+//                                             procedure.exception(graph, t);
+//                                     } else {
+//                                             procedure.exception(impl.newRestart(graph), t);
+//                                     }
+//                             } catch (Throwable t2) {
+//                                     Logger.defaultLogError(t2);
+//                             }
+//                     }
+//
+//             };
+//
+//             int sId = querySupport.getId(subject);
+//
+//             try {
+//                     QueryCache.runnerPredicates(impl, sId, impl.parent, listener, ip);
+//             } catch (DatabaseException e) {
+//                     Logger.defaultLogError(e);
+//             }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                               try {
-                                       procedure.finished();
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
-//                             impl.state.barrier.dec();
-                       }
+       }
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       procedure.exception(t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
-//                             impl.state.barrier.dec();
-                       }
+       @Override
+       final public void forEachPredicate(final ReadGraphImpl impl, final Resource subject, final MultiProcedure<Resource> procedure) {
+               
+               throw new UnsupportedOperationException();
 
-               });
+//             assert(subject != null);
+//             assert(procedure != null);
+//
+//             final ListenerBase listener = getListenerBase(procedure);
+//
+//             try {
+//                     QueryCache.runnerPredicates(impl, querySupport.getId(subject), impl.parent, listener, new IntProcedure() {
+//
+//                             @Override
+//                             public void execute(ReadGraphImpl graph, int i) {
+//                                     try {
+//                                             procedure.execute(querySupport.getResource(i));
+//                                     } catch (Throwable t2) {
+//                                             Logger.defaultLogError(t2);
+//                                     }
+//                             }
+//
+//                             @Override
+//                             public void finished(ReadGraphImpl graph) {
+//                                     try {
+//                                             procedure.finished();
+//                                     } catch (Throwable t2) {
+//                                             Logger.defaultLogError(t2);
+//                                     }
+////                           impl.state.barrier.dec();
+//                             }
+//
+//                             @Override
+//                             public void exception(ReadGraphImpl graph, Throwable t) {
+//                                     try {
+//                                             procedure.exception(t);
+//                                     } catch (Throwable t2) {
+//                                             Logger.defaultLogError(t2);
+//                                     }
+////                           impl.state.barrier.dec();
+//                             }
+//
+//                     });
+//             } catch (DatabaseException e) {
+//                     Logger.defaultLogError(e);
+//             }
 
        }
        
        @Override
        final public IntSet getPredicates(final ReadGraphImpl impl, final Resource subject) throws Throwable {
-
-               assert(subject != null);
                
-               return Predicates.queryEach2(impl, querySupport.getId(subject), this, impl.parent);
+               return QueryCacheBase.resultPredicates(impl, querySupport.getId(subject), impl.parent, null, null); 
 
        }
        
@@ -3198,38 +2342,42 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
 //             impl.state.barrier.inc();
 
-               Statements.queryEach(impl, querySupport.getId(subject), querySupport.getId(predicate), this, impl.parent, listener, new TripleIntProcedureAdapter() {
+               try {
+                       Statements.queryEach(impl, querySupport.getId(subject), querySupport.getId(predicate), this, impl.parent, listener, new TripleIntProcedureAdapter() {
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, int s, int p, int o) {
-                               try {
-                                       procedure.execute(querySupport.getStatement(s, p, o));
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
+                               @Override
+                               public void execute(ReadGraphImpl graph, int s, int p, int o) {
+                                       try {
+                                               procedure.execute(querySupport.getStatement(s, p, o));
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
                                }
-                       }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                               try {
-                                       procedure.finished();
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
+                               @Override
+                               public void finished(ReadGraphImpl graph) {
+                                       try {
+                                               procedure.finished();
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       procedure.exception(t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
                                }
+
+                               @Override
+                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                       try {
+                                               procedure.exception(t);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
+                               }
 
-               });
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
@@ -3302,7 +2450,11 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //             if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(proc, "#Statements" + sId + "#" + pId);
 //             else impl.state.barrier.inc(null, null);
 
-               Statements.queryEach(impl, sId, pId, this, impl.parent, listener, proc);
+               try {
+                       Statements.queryEach(impl, sId, pId, this, impl.parent, listener, proc);
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
@@ -3375,7 +2527,11 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //             if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(proc, "#Statements" + sId + "#" + pId);
 //             else impl.state.barrier.inc(null, null);
 
-               Statements.queryEach(impl, sId, pId, this, impl.parent, listener, proc);
+               try {
+                       Statements.queryEach(impl, sId, pId, this, impl.parent, listener, proc);
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
        
@@ -3447,38 +2603,42 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
 //             impl.state.barrier.inc();
 
-               AssertedStatements.queryEach(impl, querySupport.getId(subject), querySupport.getId(predicate), this, impl.parent, listener, new TripleIntProcedureAdapter() {
+               try {
+                       QueryCache.runnerAssertedStatements(impl, querySupport.getId(subject), querySupport.getId(predicate), impl.parent, listener, new TripleIntProcedureAdapter() {
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, int s, int p, int o) {
-                               try {
-                                       procedure.execute(graph, querySupport.getStatement(s, p, o));
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
+                               @Override
+                               public void execute(ReadGraphImpl graph, int s, int p, int o) {
+                                       try {
+                                               procedure.execute(graph, querySupport.getStatement(s, p, o));
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
                                }
-                       }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                               try {
-                                       procedure.finished(graph);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
+                               @Override
+                               public void finished(ReadGraphImpl graph) {
+                                       try {
+                                               procedure.finished(graph);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       procedure.exception(graph, t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
                                }
+
+                               @Override
+                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                       try {
+                                               procedure.exception(graph, t);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
+                               }
 
-               });
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
@@ -3498,84 +2658,65 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
 //             impl.state.barrier.inc();
 
-               Objects.runner(impl, querySupport.getId(subject), querySupport.getId(predicate), impl.parent, listener, new IntProcedure() {
+               try {
+                       QueryCache.runnerObjects(impl, querySupport.getId(subject), querySupport.getId(predicate), impl.parent, listener, new IntProcedure() {
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, int i) {
-                               try {
-                                       procedure.execute(querySupport.getResource(i));
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
+                               @Override
+                               public void execute(ReadGraphImpl graph, int i) {
+                                       try {
+                                               procedure.execute(querySupport.getResource(i));
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
                                }
-                       }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                               try {
-                                       procedure.finished();
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
+                               @Override
+                               public void finished(ReadGraphImpl graph) {
+                                       try {
+                                               procedure.finished();
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               System.out.println("forEachObject exception " + t);
-                               try {
-                                       procedure.exception(t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
                                }
+
+                               @Override
+                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                       System.out.println("forEachObject exception " + t);
+                                       try {
+                                               procedure.exception(t);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
+                               }
 
-               });
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
+       @Override
+       final public void forEachDirectPredicate(final ReadGraphImpl impl, final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
 
-//     @Override
-//     final public void forEachDirectObject(final ReadGraphImpl impl, final Resource subject, final Resource predicate, final AsyncMultiProcedure<Resource> procedure) {
-//
+               throw new UnsupportedOperationException();
+               
 //             assert(subject != null);
-//             assert(predicate != null);
 //             assert(procedure != null);
 //
 //             final ListenerBase listener = getListenerBase(procedure);
 //
-//             int sId = querySupport.getId(subject);
-//             int pId = querySupport.getId(predicate);
-//
-//             MultiIntProcedure proc = new MultiIntProcedure(procedure, impl, support);
-//
-//             if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(proc, "#DirectObjects" + sId + "#" + pId);
-//             else impl.state.barrier.inc(null, null);
-//
-//             //        final Exception caller = new Exception();
-//
-//             //        final Pair<Exception, Exception> exceptions = Pair.make(callerException, new Exception());
+//             MultiIntProcedure proc = new MultiIntProcedure(procedure, impl, querySupport);
 //
-//             DirectObjects.queryEach(impl, sId, pId, processor, impl.parent, listener, proc);
+//             int sId = querySupport.getId(subject);
 //
-//     }
-
-       @Override
-       final public void forEachDirectPredicate(final ReadGraphImpl impl, final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
-
-               assert(subject != null);
-               assert(procedure != null);
-
-               final ListenerBase listener = getListenerBase(procedure);
-
-               MultiIntProcedure proc = new MultiIntProcedure(procedure, impl, querySupport);
-
-               int sId = querySupport.getId(subject);
-
-//             if(AsyncBarrierImpl.BOOKKEEPING)  impl.state.barrier.inc(proc, "#DirectPredicates" + sId);
-//             else impl.state.barrier.inc(null, null);
-
-               DirectPredicates.queryEach(impl, sId, this, impl.parent, listener, proc);
+//             try {
+//                     QueryCache.runnerDirectPredicates(impl, sId, impl.parent, listener, proc);
+//             } catch (DatabaseException e) {
+//                     Logger.defaultLogError(e);
+//             }
 
        }
 
@@ -3641,16 +2782,59 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                final int sId = querySupport.getId(subject);
                final int pId = querySupport.getId(predicate);
 
-               Objects.runner(impl, sId, pId, impl.parent, listener, procedure);
+               try {
+                       QueryCache.runnerObjects(impl, sId, pId, impl.parent, listener, procedure);
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
                
        }
        
-       final public int getSingleObject(final ReadGraphImpl impl, final Resource subject, final Resource predicate) throws DatabaseException {
+       static class Runner2Procedure implements IntProcedure {
+           
+           public int single = 0;
+           public Throwable t = null;
+
+           public void clear() {
+               single = 0;
+               t = null;
+           }
+           
+        @Override
+        public void execute(ReadGraphImpl graph, int i) {
+            if(single == 0) single = i;
+            else single = -1;
+        }
 
-        final int sId = querySupport.getId(subject);
-        final int pId = querySupport.getId(predicate);
+        @Override
+        public void finished(ReadGraphImpl graph) {
+            if(single == -1) single = 0;
+        }
+
+        @Override
+        public void exception(ReadGraphImpl graph, Throwable throwable) {
+            single = 0;
+            this.t = throwable;
+        }
+        
+        public int get() throws DatabaseException {
+            if(t != null) {
+                if(t instanceof DatabaseException) throw (DatabaseException)t;
+                else throw new DatabaseException(t);
+            }
+            return single;
+        }
+           
+       }
+       
+       final public int getSingleObject(final ReadGraphImpl impl, final Resource subject, final Resource predicate) throws DatabaseException {
+               
+               final int sId = querySupport.getId(subject);
+               final int pId = querySupport.getId(predicate);
 
-        return Objects.runner2(impl, sId, pId, impl.parent);
+               Runner2Procedure proc = new Runner2Procedure();
+               QueryCache.runnerObjects(impl, sId, pId, impl.parent, null, proc);
+               return proc.get();
            
        }
 
@@ -4062,40 +3246,42 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                final ListenerBase listener = getListenerBase(procedure);
 
-//             impl.state.barrier.inc();
-
-               AssertedStatements.queryEach(impl, querySupport.getId(subject), querySupport.getId(predicate), this, impl.parent, listener, new TripleIntProcedure() {
+               try {
+                       QueryCache.runnerAssertedStatements(impl, querySupport.getId(subject), querySupport.getId(predicate), impl.parent, listener, new TripleIntProcedure() {
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, int s, int p, int o) {
-                               try {
-                                       procedure.execute(graph, querySupport.getResource(o));
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
+                               @Override
+                               public void execute(ReadGraphImpl graph, int s, int p, int o) {
+                                       try {
+                                               procedure.execute(graph, querySupport.getResource(o));
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
                                }
-                       }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                               try {               
-                                       procedure.finished(graph);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
+                               @Override
+                               public void finished(ReadGraphImpl graph) {
+                                       try {               
+                                               procedure.finished(graph);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       procedure.exception(graph, t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
                                }
+
+                               @Override
+                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                       try {
+                                               procedure.exception(graph, t);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
+                               }
 
-               });
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
@@ -4145,7 +3331,11 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //             if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#PrincipalTypes#" + sId);
 //             else impl.state.barrier.inc(null, null);
 
-               PrincipalTypes.queryEach(impl, sId, this, impl.parent, listener, ip);
+               try {
+                       QueryCache.runnerPrincipalTypes(impl, sId, impl.parent, listener, ip);
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
@@ -4159,39 +3349,42 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
 //             impl.state.barrier.inc();
 
-               PrincipalTypes.queryEach(impl, querySupport.getId(subject), this, impl.parent, listener, new IntProcedure() {
+               try {
+                       QueryCache.runnerPrincipalTypes(impl, querySupport.getId(subject), impl.parent, listener, new IntProcedure() {
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, int i) {
-                               try {
-                                       procedure.execute(querySupport.getResource(i));
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
+                               @Override
+                               public void execute(ReadGraphImpl graph, int i) {
+                                       try {
+                                               procedure.execute(querySupport.getResource(i));
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
                                }
-                       }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                               try {
-                                       procedure.finished();
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
+                               @Override
+                               public void finished(ReadGraphImpl graph) {
+                                       try {
+                                               procedure.finished();
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       procedure.exception(t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
                                }
-//                             impl.state.barrier.dec();
-                       }
 
-               });
+                               @Override
+                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                       try {
+                                               procedure.exception(t);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
+//                             impl.state.barrier.dec();
+                               }
 
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
        }
 
     final public void forTypes(final ReadGraphImpl impl, final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
@@ -4237,10 +3430,11 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
         int sId = querySupport.getId(subject);
 
-//      if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#Types" + sId);
-//      else impl.state.barrier.inc(null, null);
-
-        Types.queryEach(impl, sId, this, impl.parent, listener, ip);
+        try {
+                       QueryCache.runnerTypes(impl, sId, impl.parent, listener, ip);
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
     }
     
@@ -4249,7 +3443,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                assert(subject != null);
                
-               return Types.queryEach2(impl, querySupport.getId(subject), this, impl.parent);
+               return QueryCacheBase.resultTypes(impl, querySupport.getId(subject), impl.parent, null, null);
 
        }
 
@@ -4261,41 +3455,44 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                final ListenerBase listener = getListenerBase(procedure);
 
-//             impl.state.barrier.inc();
-
-               RelationInfoQuery.queryEach(impl, querySupport.getId(subject), this, impl.parent, listener, new InternalProcedure<RelationInfo>() {
+               try {
+                       
+                       QueryCache.runnerRelationInfoQuery(impl, querySupport.getId(subject), impl.parent, listener, new InternalProcedure<RelationInfo>() {
 
-                       AtomicBoolean first = new AtomicBoolean(true);
+                               AtomicBoolean first = new AtomicBoolean(true);
 
-                       @Override
-                       public void execute(final ReadGraphImpl graph, RelationInfo set) {
-                               try {
-                                       if(first.compareAndSet(true, false)) {
-                                               procedure.execute(graph, set);
+                               @Override
+                               public void execute(final ReadGraphImpl graph, RelationInfo set) {
+                                       try {
+                                               if(first.compareAndSet(true, false)) {
+                                                       procedure.execute(graph, set);
 //                                             impl.state.barrier.dec();
-                                       } else {
-                                               procedure.execute(impl.newRestart(graph), set);
+                                               } else {
+                                                       procedure.execute(impl.newRestart(graph), set);
+                                               }
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
                                        }
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
                                }
-                       }
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       if(first.compareAndSet(true, false)) {
-                                               procedure.exception(graph, t);
+                               @Override
+                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                       try {
+                                               if(first.compareAndSet(true, false)) {
+                                                       procedure.exception(graph, t);
 //                                             impl.state.barrier.dec("ReadGraphSupportImpl.1353");
-                                       } else {
-                                               procedure.exception(impl.newRestart(graph), t);
+                                               } else {
+                                                       procedure.exception(impl.newRestart(graph), t);
+                                               }
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
                                        }
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
                                }
-                       }
 
-               });
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
@@ -4307,14 +3504,13 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                final ListenerBase listener = getListenerBase(procedure);
 
-//             impl.state.barrier.inc();
-
-               SuperTypes.queryEach(impl, querySupport.getId(subject), this, impl.parent, listener, new InternalProcedure<IntSet>() {
+               try {
+                       QueryCache.runnerSuperTypes(impl, querySupport.getId(subject), impl.parent, listener, new InternalProcedure<IntSet>() {
 
-                       AtomicBoolean first = new AtomicBoolean(true);
+                               AtomicBoolean first = new AtomicBoolean(true);
 
-                       @Override
-                       public void execute(final ReadGraphImpl graph, IntSet set) {
+                               @Override
+                               public void execute(final ReadGraphImpl graph, IntSet set) {
 //                             final HashSet<Resource> result = new HashSet<Resource>();
 //                             set.forEach(new TIntProcedure() {
 //
@@ -4325,33 +3521,36 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //                                     }
 //
 //                             });
-                               try {
-                                       if(first.compareAndSet(true, false)) {
-                                               procedure.execute(graph, set);
+                                       try {
+                                               if(first.compareAndSet(true, false)) {
+                                                       procedure.execute(graph, set);
 //                                             impl.state.barrier.dec();
-                                       } else {
-                                               procedure.execute(impl.newRestart(graph), set);
+                                               } else {
+                                                       procedure.execute(impl.newRestart(graph), set);
+                                               }
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
                                        }
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
                                }
-                       }
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       if(first.compareAndSet(true, false)) {
-                                               procedure.exception(graph, t);
+                               @Override
+                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                       try {
+                                               if(first.compareAndSet(true, false)) {
+                                                       procedure.exception(graph, t);
 //                                             impl.state.barrier.dec();
-                                       } else {
-                                               procedure.exception(impl.newRestart(graph), t);
+                                               } else {
+                                                       procedure.exception(impl.newRestart(graph), t);
+                                               }
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
                                        }
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
                                }
-                       }
 
-               });
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
@@ -4402,7 +3601,13 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //             if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#DirectSuperRelations#" + sId);
 //             else impl.state.barrier.inc(null, null);
 
-               DirectSuperRelations.queryEach(impl, sId, this, impl.parent, listener, ip);
+               try {
+                       QueryCache.runnerDirectSuperRelations(impl, sId, impl.parent, listener, ip);
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
+               
+//             DirectSuperRelations.queryEach(impl, sId, this, impl.parent, listener, ip);
 
        }
 
@@ -4467,21 +3672,20 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //             if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#SuperRelations#" + sId);
 //             else impl.state.barrier.inc(null, null);
 
-               SuperRelations.queryEach(impl, sId, this, impl.parent, listener, ip);
+               try {
+                       QueryCache.runnerSuperRelations(impl, sId, impl.parent, listener, ip);
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
        final public byte[] getValue(final ReadGraphImpl impl, final Resource subject) throws DatabaseException {
-
-         int sId = querySupport.getId(subject);
-      return ValueQuery.queryEach(impl, sId, impl.parent);
-
+         return getValue(impl, querySupport.getId(subject));
        }
 
        final public byte[] getValue(final ReadGraphImpl impl, final int subject) throws DatabaseException {
-
-           return ValueQuery.queryEach(impl, subject, impl.parent);
-
+               return QueryCache.resultValueQuery(impl, subject, impl.parent, null, null); 
        }
 
        @Override
@@ -4532,13 +3736,24 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //                     if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#Value" + sId);
 //                     else impl.state.barrier.inc(null, null);
 
-                       return ValueQuery.queryEach(impl, sId, impl.parent, listener, ip);
+                       try {
+                               return QueryCacheBase.resultValueQuery(impl, sId, impl.parent, listener, ip);
+                       } catch (DatabaseException e) {
+                               Logger.defaultLogError(e);
+                       } 
+                       
 
                } else {
 
-                       return ValueQuery.queryEach(impl, sId, impl.parent, null, null);
+                       try {
+                               return QueryCacheBase.resultValueQuery(impl, sId, impl.parent, null, null);
+                       } catch (DatabaseException e) {
+                               Logger.defaultLogError(e);
+                       } 
                        
                }
+               
+               throw new IllegalStateException("Internal error");
 
        }
 
@@ -4591,7 +3806,11 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //                     if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#Value" + sId);
 //                     else impl.state.barrier.inc(null, null);
 
-                       ValueQuery.queryEach(impl, sId, impl.parent, listener, ip);
+                       try {
+                               QueryCache.runnerValueQuery(impl, sId, impl.parent, listener, ip);
+                       } catch (DatabaseException e) {
+                               Logger.defaultLogError(e);
+                       }
 
                } else {
 
@@ -4615,7 +3834,11 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                        int sId = querySupport.getId(subject);
 
-                       ValueQuery.queryEach(impl, sId, impl.parent, listener, ip);
+                       try {
+                               QueryCache.runnerValueQuery(impl, sId, impl.parent, listener, ip);
+                       } catch (DatabaseException e) {
+                               Logger.defaultLogError(e);
+                       }
 
                }
 
@@ -4695,7 +3918,11 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //             if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#DirectObjects#" + sId);
 //             else impl.state.barrier.inc(null, null);
 
-               Objects.runner(impl, sId, getInverseOf(), impl.parent, listener, ip);
+               try {
+                       QueryCache.runnerObjects(impl, sId, getInverseOf(), impl.parent, listener, ip);
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
@@ -4745,29 +3972,33 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
 //             impl.state.barrier.inc();
 
-               forBuiltin(impl, id, impl.parent, new InternalProcedure<Integer>() {
+               try {
+                       forBuiltin(impl, id, impl.parent, new InternalProcedure<Integer>() {
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, Integer result) {
-                               try {
-                                       procedure.execute(graph, querySupport.getResource(result)); 
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
+                               @Override
+                               public void execute(ReadGraphImpl graph, Integer result) {
+                                       try {
+                                               procedure.execute(graph, querySupport.getResource(result)); 
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }   
+                               }   
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       procedure.exception(graph, t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
+                               @Override
+                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                       try {
+                                               procedure.exception(graph, t);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
+                               }
 
-               });
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
@@ -4779,39 +4010,13 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                final ListenerBase listener = getListenerBase(procedure);
 
-//             impl.state.barrier.inc();
-
-               DirectPredicates.queryEach(impl, querySupport.getId(subject), this, impl.parent, listener, new IntProcedure() {
-
-                       boolean found = false;
-
-                       @Override
-                       public void execute(ReadGraphImpl graph, int object) {
-                               found = true;
-                       }
-
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                               try {
-                                       procedure.execute(graph, found);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
-//                             impl.state.barrier.dec();
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       procedure.exception(graph, t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
-//                             impl.state.barrier.dec();
-                       }
-
-               });
-
+               try {
+                       IntSet result = QueryCache.resultDirectPredicates(impl, querySupport.getId(subject), impl.parent, listener,QueryCache.emptyProcedureDirectPredicates);
+                       procedure.execute(impl, !result.isEmpty());
+               } catch (DatabaseException e) {
+                       procedure.exception(impl, e);
+               }
+                       
        }
 
        @Override
@@ -4911,30 +4116,34 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
 //             impl.state.barrier.inc();
 
-               ValueQuery.queryEach(impl, querySupport.getId(subject), impl.parent, listener, new InternalProcedure<byte[]>() {
+               try {
+                       QueryCache.runnerValueQuery(impl, querySupport.getId(subject), impl.parent, listener, new InternalProcedure<byte[]>() {
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, byte[] object) {
-                               boolean result = object != null;
-                               try {
-                                       procedure.execute(graph, result);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
+                               @Override
+                               public void execute(ReadGraphImpl graph, byte[] object) {
+                                       boolean result = object != null;
+                                       try {
+                                               procedure.execute(graph, result);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       procedure.exception(graph, t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
                                }
+
+                               @Override
+                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                       try {
+                                               procedure.exception(graph, t);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
+                               }
 
-               });
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
@@ -4946,139 +4155,139 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                final ListenerBase listener = getListenerBase(procedure);
 
-//             impl.state.barrier.inc();
-
-               OrderedSet.queryEach(impl, querySupport.getId(subject), this, impl.parent, listener, new IntProcedure() {
+               try {
+                       
+                       QueryCache.runnerOrderedSet(impl, querySupport.getId(subject), impl.parent, listener, new IntProcedure() {
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               try {
-                                       procedure.exception(graph, t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
-                               }
+                               @Override
+                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                       try {
+                                               procedure.exception(graph, t);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
-
-                       @Override
-                       public void execute(ReadGraphImpl graph, int i) {
-                               try {
-                                       procedure.execute(graph, querySupport.getResource(i));
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
                                }
-                       }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                               try {
-                                       procedure.finished(graph);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
+                               @Override
+                               public void execute(ReadGraphImpl graph, int i) {
+                                       try {
+                                               procedure.execute(graph, querySupport.getResource(i));
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
                                }
+
+                               @Override
+                               public void finished(ReadGraphImpl graph) {
+                                       try {
+                                               procedure.finished(graph);
+                                       } catch (Throwable t2) {
+                                               Logger.defaultLogError(t2);
+                                       }
 //                             impl.state.barrier.dec();
-                       }
+                               }
 
-               });
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+               }
 
        }
 
        @Override
-       final public <T> void query(final ReadGraphImpl impl, final AsyncRead<T> request, final CacheEntry parent, final AsyncProcedure<T> procedure, ListenerBase listener) {
+       final public <T> void query(final ReadGraphImpl impl, final AsyncRead<T> request, final CacheEntry parent, final AsyncProcedure<T> procedure, ListenerBase listener) throws DatabaseException {
 
                assert(request != null);
                assert(procedure != null);
 
-//             if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(request, "#" + request.toString() + ".1999");
-//             else impl.state.barrier.inc(null, null);
-
-               runAsyncRead(impl, request, parent, listener, procedure);
-
-       }
-
-       @Override
-       final public <T> T tryQuery(final ReadGraphImpl graph, final Read<T> request) throws DatabaseException {
-
-               assert(graph != null);
-               assert(request != null);
-
-               final ReadEntry entry = cache.readMap.get(request);
-               if(entry != null && entry.isReady()) {
-                   return (T)entry.get(graph, this, null);
-               } else {
-                       return request.perform(graph);
-               }
+               QueryCache.runnerAsyncReadEntry(impl, request, parent, listener, procedure);
 
        }
 
-    final public <T> T tryQuery(final ReadGraphImpl graph, final ExternalRead<T> request) throws DatabaseException {
-
-        assert(graph != null);
-        assert(request != null);
-
-        final ExternalReadEntry<T> entry = cache.externalReadMap.get(request);
-        if(entry != null && entry.isReady()) {
-            if(entry.isExcepted()) {
-                Throwable t = (Throwable)entry.getResult();
-                if(t instanceof DatabaseException) throw (DatabaseException)t;
-                else throw new DatabaseException(t);
-            } else {
-                return (T)entry.getResult();
-            }            
-        } else {
-
-            final DataContainer<T> result = new DataContainer<T>();
-            final DataContainer<Throwable> exception = new DataContainer<Throwable>();
-            
-            request.register(graph, new Listener<T>() {
-                
-                @Override
-                public void exception(Throwable t) {
-                    exception.set(t);
-                }
-
-                @Override
-                public void execute(T t) {
-                    result.set(t);
-                }
-
-                @Override
-                public boolean isDisposed() {
-                    return true;
-                }
-            
-            });
-            
-            Throwable t = exception.get();
-            if(t != null) {
-                if(t instanceof DatabaseException) throw (DatabaseException)t;
-                else throw new DatabaseException(t);
-            }
-            
-            return result.get();
-
-        }
+//     @Override
+//     final public <T> T tryQuery(final ReadGraphImpl graph, final Read<T> request) throws DatabaseException {
+//
+//             assert(graph != null);
+//             assert(request != null);
+//
+//             final ReadEntry entry = (ReadEntry)cache.getCached(request);
+//             if(entry != null && entry.isReady()) {
+//                 return (T)entry.get(graph, this, null);
+//             } else {
+//                     return request.perform(graph);
+//             }
+//
+//     }
 
-    }
+//    final public <T> T tryQuery(final ReadGraphImpl graph, final ExternalRead<T> request) throws DatabaseException {
+//
+//        assert(graph != null);
+//        assert(request != null);
+//
+//        final ExternalReadEntry<T> entry = cache.externalReadMap.get(request);
+//        if(entry != null && entry.isReady()) {
+//            if(entry.isExcepted()) {
+//                Throwable t = (Throwable)entry.getResult();
+//                if(t instanceof DatabaseException) throw (DatabaseException)t;
+//                else throw new DatabaseException(t);
+//            } else {
+//                return (T)entry.getResult();
+//            }            
+//        } else {
+//
+//            final DataContainer<T> result = new DataContainer<T>();
+//            final DataContainer<Throwable> exception = new DataContainer<Throwable>();
+//            
+//            request.register(graph, new Listener<T>() {
+//                
+//                @Override
+//                public void exception(Throwable t) {
+//                    exception.set(t);
+//                }
+//
+//                @Override
+//                public void execute(T t) {
+//                    result.set(t);
+//                }
+//
+//                @Override
+//                public boolean isDisposed() {
+//                    return true;
+//                }
+//            
+//            });
+//            
+//            Throwable t = exception.get();
+//            if(t != null) {
+//                if(t instanceof DatabaseException) throw (DatabaseException)t;
+//                else throw new DatabaseException(t);
+//            }
+//            
+//            return result.get();
+//
+//        }
+//
+//    }
        
-       @Override
-       final public <T> void tryQuery(final ReadGraphImpl graph, final AsyncRead<T> request, AsyncProcedure<T> procedure) {
-
-               assert(graph != null);
-               assert(request != null);
-
-               final AsyncReadEntry entry = cache.asyncReadMap.get(request);
-               if(entry != null && entry.isReady()) {
-                       if(entry.isExcepted()) {
-                               procedure.exception(graph, (Throwable)entry.getResult());
-                       } else {
-                               procedure.execute(graph, (T)entry.getResult());
-                       }
-               } else {
-                       request.perform(graph, procedure);
-               }
-
-       }
+//     @Override
+//     final public <T> void tryQuery(final ReadGraphImpl graph, final AsyncRead<T> request, AsyncProcedure<T> procedure) {
+//
+//             assert(graph != null);
+//             assert(request != null);
+//
+//             final AsyncReadEntry entry = cache.asyncReadMap.get(request);
+//             if(entry != null && entry.isReady()) {
+//                     if(entry.isExcepted()) {
+//                             procedure.exception(graph, (Throwable)entry.getResult());
+//                     } else {
+//                             procedure.execute(graph, (T)entry.getResult());
+//                     }
+//             } else {
+//                     request.perform(graph, procedure);
+//             }
+//
+//     }
 
        @Override
        final public <T> void query(final ReadGraphImpl impl, final MultiRead<T> request, final CacheEntry parent, final AsyncMultiProcedure<T> procedure, ListenerBase listener) {
@@ -5160,26 +4369,26 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                try {
                
-                       queryPrimitiveRead(impl, request, parent, listener, new Procedure<T>() {
+                       queryPrimitiveRead(impl, request, parent, listener, new AsyncProcedure<T>() {
+       
+                               @Override
+                               public String toString() {
+                                       return procedure.toString();
+                               }
        
                                @Override
-                               public void execute(T result) {
+                               public void execute(AsyncReadGraph graph, 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) {
+                               public void exception(AsyncReadGraph graph, Throwable throwable) {
                                        try {
-                                               procedure.exception(t);
+                                               procedure.exception(throwable);
                                        } catch (Throwable t2) {
                                                Logger.defaultLogError(t2);
                                        }
index 6ba47cac780d45bd8a5595e3a7a11f2d5a7eaaa5..3071c30c042d8730799004eec9d10b7168b70fd4 100644 (file)
@@ -16,6 +16,7 @@ import java.io.InputStream;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.VirtualGraph;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ResourceNotFoundException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
@@ -35,11 +36,11 @@ public interface QuerySupport extends ResourceTranslator {
        int getSingleInstance(int subject);
        int getSingleSuperrelation(int subject);
        int getFunctionalObject(int subject, int predicate);
-       boolean getObjects(ReadGraphImpl graph, int subject, int predicate, IntProcedure procedure);
+       boolean getObjects(ReadGraphImpl graph, int subject, int predicate, IntProcedure procedure) throws DatabaseException;
     
        org.simantics.db.DirectStatements getStatements(ReadGraphImpl graph, final int subject, QueryProcessor processor, boolean ignoreVirtual);
     
-    void getPredicates(ReadGraphImpl graph, int subject, IntProcedure procedure);
+    void getPredicates(ReadGraphImpl graph, int subject, IntProcedure procedure) throws DatabaseException;
     byte[] getValue(ReadGraphImpl graph, int resource);
     InputStream getValueStream(ReadGraphImpl graph, int resource);
     
index edc32454929542ab76a0f4a51155559a52b22324..8abbb86c8745499246b311b5648a7dcc499eecf9 100644 (file)
@@ -20,7 +20,7 @@ import org.simantics.db.request.Read;
 import org.simantics.db.request.ReadExt;
 import org.simantics.db.request.RequestFlags;
 
-final public class ReadEntry<T> extends CacheEntryBase {
+final public class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
 
        protected Read<T> request;
 
@@ -44,26 +44,14 @@ final public class ReadEntry<T> extends CacheEntryBase {
        setResult(null);
     }
        
-    final public void addOrSet(AsyncReadGraph graph, Object item) {
+    final public Object addOrSet(AsyncReadGraph graph, Object result) {
 
        assert(assertPending());
        
-//        ArrayList<Pair<AsyncProcedure<T>, AsyncBarrier>> p = null;
-
-       synchronized(this) {
-           
-            setResult(item);
-               setReady();
-//             p = procs;
-//             procs = null;
-               
-       }
-
-//        if(p != null)
-//             for(Pair<AsyncProcedure<T>, AsyncBarrier> proc : p) {
-//                     proc.first.execute(graph, (T)item);
-//                     proc.second.dec();
-//             }
+       setResult(result);
+       setReady();
+       
+       return result;
         
     }
 
@@ -73,17 +61,10 @@ final public class ReadEntry<T> extends CacheEntryBase {
         return new Query() {
 
                        @Override
-                       public void recompute(ReadGraphImpl graph_, Object provider, CacheEntry entry) {
-                               
-                               QueryProcessor qp = (QueryProcessor)provider;
+                       public void recompute(ReadGraphImpl graph) {
                                
-                       WriteGraphImpl write = qp.getCore().getSession().getService(WriteGraphImpl.class);
-                               
-                       ReadGraphImpl graph = write.newSync(entry);
-
                                try {
 
-                                       entry.setPending();
                                    T result = request.perform(graph);
                                    addOrSet(graph, result);
 
@@ -97,7 +78,7 @@ final public class ReadEntry<T> extends CacheEntryBase {
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                               processor.cache.readMap.remove(request);
+                               processor.cache.remove(ReadEntry.this);
                        }
 
                        @Override
@@ -119,7 +100,56 @@ final public class ReadEntry<T> extends CacheEntryBase {
         
     }
     
-       public Object performFromCache(ReadGraphImpl graph, Object provider,    Object procedure) {
+    @Override
+    public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+
+               ReadGraphImpl queryGraph = graph.withParent(this);
+
+               try {
+
+                       addOrSet(queryGraph, request.perform(queryGraph));
+                       return get(queryGraph, procedure);
+
+               }  catch (DatabaseException e) {
+
+                       except(e);
+                       return get(graph, procedure);
+
+               }  catch (Throwable t) {
+
+                       except(new DatabaseException(t));
+                       return get(graph, procedure);
+
+               }
+       
+    }
+
+    public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
+       
+               ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
+
+               try {
+
+                       T result = request.perform(queryGraph);
+                       if(entry != null) entry.addOrSet(queryGraph, result);
+                       procedure.execute(graph, result);
+
+               }  catch (DatabaseException e) {
+
+                       if(entry != null) entry.except(e);
+                       procedure.exception(graph, e);
+
+               }  catch (Throwable t) {
+
+                       DatabaseException dbe = new DatabaseException(t);
+                       if(entry != null) entry.except(dbe);
+                       procedure.exception(graph, dbe);
+
+               }
+       
+    }
+    
+       public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
            
         AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
 
@@ -149,8 +179,8 @@ final public class ReadEntry<T> extends CacheEntryBase {
                else return request.toString() + " - " + statusOrException;
        }
        
-       public Object get(ReadGraphImpl graph, QueryProcessor processor, Object procedure) throws DatabaseException {
-               if(procedure != null) performFromCache(graph, processor, procedure);
+       public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+               if(procedure != null) performFromCache(graph, procedure);
                checkAndThrow();
                return getResult();
        }
index f2081d35f767c9fced6b3814feb2393b0b68bca2..667d2f1256917551ecff8bc0f68ff6501528cccb 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.concurrent.Semaphore;
-import java.util.concurrent.atomic.AtomicBoolean;
-
 import org.simantics.db.RelationInfo;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
 
 final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<RelationInfo>> {
 
-       private RelationInfoQuery(final int resource) {
+       RelationInfoQuery(final int resource) {
         super(resource);
     }
     
-    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.cache.relationInfoMap.get(r);
-        if(entry == null) {
-               
-               entry = new RelationInfoQuery(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-            return entry.getResult();
-            
-        } else {
-               
-            if(!entry.isReady()) {
-               synchronized(entry) {
-                    if(!entry.isReady()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<RelationInfo>>();
-//                                     entry.procs.add(procedure);
-//                        provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                                     return entry.getResult();
-                    }
-               }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-            return entry.getResult();
-            
-        }
-
-    }
-    
-    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.cache.relationInfoMap.get(r);
-       if(entry != null && entry.isReady()) {
-               entry.performFromCache(graph, provider, procedure);
-               return entry.getResult();
-       }
-
-        return runner(graph, r, provider, parent, listener, procedure);
-         
-    }
-     
-    final public static RelationInfoQuery probe(ReadGraphImpl graph, int resource) {
-        
-        final int thread = graph.thread(resource);
-       RelationInfoQuery entry = (RelationInfoQuery)graph.processor.cache.relationInfoMap.get(resource);
-       if(entry != null && entry.isReady()) {
-               return entry;
-       } else {
-               return null;
-       }
-         
-    }
-
-    @Override
-       public UnaryQuery<InternalProcedure<RelationInfo>> getEntry(QueryProcessor provider) {
-        return provider.cache.relationInfoMap.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.relationInfoMap.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.cache.relationInfoMap.remove(id);
+               provider.cache.remove(this);
        }
 
-       private void computeAssertions(ReadGraphImpl graph, final boolean isFinal, final boolean isFunctional, final QueryProcessor queryProvider, final InternalProcedure<RelationInfo> proc) {
+       private static void computeAssertions(ReadGraphImpl graph, int r, final boolean isFinal, final boolean isFunctional, RelationInfoQuery entry, final InternalProcedure<RelationInfo> proc) throws DatabaseException {
 
-           final int isUsedInAssertion = queryProvider.getHasPredicateInverse();
+               QueryProcessor processor = graph.processor;
+               
+           final int isUsedInAssertion = processor.getHasPredicateInverse();
         assert(isUsedInAssertion != 0);
                
-        DirectObjects.queryEach(graph, id, isUsedInAssertion, queryProvider, this, null, new IntProcedure() {
+        QueryCache.runnerDirectObjects(graph, r, isUsedInAssertion, entry, null, new IntProcedure() {
 
-                       AtomicBoolean done = new AtomicBoolean(false);
+               boolean done = false;
                        
                        @Override
-                       public void execute(ReadGraphImpl graph, int i) {
-                               if(done.compareAndSet(false, true)) {
-//                                     System.err.println("Assertions for relation " + id);
-                                       RelationInfo result = new RelationInfo(id, isFunctional, isFinal, true);
-                                       addOrSet(graph, result, queryProvider);
-                                       proc.execute(graph, result);
-                               }
+                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                               done = true;
+                               RelationInfo result = new RelationInfo(r, isFunctional, isFinal, true);
+                               if(entry != null) entry.setResult(result);
+                               proc.execute(graph, result);
                        }
 
                        @Override
-                       public void finished(ReadGraphImpl graph) {
-                               if(done.compareAndSet(false, true)) {
-//                                     System.err.println("No assertions for relation " + id);
-                                       RelationInfo result = new RelationInfo(id, isFunctional, isFinal, false);
-                                       addOrSet(graph, result, queryProvider);
+                       public void finished(ReadGraphImpl graph) throws DatabaseException {
+                               if(!done) {
+                                       done = true;
+                                       RelationInfo result = new RelationInfo(r, isFunctional, isFinal, false);
+                                       if(entry != null) entry.setResult(result);
                                        proc.execute(graph, result);
                                }
                        }
 
                        @Override
-                       public void exception(ReadGraphImpl graph, Throwable throwable) {
-                               if(done.compareAndSet(false, true)) {
+                       public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+                               if(!done) {
+                                       done = true;
                                        DatabaseException e = new DatabaseException("Internal error in RelationInfoQuery");
-                                       except(e);
+                                       if(entry != null) entry.except(e);
                                        proc.exception(graph, e);
                                }
                        }
                        
                });
                
-//         Types.queryEach(callerThread, id, queryProvider, this, null, new InternalProcedure<IntSet>() {
-//             
-//          @Override
-//          public void execute(int callerThread, IntSet types) {
-//               computeAssertions(callerThread, isFinal, isFunctional, queryProvider, proc);
-////             
-////             
-//////              System.out.println("RelationInfoQuery: computeTypes execute " + types);
-////
-////              RelationInfo result = new RelationInfo(id, types.contains(queryProvider.getFunctionalRelation()), isFinal);
-////              
-////              addOrSet(callerThread, result, queryProvider);
-////              
-////              proc.execute(callerThread, result);
-////              
-//          }
-//                     
-//                     @Override
-//                     public void exception(int callerThread, Throwable t) {
-//                             proc.exception(callerThread, t);
-//          }
-//
-//      });
-
        }
        
-       private void computeTypes(ReadGraphImpl graph, final boolean isFinal, final QueryProcessor queryProvider, final InternalProcedure<RelationInfo> proc) {
-        
-//        System.out.println("RelationInfoQuery: computeTypes " + id);
-
-           Types.queryEach(graph, id, queryProvider, this, null, new InternalProcedure<IntSet>() {
-               
-            @Override
-            public void execute(ReadGraphImpl graph, IntSet types) {
-               computeAssertions(graph, isFinal, types.contains(queryProvider.getFunctionalRelation()), queryProvider, proc);
-//
-////                System.out.println("RelationInfoQuery: computeTypes execute " + types);
-//
-//                RelationInfo result = new RelationInfo(id, types.contains(queryProvider.getFunctionalRelation()), isFinal);
-//                
-//                addOrSet(callerThread, result, queryProvider);
-//                
-//                proc.execute(callerThread, result);
-//                
-            }
-                       
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               proc.exception(graph, t);
-            }
-
-        });
-
-       }
-       
-       @Override
-       public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<RelationInfo> procedure, boolean store) {
-           
-//        System.out.println("RelationInfoQuery computeForEach begin " + id + " " + getResult() + " " + statusOrException);
-
+       public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
+               
+               QueryProcessor provider = graph.processor;
+               
            final int superRelationOf = provider.getSuperrelationOf();
         assert(superRelationOf != 0);
         
-        DirectPredicates.queryEach(graph, id, provider, this, null, new IntProcedure() {
-            
-            boolean found = false;
-
-            @Override
-            public void execute(ReadGraphImpl graph, int i) {
-//                System.out.println("RelationInfoQuery: execute " + i + " super = " + superRelationOf);
-                if(i == superRelationOf) {
-                    computeTypes(graph, false, provider, procedure);
-                    found = true;
-                }
-            }
-
-            @Override
-            public void finished(ReadGraphImpl graph) {
-//                System.out.println("RelationInfoQuery: finished");
-                if(!found) {
-                    computeTypes(graph, true, provider, procedure);
-                }
-            }
-            
-            @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
-//                System.out.println("RelationInfoQuery: exception");
-                procedure.exception(graph, t);
-            }
-
-        });
+        IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null, QueryCache.emptyProcedureDirectPredicates);
+        IntSet types = QueryCache.resultTypes(graph, r, entry, null, QueryCache.emptyProcedureTypes);
         
-        return getResult();
+       computeAssertions(graph, r, !direct.contains(superRelationOf), types.contains(graph.processor.getFunctionalRelation()), entry, procedure);
         
-    }
+       }
+
+       @Override
+       public Object compute(ReadGraphImpl graph, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
+               computeForEach(graph, id, this, procedure);
+               return getResult();
+       }
     
     @Override
     public String toString() {
        return "RelationInfoQuery[" + id + "]";
     }
 
-    public void addOrSet(ReadGraphImpl graph, final RelationInfo result, final QueryProcessor provider) {
-        
-       assert(isPending());
-       
-//     ArrayList<InternalProcedure<RelationInfo>> p = null;
-       
-       synchronized(this) {
-
-           setResult(result);
-           setReady();
-        
-//             p = procs;
-//             procs = null;
-               
-       }
-       
-//     if(p != null) {
-//          for(InternalProcedure<RelationInfo> proc : p) 
-//              proc.execute(graph, (RelationInfo)result);
+//    public void addOrSet(ReadGraphImpl graph, final RelationInfo result, final QueryProcessor provider) {
+//        
+//     assert(isPending());
+//     
+//     synchronized(this) {
+//
+//         setResult(result);
+//         setReady();
+//             
 //     }
-        
-    }            
+//        
+//    }
+    
+    @Override
+    public void setResult(Object result) {
+       super.setResult(result);
+       if(!(result instanceof RelationInfo) && !(result == NO_RESULT))
+               System.err.println("foo");
+       setReady();
+    }
 
     @Override
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<RelationInfo> procedure) {
+    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
 
         assert(isReady());
         
@@ -281,15 +133,17 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
-        
-        computeForEach(graph, provider, new InternalProcedure<RelationInfo>() {
+    public int type() {
+        return RequestFlags.IMMEDIATE_UPDATE;
+    }
+
+       @Override
+       public void recompute(ReadGraphImpl graph) throws DatabaseException {
+               
+        compute(graph, new InternalProcedure<RelationInfo>() {
 
             @Override
             public void execute(ReadGraphImpl graph, RelationInfo result) {
-                s.release();
             }
             
             @Override
@@ -297,22 +151,8 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
                 throw new Error("Error in recompute.", t);
             }
 
-        }, true);
-        
-       while(!s.tryAcquire()) {
-               provider.resume(graph);
-       }
-//        try {
-//            s.acquire();
-//        } catch (InterruptedException e) {
-//            throw new Error(e);
-//        }
-
-    }
-    
-    @Override
-    public int type() {
-        return RequestFlags.IMMEDIATE_UPDATE;
-    }
+        });
+               
+       }
     
 }
index d251a65c11b93dc4c70b3673d4b50518e912c5e5..121653b0faa799ec3b4cadc153dfd481ef5a513d 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.simantics.db.RelationInfo;
 import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
@@ -30,52 +28,8 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
     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.cache.statementsMap.get(id(r1,r2));
-
-    }
-    
-       final static Collection<Statements> entries(final QueryProcessor processor, final int 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.cache.statementsMap.get(id(r1,r2));
-        if(entry == null) {
-               
-               entry = new Statements(r1, r2);
-               entry.setPending();
-               entry.clearResult(processor.querySupport);
-               entry.putEntry(processor);
-               
-               processor.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-               
-            if(entry.isPending()) {
-               synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-//                        if(entry.procs == null) entry.procs = new ArrayList<TripleIntProcedure>();
-//                     entry.procs.add(procedure);
-//                     processor.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                     return;
-                    }
-               }
-            }
-            
-            processor.performForEach(graph, entry, parent, listener, procedure);
-            
-        }
-
-    }
     
-    final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
+    final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException {
         
        assert(r1 != 0);
        assert(r2 != 0);
@@ -85,23 +39,13 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                return;
         }
         
-        runner(graph, r1, r2, parent, listener, procedure);
+        QueryCache.runnerStatements(graph, r1, r2, parent, listener, procedure);
          
     }
 
-    @Override
-    public BinaryQuery<TripleIntProcedure> getEntry(QueryProcessor provider) {
-        return provider.cache.statementsMap.get(id);
-    }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-           provider.cache.statementsMap.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-        provider.cache.statementsMap.remove(id);
+        provider.cache.remove(this);
        }
        
     final static TripleIntProcedure NOPT = new TripleIntProcedure() {
@@ -121,7 +65,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
        
     };
        
-    final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Statements entry) {
+    final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Statements entry) throws DatabaseException {
        
         class AssertionMapProc implements IntProcedure {
                
@@ -151,10 +95,9 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                }
                
             @Override
-            public void execute(ReadGraphImpl graph, int type) {
-                AssertedStatements stms = AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, NOPT);
+            public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
                 if(result == null) {
-                       result = stms.getResult();
+                       result = QueryCache.resultAssertedStatements(graph, type, r2, entry, null, NOPT);
                 } else {
                        if (first) {
                                IntArray ia = result;
@@ -164,7 +107,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                }
                                first = false;
                        }
-                       IntArray ia = stms.getResult();
+                       IntArray ia = QueryCache.resultAssertedStatements(graph, type, r2, entry, null, NOPT);
                        if(ia.data != null) {
                                for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
                        }
@@ -184,13 +127,13 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
         AssertionMapProc amp = new AssertionMapProc();
 
         // This dependency could be cut
-       PrincipalTypes.queryEach(graph, r1, graph.processor, entry, null, amp);
+        QueryCache.runnerPrincipalTypes(graph, r1, entry, null, amp);
        
         return amp.result;
        
     }
        
-    final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) {
+    final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException {
         
        IntArray map = getAssertionMap(graph, r1, r2, entry);
        if(map == null) {
@@ -224,14 +167,24 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                int candidateS = map.data[0];
                int candidateP = map.data[1];
                int candidateO = map.data[2];
-
-                       SuperTypes candidate = SuperTypes.queryEach(graph, candidateS, graph.processor, entry, null, NOP);
-                       if(candidate.isExcepted()) {
-                               if(entry != null) entry.except((Throwable)candidate.getResult());
-                               procedure.exception(graph, (Throwable)candidate.getResult());
+               
+                       IntSet candidateIs = null;
+                       try {
+                               candidateIs = QueryCache.resultSuperTypes(graph, candidateS, entry, null, NOP);
+                       } catch (DatabaseException e) {
+                               if(entry != null) entry.except(e);
+                               procedure.exception(graph, e);
                                return;
                        }
-                       IntSet candidateIs = candidate.getResult();
+
+
+//                     SuperTypes candidate = SuperTypes.queryEach(graph, candidateS, graph.processor, entry, null, NOP);
+//                     if(candidate.isExcepted()) {
+//                             if(entry != null) entry.except((Throwable)candidate.getResult());
+//                             procedure.exception(graph, (Throwable)candidate.getResult());
+//                             return;
+//                     }
+//                     IntSet candidateIs = candidate.getResult();
                
                        for(int i=3;i<map.size();i+=3) {
 
@@ -247,13 +200,22 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                        
                                } else {
                                
-                                       SuperTypes next = SuperTypes.queryEach(graph, nextS, graph.processor, entry, null, NOP);
-                                       if(next.isExcepted()) {
-                                               if(entry != null) entry.except((Throwable)next.getResult());
-                                               procedure.exception(graph, (Throwable)next.getResult());
-                                               return;
-                                       }
-                                       IntSet nextIs = next.getResult();
+                                       IntSet nextIs = null;
+                                       try {
+                                               nextIs = QueryCache.resultSuperTypes(graph, nextS, entry, null, NOP);
+                                       } catch (DatabaseException e) {
+                                               if(entry != null) entry.except(e);
+                                               procedure.exception(graph, e);
+                                               return;
+                                       }
+                                       
+//                                     SuperTypes next = SuperTypes.queryEach(graph, nextS, graph.processor, entry, null, NOP);
+//                                     if(next.isExcepted()) {
+//                                             if(entry != null) entry.except((Throwable)next.getResult());
+//                                             procedure.exception(graph, (Throwable)next.getResult());
+//                                             return;
+//                                     }
+//                                     IntSet nextIs = next.getResult();
                                        
                                        if(nextIs.contains(candidateS)) {
 
@@ -306,7 +268,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
     };
        
        // Search for one statement
-       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) {
+       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
         
         if(ri.isFinal) {
             
@@ -322,7 +284,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
                        @Override
-                       public void execute(ReadGraphImpl graph, int i) {
+                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                if(entry != null) entry.addOrSetFunctional(r1, r2, i);
                                else procedure.execute(graph, r1, r2, i);
                        }
@@ -360,10 +322,11 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
             final AtomicBoolean found = new AtomicBoolean(false);
             
             // Note! The dependency is intentionally cut!
-            DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
+            IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null, QueryCache.emptyProcedureDirectPredicates);
+            direct.forEach(graph, new SyncIntProcedure() {
                 
                 @Override
-                public void run(ReadGraphImpl graph) {
+                public void run(ReadGraphImpl graph) throws DatabaseException {
                     
                     if(found.get()) {
                        if(entry != null) entry.finish(graph, procedure);
@@ -371,14 +334,14 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                     } else {
                     
                            // Check for assertions
-                           forSingleAssertion(graph, r1, r2, entry, procedure);
+                       forSingleAssertion(graph, r1, r2, entry, procedure);
                            
                     }
                     
                 }
 
                 @Override
-                public void execute(ReadGraphImpl graph, final int pred) {
+                public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
                     
                     if(found.get()) return;
 
@@ -387,10 +350,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                         inc();
                         
                         // Note! The dependency is intentionally cut!
-                        DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+                        QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
 
                             @Override
-                            public void execute(ReadGraphImpl graph, int i) {
+                            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                 
                                if(found.compareAndSet(false, true)) {
                                     
@@ -408,12 +371,12 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                             }
 
                             @Override
-                            public void finished(ReadGraphImpl graph) {
+                            public void finished(ReadGraphImpl graph) throws DatabaseException {
                                 dec(graph);
                             }
                                                
                                                @Override
-                                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                        procedure.exception(graph, t);
                                 dec(graph);
                                    }
@@ -424,10 +387,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                         
                         inc();
                     
-                        SuperRelations.queryEach(graph, pred, graph.processor, entry, null, new InternalProcedure<IntSet>() {
+                        QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
     
                             @Override
-                            public void execute(ReadGraphImpl graph, IntSet result) {
+                            public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
                                 
                                 if(found.get()) {
                                     dec(graph);
@@ -439,10 +402,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                     inc();
                                     
                                     // Note! The dependency is intentionally cut!
-                                    DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {
+                                    QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
     
                                         @Override
-                                        public void execute(ReadGraphImpl graph, int i) {
+                                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                             
                                                if(found.compareAndSet(false, true)) {
                                                 
@@ -460,12 +423,12 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                         }
     
                                         @Override
-                                        public void finished(ReadGraphImpl graph) {
+                                        public void finished(ReadGraphImpl graph) throws DatabaseException {
                                             dec(graph);
                                         }
                                                        
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                                procedure.exception(graph, t);
                                             dec(graph);
                                            }
@@ -479,7 +442,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                             }
                                                
                                                @Override
-                                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                        procedure.exception(graph, t);
                                 dec(graph);
                                    }
@@ -491,10 +454,8 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                 }
 
                 @Override
-                public void finished(ReadGraphImpl graph) {
-                    
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
                     dec(graph);
-                    
                 }
                 
             });
@@ -503,12 +464,12 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
        }
     
-    final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) {
+    final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException {
 
-        PrincipalTypes.queryEach(graph, r1, graph.processor, entry, null, new SyncIntProcedure() {
+       QueryCache.runnerPrincipalTypes(graph, r1, entry, null, new SyncIntProcedure() {
             
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
                 
                 if(entry != null) entry.finish(graph, procedure);
                 else procedure.finished(graph);
@@ -518,18 +479,18 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
             TripleIntProcedure proc = new TripleIntProcedureAdapter() {
 
                 @Override
-                public void execute(ReadGraphImpl graph, int s, int p, int o) {
+                public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
                        if(entry != null) entry.addOrSet(s, p, o);
                        else procedure.execute(graph, s, p, o);
                 }
 
                 @Override
-                public void finished(ReadGraphImpl graph) {
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
                     dec(graph);
                 }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                     dec(graph);
                                        procedure.exception(graph, t);
                    }
@@ -537,21 +498,20 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
             }; 
 
             @Override
-            public void execute(ReadGraphImpl graph, int type) {
+            public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
 
                 inc();
-                
-                AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, proc);
+                QueryCache.runnerAssertedStatements(graph, type, r2, entry, null, proc);
                 
             }
             
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);       
             }
             
             @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
+            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                 dec(graph);
             }
             
@@ -560,20 +520,20 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
     }
 
-       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry,  final RelationInfo ri, final TripleIntProcedure procedure) {
+       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry,  final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
 
                if(ri.isFinal) {
 
                        graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
 
                                @Override
-                               public void execute(ReadGraphImpl graph, int i) {
+                               public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                        if(entry != null) entry.addOrSet(r1, r2, i);
                                        else procedure.execute(graph, r1, r2, i);
                                }
 
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
                                        procedure.exception(graph, t);
                                }
@@ -594,38 +554,39 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
         } else {
 
             // Note! The dependency is intentionally cut!
-            DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {
+            IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null, QueryCache.emptyProcedureDirectPredicates);
+            direct.forEach(graph, new SyncIntProcedure() {
                 
                 @Override
-                public void run(ReadGraphImpl graph) {
+                public void run(ReadGraphImpl graph) throws DatabaseException {
                     
                     forAssertions(graph, r1, r2, entry, procedure);
                     
                 }
 
                 @Override
-                public void execute(ReadGraphImpl graph, final int pred2) {
+                public void execute(ReadGraphImpl graph, final int pred2) throws DatabaseException {
 
                     if(pred2 == r2) {
                         
                         inc();
                         
                         // Note! The dependency is intentionally cut!
-                        DirectObjects.queryEach(graph, r1, pred2, graph.processor, null, null, new IntProcedure() {
+                        QueryCache.runnerDirectObjects(graph, r1, pred2, null, null, new IntProcedure() {
 
                             @Override
-                            public void execute(ReadGraphImpl graph, int i) {
+                            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                if(entry != null) entry.addOrSet(r1, pred2, i);
                                else procedure.execute(graph, r1, pred2, i);
                             }
 
                             @Override
-                            public void finished(ReadGraphImpl graph) {
+                            public void finished(ReadGraphImpl graph) throws DatabaseException {
                                 dec(graph);
                             }
                                                
                                                @Override
-                                               public void exception(ReadGraphImpl graph, Throwable t) {
+                                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                        procedure.exception(graph, t);
                                 dec(graph);
                                    }
@@ -638,28 +599,28 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                         try {
                             
-                            IntSet result = SuperRelations.queryEach2(graph, pred2, graph.processor, entry, null, null);
+                            IntSet result = QueryCache.resultSuperRelations(graph, pred2, entry, null, null);
                             if(result.contains(r2)) {
 
                                 inc();
 
                                 // Note! The dependency is intentionally cut!
-                                DirectObjects.queryEach(graph, r1, pred2, graph.processor, null, null, new IntProcedure() {
+                                QueryCache.runnerDirectObjects(graph, r1, pred2, null, null, new IntProcedure() {
 
                                     @Override
-                                    public void execute(ReadGraphImpl graph, int i) {
+                                    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                         if(entry != null) entry.addOrSet(r1, pred2, i);
                                         else procedure.execute(graph, r1, pred2, i);
 
                                     }
 
                                     @Override
-                                    public void finished(ReadGraphImpl graph) {
+                                    public void finished(ReadGraphImpl graph) throws DatabaseException {
                                         dec(graph);
                                     }
 
                                     @Override
-                                    public void exception(ReadGraphImpl graph, Throwable t) {
+                                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                         procedure.exception(graph, t);
                                         dec(graph);
                                     }
@@ -677,7 +638,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                 }
 
                 @Override
-                public void finished(ReadGraphImpl graph) {
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
                     dec(graph);
                 }
                 
@@ -688,28 +649,17 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
     }
     
     @Override
-    public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final TripleIntProcedure procedure, final boolean store) {
+    public Object compute(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
        computeForEach(graph, r1(), r2(), this, procedure);
+       return getResult();
     }
        
-    public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) {
-
-       RelationInfoQuery riEntry = RelationInfoQuery.probe(graph, r2);
-       if(riEntry != null) {
-               RelationInfo ri = riEntry.getResult();
-               graph.ensureLoaded(r1, r2);       
-               if(ri.isFunctional) {
-                       computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               } else {
-                       computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               }
-               return;
-       }
+    public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException {
        
-        RelationInfoQuery.queryEach(graph, r2, graph.processor, entry, null, new InternalProcedure<RelationInfo>() {
+       QueryCache.runnerRelationInfoQuery(graph, r2, entry, null, new InternalProcedure<RelationInfo>() {
 
             @Override
-            public void execute(ReadGraphImpl graph, final RelationInfo ri) {
+            public void execute(ReadGraphImpl graph, final RelationInfo ri) throws DatabaseException {
                
                graph.ensureLoaded(r1, r2);
                if(ri.isFunctional) {
@@ -721,7 +671,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
             }
                        
                        @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
+                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                procedure.exception(graph, t);
             }
 
@@ -734,38 +684,17 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
        return "Statements[" + r1() + " - " + r2() + "]";
     }
 
-    final private void finish(ReadGraphImpl graph, TripleIntProcedure procedure) {
+    final private void finish(ReadGraphImpl graph, TripleIntProcedure procedure) throws DatabaseException {
        
         assert(assertPending());
 
-//        ArrayList<TripleIntProcedure> p = null;
-
         synchronized(this) {
-
             setReady();
-//            p = procs;
-//            procs = null;
-
         }
 
         IntArray v = (IntArray)getResult();
         final IntArray value = (IntArray)getResult();
 
-//        if(p != null) {
-//
-//             for(TripleIntProcedure proc : p) {
-//                     for(int i=0;i<value.size();i+=3) {
-//                             proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
-//                     }
-//             }
-//             for(int i=0;i<value.size();i+=3) {
-//                     procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
-//             }
-//
-//            for(TripleIntProcedure proc : p) proc.finished(graph);
-//
-//        }
-
         for(int i=0;i<value.size();i+=3) {
                procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
         }
@@ -774,35 +703,6 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
        
     }
 
-//    final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
-//        
-//     assert(isPending());
-//
-//        ArrayList<TripleIntProcedure> p = null;
-//
-//        synchronized(this) {
-//             
-//             setReady();
-//            p = procs;
-//            procs = null; 
-//        
-//        }
-//        
-//        if(p != null) {
-//             
-//             final IntArray value = (IntArray)getResult();
-//             for(TripleIntProcedure proc : p) {
-//                     for(int i=0;i<value.size();i+=3) {
-//                             proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
-//                     }
-//             }
-//
-//             for(TripleIntProcedure proc : p) proc.finished(graph);
-//
-//        }
-//
-//    }
-    
     synchronized public void addOrSet(int s, int p, int o) {
         
        assert(assertPending());
@@ -840,7 +740,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
     }
     
     @Override
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
+    public Object performFromCache(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
 
        assert(isReady());
 
@@ -859,34 +759,20 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        final Semaphore s = new Semaphore(0);
-       
-        computeForEach(graph, provider, new TripleIntProcedureAdapter() {
+        compute(graph, new TripleIntProcedureAdapter() {
 
             @Override
             public void finished(ReadGraphImpl graph) {
-                s.release();
             }
                        
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable t) {
                                new Error("Error in recompute.", t).printStackTrace();
-                               s.release();
             }
 
-        }, true);
-        
-       while(!s.tryAcquire()) {
-               provider.resume(graph);
-       }
-        
-//        try {
-//            s.acquire();
-//        } catch (InterruptedException e) {
-//            throw new Error(e);
-//        }
+        });
         
     }
 
index f879d6037877382c273d20d38da99845c2d0aa2c..f3058edd7e7e97e958a030a32f72db1921c85c26 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.request.RequestFlags;
 
-abstract public class StringQuery<Procedure> extends CacheEntryBase implements Query {
+abstract public class StringQuery<Procedure> extends CacheEntryBase<Procedure> implements Query {
 
     final public String id;
     final public int hash;
@@ -56,21 +55,18 @@ abstract public class StringQuery<Procedure> extends CacheEntryBase implements Q
         return this;
     }
     
-    @Override
-    public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-        throw new Error("Recompute of primitive queries is not supported.");
-    }
-    
-    @Override
-    public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-       throw new Error("Not possible.");
-    }
+//    @Override
+//    public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
+//        throw new Error("Recompute of primitive queries is not supported.");
+//    }
+//    
+//    @Override
+//    public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) throws DatabaseException {
+//        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);
-    abstract public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure);
-    abstract public void putEntry(QueryProcessor provider);
+    //abstract public void recompute(ReadGraphImpl graph, QueryProcessor provider) throws DatabaseException;
+    //abstract public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure) throws DatabaseException;
     abstract public void removeEntry(QueryProcessor provider);
-    abstract public StringQuery<Procedure> getEntry(QueryProcessor provider);
        
 }
index fc3cbebab6753460c3b396831845d11b13bdf937..479bd71cd4d7bb6d48303c3c38688690678f379e 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.procedure.TIntProcedure;
-import gnu.trove.set.hash.TIntHashSet;
-
-import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.procedure.ListenerBase;
+
+import gnu.trove.procedure.TIntProcedure;
+import gnu.trove.set.hash.TIntHashSet;
 
 final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>> {
     
-    private SuperRelations(final int resource) {
+    SuperRelations(final int resource) {
         super(resource);
     }
     
     final static SuperRelations entry(final QueryProcessor provider, final int 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.cache.superRelationsMap.get(r);
-        if(entry == null) {
-
-               entry = new SuperRelations(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-               return (IntSet)provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-               
-            if(!entry.isReady()) {
-                throw new IllegalStateException();
-            }
-            return (IntSet)provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        }
-
-    }
-    
-    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.cache.superRelationsMap.get(r);
-        if(entry == null) {
-
-            entry = new SuperRelations(r);
-            entry.setPending();
-            entry.clearResult(provider.querySupport);
-            entry.putEntry(provider);
-            
-            return (IntSet)provider.performForEach2(graph, entry, parent, listener, procedure);
-            
-        } else {
-            
-            if(!entry.isReady()) {
-                throw new IllegalStateException();
-            }
-            return (IntSet)provider.performForEach2(graph, entry, parent, listener, procedure);
-            
-        }
-
-    }    
-    
-    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.cache.superRelationsMap.get(r);
-               if(entry != null && entry.isReady()) {
-                   entry.performFromCache(graph, provider, procedure);
-                   return;
-               }
-        }
-        
-        runner(graph, r, provider, parent, listener, procedure);
-         
     }
 
-    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.cache.superRelationsMap.get(r);
-            if(entry != null && entry.isReady()) {
-                return (IntSet)entry.get(graph, provider, procedure);
-            }
-        }
-        
-        return runner2(graph, r, provider, parent, listener, procedure);
-         
-    }
-    
-       @Override
-       public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.cache.superRelationsMap.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.superRelationsMap.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.cache.superRelationsMap.remove(id);
+               provider.cache.remove(this);
        }
        
        static int histoCounter = 0;
@@ -156,15 +71,17 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
     }
 
        @Override
-       public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, final boolean store) {
+       public Object compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
 
-               provider.querySupport.ensureLoaded(graph, id);
+               QueryProcessor processor = graph.processor;
+               
+               processor.querySupport.ensureLoaded(graph, id);
                
            final InternalProcedure<IntSet> proc = (InternalProcedure<IntSet>)procedure;
 
-           final int subrelationOf = provider.getSubrelationOf();
+           final int subrelationOf = processor.getSubrelationOf();
 
-           final IntSet result = new IntSet(provider.querySupport);
+           final IntSet result = new IntSet(processor.querySupport);
 
            final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure, InternalProcedure<IntSet> {
                @Override
@@ -181,9 +98,9 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
                        add(r);
                }
                @Override
-               final public void execute(ReadGraphImpl graph, IntSet set) {
+               final public void execute(ReadGraphImpl graph, IntSet set) throws DatabaseException {
                        set.forEach(this);
-                       addOrSet(graph, result, provider);
+                       addOrSet(graph, result, processor);
                        proc.execute(graph, result);
                }
                @Override
@@ -198,19 +115,19 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
 
            final DirectProcedure directProc = new DirectProcedure();
 
-           provider.querySupport.getObjects(graph, id, subrelationOf, directProc);
+          processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
 
            int size = directProc.size();
 
            if(size == 0) {
 
-               addOrSet(graph, EMPTY_SET, provider);
+               addOrSet(graph, EMPTY_SET, processor);
                proc.execute(graph, EMPTY_SET);
 
            } else if (size == 1) {
 
                result.add(directProc.single);
-               SuperRelations.queryEach(graph, directProc.single, provider, SuperRelations.this, null, directProc);
+               QueryCache.runnerSuperRelations(graph, directProc.single, SuperRelations.this, null, directProc);
 
            } else {
 
@@ -232,26 +149,35 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
 
                        @Override
                        public boolean execute(int arg0) {
+                               try {
+                                               return execute0(arg0);
+                                       } catch (DatabaseException e) {
+                                               Logger.defaultLogError(e);
+                                       }
+                               return false;
+                       }
+
+                       public boolean execute0(int arg0) throws DatabaseException {
 
                                synchronized(result) {
                                        result.add(arg0);
                                }
 
-                               SuperRelations.queryEach(graph, arg0, provider, SuperRelations.this, null, new InternalProcedure<IntSet>() {
+                               QueryCache.runnerSuperRelations(graph, arg0, SuperRelations.this, null, new InternalProcedure<IntSet>() {
 
                                        @Override
-                                       public void execute(ReadGraphImpl graph, IntSet set) {
+                                       public void execute(ReadGraphImpl graph, IntSet set) throws DatabaseException {
                                                set.forEach(addToResult);
                                                int current = finishes.addAndGet(1);
                                                if(current == directProc.size()) {
-                                                       addOrSet(graph, result, provider);
+                                                       addOrSet(graph, result, processor);
                                                        proc.execute(graph, result);
                                                        return; 
                                                }
                                        }
 
                                        @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                proc.exception(graph, t);
                                        }
 
@@ -278,29 +204,18 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
 
         assert(!isReady());
 
-//        ArrayList<InternalProcedure<IntSet>> p = null;
-
         synchronized(this) {
         
             value.trim();
             setResult(value);
             setReady();
-//            p = procs;
-//            procs = null; 
         
         }
-
-//        if(p != null) {
-//             IntSet v = (IntSet)getResult();
-//             if(v != null) {
-//                 for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
-//             }
-//        }
         
     }
     
     @Override
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
+    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
               
         assert(isReady());
 
@@ -315,31 +230,22 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
     }
 
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        final Semaphore s = new Semaphore(0);
-
-        computeForEach(graph, provider, new InternalProcedure<IntSet>() {
+        compute(graph, new InternalProcedure<IntSet>() {
 
                @Override
                public void execute(ReadGraphImpl graph, IntSet result) {
-                s.release();
                }
 
             @Override
             public void exception(ReadGraphImpl graph, Throwable t) {
-               s.release();
                 new Error("Error in recompute.", t).printStackTrace();
             }
 
-        }, true);
-
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+        });
         
     }
-    
 
     @Override
     boolean isImmutable(ReadGraphImpl graph) {
index 1fce568707564691fe7420ef072af0c8f0b7924e..06fabb2d7380c73a803948912bd899727e88f247 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.procedure.TIntProcedure;
-
-import java.util.concurrent.Semaphore;
-
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.ListenerBase;
 
+import gnu.trove.procedure.TIntProcedure;
+
 final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
        
-    private SuperTypes(final int resource) {
+    SuperTypes(final int resource) {
         super(resource);
     }
     
-    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.cache.superTypesMap.get(r);
-        if(entry == null) {
-               
-               entry = new SuperTypes(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-            return entry;
-            
-        } else {
-               
-            if(!entry.isReady()) {
-               synchronized(entry) {
-                    if(!entry.isReady()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<IntSet>>();
-//                     entry.procs.add(procedure);
-//                                             provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                     return entry;
-                    }
-               }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-        
-        return entry;
-
-    }
-    
-    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.cache.superTypesMap.get(r);
-               if(entry != null && entry.isReady()) { 
-                       entry.performFromCache(graph, provider, procedure);
-                       return entry;
-               }
-        }
-        
-        return runner(graph, r, parent, provider, listener, procedure);
-         
-    }
+//    final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+//        return QueryCache.runnerSuperTypes(graph, r, parent, listener, procedure);
+//    }
        
        @Override
-       public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.cache.superTypesMap.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.superTypesMap.put(id, this);
+       final public void removeEntry(QueryProcessor provider) {
+               provider.cache.remove(this);
        }
 
        @Override
-       final public void removeEntry(QueryProcessor provider) {
-               provider.cache.superTypesMap.remove(id);
+       public Object compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+               return computeForEach(graph, id, this, procedure);
+       }
+       
+       private static void addOrSet(ReadGraphImpl graph, SuperTypes entry, IntSet value) {
+               if(entry != null) {
+                       entry.addOrSet(graph, value, graph.processor);
+               }
        }
+       
+       public static Object computeForEach(ReadGraphImpl graph, int r, SuperTypes entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
 
-       @Override
-       public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {
+               QueryProcessor provider = graph.processor;
 
         final int inherits = provider.getInherits();
-        
-        final CacheEntry parent = graph.parent;
-        
-        assert(graph.parent != this);
 
         final IntSet result = new IntSet(provider.querySupport);
         
@@ -109,20 +62,20 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
             }
         };
 
-        DirectObjects.queryEach(graph, id, inherits, provider, this, null, new SyncIntProcedure() {
+        QueryCache.runnerDirectObjects(graph, r, inherits, entry, null, new SyncIntProcedure() {
             
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
 
-                addOrSet(graph, result, provider);
+                addOrSet(graph, entry, result);
                 procedure.execute(graph, result);
                 
             }
             
             @Override
-            public void execute(ReadGraphImpl graph, final int i) {
+            public void execute(ReadGraphImpl graph, final int i) throws DatabaseException {
                 
-               assert(graph.parent == parent);
+//             assert(graph.parent == parent);
                
                synchronized(result) {
                        result.add(i);
@@ -130,10 +83,10 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
                 
                 inc();
 
-                SuperTypes.queryEach(graph, i, provider, SuperTypes.this, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, IntSet types) {
+                    public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
 
                         types.forEach(addToResult);
                         dec(graph);
@@ -141,7 +94,7 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        procedure.exception(graph, t);
                    }
 
@@ -150,7 +103,7 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
             
@@ -169,29 +122,18 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
        
         assert(!isReady());
 
-//        ArrayList<InternalProcedure<IntSet>> p = null;
-
         synchronized(this) {
         
             value.trim();
             setResult(value);
             setReady();
-//            p = procs;
-//            procs = null; 
         
         }
-
-//        if(p != null) {
-//             IntSet v = (IntSet)getResult();
-//             if(v != null) {
-//                 for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
-//             }
-//        }
         
     }
 
     @Override
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
+    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
        
        assert(isReady());
 
@@ -206,28 +148,20 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-        computeForEach(graph, provider, new InternalProcedure<IntSet>() {
+        compute(graph, new InternalProcedure<IntSet>() {
 
                @Override
                public void execute(ReadGraphImpl graph, IntSet result) {
-                s.release();
                }
 
             @Override
             public void exception(ReadGraphImpl graph, Throwable t) {
-               s.release();
                 new Error("Error in recompute.", t).printStackTrace();
             }
 
-        }, true);
-
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+        });
         
     }
     
index f79910358e1706770f8fb033dcbb39e68de9aeb1..42b0d7a407c2e48d8d38bdcf6f0fe1f92546de3a 100644 (file)
@@ -11,6 +11,7 @@
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
 
@@ -29,7 +30,7 @@ abstract public class SyncIntProcedure implements IntProcedure, ThreadRunnable {
 //     barrier.incrementAndGet();
     }
     
-    final protected void dec(ReadGraphImpl graph) {
+    final protected void dec(ReadGraphImpl graph) throws DatabaseException {
 //     System.err.println("dec " + barrier.get());
        if((--barrier) == 0) {
             run(graph);
@@ -37,7 +38,7 @@ abstract public class SyncIntProcedure implements IntProcedure, ThreadRunnable {
     }
     
     @Override
-    public void exception(ReadGraphImpl graph, Throwable t) {
+    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
        throw new Error("Errors are not supported,", t);
     }
     
index 779495484cdcc87a8620403970f8eba39cbe985f..8466578a45d4646ed88ce6cf0641ccdfec5e576c 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
 public interface ThreadRunnable {
 
-    void run(ReadGraphImpl graph);
+    void run(ReadGraphImpl graph) throws DatabaseException;
     
 }
index 341dd48f9bf5b37a71e409e6ef7019389fa7477c..e5636717789e4bc1f254208a23a7f2737afcaa2b 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
 
 public interface TripleIntProcedure {
 
-    void execute(ReadGraphImpl graph, int s, int p, int o);
-    void finished(ReadGraphImpl graph);
-    void exception(ReadGraphImpl graph, Throwable throwable);
+    void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException ;
+    void finished(ReadGraphImpl graph) throws DatabaseException ;
+    void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException;
     
 }
index 6a96e11eaf65f857789740f77caa5e22ec48de91..9da45946999098752d3a5322f1e13eb66874b046 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.procedure.TIntProcedure;
-
-import java.util.concurrent.Semaphore;
-
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.ListenerBase;
 
+import gnu.trove.procedure.TIntProcedure;
+
 final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
     
-//     public ArrayList<InternalProcedure<IntSet>> procs = null;
-       
-    private TypeHierarchy(final int resource) {
+    TypeHierarchy(final int resource) {
         super(resource);
     }
     
-    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.cache.typeHierarchyMap.get(r);
-        if(entry == null) {
-
-               entry = new TypeHierarchy(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-               
-            if(!entry.isReady()) {
-               synchronized(entry) {
-                    if(!entry.isReady()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<IntSet>>();
-//                     entry.procs.add(procedure);
-//                                             provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                     return;
-                    }
-               }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-
-    }
-    
-    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.cache.typeHierarchyMap.get(r);
-               if(entry != null && entry.isReady()) { 
-                       entry.performFromCache(graph, provider, procedure);
-                       return;
-               }
-        }
-        
-        runner(graph, r, parent, provider, listener, procedure);
-         
+    final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+        QueryCache.runnerTypeHierarchy(graph, r, parent, listener, procedure);
     }
-       
-       @Override
-       public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.cache.typeHierarchyMap.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.typeHierarchyMap.put(id, this);
-       }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.cache.typeHierarchyMap.remove(id);
+               provider.cache.remove(this);
        }
 
        @Override
-       public IntSet computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {
+       public IntSet compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
 
-        final IntSet result = new IntSet(provider.querySupport, id);
+               QueryProcessor processor = graph.processor;
+               
+        final IntSet result = new IntSet(processor.querySupport, id);
         
         final TIntProcedure addToResult = new TIntProcedure() {
             @Override
@@ -99,19 +48,19 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
             }
         };
         
-        SuperTypes.queryEach(graph, id, provider, TypeHierarchy.this, null, new InternalProcedure<IntSet>() {
+        QueryCache.runnerSuperTypes(graph, id, TypeHierarchy.this, null, new InternalProcedure<IntSet>() {
 
                @Override
-               public void execute(ReadGraphImpl graph, IntSet types) {
+               public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
 
                        types.forEach(addToResult);
-                       addOrSet(graph, result, provider);
+                       addOrSet(graph, result, processor);
                        procedure.execute(graph, result); 
 
                }
 
                @Override
-               public void exception(ReadGraphImpl graph, Throwable t) {
+               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                        procedure.exception(graph, t);
                }
 
@@ -130,29 +79,18 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
        
         assert(!isReady());
 
-//        ArrayList<InternalProcedure<IntSet>> p = null;
-
         synchronized(this) {
         
             value.trim();
             setResult(value);
             setReady();
-//            p = procs;
-//            procs = null; 
         
         }
-
-//        if(p != null) {
-//             IntSet v = (IntSet)getResult();
-//             if(v != null) {
-//                 for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
-//             }
-//        }
         
     }
 
     @Override
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
+    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
        
        assert(isReady());
 
@@ -167,33 +105,26 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
-
-        computeForEach(graph, provider, new InternalProcedure<IntSet>() {
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
+    
+        compute(graph, new InternalProcedure<IntSet>() {
 
                @Override
                public void execute(ReadGraphImpl graph, IntSet result) {
-                s.release();
                }
 
             @Override
             public void exception(ReadGraphImpl graph, Throwable t) {
-               s.release();
                 new Error("Error in recompute.", t).printStackTrace();
             }
 
-        }, true);
-
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+        });
         
     }
     
     @Override
     boolean isImmutable(ReadGraphImpl graph) {
        return graph.processor.isImmutable(id);
-    }    
+    }
+
 }
index 0f49188506099bfb4a13c64faffb05daea1c02c3..96afa8c4f586bd8fb83135e4eca2c4803542c3f8 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.procedure.TIntProcedure;
-
-import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.procedure.ListenerBase;
+
+import gnu.trove.procedure.TIntProcedure;
 
 final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
-    
-//     public ArrayList<InternalProcedure<IntSet>> procs;
        
-    private Types(final int resource) {
+    Types(final int resource) {
         super(resource);
     }
-    
-    final static Types entry(final QueryProcessor provider, final int 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.cache.typesMap.get(r); 
-        if(entry == null) {
-               
-               entry = new Types(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-
-               if(!entry.isReady()) {
-                   throw new IllegalStateException();
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-       
-    }
-    
-    final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
-
-       Types entry = (Types)provider.cache.typesMap.get(r); 
-        if(entry == null) {
-               
-               entry = new Types(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
-            
-        } else {
-
-               if(!entry.isReady()) {
-                   throw new IllegalStateException();
-            }
-            return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
-            
-        }
-       
-    }
-    
-    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.cache.typesMap.get(r);
-       
-       if(parent == null && listener == null) {
-               if(entry != null && entry.isReady()) { 
-                       entry.performFromCache(graph, provider, procedure);
-                       return;
-               }
-        }
-       
-        runner(graph, r, provider, entry, parent, listener, procedure);
-         
-    }
 
-    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.cache.typesMap.get(r);
-            if(entry != null && entry.isReady()) {
-                return (IntSet)entry.get(graph, provider, null);
-            }
-        }
-        
-        return runner2(graph, r, provider, parent);
-         
-    }
-    
        @Override
-       public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.cache.typesMap.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.typesMap.put(id, this);
+       final public void removeEntry(QueryProcessor provider) {
+               provider.cache.remove(this);
        }
 
        @Override
-       final public void removeEntry(QueryProcessor provider) {
-               provider.cache.typesMap.remove(id);
+       public Object compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+               computeForEach(graph, id, this, procedure);
+               return getResult();
        }
 
-       @Override
-       public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor queryProvider, final InternalProcedure<IntSet> procedure, final boolean store) {
+       public static void computeForEach(final ReadGraphImpl graph, int id, Types entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+
+               if(entry != null)
+                       if(entry.isReady())
+                               System.err.println("asd");
+               
+               assert(procedure != null);
 
-               queryProvider.querySupport.ensureLoaded(graph, id);
-               int ret = queryProvider.querySupport.getSingleInstance(id);
+               QueryProcessor processor = graph.processor;
+
+               processor.querySupport.ensureLoaded(graph, id);
+               
+               int ret = processor.querySupport.getSingleInstance(id);
                if(ret > 0) {
 
-                       TypeHierarchy.queryEach(graph, ret, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+                       TypeHierarchy.queryEach(graph, ret, processor, entry, null, new InternalProcedure<IntSet>() {
 
                                @Override
-                               public void execute(ReadGraphImpl graph, IntSet types) {
+                               public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
 
-                                       addOrSet(graph, types, queryProvider);
+                                       if(entry != null) entry.addOrSet(graph, types, processor);
                                        procedure.execute(graph, types);
 
                                }
 
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        procedure.exception(graph, t);
                                }
 
                        });
 
-                       return getResult();
-
                }
                
-               final int instanceOf = queryProvider.getInstanceOf();
-        final int inherits = queryProvider.getInherits();
-        final int subrelationOf = queryProvider.getSubrelationOf();
+               final int instanceOf = processor.getInstanceOf();
+        final int inherits = processor.getInherits();
+        final int subrelationOf = processor.getSubrelationOf();
 
-        final IntSet result = new IntSet(queryProvider.querySupport);
+        final IntSet result = new IntSet(processor.querySupport);
         
         final TIntProcedure addToResult = new TIntProcedure() {
             @Override
@@ -169,17 +91,17 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
         SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
             
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
                
                 if(finishes.addAndGet(1) == 3) {
-                    if(store) addOrSet(graph, result, queryProvider);
+                    if(entry != null) entry.addOrSet(graph, result, processor);
                     procedure.execute(graph, result);
                }
                 
             }
             
             @Override
-            public void execute(ReadGraphImpl graph, int i) {
+            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                 
                synchronized(result) {
                        result.add(i);
@@ -187,16 +109,16 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
                 
                 inc();
 
-                SuperTypes.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, IntSet types) {
+                    public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
                         types.forEach(addToResult);
                         dec(graph);
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        procedure.exception(graph, t);
                         dec(graph);
                    }
@@ -206,7 +128,7 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
             
@@ -215,31 +137,31 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
         SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
             
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
 
                int current = finishes.addAndGet(1);
                if(current == 3) {
-                    if(store) addOrSet(graph, result, queryProvider);
+                    if(entry != null) entry.addOrSet(graph, result, processor);
                     procedure.execute(graph, result);
                }
                 
             }
             
             @Override
-            public void execute(ReadGraphImpl graph, int i) {
+            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                 
                 inc();
 
-                Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, IntSet types) {
+                    public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
                         types.forEach(addToResult);
                         dec(graph);
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        procedure.exception(graph, t);
                         dec(graph);
                    }
@@ -249,7 +171,7 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
 
                 dec(graph);
 
@@ -260,25 +182,25 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
         SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
 
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
 
                int current = finishes.addAndGet(1);
                if(current == 3) {
-                    if(store) addOrSet(graph, result, queryProvider);
+                    if(entry != null) entry.addOrSet(graph, result, processor);
                     procedure.execute(graph, result);
                }
                 
             }
             
             @Override
-            public void execute(ReadGraphImpl graph, int i) {
+            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                 
                 inc();
                 
-                Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, IntSet types) {
+                    public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
 
                         types.forEach(addToResult);
                         dec(graph);
@@ -286,7 +208,7 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        procedure.exception(graph, t);
                         dec(graph);
                    }
@@ -296,7 +218,7 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 
                 dec(graph);
 
@@ -304,14 +226,14 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
             
         }; 
         
-        queryProvider.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
+        processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
         instanceOfProcedure.finished(graph);
-        queryProvider.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
+        processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
         inheritsProcedure.finished(graph);
-        queryProvider.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
+        processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
         subrelationOfProcedure.finished(graph);
         
-        return result;
+        if(entry != null) entry.finish();
         
     }
     
@@ -324,18 +246,20 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
        
         assert(!isReady());
 
-        synchronized(this) {
-        
-            value.trim();
-            setResult(value);
-            setReady();
-        
-        }
+        setResult(value);
         
     }
     
+    void finish() {
+
+       IntSet result = getResult(); 
+       result.trim();
+       setReady();
+       
+    }
+    
     @Override
-    final public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
+    final public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
         
        assert(isReady());
        
@@ -350,28 +274,20 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-        computeForEach(graph, provider, new InternalProcedure<IntSet>() {
+        compute(graph, new InternalProcedure<IntSet>() {
 
                @Override
                public void execute(ReadGraphImpl graph, IntSet result) {
-                s.release();
                }
 
             @Override
             public void exception(ReadGraphImpl graph, Throwable t) {
-               s.release();
                 new Error("Error in recompute.", t).printStackTrace();
             }
 
-        }, true);
-
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+        });
         
     }
 
index 1098ed31398bb1ce8a88680416f1179ff10eec52..5a44fc2eb608a273139501ac827a264265f1f5d3 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.map.hash.TObjectIntHashMap;
-
-import java.util.concurrent.Semaphore;
-
 import org.simantics.databoard.util.URIStringUtils;
 import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.ListenerBase;
 
+import gnu.trove.map.hash.TObjectIntHashMap;
+
 public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
 
-//    public ArrayList<InternalProcedure<Integer>> procs = null;
-    
-    private URIToResource(final String id) {
+    URIToResource(final String id) {
         super(id);
     }
-    
-    final static URIToResource entry(final QueryProcessor provider, final String 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.cache.uriToResourceMap.get(id); 
-        if(entry == null) {
-
-               entry = new URIToResource(id);
-               entry.setPending();
-               entry.clearResult(processor.querySupport);
-               entry.putEntry(processor);
-
-               processor.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-               
-            if(entry.isPending()) {
-                synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<Integer>>();
-//                        entry.procs.add(procedure);
-//                        processor.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                        return;
-                    }
-                }
-            }
-            
-            processor.performForEach(graph, entry, parent, listener, procedure);
-            
-        }
-        
-    }
-    
-    final public static void queryEach(ReadGraphImpl graph, final String id, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {
-        
-        assert(id != null);
-        
-        if(graph.parent == null && listener == null) {
-               URIToResource.computeForEach(graph, id, null, procedure);
-        } else {
-            runner(graph, id, parent, listener, procedure);
-        }
-         
-    }
-     
-    @Override
-    public URIToResource getEntry(QueryProcessor provider) {
-        return provider.cache.uriToResourceMap.get(id);
-    }
-    
-    @Override
-    public void putEntry(QueryProcessor provider) {
-        provider.cache.uriToResourceMap.put(id, this);
-    }
 
     @Override
     final public void removeEntry(QueryProcessor provider) {
-        provider.cache.uriToResourceMap.remove(id);
-    }
-
-    private void lookup(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure, final String namespace, final String name) {
-        
-        NamespaceIndex.queryEach(graph, namespace, processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {
-
-            @Override
-            public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
-
-                if(index != null) {
-                    int result = index.get(name);
-                    if(result != 0) {
-                        addOrSet(graph, processor, result);
-                        procedure.execute(graph, result);
-                        return;
-                    }
-                }
-                
-                addOrSet(graph, processor, new Integer(0));
-                procedure.execute(graph, new Integer(0));
-
-            }
-
-            @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
-                except(t);
-                procedure.exception(graph, t);
-            }
-
-        });
-
+       provider.cache.remove(this);
     }
 
-    private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure<Integer> procedure, final String namespace, final String name) {
+    private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure<Integer> procedure, final String namespace, final String name) throws DatabaseException {
         
-        NamespaceIndex.queryEach(graph, namespace, graph.processor, entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {
+       QueryCache.runnerNamespaceIndex(graph, namespace, entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {
 
             @Override
-            public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
+            public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) throws DatabaseException {
 
                 if(index != null) {
                     int result = index.get(name);
@@ -148,7 +54,7 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
             }
 
             @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
+            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                 if(entry != null) entry.except(t);
                 procedure.exception(graph, t);
             }
@@ -158,29 +64,12 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
     }
     
     @Override
-    public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure) {
-        
-//     new Exception("URIToResource " + id).printStackTrace();
-       
-        if("http://".equals(id) || "http:/".equals(id)) {
-            
-            addOrSet(graph, processor, processor.getRootLibrary());
-            procedure.execute(graph, processor.getRootLibrary());
-
-        } else {
-            
-            final String[] parts = URIStringUtils.splitURI(id);
-            if (parts != null) {
-                lookup(graph, processor, procedure, parts[0], parts[1]);
-            } else {
-                lookup(graph, processor, procedure, "http://", id.replaceFirst("http://", ""));
-            }
-
-        }
-        
+    public Object compute(ReadGraphImpl graph, final InternalProcedure<Integer> procedure) throws DatabaseException {
+       computeForEach(graph, id, this, procedure);
+        return getResult();
     }
 
-    final private static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) {
+    static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) throws DatabaseException {
         
         if("http://".equals(id) || "http:/".equals(id)) {
             
@@ -205,19 +94,10 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
 
         assert(isPending());
 
-//        ArrayList<InternalProcedure<Integer>> p = null;
-
         synchronized(this) {
-
             setResult(result);
             setReady();
-//            p = procs;
-//            procs = null;
-            
         }
-
-//        if(p != null)
-//             for(InternalProcedure<Integer> proc : p) proc.execute(graph, result);
         
     }
     
@@ -227,30 +107,25 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
     }
 
     @Override
-    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<Integer> procedure) {
+    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<Integer> procedure) throws DatabaseException {
         
         assert(isReady());
         
-       if(handleException(graph, procedure)) return;
-        
-        if(isExcepted()) {
-               procedure.exception(graph, (Throwable)statusOrException);
-        } else {
-            procedure.execute(graph, (Integer)getResult());
-        }
+       if(handleException(graph, procedure)) return (Throwable)statusOrException;
+
+       Integer result = (Integer)getResult();
+       procedure.execute(graph, result);
+       return result;
         
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        final Semaphore s = new Semaphore(0);
-        
-        computeForEach(graph, provider, new InternalProcedure<Integer>() {
+        compute(graph, new InternalProcedure<Integer>() {
 
             @Override
             public void execute(ReadGraphImpl graph, Integer result) {
-                s.release();
             }
             
             @Override
@@ -261,10 +136,6 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
 
         });
         
-       while(!s.tryAcquire()) {
-               provider.resume(graph);
-       }
-        
     }
        
 }
index ef60150702103b1f4532cab1e40443b6cb30fc36..5588af20057d35b720932bd0cb036a21e640f5f4 100644 (file)
@@ -15,7 +15,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.request.RequestFlags;
 
-abstract public class UnaryQuery<Procedure> extends CacheEntryBase implements Query {
+abstract public class UnaryQuery<Procedure> extends CacheEntryBase<Procedure> implements Query {
        
     final public int id;
 
@@ -52,26 +52,12 @@ abstract public class UnaryQuery<Procedure> extends CacheEntryBase implements Qu
         return this;
     }
     
-    @Override
-    public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-        recompute(graph, (QueryProcessor)provider);
-    }
-    
-    @Override
-    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 Object computeForEach(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure, boolean store);
-    abstract public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure);
-    abstract public void putEntry(QueryProcessor provider);
+//    abstract public void recompute(ReadGraphImpl graph) throws DatabaseException;
     abstract public void removeEntry(QueryProcessor provider);
-    abstract public UnaryQuery<Procedure> getEntry(QueryProcessor provider);
        
-    public Object get(ReadGraphImpl graph, QueryProcessor processor, Object procedure) throws Throwable {
+    public Object get(ReadGraphImpl graph, Procedure procedure) throws Throwable {
         if(procedure != null) {
-            performFromCache(graph, processor, procedure);
+            performFromCache(graph, procedure);
         } else {
             checkAndThrow();
         }
index f259090aa690448dd9ee604f87b1eef6b49d8081..fd1035a78eed6bf5722830f290536c82f6b39ba5 100644 (file)
@@ -15,6 +15,7 @@ import gnu.trove.impl.hash.THash;
 
 import java.lang.reflect.Array;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
 
@@ -35,39 +36,29 @@ abstract public class UnaryQueryHash<Procedure> extends THash {
     protected final UnaryQuery<Procedure> REMOVED = new UnaryQuery<Procedure>(-1) {
 
         @Override
-        public Object computeForEach(ReadGraphImpl graph, QueryProcessor provider, Object procedure, boolean store) {
-            throw new Error("Not possible.");
-        }
-
-        @Override
-        public UnaryQuery<Procedure> getEntry(QueryProcessor provider) {
-            throw new Error("Not possible.");
-        }
-
-        @Override
-        public void putEntry(QueryProcessor provider) {
+        public void removeEntry(QueryProcessor provider) {
             throw new Error("Not possible.");
         }
 
         @Override
-        public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure) {
+        public int type() {
             throw new Error("Not possible.");
         }
 
-        @Override
-        public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+               @Override
+               public void recompute(ReadGraphImpl graph) throws DatabaseException {
             throw new Error("Not possible.");
-        }
+               }
 
-        @Override
-        public void removeEntry(QueryProcessor provider) {
+               @Override
+               public Object compute(ReadGraphImpl graph, Procedure procedure) throws DatabaseException {
             throw new Error("Not possible.");
-        }
+               }
 
-        @Override
-        public int type() {
+               @Override
+               Object performFromCache(ReadGraphImpl graph, Procedure procedure) throws DatabaseException {
             throw new Error("Not possible.");
-        }
+               }
         
     };
 
index 770a25cb93abc0e3fa75b2004efabc396da0fed2..1521d31936d8e3946addd015b62ac9ce68b77cbe 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.concurrent.Semaphore;
-
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.procedure.ListenerBase;
 
 final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {
     
-    private ValueQuery(final int resource) {
+    ValueQuery(final int resource) {
         super(resource);
     }
-    
-    final static ValueQuery entry(final QueryProcessor provider, final int 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.cache.valueMap.get(r); 
-        if(entry == null) {
-               
-               entry = new ValueQuery(r);
-               entry.setPending();
-               entry.clearResult(processor.querySupport);
-               entry.putEntry(processor);
-               
-               return (byte[])processor.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-               
-               return (byte[])processor.performForEach(graph, entry, parent, listener, procedure);
-            
-        }
-
-    }
-    
-    final public static byte[] queryEach(ReadGraphImpl graph, final int r, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<byte[]> procedure) {
-        
-       assert(r != 0);
-       
-       if(graph.parent == null && listener == null) {
-               return ValueQuery.computeForEach(graph, r, null, procedure);
-        } else {
-               return runner(graph, r, parent, listener, procedure);
-        }
-         
-    }
-
-    final public static byte[] queryEach(ReadGraphImpl graph, final int r, final CacheEntry parent) {
-        
-        assert(r != 0);
-        
-        if(graph.parent == null) {
-            return ValueQuery.computeForEach(graph, r);
-        } else {
-            return runner(graph, r, parent, null, null);
-        }
-         
-    }
-    
-       @Override
-       public UnaryQuery<InternalProcedure<byte[]>> getEntry(QueryProcessor provider) {
-        return provider.cache.valueMap.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.cache.valueMap.put(id, this);
-       }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.cache.valueMap.remove(id);
+               provider.cache.remove(this);
        }
-       
                
-       public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure<byte[]> procedure) {
+       public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure<byte[]> procedure) throws DatabaseException {
 
                graph.ensureLoaded(r);
                
@@ -115,7 +52,7 @@ final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {
     }
        
        @Override
-       public Object computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final InternalProcedure<byte[]> procedure, final boolean store) {
+       public Object compute(ReadGraphImpl graph, final InternalProcedure<byte[]> procedure) throws DatabaseException {
                return computeForEach(graph, id, this, procedure);
     }
     
@@ -125,20 +62,17 @@ final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {
     }
 
     @Override
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor queryProvider, InternalProcedure<byte[]> procedure) {
-       return computeForEach(graph, queryProvider, procedure, false);
+    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<byte[]> procedure) throws DatabaseException {
+       return compute(graph, procedure);
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-
-       final Semaphore s = new Semaphore(0);
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
        
-        computeForEach(graph, provider, new InternalProcedure<byte[]>() {
+        compute(graph,  new InternalProcedure<byte[]>() {
 
             @Override
             public void execute(ReadGraphImpl graph, byte[] result) {
-               s.release();
             }
                        
                        @Override
@@ -146,17 +80,13 @@ final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {
                                throw new Error("Error in recompute.", t);
             }
 
-        }, true);
-        
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+        });
         
     }
     
     @Override
     boolean isImmutable(ReadGraphImpl graph) {
        return graph.processor.isImmutable(id);
-    }    
+    }
     
 }
index 3925740900f5902e4e7849d371d333945dff52aa..2974c90258fa87dd2a94b12a40df828f223e2e73 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.cache.asyncReadMap.get(request);
+        CacheEntryBase<?> entry = session.queryProvider2.cache.entryAsyncRead(session.queryProvider2, 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.cache.asyncMultiReadMap.get(request);
+        CacheEntryBase<?> entry = session.queryProvider2.cache.entryAsyncMultiRead(session.queryProvider2, 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.cache.readMap.get(request);
+        CacheEntryBase<?> entry = session.queryProvider2.cache.entryRead(session.queryProvider2, 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.cache.multiReadMap.get(request);
+        CacheEntryBase<?> entry = session.queryProvider2.cache.entryMultiRead(session.queryProvider2, request);
         if(entry != null) {
             for(CacheEntry parent : entry.getParents(session.queryProvider2)) result.add(parent);
         }
index 190db364cbdf8bda86c56cc2c4f427990200c352..b3b403ea7175cdf94bb98f15d58d87e9ca53e572 100644 (file)
@@ -1,7 +1,5 @@
 package fi.vtt.simantics.procore.internal;
 
-import gnu.trove.set.hash.TIntHashSet;
-
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.util.Collection;
@@ -36,6 +34,8 @@ import org.simantics.db.procore.cluster.ClusterSmall;
 import org.simantics.db.service.SerialisationSupport;
 import org.simantics.utils.DataContainer;
 
+import gnu.trove.set.hash.TIntHashSet;
+
 public class QuerySupportImpl implements QuerySupport {
        
        final SessionImplSocket session;
@@ -435,178 +435,8 @@ public class QuerySupportImpl implements QuerySupport {
        
     }
 
-//     @Override
-//    public void getSingleSuperrelation(ReadGraphImpl graph, final int subject, final AsyncProcedure<Resource> procedure) {
-//     
-//     // Do not process this information for virtual resources
-//     if(subject < 0) {
-//             procedure.execute(graph, null);
-//                     graph.state.barrier.dec();
-//             return;
-//     }
-//
-//     final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
-//        if (cluster == null)
-//            System.out.println("null cluster: " + Integer.toString(subject, 16));
-//        
-//        assert (cluster != null);
-//
-//        if(!cluster.isLoaded()) {
-//            
-//             procedure.execute(graph, null);
-//                     graph.state.barrier.dec();
-//             return;
-//
-////           queryProvider2.requestCluster(callerThread, cluster.getClusterId(), new Callback<Integer>() {
-////   
-////                           @Override
-////                           public void run(Integer i) {
-////   
-////                                   queryProvider2.schedule(i, callerThread, new Runnable() {
-////   
-////                                           @Override
-////                                           public void run() {
-////                                                   
-////                                                   try  {
-////                                                   
-////                                               ClusterI.CompleteTypeEnum type = cluster.getCompleteType(callerThread, subject, SessionImplSocket.this);
-////                                               if(ClusterI.CompleteTypeEnum.SubrelationOf == type) {
-////                                                   int result = cluster.getCompleteObjectKey(callerThread, subject, SessionImplSocket.this);
-////                                                   assert(result > 0);
-////                                                   procedure.execute(graph, getResourceByKey(result));
-////                                               } else {
-////                                                   procedure.execute(graph, null);
-////                                               }
-////                                                           graph.state.barrier.dec();
-////                                                           
-////                                                   } catch (DatabaseException e) {
-////                                                           e.printStackTrace();
-////                                                   }
-////                                                   
-////                                           }
-////   
-////                                   });
-////   
-////                           }
-////   
-////                   });
-//                     
-//        } else {
-//
-//             try {
-//
-//                     ClusterI.CompleteTypeEnum type = cluster.getCompleteType(graph.callerThread, subject, clusterSupport);
-//                     if(ClusterI.CompleteTypeEnum.SubrelationOf == type) {
-//                             int result = cluster.getCompleteObjectKey(graph.callerThread, subject, clusterSupport);
-//                             assert(result > 0);
-//                             procedure.execute(graph, new ResourceImpl(resourceSupport, result));
-//                     } else {
-//                             procedure.execute(graph, null);
-//                     }
-//                     graph.state.barrier.dec();
-//
-//             } catch (DatabaseException e) {
-//                     e.printStackTrace();
-//             }
-//                     
-//        }
-//        
-//        
-//    }
-
-//     @Override
-//    public void getObjects2(final int callerThread, final int subject, final int predicate, final IntProcedure procedure) {
-//             ensureLoaded(callerThread, subject, predicate, new Runnable() {
-//
-//                     @Override
-//                     public void run() {
-//                             safeGetObjects2(callerThread, subject, predicate, procedure);
-//                     }
-//                     
-//             });
-//     }
-
-//    public void safeGetObjects2(final ReadGraphImpl graph, final int subject, final int predicate, final IntProcedure procedure) {
-//
-//        assert (subject != 0);
-//        assert (predicate != 0);
-////        System.out.println("getObjects2: s=" + subject + "p=" + predicate);
-//        Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(subject);
-//        if (providers != null) {
-//
-//            final TIntHashSet result = new TIntHashSet(16);
-//
-//            for (VirtualGraph provider : providers) {
-//
-//                for (int id : ((VirtualGraphImpl)provider).getObjects(subject, predicate)) {
-//
-//                    if (result.add(id)) {
-//                     procedure.execute(graph, id);
-//                    }
-//
-//                }
-//
-//            }
-//
-//            if (subject < 0)
-//                return;
-//
-//            final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
-//
-//            assert (testCluster(subject, cluster));
-//            
-//            // wheels within wheels
-//            final ClusterI.ObjectProcedure<Object> proc = new ClusterI.ObjectProcedure<Object>() {
-//
-//                @Override
-//                public boolean execute(int callerThread, Object context, int object) {
-//
-//                    if (result.add(object)) {
-//                     procedure.execute(graph.newAsync(callerThread), object);
-//                    }
-//
-//                    return false; // continue looping
-//
-//                }
-//                
-//                             @Override
-//                             public boolean found() {
-//                                     throw new UnsupportedOperationException();
-//                             }
-//                
-//            };
-//
-//            try {
-//                cluster.forObjects(graph.callerThread, subject, predicate, proc, null, clusterSupport);
-//            } catch (DatabaseException e) {
-//                Logger.defaultLogError(e);
-//            } catch (Throwable t) {
-//                Logger.defaultLogError(t);
-//             t.printStackTrace();
-//            }
-//            return;
-//            
-//        }
-//        
-//        assert(subject > 0);
-//
-//        final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
-//
-//        assert (testCluster(subject, cluster));
-//        
-//        try {
-//            cluster.forObjects(graph.callerThread, subject, predicate, new Wheels(procedure), null, clusterSupport);
-//        } catch (DatabaseException e) {
-//            Logger.defaultLogError(e);
-//        } catch (Throwable t) {
-//             t.printStackTrace();
-//            Logger.defaultLogError(t);
-//        }
-//        
-//    }
-
        @Override
-    public boolean getObjects(final ReadGraphImpl graph, final int subject, final int predicate, final IntProcedure procedure) {
+    public boolean getObjects(final ReadGraphImpl graph, final int subject, final int predicate, final IntProcedure procedure) throws DatabaseException {
                
                assert (subject != 0);
                assert (predicate != 0);
@@ -656,7 +486,11 @@ public class QuerySupportImpl implements QuerySupport {
 
                                found.set(true);
                                if (result.add(object)) {
-                                       procedure.execute(graph, object);
+                                       try {
+                                                       procedure.execute(graph, object);
+                                               } catch (DatabaseException e) {
+                                                       Logger.defaultLogError(e);
+                                               }
                                }
 
                                return false; // continue looping
@@ -686,8 +520,11 @@ public class QuerySupportImpl implements QuerySupport {
                        public boolean execute(Object context, int object) {
 
                                found = true;
-                               procedure.execute(graph, object);
-
+                               try {
+                                               procedure.execute(graph, object);
+                                       } catch (DatabaseException e) {
+                                               Logger.defaultLogError(e);
+                                       }
                                return false; // continue looping
 
                        }
@@ -1041,15 +878,8 @@ public class QuerySupportImpl implements QuerySupport {
 
     }
 
-//    @Override
-//    public void getStatements(ReadGraphImpl graph, final int subject, final Procedure<DirectStatements> procedure) {
-//     
-//             procedure.exception(new DatabaseException("Not supported"));
-//             
-//    }
-
     @Override
-    public void getPredicates(final ReadGraphImpl graph, final int subject, final IntProcedure procedure) {
+    public void getPredicates(final ReadGraphImpl graph, final int subject, final IntProcedure procedure) throws DatabaseException {
 
         final TIntHashSet result = new TIntHashSet(16);
 
@@ -1077,19 +907,6 @@ public class QuerySupportImpl implements QuerySupport {
         }
 
         ClusterI proxy = clusterTable.getClusterByResourceKey(subject);
-//        if(!proxy.isLoaded()) {
-//             
-//             proxy.load(callerThread, session, new Runnable() {
-//
-//                             @Override
-//                             public void run() {
-//                                     getPredicates(callerThread, subject, procedure);
-//                             }
-//                     
-//             });
-//             return;
-//             
-//        }
         assert (proxy != null);
 
         final DataContainer<Integer> got = new DataContainer<Integer>(0);
@@ -1097,7 +914,11 @@ public class QuerySupportImpl implements QuerySupport {
             @Override
             public boolean execute(Object context, int predicate, int oi) {
                 if (result.add(predicate)) {
-                    procedure.execute(graph, predicate);
+                    try {
+                                               procedure.execute(graph, predicate);
+                                       } catch (DatabaseException e) {
+                                               Logger.defaultLogError(e);
+                                       }
                 }
                 got.set(got.get() + 1);
                 return false; // continue looping
index 9290ac99fdd98a5792a542a0b51b7309941f1704..b7adbe49bd40c5466fe7a039eb7511ffa71c6b93 100644 (file)
@@ -466,7 +466,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                     }
 
 
-                    assert(!queryProvider2.dirty);
+                    assert(!queryProvider2.cache.dirty);
 
                 } catch (Throwable e) {
 
@@ -1514,7 +1514,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                     if (listener != null) {
 
                        try {
-                               newGraph.processor.queryRead(newGraph, request, null, new AsyncProcedure<T>() {
+                               newGraph.processor.query(newGraph, request, null, new AsyncProcedure<T>() {
 
                                        @Override
                                        public void exception(AsyncReadGraph graph, Throwable t) {
@@ -1625,9 +1625,11 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
                     if (listener != null) {
 
-                        newGraph.processor.query(newGraph, request, null, procedure, listener);
-
-//                        newGraph.waitAsync(request);
+                        try {
+                                                       newGraph.processor.query(newGraph, request, null, procedure, listener);
+                                               } catch (DatabaseException e) {
+                                                       Logger.defaultLogError(e);
+                                               }
 
                     } else {
 
@@ -3507,7 +3509,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
     public int getAmountOfQueryThreads() {
         // This must be a power of two
-        return 1;
+        return 4;
 //        return Integer.highestOneBit(Runtime.getRuntime().availableProcessors());
     }