]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Trying to remove synchronization problems
authorAntti Villberg <antti.villberg@semantum.fi>
Sun, 22 Jul 2018 22:49:21 +0000 (01:49 +0300)
committerAntti Villberg <antti.villberg@semantum.fi>
Thu, 26 Jul 2018 21:00:26 +0000 (00:00 +0300)
Change-Id: I52a00b8405aa1ca969fe339befa787f79c9ad10a

26 files changed:
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedPredicates.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/CacheEntryBase.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CodeGen.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CollectionUnaryQuery.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectPredicates.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectSuperRelations.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/OrderedSet.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Predicates.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PrincipalTypes.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.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/SuperRelations.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQueryP.java [new file with mode: 0644]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java

index 184276412f7a3251690dd62b72be51c67eeceb1f..b4104e965fbc0b56be90fc40a8f0cd07eef5ab3a 100644 (file)
@@ -15,11 +15,9 @@ 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;
-import org.simantics.db.procedure.ListenerBase;
 
 
-final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure> {
+final public class AssertedPredicates extends UnaryQuery<IntProcedure> {
        
     AssertedPredicates(final int r) {
         super(r);
@@ -28,11 +26,21 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
     public static AssertedPredicates newInstance(final int r) {
         return new AssertedPredicates(r);
     }
-    
-//    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 clearResult(QuerySupport support) {
+        setResult(new IntArray());
+    }
+    
+    @Override
+    final public void setReady() {
+        super.setReady();
+        IntArray v = (IntArray)getResult();
+        int size = v.size();
+        if(size == 0) setResult(IntArray.EMPTY);
+        else v.trim();
+    }
+    
        @Override
        final public void removeEntry(QueryProcessor provider) {
            provider.cache.remove(this);
index 75169d02e9946ca0a1dea3f1e03a3dba2ce49b97..69a85220b954cac8d1dc0e24ef25ca07edd0c407 100644 (file)
@@ -14,84 +14,72 @@ package org.simantics.db.impl.query;
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.common.GraphSemaphore;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.RuntimeDatabaseException;
 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<AsyncProcedure<T>> {
+final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
 
     protected AsyncRead<T> request;
 
     AsyncReadEntry(AsyncRead<T> request) {
-       this.request = request;
-       if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
+        this.request = request;
+        if (DebugPolicy.QUERY_STATE)
+            System.out.println("[QUERY STATE]: created " + this);
     }
 
     @Override
     int makeHash() {
-       return request.hashCode();
+        return request.hashCode();
     }
-    
+
     @Override
     public Object getOriginalRequest() {
         return request;
     }
-    
+
     @Override
     public void discard() {
-       super.discard();
-       setResult(null);
+        super.discard();
+        setResult(null);
     }
-    
-    final public void addOrSet(AsyncReadGraph graph, Object item) {
 
-//     System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + item);
-       
-       assert(isPending());
-
-        synchronized(this) {
-            setResult(item);
-               setReady();
-        }
-        
-    }
-    
-    
     public void except(AsyncReadGraph graph, Throwable t) {
-       
-       assert(isPending());
 
-        synchronized(this) {
+        assert (isPending());
+
+        synchronized (this) {
             except(t);
         }
-       
+
     }
-    
-    
+
     @Override
     final public Query getQuery() {
-       
+
         return new Query() {
 
-                       @Override
-                       public void recompute(ReadGraphImpl graph) {
+            @Override
+            public void recompute(ReadGraphImpl graph) {
 
-                               try {
+                try {
 
-                                       GraphSemaphore s = new GraphSemaphore(graph, 0);
-                                       
-                                   request.perform(graph , new AsyncProcedure<T>() {
+                    GraphSemaphore s = new GraphSemaphore(graph, 0);
+
+                    request.perform(graph, new AsyncProcedure<T>() {
 
                         @Override
                         public void execute(AsyncReadGraph graph, T result) {
-                            addOrSet(graph, result);
+                            setResult(result);
+                            setReady();
                             s.release();
                         }
-                               
-                               @Override
-                               public void exception(AsyncReadGraph graph, Throwable t) {
-                                   except(t);
+
+                        @Override
+                        public void exception(AsyncReadGraph graph, Throwable t) {
+                            except(t);
                             s.release();
                         }
 
@@ -99,144 +87,129 @@ final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
 
                     s.waitFor(1);
 
-                               } catch (Throwable t) {
-                                   except(t);
+                } catch (Throwable t) {
+                    except(t);
                 }
-                               
-                       }
-
-                       @Override
-                       public void removeEntry(QueryProcessor qp) {
-                       qp.cache.remove(AsyncReadEntry.this);
-                       }
-
-                       @Override
-                       public int type() {
-                               return request.getFlags();
-                       }
-                       
-                       @Override
-                       public String toString() {
-                               if(request == null) return "DISCARDED";
-                               else if(isExcepted()) return request.toString() + " " + getResult();
-                               else return request.toString() + " " + statusOrException;
-                       }
-               
+
+            }
+
+            @Override
+            public void removeEntry(QueryProcessor qp) {
+                qp.cache.remove(AsyncReadEntry.this);
+            }
+
+            @Override
+            public int type() {
+                return request.getFlags();
+            }
+
+            @Override
+            public String toString() {
+                if (request == null)
+                    return "DISCARDED";
+                else if (isExcepted())
+                    return request.toString() + " " + getResult();
+                else
+                    return request.toString() + " " + statusOrException;
+            }
+
         };
-        
+
     }
 
     @Override
-       public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
-               
-           if(isExcepted()) {
-            
+    public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
+
+        if (isExcepted()) {
+
             try {
-                proc.exception(graph, (Throwable)getResult());
+                proc.exception(graph, (Throwable) getResult());
             } catch (Throwable t) {
                 t.printStackTrace();
             }
-            
+
         } else {
-            
+
             try {
-                proc.execute(graph, (T)getResult());
+                proc.execute(graph, (T) getResult());
             } catch (Throwable t) {
                 t.printStackTrace();
             }
-            
+
+        }
+
+        return getResult();
+
+    }
+
+    public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry,
+            AsyncProcedure<T> procedure_) throws DatabaseException {
+
+        AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
+
+        GraphSemaphore s = new GraphSemaphore(parentGraph, 0);
+
+        ReadGraphImpl queryGraph = parentGraph.withParent(entry);
+
+        request.perform(queryGraph, new AsyncProcedure<T>() {
+
+            @Override
+            public void execute(AsyncReadGraph returnGraph, T result) {
+                try {
+                    procedure.execute(parentGraph, result);
+                } catch (Throwable t) {
+                    t.printStackTrace();
+                }
+                s.release();
+            }
+
+            @Override
+            public void exception(AsyncReadGraph returnGraph, Throwable t) {
+                try {
+                    procedure.exception(parentGraph, t);
+                } catch (Throwable t2) {
+                    t2.printStackTrace();
+                }
+                s.release();
+            }
+
+            @Override
+            public String toString() {
+                return procedure.toString();
+            }
+
+        });
+
+        try {
+            s.waitFor(1);
+        } catch (InterruptedException e) {
+            throw new RuntimeDatabaseException(e);
         }
-               
-           return getResult();
-           
-       }
-
-    //@Override
-    public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
-
-       ReadGraphImpl queryGraph = graph.withParent(this);
-
-       request.perform(queryGraph, new AsyncProcedure<T>() {
-
-               @Override
-               public void execute(AsyncReadGraph returnGraph, T result) {
-                       ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
-                       AsyncReadEntry.this.addOrSet(graph, result);
-                       try {
-                               procedure.execute(graph, 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();
-                       AsyncReadEntry.this.except(graph, t);
-                       try {
-                               procedure.exception(graph, t);
-                       } catch (Throwable t2) {
-                               t2.printStackTrace();
-                       }
-                       //                                      parentBarrier.dec(query);
-               }
-
-               @Override
-               public String toString() {
-                       return procedure.toString();
-               }
-
-       });
-
-               return getResult();
-       
+
+        if (entry != null)
+            entry.performFromCache(parentGraph, procedure_);
+
+    }
+
+    @Override
+    public String toString() {
+        if (isDiscarded())
+            return "DISCARDED " + request.toString();
+        else if (isExcepted())
+            return request.toString() + " " + getResult();
+        else
+            return request.toString() + " " + statusOrException;
     }
-    
-    public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
-
-               ReadGraphImpl queryGraph = parentGraph.withParent(entry);
-
-               request.perform(queryGraph, new AsyncProcedure<T>() {
-
-                       @Override
-                       public void execute(AsyncReadGraph returnGraph, T result) {
-                               ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
-                               if(entry != null) entry.addOrSet(parentGraph, result);
-                               try {
-                                       procedure.execute(parentGraph, result);
-                               } catch (Throwable t) {
-                                       t.printStackTrace();
-                               }
-                       }
-
-                       @Override
-                       public void exception(AsyncReadGraph returnGraph, Throwable t) {
-                               ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
-                               if(entry != null) entry.except(parentGraph, t);
-                               try {
-                                       procedure.exception(parentGraph, t);
-                               } catch (Throwable t2) {
-                                       t2.printStackTrace();
-                               }
-                       }
-
-                       @Override
-                       public String toString() {
-                               return procedure.toString();
-                       }
-
-               });
-       
+
+    @Override
+    public void execute(AsyncReadGraph graph, T result) {
+        setResult(result);
+        setReady();
+    }
+
+    @Override
+    public void exception(AsyncReadGraph graph, Throwable throwable) {
+        except(throwable);
     }
-    
-    
-       @Override
-       public String toString() {
-               if(isDiscarded()) return "DISCARDED " + request.toString();
-               else if(isExcepted()) return request.toString() + " " + getResult();
-               else return request.toString() + " " + statusOrException;
-       }
 
 }
index dea5de5a78d791799a63ae9898fb48dfdee6c84f..8c43135732288a1ac70a6529ab97bf5e0b358007 100644 (file)
@@ -108,14 +108,14 @@ abstract public class CacheEntryBase<Procedure> extends CacheEntry<Procedure> {
     }
 
     @Override
-    public void except(Throwable t) {
+    public void except(Throwable throwable) {
        if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: excepted " + this);
        if(statusOrException != DISCARDED) {
                statusOrException = EXCEPTED;
-               result = t;
+               result = throwable;
        } else {
-               LOGGER.warn("Cache entry got excepted status after being discarded: " + getClass().getSimpleName(), t);
-               result = t;
+               LOGGER.warn("Cache entry got excepted status after being discarded: " + getClass().getSimpleName(), throwable);
+               result = throwable;
        }
     }
     
index 7dd00d29ba7815443809c556070647c78d8a6ebc..19c36c77c67491483d2d0d48b4505522e5007968 100644 (file)
@@ -21,170 +21,130 @@ import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.service.CollectionSupport;
 
-final public class ChildMap extends UnaryQuery<InternalProcedure<ObjectResourceIdMap<String>>> {
-       
+final public class ChildMap extends UnaryQueryP<ObjectResourceIdMap<String>> {
+
     ChildMap(final int r) {
         super(r);
     }
-    
-       @Override
-       final public void removeEntry(QueryProcessor provider) {
-               provider.cache.remove(this);
-       }
-       
-    //@Override
-       public Object compute(ReadGraphImpl graph, final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
-               computeForEach(graph, id, this, procedure);
-               return getResult();
+
+    @Override
+    final public void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
     }
 
-       public static void computeForEach(ReadGraphImpl graph, final int root, final ChildMap entry, final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
-               
-               if(root == 0) {
-                       if(entry != null)
-                               entry.add2(graph, null);
+    @Override
+    public void compute(ReadGraphImpl graph, final InternalProcedure<ObjectResourceIdMap<String>> procedure)
+            throws DatabaseException {
+        computeForEach(graph, id, this, procedure);
+    }
+
+    public static void computeForEach(ReadGraphImpl graph, final int root, final ChildMap entry,
+            final InternalProcedure<ObjectResourceIdMap<String>> procedure_) throws DatabaseException {
+
+        InternalProcedure<ObjectResourceIdMap<String>> procedure = entry != null ? entry : procedure_;
+
+        computeForEach2(graph, root, entry, procedure);
+
+        if (entry != null)
+            entry.performFromCache(graph, procedure_);
+
+    }
+
+    public static void computeForEach2(ReadGraphImpl graph, final int root, final ChildMap parent,
+            final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
+
+        if (root == 0) {
             procedure.execute(graph, null);
             return;
-               }
-
-               QueryProcessor processor = graph.processor;
-               
-               final int consistsOf = processor.getConsistsOf();
-               final int hasName = processor.getHasName();
-        
-               ObjectResourceIdMap<String> result = graph.getService(CollectionSupport.class).createObjectResourceMap(String.class);
-        
-        QueryCache.runnerObjects(graph, root, consistsOf, entry, null, new SyncIntProcedure() {
-               
-               @Override
-                       public void run(ReadGraphImpl graph) throws DatabaseException {
-                       
-                       if(entry != null) entry.add2(graph, result);
-                       procedure.execute(graph, result);
-                               
-                       }
-
-                       @Override
-                       public void finished(ReadGraphImpl graph) throws DatabaseException {
-                               dec(graph);
-                       }
-
-               @Override
-               public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
-
-                       inc();
-                       
-                       QueryCache.runnerObjects(graph, obj, hasName, entry, null, new IntProcedure() {
-                       
-                       @Override
-                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-
-                               inc();
-
-                               QueryCache.runnerValueQuery(graph, i, entry, null, new InternalProcedure<byte[]>() {
-                                       
-                                       @Override
-                                       public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException {
-                                               
-                                               if(value != null) {
-
-                                       try {
-
-                                               Binding b = WriteBindings.STRING;
-                                           Serializer serializer = b.serializer();
-                                           final String part = (String)serializer.deserialize(value);
-                                           result.putId(part, obj);
-                                           
-                                               } catch (Throwable e) {
-                                           if(DebugException.DEBUG) new DebugException(e).printStackTrace();
-                                       }
-                                               
-                                               }
-                                               
-                                       dec(graph);
-                                       
-                                       }
-                                       
-                                       @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                                       if(entry != null) entry.except(t);
-                                               dec(graph);
+        }
+
+        QueryProcessor processor = graph.processor;
+
+        final int consistsOf = processor.getConsistsOf();
+        final int hasName = processor.getHasName();
+
+        ObjectResourceIdMap<String> result = graph.getService(CollectionSupport.class)
+                .createObjectResourceMap(String.class);
+
+        QueryCache.runnerObjects(graph, root, consistsOf, parent, null, new SyncIntProcedure() {
+
+            @Override
+            public void run(ReadGraphImpl graph) throws DatabaseException {
+                procedure.execute(graph, result);
+            }
+
+            @Override
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
+                dec(graph);
+            }
+
+            @Override
+            public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
+
+                inc();
+
+                QueryCache.runnerObjects(graph, obj, hasName, parent, null, new IntProcedure() {
+
+                    @Override
+                    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+
+                        inc();
+
+                        QueryCache.runnerValueQuery(graph, i, parent, null, new InternalProcedure<byte[]>() {
+
+                            @Override
+                            public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException {
+
+                                if (value != null) {
+
+                                    try {
+
+                                        Binding b = WriteBindings.STRING;
+                                        Serializer serializer = b.serializer();
+                                        final String part = (String) serializer.deserialize(value);
+                                        result.putId(part, obj);
+
+                                    } catch (Throwable e) {
+                                        if (DebugException.DEBUG)
+                                            new DebugException(e).printStackTrace();
+                                    }
+
+                                }
+
+                                dec(graph);
+
+                            }
+
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                dec(graph);
                             }
 
-                               });
-                               
-                       }
-                       
-                       @Override
-                       public void finished(ReadGraphImpl graph) throws DatabaseException {
-                               dec(graph);
-                       }
-                               
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       if(entry != null) entry.except(t);
+                        });
+
+                    }
+
+                    @Override
+                    public void finished(ReadGraphImpl graph) throws DatabaseException {
+                        dec(graph);
+                    }
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                         dec(graph);
                     }
 
                 });
 
-               }
-               
+            }
+
         });
-        
-    }
 
-    @Override
-    public String toString() {
-       return "ChildMap[" + id + "]";
     }
 
-    private void add2(ReadGraphImpl graph, ObjectResourceIdMap<String> result) {
-       
-       assert(isPending());
-
-        synchronized(this) {
-
-            setResult(result);
-               setReady();
-        
-        }
-       
-    }
-    
     @Override
-    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
-        
-        assert(isReady());
-        
-       if(handleException(graph, procedure)) return (Throwable)statusOrException;
-        
-       ObjectResourceIdMap<String> result = (ObjectResourceIdMap<String>)getResult();
-       
-        procedure.execute(graph, result);
-        
-        return result;
-        
+    public String toString() {
+        return "ChildMap[" + id + "]";
     }
-    
-    @Override
-    public void recompute(ReadGraphImpl graph) throws DatabaseException {
-        
-        compute(graph, new InternalProcedure<ObjectResourceIdMap<String>>() {
-
-            @Override
-            public void execute(ReadGraphImpl graph, ObjectResourceIdMap<String> result) {
-            }
-                       
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                               throw new Error("Error in recompute.", t);
-            }
 
-        });
-        
-    }
-    
 }
-
index deecbabbc143a0408a0a55c0beceadd6ffd5868d..3d5f14ac493603d800102438e65ff5751786e8ec 100644 (file)
@@ -14,8 +14,6 @@ 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" };
@@ -188,15 +186,14 @@ public class CodeGen {
                     generateQuery(content, "ReadEntry", signatureRead, true, true);
                     generateQuery(content, "AsyncReadEntry", signatureAsyncRead, true, true);
                     generateQuery(content, "Types", signatureR1IntSet, true, false);
-                    //generateQuery(content, "NamespaceIndex", signatureID2, true);
                     generateQuery(content, "ChildMap", signatureChildMap, true, false);
+                    generateQuery(content, "TypeHierarchy", signatureR1IntSet, true, false);
+                    generateQuery(content, "SuperTypes", signatureR1IntSet, true, false);
+                    generateQuery(content, "SuperRelations", signatureR1IntSet, true, false);
 
-                    generateQuery(content, "AssertedStatements", signatureR2TIP, false, false);
                     generateQuery(content, "AssertedPredicates", signatureR1IP, false, false);
+                    generateQuery(content, "AssertedStatements", signatureR2TIP, false, false);
                     generateQuery(content, "DirectSuperRelations", signatureR1IP, false, false);
-                    generateQuery(content, "SuperTypes", signatureR1IntSet, false, false);
-                    generateQuery(content, "TypeHierarchy", signatureR1IntSet, false, false);
-                    generateQuery(content, "SuperRelations", signatureR1IntSet, false, false);
                     generateQuery(content, "MultiReadEntry", signatureMultiRead, false, false);
                     generateQuery(content, "AsyncMultiReadEntry", signatureAsyncMultiRead, false, false);
                     generateQuery(content, "ExternalReadEntry", signatureExternalRead, false, false);
index bde26c36d319cfcb5445f227717ad40b93fe1c5c..f0389ccfea61071cf197b22003099a8202234e43 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.impl.procedure.IntProcedureAdapter;
 
-
-
-abstract public class CollectionUnaryQuery<T> extends UnaryQuery<T> {
+abstract public class CollectionUnaryQuery extends UnaryQuery<IntProcedure> implements IntProcedure {
     
     public CollectionUnaryQuery(final int id) {
         super(id);
     }
     
+    abstract public void compute(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException;
+
     @Override
-    public void clearResult(QuerySupport support) {
+    final public void clearResult(QuerySupport support) {
        setResult(new IntArray());
     }
     
     @Override
-    public void setReady() {
+    final public void setReady() {
        super.setReady();
        IntArray v = (IntArray)getResult();
        int size = v.size();
@@ -34,4 +37,63 @@ abstract public class CollectionUnaryQuery<T> extends UnaryQuery<T> {
         else v.trim();
     }
     
+    @Override
+    final public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
+
+        assert(isReady());
+
+        if(handleException(graph, procedure)) return EXCEPTED;
+        
+        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 getResult();
+
+    }
+
+    @Override
+    final 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();
+            }
+
+        });
+
+    }
+    
+    @Override
+    final boolean isImmutable(ReadGraphImpl graph) {
+        return graph.processor.isImmutable(id);
+    }
+
+    @Override
+    final public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+        IntArray v = (IntArray)getResult();
+        v.add(i);
+    }
+
+    @Override
+    final public void finished(ReadGraphImpl graph) throws DatabaseException {
+        setReady();
+    }
+
+    @Override
+    final public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
+    }
+    
 }
index 117056410bfacbbe1492ea3407ba56472a3ff369..8f2c7e0d8bb1ea814e6454b58dd726f57d26ef59 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-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> {
+final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> implements IntProcedure {
 
-       DirectObjects(final int r1, final int r2) {
-               super(r1, r2);
-       }
+    DirectObjects(final int r1, final int r2) {
+        super(r1, r2);
+    }
 
-       @Override
-       public int type() {
-               return RequestFlags.INVALIDATE;
-       }
-       
-//    @Override
-//    public void clearResult(QuerySupport support) {
-//     setResult(INVALID_RESULT);
-//    }
+    @Override
+    public int type() {
+        return RequestFlags.INVALIDATE;
+    }
 
-//     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.remove(this);
+    }
 
-       @Override
-       final public void removeEntry(QueryProcessor provider) {
-               provider.cache.remove(this);
-       }
+    // @Override
+    public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
+        computeForEach(graph, r1(), r2(), this, procedure);
+        return getResult();
+    }
 
-       //@Override
-       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_) throws DatabaseException {
 
-       static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) throws DatabaseException {
+        IntProcedure procedure = entry != null ? entry : procedure_;
 
-               QueryProcessor processor = graph.processor;
-               
-               processor.querySupport.ensureLoaded(graph, r1, r2);
+        QueryProcessor processor = graph.processor;
 
-               processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
+        processor.querySupport.ensureLoaded(graph, r1, r2);
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                               procedure.execute(graph, i);
-                               if(entry != null) entry.add(i);
-                       }
+        processor.querySupport.getObjects(graph, r1, r2, procedure);
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                       }
+        procedure.finished(graph);
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                       }
+        if (entry != null)
+            entry.performFromCache(graph, procedure_);
 
-               });
+    }
 
-               if(entry != null) entry.finish(graph, processor);
-               procedure.finished(graph);
+    @Override
+    public String toString() {
+        return "DirectObjects[" + r1() + " - " + r2() + "]";
+    }
 
-       }
+    @Override
+    public void setReady() {
+        statusOrException = READY;
+    }
 
-       @Override
-       public String toString() {
-               return "DirectObjects[" + r1() + " - " + r2() + "]";
-       }
+//     final public void add(int add) {
+//
+//             assert(isPending());
+//
+//             IntArray value = (IntArray)getResult();
+//             value.add(add);
+//
+//     }
 
-       @Override
-       public void setReady() {
-               statusOrException = READY;
-       }
-       
-       final public void add(int add) {
+    @Override
+    public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
 
-               assert(isPending());
+        assert (isReady());
 
-               IntArray value = (IntArray)getResult();
-               value.add(add);
+        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]);
+        }
 
-       final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
-               setReady();
-       }
+        procedure.finished(graph);
 
-       @Override
-       public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
+        return value;
 
-               assert(isReady());
+    }
 
-               if(handleException(graph, procedure)) return getResult();
+    @Override
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-               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]);
-               }
+        compute(graph, new IntProcedure() {
 
-               procedure.finished(graph);
-               
-               return value;
+            @Override
+            public void finished(ReadGraphImpl graph) {
+            }
 
-       }
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) {
+                throw new Error("Error in recompute.", t);
+            }
 
-       @Override
-       public void recompute(ReadGraphImpl graph) throws DatabaseException {
-               
-               compute(graph, new IntProcedure() {
+            @Override
+            public void execute(ReadGraphImpl graphd, int i) {
+            }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                       }
+        });
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               throw new Error("Error in recompute.", t);
-                       }
+    }
 
-                       @Override
-                       public void execute(ReadGraphImpl graphd, int i) {
-                       }
+    @Override
+    boolean isImmutable(ReadGraphImpl graph) {
+        return graph.processor.isImmutable(r1());
+    }
 
-               });
+    @Override
+    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+        IntArray value = (IntArray) getResult();
+        value.add(i);
+    }
 
-       }
+    @Override
+    public void finished(ReadGraphImpl graph) throws DatabaseException {
+        setReady();
+    }
 
     @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(r1());
+    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
     }
-       
-}
 
+}
index 63c54effe47f2e405320abe19e7a431af686bc70..bb2e1530e224f3b00626cfdea188b97b591beb64 100644 (file)
@@ -16,30 +16,26 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 
-final public class DirectPredicates extends CollectionUnaryQuery<InternalProcedure<IntSet>> {
+final public class DirectPredicates extends UnaryQueryP<IntSet> {
 
        DirectPredicates(final int resource) {
                super(resource);
        }
        
-       @Override
-       public void clearResult(QuerySupport support) {
-               // The cached result is never used
-               setResult(INVALID_RESULT);
-       }
-       
        @Override
        final public void removeEntry(QueryProcessor provider) {
                provider.cache.remove(this);
        }
 
-       //@Override
-       public Object compute(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
-           return computeForEach(graph, id, this, procedure);
+       @Override
+       public void compute(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
+           computeForEach(graph, id, this, procedure);
        }
        
-       public static Object computeForEach(ReadGraphImpl graph, int id, final DirectPredicates entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+       public static Object computeForEach(ReadGraphImpl graph, int id, final DirectPredicates entry, final InternalProcedure<IntSet> procedure_) throws DatabaseException {
 
+           InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
+           
                graph.processor.querySupport.ensureLoaded(graph, id);
                
                final IntSet list = new IntSet(graph.processor.querySupport);
@@ -61,13 +57,10 @@ final public class DirectPredicates extends CollectionUnaryQuery<InternalProcedu
                        }
 
                });
-
-               if(entry != null) {
-                   entry.setResult(list);
-                   entry.setReady();
-               }
                
                procedure.execute(graph, list);
+
+               if(entry != null) entry.performFromCache(graph, procedure_);
                
                return list;
 
@@ -78,48 +71,4 @@ final public class DirectPredicates extends CollectionUnaryQuery<InternalProcedu
                return "DirectPredicates[" + id + "]";
        }
 
-       @Override
-       public void setReady() {
-               statusOrException = READY;
-       }
-
-       @Override
-       public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
-
-               assert(isReady());
-
-       if(handleException(graph, procedure)) return EXCEPTED;
-       
-       IntSet result = getResult();
-       
-        procedure.execute(graph, result);
-        
-        return result;
-
-       }
-
-       @Override
-       public void recompute(ReadGraphImpl graph) throws DatabaseException {
-
-               compute(graph, new InternalProcedure<IntSet>() {
-
-                       @Override
-                       public void execute(ReadGraphImpl graph, IntSet set) {
-                       }
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               new Error("Error in recompute.", t).printStackTrace();
-                       }
-
-               });
-
-       }
-
-
-    @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(id);
-    }
-
 }
index 621bdad969157b7a9f1f64726e5ffbf1b5f4ae15..3073b3461abc8086cc77846a450739a1f975175c 100644 (file)
@@ -11,8 +11,6 @@
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-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;
index 1883d9c1f52e67d647e4c011c628c2aa38f4c47a..4d5279c51534fce6b7177567e9fba1bdfdf33099 100644 (file)
@@ -14,7 +14,6 @@ package org.simantics.db.impl.query;
 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;
@@ -23,8 +22,6 @@ import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.procedure.AsyncMultiProcedure;
 import org.simantics.db.request.RequestFlags;
 
-import gnu.trove.procedure.TIntProcedure;
-
 /*
  * Size analysis:
  * java 8 byte
@@ -38,7 +35,7 @@ import gnu.trove.procedure.TIntProcedure;
  * 
  */
 
-final public class Objects extends CollectionBinaryQuery<IntProcedure> {
+final public class Objects extends CollectionBinaryQuery<IntProcedure> implements IntProcedure {
 
        public Objects(final int r1, final int r2) {
                super(r1, r2);
@@ -117,33 +114,21 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
        }
 
-       final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException {
+       final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final IntProcedure procedure) throws DatabaseException {
 
-               IntArray map = getAssertionMap(graph, r1, r2, entry);
+               IntArray map = getAssertionMap(graph, r1, r2, parent);
                if(map == null) {
-                       if(entry != null) entry.finish(graph, procedure);
-                       else procedure.finished(graph);
+                       procedure.finished(graph);
                        return;
                }
 
                int size = map.size();
                if(size == 3) {
-
-                       int value = map.data[2];
-
-                       if(entry != null) {
-                               entry.addOrSetFunctional(value);
-                               entry.finish(graph, procedure);
-                       } else {
-                               procedure.execute(graph, value);
-                               procedure.finished(graph);
-                       }
-
+                   int value = map.data[2];
+                   procedure.execute(graph, value);
+                   procedure.finished(graph);
                } else if(size == 0) {
-
-                       if(entry != null) entry.finish(graph, procedure);
-                       else procedure.finished(graph);
-
+                       procedure.finished(graph);
                } else {
 
                        int candidateS = map.data[0];
@@ -151,9 +136,8 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                        IntSet candidateIs = null;
                        try {
-                               candidateIs = QueryCache.resultSuperTypes(graph, candidateS, entry, null);
+                               candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null);
                        } catch (DatabaseException e) {
-                               if(entry != null) entry.except(e);
                                procedure.exception(graph, e);
                                return;
                        }
@@ -173,9 +157,8 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                IntSet nextIs = null;
                                                try {
-                                                       nextIs = QueryCache.resultSuperTypes(graph, nextS, entry, null);
+                                                       nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
                                                } catch (DatabaseException e) {
-                                                       if(entry != null) entry.except(e);
                                                        procedure.exception(graph, e);
                                                        return;
                                                }
@@ -200,8 +183,6 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                        // candidate and next are unrelated => error
                                                        ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions " + r1 + ", " + r2 + " " + map , r1);
-
-                                                       if(entry != null) entry.except(exception);
                                                        procedure.exception(graph, exception);
                                                        return;                                         
 
@@ -213,13 +194,8 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                        }
 
-                       if(entry != null) {
-                               entry.addOrSetFunctional(candidateO);
-                               entry.finish(graph, procedure);
-                       } else {
-                               procedure.execute(graph, candidateO);
-                               procedure.finished(graph);
-                       }
+                       procedure.execute(graph, candidateO);
+                       procedure.finished(graph);
 
                }
 
@@ -260,7 +236,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        }
 
        // 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) throws DatabaseException {
+       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
 
                if(ri.isFinal) {
 
@@ -269,7 +245,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        if(result == 0) {
 
                                // Check for assertions
-                               forSingleAssertion(graph, r1, r2, entry, procedure);
+                               forSingleAssertion(graph, r1, r2, parent, procedure);
 
                        } else if (result == -1) {
 
@@ -277,8 +253,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                        @Override
                                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                               if(entry != null) entry.addOrSetFunctional(i);
-                                               else procedure.execute(graph, i);
+                                           procedure.execute(graph, i);
                                        }
 
                                        @Override
@@ -293,18 +268,13 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                });
 
                                // Check for assertions
-                               forSingleAssertion(graph, r1, r2, entry, procedure);
+                               forSingleAssertion(graph, r1, r2, parent, procedure);
 
                        } else {
 
                                // If functional relation was found there is no need to check assertions
-                               if(entry != null) {
-                                       entry.addOrSetFunctional(result);
-                                       entry.finish(graph, procedure);
-                               } else {
-                                       procedure.execute(graph, result);
-                                       procedure.finished(graph);
-                               }
+                procedure.execute(graph, result);
+                procedure.finished(graph);
 
                                
                        }
@@ -321,12 +291,11 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                                public void run(ReadGraphImpl graph) throws DatabaseException {
 
                                        if(found) {
-                                               if(entry != null) entry.finish(graph, procedure);
-                                               else procedure.finished(graph);
+                                               procedure.finished(graph);
                                        } else {
 
                                                // Check for assertions
-                                               forSingleAssertion(graph, r1, r2, entry, procedure);
+                                               forSingleAssertion(graph, r1, r2, parent, procedure);
 
                                        }
 
@@ -347,15 +316,12 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                                if(!found) {
 
-                                                                       if(entry != null) entry.addOrSetFunctional(i);
-                                                                       else procedure.execute(graph, i);
-
+                                                                       procedure.execute(graph, i);
                                                                        found = true;
 
                                                                } else {
 
                                                                        ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
-                                                                       if(entry != null) entry.except(exception);
                                                                        procedure.exception(graph, exception);
 
                                                                }
@@ -375,7 +341,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                        } else {
 
-                                               QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
+                                               QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
 
                                                        @Override
                                                        public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
@@ -392,15 +358,12 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                                                        if(!found) {
 
-                                                                                               if(entry != null) entry.addOrSetFunctional(i);
-                                                                                               else procedure.execute(graph, i);
-
+                                                                                               procedure.execute(graph, i);
                                                                                                found = true;
 
                                                                                        } else {
 
                                                                                                ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
-                                                                                               if(entry != null) entry.except(exception);
                                                                                                procedure.exception(graph, exception);
 
                                                                                        }
@@ -447,25 +410,21 @@ 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) throws DatabaseException {
+       final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final IntProcedure procedure) throws DatabaseException {
 
                // Note! The dependency is intentionally cut!
                QueryCache.runnerPrincipalTypes(graph, r1, null, null, new SyncIntProcedure() {
 
                        @Override
                        public void run(ReadGraphImpl graph) throws DatabaseException {
-
-                               if(entry != null) entry.finish(graph, procedure);
-                               else procedure.finished(graph);
-
+                               procedure.finished(graph);
                        }
 
                        TripleIntProcedure proc = new TripleIntProcedure() {
 
                                @Override
                                public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
-                                       if(entry != null) entry.addOrSet(o);
-                                       else procedure.execute(graph, o);
+                                       procedure.execute(graph, o);
                                }
 
                                @Override
@@ -486,7 +445,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
 
                                inc();
-                               QueryCache.runnerAssertedStatements(graph, type, r2, entry, null, proc);
+                               QueryCache.runnerAssertedStatements(graph, type, r2, parent, null, proc);
 
                        }
 
@@ -501,16 +460,14 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
        }
 
        final public static void computeNotFunctionalFinalIndex(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, RelationInfo ri, AsyncMultiProcedure<Resource> procedure) {
-
                throw new Error();
-
        }
 
        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) throws DatabaseException {
+       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, RelationInfo ri, final IntProcedure procedure) throws DatabaseException {
 
                if(ri.isFinal) {
 
@@ -518,8 +475,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                @Override
                                public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                       if(entry != null) entry.addOrSet(i);
-                                       else procedure.execute(graph, i);
+                                       procedure.execute(graph, i);
                                }
 
                                @Override
@@ -535,10 +491,9 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                        });
 
                        if(ri.isAsserted) {
-                               forAssertions(graph, r1, r2, entry, procedure);
+                               forAssertions(graph, r1, r2, parent, procedure);
                        } else {
-                               if(entry != null) entry.finish(graph, procedure);
-                               else procedure.finished(graph);
+                               procedure.finished(graph);
                        }
 
                } else {
@@ -549,9 +504,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                @Override
                                public void run(ReadGraphImpl graph) throws DatabaseException {
-
-                                       forAssertions(graph, r1, r2, entry, procedure);
-
+                                       forAssertions(graph, r1, r2, parent, procedure);
                                }
 
                                @Override
@@ -566,8 +519,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                        @Override
                                                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                                               if(entry != null) entry.addOrSet(i);
-                                                               else procedure.execute(graph, i);
+                                                               procedure.execute(graph, i);
                                                        }
 
                                                        @Override
@@ -587,7 +539,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                inc();
 
-                                               QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
+                                               QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
 
                                                        @Override
                                                        public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
@@ -601,8 +553,7 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
 
                                                                                @Override
                                                                                public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                                                                       if(entry != null) entry.addOrSet(i);
-                                                                                       else procedure.execute(graph, i);
+                                                                                       procedure.execute(graph, i);
                                                                                }
 
                                                                                @Override
@@ -654,15 +605,19 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                return getResult();
        }
 
-       public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException {
-
-               RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null);
-               graph.ensureLoaded(r1, r2);       
-               if(ri.isFunctional) {
-                       computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               } else {
-                       computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               }
+       public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure_) throws DatabaseException {
+
+           IntProcedure procedure = entry != null ? entry : procedure_;
+           
+        RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null);
+        graph.ensureLoaded(r1, r2);
+        if(ri.isFunctional) {
+            computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
+        } else {
+            computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
+        }
+               
+               if(entry != null) entry.performFromCache(graph, procedure_);
 
        }
 
@@ -671,49 +626,49 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                return "Objects[" + r1() + " - " + r2() + "]";
        }
 
-       final private void finish(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
-
-               assert(assertPending());
-
-               synchronized(this) {
-                       setReady();
-               }
-
-               IntArray v = (IntArray)getResult();
-
-               if(v.data == null) {
-                       if(v.sizeOrData != IntArray.NO_DATA) {
-                               procedure.execute(graph, v.sizeOrData);
-                       }
-               } else {
-                       for(int i = 0;i < v.sizeOrData ; i++) {
-                               procedure.execute(graph, v.data[i]);
-                       }
-               }
-
-               procedure.finished(graph);
-
-       }
-
-       final public void addOrSet(int add) {
-
-               assert(assertPending());
-
-               IntArray value = (IntArray)getResult();
-               synchronized(value) {
-                       value.add(add);
-               }
-
-       }
-
-       final public void addOrSetFunctional(int add) {
-
-               assert(isPending());
-
-               IntArray value = (IntArray)getResult();
-               value.add(add);
-
-       }
+//     final private void finish(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
+//
+//             assert(assertPending());
+//
+//             synchronized(this) {
+//                     setReady();
+//             }
+//
+//             IntArray v = (IntArray)getResult();
+//
+//             if(v.data == null) {
+//                     if(v.sizeOrData != IntArray.NO_DATA) {
+//                             procedure.execute(graph, v.sizeOrData);
+//                     }
+//             } else {
+//                     for(int i = 0;i < v.sizeOrData ; i++) {
+//                             procedure.execute(graph, v.data[i]);
+//                     }
+//             }
+//
+//             procedure.finished(graph);
+//
+//     }
+
+//     final public void addOrSet(int add) {
+//
+//             assert(assertPending());
+//
+//             IntArray value = (IntArray)getResult();
+//             synchronized(value) {
+//                     value.add(add);
+//             }
+//
+//     }
+
+//     final public void addOrSetFunctional(int add) {
+//
+//             assert(isPending());
+//
+//             IntArray value = (IntArray)getResult();
+//             value.add(add);
+//
+//     }
 
        @Override
        public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
@@ -763,4 +718,22 @@ final public class Objects extends CollectionBinaryQuery<IntProcedure> {
                return graph.processor.isImmutable(r1());
        }
 
+    @Override
+    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+      IntArray value = (IntArray)getResult();
+      synchronized(value) {
+          value.add(i);
+      }
+    }
+
+    @Override
+    public void finished(ReadGraphImpl graph) throws DatabaseException {
+        setReady();
+    }
+
+    @Override
+    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
+    }
+
 }
index 9e7bb5c92ee39778b877f428a0aa336fe629db61..69ac07edd5a927700ea8354dca6725ca3e90d6e2 100644 (file)
@@ -17,10 +17,8 @@ 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> {
+final public class OrderedSet extends CollectionUnaryQuery {
        
        public OrderedSet(final int r) {
         super(r);
@@ -31,7 +29,7 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
         provider.cache.remove(this);
        }
 
-       private static int nextElement(ReadGraphImpl graph, int current, int orderedSet, OrderedSet entry, final IntProcedure procedure) throws DatabaseException {
+       private static int nextElement(ReadGraphImpl graph, int current, int orderedSet, OrderedSet parent, final IntProcedure procedure) throws DatabaseException {
            
                QueryProcessor processor = graph.processor;
                
@@ -39,12 +37,11 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
                
                AtomicInteger res = new AtomicInteger(0);
                
-               boolean found = processor.querySupport.getObjects(graph, current, orderedSet, new IntProcedure() {
+               processor.querySupport.getObjects(graph, current, orderedSet, new IntProcedure() {
 
                        @Override
                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                if(i != orderedSet) {
-                                       if(entry != null) entry.addOrSet(i);
                                        procedure.execute(graph, i);
                                }
                                res.set(i);
@@ -63,7 +60,6 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
                });
 
                if(res.get() == orderedSet) {
-                       if(entry != null) entry.finish(graph, processor);
                        procedure.finished(graph);
                }
                
@@ -72,85 +68,26 @@ final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
        }
        
     @Override
-    public void clearResult(QuerySupport support) {
-        setResult(new IntArray());
-    }
-       
-    //@Override
-    public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
-       computeForEach(graph, id, this, procedure);
-        return getResult();
+    public void compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
+        computeForEach(graph, id, this, procedure);
     }
 
-    static void computeForEach(ReadGraphImpl graph, int orderedSet, final OrderedSet entry, final IntProcedure procedure) throws DatabaseException {
+    static void computeForEach(ReadGraphImpl graph, int orderedSet, final OrderedSet entry, final IntProcedure procedure_) throws DatabaseException {
+        
+        IntProcedure procedure = entry != null ? entry : procedure_;
 
        int current = nextElement(graph, orderedSet, orderedSet, entry, procedure);
        while(current != orderedSet) {
                current = nextElement(graph, current, orderedSet, entry, procedure);
        }
 
+       if(entry != null) entry.performFromCache(graph, procedure_);
+       
     }
     
     @Override
     public String toString() {
        return "OrderedSet[" + id + "]";
     }
-
-    final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
-        
-       assert(isPending());
-
-        synchronized(this) {
-               setReady();
-        }
-        
-    }
-
-    final public void addOrSet(int add) {
-        
-        assert(isPending());
-        
-        IntArray value = (IntArray)getResult();
-        value.add(add);
-        
-    }
-
-    @Override
-    public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
-       
-       assert(isReady());
-
-       if(handleException(graph, procedure)) return EXCEPTED;
-       
-        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) throws DatabaseException {
-        
-        compute(graph, new IntProcedureAdapter() {
-
-            @Override
-            public void finished(ReadGraphImpl graph) {
-            }
-                       
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               throw new Error("Error in recompute.", t);
-            }
-
-        });
-        
-    }
     
 }
index 03b17bba5cfba708064f2a9ed84b115348af8f4d..9445db03b4e048ca1488cb22f45eeaa56da9bdd1 100644 (file)
 package org.simantics.db.impl.query;
 
 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;
 
 import gnu.trove.procedure.TIntProcedure;
 
-final public class Predicates extends UnaryQuery<InternalProcedure<IntSet>> {
+final public class Predicates extends UnaryQueryP<IntSet> {
 
        Predicates(final int r) {
         super(r);
@@ -32,9 +30,9 @@ final public class Predicates extends UnaryQuery<InternalProcedure<IntSet>> {
         provider.cache.remove(this);
        }
     
-    final static private void forAssertions(ReadGraphImpl graph, int r, Predicates entry, final IntSet set) throws DatabaseException {
+    final static private void forAssertions(ReadGraphImpl graph, int r, Predicates parent, final IntSet set) throws DatabaseException {
 
-       QueryCache.runnerPrincipalTypes(graph, r, entry, null, new SyncIntProcedure() {
+       QueryCache.runnerPrincipalTypes(graph, r, parent, null, new SyncIntProcedure() {
             
             @Override
             public void run(ReadGraphImpl graph) throws DatabaseException {
@@ -63,7 +61,7 @@ final public class Predicates extends UnaryQuery<InternalProcedure<IntSet>> {
             public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
 
                inc();
-                QueryCache.runnerAssertedPredicates(graph, type, entry, null, proc);
+                QueryCache.runnerAssertedPredicates(graph, type, parent, null, proc);
                 
             }
             
@@ -77,13 +75,14 @@ final public class Predicates extends UnaryQuery<InternalProcedure<IntSet>> {
 
     }
 
-       //@Override
-       public Object compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+       @Override
+       public void 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 {
+       public static void computeForEach(ReadGraphImpl graph, final int r, final Predicates entry, final InternalProcedure<IntSet> procedure_) throws DatabaseException {
+           
+           InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
        
        IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null);
                
@@ -91,16 +90,8 @@ final public class Predicates extends UnaryQuery<InternalProcedure<IntSet>> {
        forAssertions(graph, r, entry, result);
        
        if(result.isEmpty()) {
-               
-               if(entry != null) {
-                       entry.setResult(direct);
-                       entry.setReady();
-               }
-               
                procedure.execute(graph, direct);
-               
        } else {
-               
                direct.forEach(new TIntProcedure() {
                        @Override
                        public boolean execute(int value) {
@@ -108,16 +99,10 @@ final public class Predicates extends UnaryQuery<InternalProcedure<IntSet>> {
                                return true;
                        }
                });
-
-               if(entry != null) {
-                       entry.setResult(result);
-                       entry.setReady();
-               }
-               
                procedure.execute(graph, result);
-               
        }
-               
+       
+       if(entry != null) entry.performFromCache(graph, procedure_);
         
     }
     
@@ -126,61 +111,14 @@ final public class Predicates extends UnaryQuery<InternalProcedure<IntSet>> {
        return "Predicates[" + id + "]";
     }
 
-    final public void finish(final ReadGraphImpl graph, QueryProcessor provider) {
-        
-        synchronized(this) {
-               setReady();
-        }
-
-    }
-
     @Override
     public void clearResult(QuerySupport support) {
        setResult(new IntSet(support));
     }
     
-    @Override
-    public Object performFromCache(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
-       
-       assert(isReady());
-
-       if(handleException(graph, procedure)) return EXCEPTED;
-       
-       IntSet result = getResult();
-       
-       procedure.execute(graph, result);
-        
-        return result;
-        
-    }
-    
-    @Override
-    public void recompute(ReadGraphImpl graph) throws DatabaseException {
-        
-        compute(graph, new InternalProcedure<IntSet>() {
-
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               throw new Error("Error in recompute.", t);
-            }
-
-                       @Override
-                       public void execute(ReadGraphImpl graph, IntSet i) {
-                       }
-
-        });
-        
-    }
-    
     @Override
     public int type() {
         return RequestFlags.IMMEDIATE_UPDATE;
     }
-
-
-    @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(id);
-    }
     
 }
index af4d20ebca440dca89c2cdedef24bf11248f02f4..a5a4a89970aacb1a0c3dcc28afdb58279f0a6b50 100644 (file)
@@ -13,19 +13,17 @@ package org.simantics.db.impl.query;
 
 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;
-import org.simantics.db.procedure.ListenerBase;
 
 import gnu.trove.procedure.TIntProcedure;
 import gnu.trove.set.hash.TIntHashSet;
 
-final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
+final public class PrincipalTypes extends CollectionUnaryQuery {
 
        PrincipalTypes(final int resource) {
                super(resource);
@@ -75,26 +73,34 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
        }
 
-       //@Override
-       public Object compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
-               return computeForEach(graph, id, this, proc);
+       @Override
+       public void compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
+               computeForEach(graph, id, this, proc);
        }
        
-       public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException {
+       public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure procedure_) throws DatabaseException {
+
+           IntProcedure procedure = entry != null ? entry : procedure_;
+           
+           Object result = computeForEach2(graph, id, entry, procedure);
+           
+           if(entry != null) entry.performFromCache(graph, procedure_);
+           
+           return result;
+           
+       }
+           
+       public static Object computeForEach2(final ReadGraphImpl graph, final int id, final PrincipalTypes parent, final IntProcedure procedure) throws DatabaseException {
 
-               QueryProcessor provider = graph.processor;
+           QueryProcessor provider = graph.processor;
                
                provider.querySupport.ensureLoaded(graph, id);
                assert(id != 0);
 
                int ret = provider.querySupport.getSingleInstance(id);
                if(ret > 0) {
-                       if(entry != null) {
-                               entry.add(ret);
-                               entry.finish(graph, provider);
-                       }
-                       proc.execute(graph, ret);
-                       proc.finished(graph);
+                       procedure.execute(graph, ret);
+                       procedure.finished(graph);
                        return ret;
                }
 
@@ -114,7 +120,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                               proc.exception(graph, t);
+                               procedure.exception(graph, t);
                        }
 
                        @Override
@@ -132,7 +138,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
                        @Override
                        public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                               proc.exception(graph, t);
+                               procedure.exception(graph, t);
                        }
 
                        @Override
@@ -148,26 +154,19 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                if(indirect.size() == 0) {
                        int size = material.size(); 
                        if(size == 0) {
-                               if(entry != null) entry.finish(graph, provider);
-                               proc.finished(graph);
+                               procedure.finished(graph);
                                return null;
                        } else if(size == 1) {
                                int single = material.single; 
-                               if(entry != null) {
-                                       entry.add(single);
-                                       entry.finish(graph, provider);
-                               }
-                               proc.execute(graph, single);
-                               proc.finished(graph);
+                               procedure.execute(graph, single);
+                               procedure.finished(graph);
                                return single;
                        } else {
-                               addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
+                               addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
                                return null;
                        }
                }
 
-//             final AtomicInteger finishes = new AtomicInteger(0);
-
                indirect.forEach(new TIntProcedure() {
 
                        int finishes = 0;
@@ -190,27 +189,22 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                                        if(current == indirect.size()) {
                                                int size = material.size(); 
                                                if(size == 0) {
-                                                       if(entry != null) entry.finish(graph, provider);
-                                                       proc.finished(graph);
+                                                       procedure.finished(graph);
                                                        return true;
                                                } else if(size == 1) {
                                                        int single = material.single; 
-                                                       if(entry != null) {
-                                                               entry.add(single);
-                                                               entry.finish(graph, provider);
-                                                       }
-                                                       proc.execute(graph, single);
-                                                       proc.finished(graph);
+                                                       procedure.execute(graph, single);
+                                                       procedure.finished(graph);
                                                        return true;
                                                } else {
-                                                       addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
+                                                       addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
                                                        return true;
                                                }
                                        }
                                        return true;
                                }
 
-                               QueryCache.runnerPrincipalTypes(graph, arg0, entry, null, new IntProcedure() {
+                               QueryCache.runnerPrincipalTypes(graph, arg0, parent, null, new IntProcedure() {
 
                                        @Override
                                        public void execute(ReadGraphImpl graph, int i) {
@@ -226,20 +220,15 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
                                                if(current == indirect.size()) {
                                                        int size = material.size(); 
                                                        if(size == 0) {
-                                                               if(entry != null) entry.finish(graph, provider);
-                                                               proc.finished(graph);
+                                                               procedure.finished(graph);
                                                                return;
                                                        } else if(size == 1) {
                                                                int single = material.single; 
-                                                               if(entry != null) {
-                                                                       entry.add(single);
-                                                                       entry.finish(graph, provider);
-                                                               }
-                                                               proc.execute(graph, single);
-                                                               proc.finished(graph);
+                                                               procedure.execute(graph, single);
+                                                               procedure.finished(graph);
                                                                return;
                                                        } else {
-                                                               addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc);
+                                                               addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure);
                                                                return;
                                                        }
                                                }
@@ -248,7 +237,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
                                        @Override
                                        public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                               proc.exception(graph, t);
+                                               procedure.exception(graph, t);
                                        }
 
                                });
@@ -263,16 +252,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) throws DatabaseException {
-
-               if(entry != null) {
-                       for(int i : material) {
-                               if(!rejects.contains(i)) {
-                                       entry.add(i);
-                               }
-                       }
-                       entry.finish(graph, provider);
-               }
+       private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final IntProcedure proc) throws DatabaseException {
 
                for(int i : material) {
                        if(!rejects.contains(i)) {
@@ -286,26 +266,25 @@ 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) throws DatabaseException {
-
-               //        if((counter++ % 500) == 0) System.out.println("PT " + counter + " mat = " + material.length);
+       private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes parent, final IntProcedure proc) throws DatabaseException {
 
                if(index == material.length) { 
-                       finish(graph, rejects, material, entry, provider, proc);
+                       finish(graph, rejects, material, proc);
                        return;
                }
 
                int type = material[index++];
                while(rejects.contains(type)) {
                        if(index == material.length) { 
-                               finish(graph, rejects, material, entry, provider, proc);
+                               finish(graph, rejects, material, proc);
                                return;
                        }
                        type = material[index++];
                }
+               
                final int nextIndex = index;
 
-               QueryCache.runnerSuperTypes(graph, type, entry, null, new InternalProcedure<IntSet>() {
+               QueryCache.runnerSuperTypes(graph, type, parent, null, new InternalProcedure<IntSet>() {
 
                        @Override
                        public void execute(ReadGraphImpl graph, IntSet supers) throws DatabaseException {
@@ -324,7 +303,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
 
                                }
 
-                               addPrincipalType(graph, rejects, material, nextIndex, provider, entry, proc);
+                               addPrincipalType(graph, rejects, material, nextIndex, provider, parent, proc);
 
                        }
 
@@ -341,65 +320,6 @@ final public class PrincipalTypes extends CollectionUnaryQuery<IntProcedure> {
        public String toString() {
                return "PrincipalTypes[" + id + "]";
        }
-
-       final private void add(int val) {
-               assert(isPending());
-               IntArray v = (IntArray)getResult();
-               v.add(val);
-       }
-
-       final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
-
-               assert(isPending());
-               synchronized(this) {
-                       setReady();
-               }
-
-       }
-
-       @Override
-       public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
-
-               assert(isReady());
-
-       if(handleException(graph, procedure)) return EXCEPTED;
-               
-               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 getResult();
-
-       }
-
-       @Override
-       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();
-                       }
-
-               });
-
-       }
-
-
-    @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(id);
-    }
     
     @Override
     protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {
index 38ed21026f9ff2bbcbc70d7936f0aca30e7fc497..1467015512e38f81e6639fa8077f1808ec37865d 100644 (file)
@@ -16,8 +16,6 @@ import org.simantics.db.request.ExternalRead;
 import org.simantics.db.request.MultiRead;
 import org.simantics.db.request.Read;
 
-import gnu.trove.map.hash.TObjectIntHashMap;
-
 public class QueryCache extends QueryCacheBase {
 
     public QueryCache(QuerySupport querySupport, int threads) {
@@ -674,15 +672,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    AssertedStatements getOrCreateAssertedStatements(QueryProcessor processor, int r1, int r2) throws DatabaseException {
-        AssertedStatements existing = null;
-        synchronized(assertedStatementsMap) {
-            existing = (AssertedStatements)assertedStatementsMap.get(r1,r2);
+    TypeHierarchy getOrCreateTypeHierarchy(QueryProcessor processor, int r) throws DatabaseException {
+        TypeHierarchy existing = null;
+        synchronized(typeHierarchyMap) {
+            existing = (TypeHierarchy)typeHierarchyMap.get(r);
             if(existing == null) {
-                existing = new AssertedStatements(r1,r2);
+                existing = new TypeHierarchy(r);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                assertedStatementsMap.put(keyR2(r1,r2), existing);
+                typeHierarchyMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
@@ -695,34 +693,38 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(AssertedStatements entry) {
-        synchronized(assertedStatementsMap) {
-            assertedStatementsMap.remove(entry.id);
+    void remove(TypeHierarchy entry) {
+        synchronized(typeHierarchyMap) {
+            typeHierarchyMap.remove(entry.id);
         }
     }
     
-    public static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException {
+    public static void runnerTypeHierarchy(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        AssertedStatements entry = (AssertedStatements)cache.getOrCreateAssertedStatements(graph.processor, r1,r2);
-        TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedStatements;
+        if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+            TypeHierarchy.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(graph.processor, r);
+        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureTypeHierarchy;
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
         if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
           assert(entry.isPending());
-            entry.compute(graph, procedure_);
+            TypeHierarchy.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
     
-    AssertedPredicates getOrCreateAssertedPredicates(QueryProcessor processor, int r) throws DatabaseException {
-        AssertedPredicates existing = null;
-        synchronized(assertedPredicatesMap) {
-            existing = (AssertedPredicates)assertedPredicatesMap.get(r);
+    SuperTypes getOrCreateSuperTypes(QueryProcessor processor, int r) throws DatabaseException {
+        SuperTypes existing = null;
+        synchronized(superTypesMap) {
+            existing = (SuperTypes)superTypesMap.get(r);
             if(existing == null) {
-                existing = new AssertedPredicates(r);
+                existing = new SuperTypes(r);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                assertedPredicatesMap.put(keyR(r), existing);
+                superTypesMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
@@ -735,34 +737,38 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(AssertedPredicates entry) {
-        synchronized(assertedPredicatesMap) {
-            assertedPredicatesMap.remove(entry.id);
+    void remove(SuperTypes entry) {
+        synchronized(superTypesMap) {
+            superTypesMap.remove(entry.id);
         }
     }
     
-    public static void runnerAssertedPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
+    public static void runnerSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        AssertedPredicates entry = (AssertedPredicates)cache.getOrCreateAssertedPredicates(graph.processor, r);
-        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedPredicates;
+        if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+            SuperTypes.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(graph.processor, r);
+        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperTypes;
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
         if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
           assert(entry.isPending());
-            entry.compute(graph, procedure_);
+            SuperTypes.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
     
-    DirectSuperRelations getOrCreateDirectSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
-        DirectSuperRelations existing = null;
-        synchronized(directSuperRelationsMap) {
-            existing = (DirectSuperRelations)directSuperRelationsMap.get(r);
+    SuperRelations getOrCreateSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
+        SuperRelations existing = null;
+        synchronized(superRelationsMap) {
+            existing = (SuperRelations)superRelationsMap.get(r);
             if(existing == null) {
-                existing = new DirectSuperRelations(r);
+                existing = new SuperRelations(r);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                directSuperRelationsMap.put(keyR(r), existing);
+                superRelationsMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
@@ -775,34 +781,38 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(DirectSuperRelations entry) {
-        synchronized(directSuperRelationsMap) {
-            directSuperRelationsMap.remove(entry.id);
+    void remove(SuperRelations entry) {
+        synchronized(superRelationsMap) {
+            superRelationsMap.remove(entry.id);
         }
     }
     
-    public static void runnerDirectSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
+    public static void runnerSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        DirectSuperRelations entry = (DirectSuperRelations)cache.getOrCreateDirectSuperRelations(graph.processor, r);
-        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectSuperRelations;
+        if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+            SuperRelations.computeForEach(graph, r, null, procedure);
+            return;
+        }
+        SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(graph.processor, r);
+        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperRelations;
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
         if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
           assert(entry.isPending());
-            entry.compute(graph, procedure_);
+            SuperRelations.computeForEach(graph, r, entry, procedure_);
             if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
         }
     }
     
-    SuperTypes getOrCreateSuperTypes(QueryProcessor processor, int r) throws DatabaseException {
-        SuperTypes existing = null;
-        synchronized(superTypesMap) {
-            existing = (SuperTypes)superTypesMap.get(r);
+    AssertedPredicates getOrCreateAssertedPredicates(QueryProcessor processor, int r) throws DatabaseException {
+        AssertedPredicates existing = null;
+        synchronized(assertedPredicatesMap) {
+            existing = (AssertedPredicates)assertedPredicatesMap.get(r);
             if(existing == null) {
-                existing = new SuperTypes(r);
+                existing = new AssertedPredicates(r);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                superTypesMap.put(keyR(r), existing);
+                assertedPredicatesMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
@@ -815,16 +825,16 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(SuperTypes entry) {
-        synchronized(superTypesMap) {
-            superTypesMap.remove(entry.id);
+    void remove(AssertedPredicates entry) {
+        synchronized(assertedPredicatesMap) {
+            assertedPredicatesMap.remove(entry.id);
         }
     }
     
-    public static void runnerSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+    public static void runnerAssertedPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(graph.processor, r);
-        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperTypes;
+        AssertedPredicates entry = (AssertedPredicates)cache.getOrCreateAssertedPredicates(graph.processor, r);
+        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedPredicates;
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
         if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
@@ -834,15 +844,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    TypeHierarchy getOrCreateTypeHierarchy(QueryProcessor processor, int r) throws DatabaseException {
-        TypeHierarchy existing = null;
-        synchronized(typeHierarchyMap) {
-            existing = (TypeHierarchy)typeHierarchyMap.get(r);
+    AssertedStatements getOrCreateAssertedStatements(QueryProcessor processor, int r1, int r2) throws DatabaseException {
+        AssertedStatements existing = null;
+        synchronized(assertedStatementsMap) {
+            existing = (AssertedStatements)assertedStatementsMap.get(r1,r2);
             if(existing == null) {
-                existing = new TypeHierarchy(r);
+                existing = new AssertedStatements(r1,r2);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                typeHierarchyMap.put(keyR(r), existing);
+                assertedStatementsMap.put(keyR2(r1,r2), existing);
                 size++;
                 return existing;
             }
@@ -855,16 +865,16 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(TypeHierarchy entry) {
-        synchronized(typeHierarchyMap) {
-            typeHierarchyMap.remove(entry.id);
+    void remove(AssertedStatements entry) {
+        synchronized(assertedStatementsMap) {
+            assertedStatementsMap.remove(entry.id);
         }
     }
     
-    public static void runnerTypeHierarchy(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+    public static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(graph.processor, r);
-        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureTypeHierarchy;
+        AssertedStatements entry = (AssertedStatements)cache.getOrCreateAssertedStatements(graph.processor, r1,r2);
+        TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedStatements;
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
         if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
@@ -874,15 +884,15 @@ public class QueryCache extends QueryCacheBase {
         }
     }
     
-    SuperRelations getOrCreateSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
-        SuperRelations existing = null;
-        synchronized(superRelationsMap) {
-            existing = (SuperRelations)superRelationsMap.get(r);
+    DirectSuperRelations getOrCreateDirectSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
+        DirectSuperRelations existing = null;
+        synchronized(directSuperRelationsMap) {
+            existing = (DirectSuperRelations)directSuperRelationsMap.get(r);
             if(existing == null) {
-                existing = new SuperRelations(r);
+                existing = new DirectSuperRelations(r);
                 existing.clearResult(querySupport);
                 existing.setPending();
-                superRelationsMap.put(keyR(r), existing);
+                directSuperRelationsMap.put(keyR(r), existing);
                 size++;
                 return existing;
             }
@@ -895,16 +905,16 @@ public class QueryCache extends QueryCacheBase {
         return existing;
     }
     
-    void remove(SuperRelations entry) {
-        synchronized(superRelationsMap) {
-            superRelationsMap.remove(entry.id);
+    void remove(DirectSuperRelations entry) {
+        synchronized(directSuperRelationsMap) {
+            directSuperRelationsMap.remove(entry.id);
         }
     }
     
-    public static void runnerSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+    public static void runnerDirectSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
         QueryCache cache  = graph.processor.cache;
-        SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(graph.processor, r);
-        InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperRelations;
+        DirectSuperRelations entry = (DirectSuperRelations)cache.getOrCreateDirectSuperRelations(graph.processor, r);
+        IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectSuperRelations;
         ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
         if(entry.isReady()) entry.performFromCache(graph, procedure_);
         else {
index 322e820a2aa76fe0eda4f0bfdc2f971d472d30da..2d3c92d8242ed3259ea0518520262666cbabd423 100644 (file)
@@ -640,7 +640,7 @@ public class QueryCacheBase {
                int counter = 0;
                while(entry.isPending()) {
                        try {
-                               SessionTask task = processor.getOwnTask(processor.thread.get());
+                               SessionTask task = null;//processor.getOwnTask(processor.thread.get());
                                if(task != null) {
                                        task.run(processor.thread.get());
                                } else {
index 115a4a5a1509db803a88140b038d9f475b1e3305..56267cc12d39be24f0d245897977dafe0ec04dfd 100644 (file)
@@ -14,185 +14,167 @@ package org.simantics.db.impl.query;
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
-import org.simantics.db.impl.graph.WriteGraphImpl;
 import org.simantics.db.procedure.AsyncProcedure;
 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<AsyncProcedure<T>> {
+final public class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
 
-       protected Read<T> request;
+    protected Read<T> request;
 
-       public ReadEntry(Read<T> request) {
-       this.request = request;
+    public ReadEntry(Read<T> request) {
+        this.request = request;
     }
-    
+
     @Override
     int makeHash() {
-       return request.hashCode();
+        return request.hashCode();
     }
-       
+
     @Override
     public Object getOriginalRequest() {
         return request;
     }
-    
+
     @Override
     public void discard() {
-       super.discard();
-       setResult(null);
-    }
-       
-    final public Object addOrSet(AsyncReadGraph graph, Object result) {
-       
-//     System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + result);
-
-       assert(assertPending());
-       
-       setResult(result);
-       setReady();
-       
-       return result;
-        
+        super.discard();
+        setResult(null);
     }
 
     @Override
     final public Query getQuery() {
-        
+
         return new Query() {
 
-                       @Override
-                       public void recompute(ReadGraphImpl graph) {
-                               
-                               try {
+            @Override
+            public void recompute(ReadGraphImpl graph) {
+
+                try {
+
+                    T result = request.perform(graph);
+                    setResult(result);
+                    setReady();
+
+                } catch (Throwable t) {
+
+                    except(t);
 
-                                   T result = request.perform(graph);
-                                   addOrSet(graph, result);
+                }
+
+            }
 
-                               } catch (Throwable t) {
+            @Override
+            public void removeEntry(QueryProcessor processor) {
+                processor.cache.remove(ReadEntry.this);
+            }
 
-                                       except(t);
-                    
+            @Override
+            public int type() {
+                if (request instanceof ReadExt) {
+                    return ((ReadExt) request).getType();
+                } else {
+                    return RequestFlags.INVALIDATE;
                 }
-                               
-                       }
-
-                       @Override
-                       public void removeEntry(QueryProcessor processor) {
-                               processor.cache.remove(ReadEntry.this);
-                       }
-
-                       @Override
-                       public int type() {
-                               if(request instanceof ReadExt) {
-                                       return ((ReadExt)request).getType();
-                               } else {
-                                       return RequestFlags.INVALIDATE;
-                               }
-                       }
-                       
-                       @Override
-                       public String toString() {
-                               if(request == null) return "DISCARDED";
-                               else return request.toString() + statusOrException;
-                       }
-               
+            }
+
+            @Override
+            public String toString() {
+                if (request == null)
+                    return "DISCARDED";
+                else
+                    return request.toString() + statusOrException;
+            }
+
         };
-        
+
     }
-    
-    //@Override
-    public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
 
-               ReadGraphImpl queryGraph = graph.withParent(this);
+    public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry,
+            AsyncProcedure<T> procedure_) throws DatabaseException {
 
-               try {
+        AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
 
-                       addOrSet(queryGraph, request.perform(queryGraph));
-                       return get(queryGraph, procedure);
+        ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
 
-               }  catch (DatabaseException e) {
+        try {
 
-                       except(e);
-                       return get(graph, procedure);
+            T result = request.perform(queryGraph);
+            procedure.execute(graph, result);
 
-               }  catch (Throwable t) {
+        } catch (DatabaseException e) {
 
-                       except(new DatabaseException(t));
-                       return get(graph, procedure);
+            procedure.exception(graph, e);
 
-               }
-       
-    }
+        } catch (Throwable t) {
 
-    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;
+            DatabaseException dbe = new DatabaseException(t);
+            procedure.exception(graph, dbe);
 
-               try {
+        }
 
-                       T result = request.perform(queryGraph);
-                       if(entry != null) entry.addOrSet(queryGraph, result);
-                       procedure.execute(graph, result);
+        if (entry != null)
+            entry.performFromCache(queryGraph, procedure_);
 
-               }  catch (DatabaseException e) {
+    }
 
-                       if(entry != null) entry.except(e);
-                       procedure.exception(graph, e);
+    public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
 
-               }  catch (Throwable t) {
+        AsyncProcedure<T> proc = (AsyncProcedure<T>) procedure;
 
-                       DatabaseException dbe = new DatabaseException(t);
-                       if(entry != null) entry.except(dbe);
-                       procedure.exception(graph, dbe);
+        if (proc != null) {
+            if (isExcepted()) {
+                try {
+                    proc.exception(graph, (Throwable) getResult());
+                } catch (Throwable t) {
+                    t.printStackTrace();
+                }
+            } else {
+                try {
+                    proc.execute(graph, (T) getResult());
+                } catch (Throwable t) {
+                    t.printStackTrace();
+                }
+            }
+        }
+
+        return (T) getResult();
 
-               }
-       
     }
-    
-       public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
-           
-        AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
-
-        if(proc != null) {
-               if(isExcepted()) {
-                       try {
-                               proc.exception(graph, (Throwable)getResult());
-                       } catch (Throwable t) {
-                               t.printStackTrace();
-                       }
-               } else {
-                       try {
-                               proc.execute(graph, (T)getResult());
-                       } catch (Throwable t) {
-                               t.printStackTrace();
-                       }
-               }
+
+    @Override
+    public String toString() {
+        if (request == null)
+            return "DISCARDED";
+        else
+            return request.toString() + " - " + statusOrException;
+    }
+
+    public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+        if (procedure != null)
+            performFromCache(graph, procedure);
+        checkAndThrow();
+        return getResult();
+    }
+
+    @Override
+    boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
+        if (request instanceof ReadExt) {
+            return ((ReadExt) request).isImmutable(graph);
         }
-               
-           return (T)getResult();
-           
-       }
-       
-       @Override
-       public String toString() {
-               if(request == null) return "DISCARDED";
-               else return request.toString() + " - " + statusOrException;
-       }
-       
-       public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
-               if(procedure != null) performFromCache(graph, procedure);
-               checkAndThrow();
-               return getResult();
-       }
-       
-       @Override
-       boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
-               if(request instanceof ReadExt) {
-                       return ((ReadExt)request).isImmutable(graph);
-               }
-               return false;
-       }
+        return false;
+    }
+
+    @Override
+    public void execute(AsyncReadGraph graph, T result) {
+        setResult(result);
+        setReady();
+    }
+
+    @Override
+    public void exception(AsyncReadGraph graph, Throwable throwable) {
+        except(throwable);
+    }
 
 }
index 204a0005af1178a19efc6bc2edee8f9ea3747f07..b0a2a1198e2dd304692717b1329ff5a5d7678796 100644 (file)
@@ -17,7 +17,7 @@ import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.request.RequestFlags;
 
-final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<RelationInfo>> {
+final public class RelationInfoQuery extends UnaryQueryP<RelationInfo> {
 
        RelationInfoQuery(final int resource) {
         super(resource);
@@ -28,14 +28,14 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
                provider.cache.remove(this);
        }
 
-       private static void computeAssertions(ReadGraphImpl graph, int r, final boolean isFinal, final boolean isFunctional, RelationInfoQuery entry, final InternalProcedure<RelationInfo> proc) throws DatabaseException {
+       private static void computeAssertions(ReadGraphImpl graph, int r, final boolean isFinal, final boolean isFunctional, RelationInfoQuery parent, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
 
                QueryProcessor processor = graph.processor;
                
            final int isUsedInAssertion = processor.getHasPredicateInverse();
         assert(isUsedInAssertion != 0);
                
-        QueryCache.runnerDirectObjects(graph, r, isUsedInAssertion, entry, null, new IntProcedure() {
+        QueryCache.runnerDirectObjects(graph, r, isUsedInAssertion, parent, null, new IntProcedure() {
 
                boolean done = false;
                        
@@ -44,8 +44,7 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
                                if(done) return;
                                done = true;
                                RelationInfo result = new RelationInfo(r, isFunctional, isFinal, true);
-                               if(entry != null) entry.setResult(result);
-                               proc.execute(graph, result);
+                               procedure.execute(graph, result);
                        }
 
                        @Override
@@ -53,8 +52,7 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
                                if(done) return;
                                done = true;
                                RelationInfo result = new RelationInfo(r, isFunctional, isFinal, false);
-                               if(entry != null) entry.setResult(result);
-                               proc.execute(graph, result);
+                               procedure.execute(graph, result);
                        }
 
                        @Override
@@ -62,15 +60,16 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
                                if(done) return;
                                done = true;
                                DatabaseException e = new DatabaseException("Internal error in RelationInfoQuery");
-                               if(entry != null) entry.except(e);
-                               proc.exception(graph, e);
+                               procedure.exception(graph, e);
                        }
                        
                });
                
        }
        
-       public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
+       public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure<RelationInfo> procedure_) throws DatabaseException {
+           
+           InternalProcedure<RelationInfo> procedure = entry != null ? entry : procedure_;
                
                QueryProcessor provider = graph.processor;
                
@@ -81,77 +80,30 @@ final public class RelationInfoQuery extends UnaryQuery<InternalProcedure<Relati
         IntSet types = QueryCache.resultTypes(graph, r, entry, null);
         
        computeAssertions(graph, r, !direct.contains(superRelationOf), types.contains(graph.processor.getFunctionalRelation()), entry, procedure);
+       
+       if(entry != null) entry.performFromCache(graph, procedure_);
         
        }
 
-       //@Override
-       public Object compute(ReadGraphImpl graph, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
+       @Override
+       public void 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());
-//     
-//     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, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
-
-        assert(isReady());
-        
-       if(handleException(graph, procedure)) return EXCEPTED;
-        
-       RelationInfo result = getResult();
-       
-        procedure.execute(graph, result);
-        
-        return result;
-
-    }
     
     @Override
     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) {
-            }
-            
-            @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
-                throw new Error("Error in recompute.", t);
-            }
-
-        });
-               
-       }
     
 }
index ad4700aa826d6178f9447f81fa8e606ddfd13f0f..a71df27fd38a06d8fbecf0cd6b08b63bec68895d 100644 (file)
@@ -23,7 +23,7 @@ import org.simantics.db.impl.procedure.TripleIntProcedureAdapter;
 import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
 
-final public class Statements extends CollectionBinaryQuery<TripleIntProcedure> {
+final public class Statements extends CollectionBinaryQuery<TripleIntProcedure> implements TripleIntProcedure {
        
     public Statements(final int r1, final int r2) {
         super(r1, r2);
@@ -133,16 +133,11 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
        
     }
        
-    final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException {
-        
-       if(entry != null) {
-               assert(entry.isPending());
-       }
+    final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final TripleIntProcedure procedure) throws DatabaseException {
        
-       IntArray map = getAssertionMap(graph, r1, r2, entry);
+       IntArray map = getAssertionMap(graph, r1, r2, parent);
        if(map == null) {
-            if(entry != null) entry.finish(graph, procedure);
-               else procedure.finished(graph);
+               procedure.finished(graph);
             return;
        }
         
@@ -153,18 +148,12 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                int p = map.data[1];
                int o = map.data[2];
                
-            if(entry != null) {
-               entry.addOrSetFunctional(s,p,o);
-               entry.finish(graph, procedure);
-            } else {
                        procedure.execute(graph, s,p,o);
                        procedure.finished(graph);
-               }
 
         } else if(size == 0) {
 
-            if(entry != null) entry.finish(graph, procedure);
-               else procedure.finished(graph);
+               procedure.finished(graph);
                
         } else {
 
@@ -174,22 +163,12 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                
                        IntSet candidateIs = null;
                        try {
-                               candidateIs = QueryCache.resultSuperTypes(graph, candidateS, entry, null);
+                               candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null);
                        } catch (DatabaseException e) {
-                               if(entry != null) entry.except(e);
                                procedure.exception(graph, e);
                                return;
                        }
 
-
-//                     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) {
 
                                int nextS = map.data[i];
@@ -206,20 +185,11 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                
                                        IntSet nextIs = null;
                                        try {
-                                               nextIs = QueryCache.resultSuperTypes(graph, nextS, entry, null);
+                                               nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
                                        } 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)) {
 
@@ -231,14 +201,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                                candidateIs = nextIs;
                                                
                                        } else {
-
                                                // candidate and next are unrelated => error
                                                ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions.", r1);
-                                               
-                                               if(entry != null) entry.except(exception);
                                                procedure.exception(graph, exception);
                                                return;                                         
-                                               
                                        }
                                
                                }
@@ -247,13 +213,8 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                        
                }
                
-               if(entry != null) {
-               entry.addOrSetFunctional(candidateS, candidateP, candidateO);
-               entry.finish(graph, procedure);
-            } else {
-                       procedure.execute(graph, candidateS, candidateP, candidateO);
-                       procedure.finished(graph);
-               }
+            procedure.execute(graph, candidateS, candidateP, candidateO);
+            procedure.finished(graph);
                
         }
         
@@ -272,7 +233,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) throws DatabaseException {
+       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
         
         if(ri.isFinal) {
             
@@ -281,7 +242,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                if(result == 0) {
 
                // Check for assertions
-               forSingleAssertion(graph, r1, r2, entry, procedure);
+               forSingleAssertion(graph, r1, r2, parent, procedure);
 
                } else if(result == -1) {
 
@@ -289,8 +250,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                        @Override
                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                               if(entry != null) entry.addOrSetFunctional(r1, r2, i);
-                               else procedure.execute(graph, r1, r2, i);
+                               procedure.execute(graph, r1, r2, i);
                        }
 
                        @Override
@@ -305,18 +265,13 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                });
 
                // Check for assertions
-               forSingleAssertion(graph, r1, r2, entry, procedure);
+               forSingleAssertion(graph, r1, r2, parent, procedure);
                        
                } else {
 
                // If functional relation was found there is no need to check assertions
-                       if(entry != null) {
-                               entry.addOrSetFunctional(r1, r2, result);
-                               entry.finish(graph, procedure);
-                       } else {
-                               procedure.execute(graph, r1, r2, result);
-                               procedure.finished(graph);
-                       }
+                procedure.execute(graph, r1, r2, result);
+                procedure.finished(graph);
                        
                }
 
@@ -333,13 +288,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                 public void run(ReadGraphImpl graph) throws DatabaseException {
                     
                     if(found.get()) {
-                       if(entry != null) entry.finish(graph, procedure);
-                       else procedure.finished(graph);
+                       procedure.finished(graph);
                     } else {
-                    
-                           // Check for assertions
-                       forSingleAssertion(graph, r1, r2, entry, procedure);
-                           
+                        // Check for assertions
+                        forSingleAssertion(graph, r1, r2, parent, procedure);
                     }
                     
                 }
@@ -360,16 +312,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                 
                                if(found.compareAndSet(false, true)) {
-                                    
-                                    if(entry != null) entry.addOrSetFunctional(r1, pred, i);
-                                    else procedure.execute(graph, r1, pred, i);
-                                       
+                                    procedure.execute(graph, r1, pred, i);
                                } else {
-
                                        ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
-                                       if(entry != null) entry.except(exception);
                                        procedure.exception(graph, exception);
-                                       
                                }
 
                             }
@@ -391,7 +337,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                         
                         inc();
                     
-                        QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
+                        QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
     
                             @Override
                             public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
@@ -412,16 +358,10 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                                         public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
                                             
                                                if(found.compareAndSet(false, true)) {
-                                                
-                                               if(entry != null) entry.addOrSetFunctional(r1, pred, i);
-                                               else procedure.execute(graph, r1, pred, i);
-                                                       
+                                               procedure.execute(graph, r1, pred, i);
                                                } else {
-
                                                ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
-                                               if(entry != null) entry.except(exception);
                                                procedure.exception(graph, exception);
-                                                       
                                                }
                                             
                                         }
@@ -468,24 +408,20 @@ 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) throws DatabaseException {
+    final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final TripleIntProcedure procedure) throws DatabaseException {
 
-       QueryCache.runnerPrincipalTypes(graph, r1, entry, null, new SyncIntProcedure() {
+       QueryCache.runnerPrincipalTypes(graph, r1, parent, null, new SyncIntProcedure() {
             
             @Override
             public void run(ReadGraphImpl graph) throws DatabaseException {
-                
-                if(entry != null) entry.finish(graph, procedure);
-                else procedure.finished(graph);
-                
+                procedure.finished(graph);
             }
             
             TripleIntProcedure proc = new TripleIntProcedureAdapter() {
 
                 @Override
                 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);
+                       procedure.execute(graph, s, p, o);
                 }
 
                 @Override
@@ -503,10 +439,8 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
             @Override
             public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
-
                 inc();
-                QueryCache.runnerAssertedStatements(graph, type, r2, entry, null, proc);
-                
+                QueryCache.runnerAssertedStatements(graph, type, r2, parent, null, proc);
             }
             
             @Override
@@ -524,7 +458,7 @@ 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) throws DatabaseException {
+       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
 
                if(ri.isFinal) {
 
@@ -532,8 +466,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                                @Override
                                public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                       if(entry != null) entry.addOrSet(r1, r2, i);
-                                       else procedure.execute(graph, r1, r2, i);
+                                       procedure.execute(graph, r1, r2, i);
                                }
 
                                @Override
@@ -549,10 +482,9 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                        });
 
                        if(ri.isAsserted) {
-                               forAssertions(graph, r1, r2, entry, procedure);
+                               forAssertions(graph, r1, r2, parent, procedure);
                        } else {
-                               if(entry != null) entry.finish(graph, procedure);
-                               else procedure.finished(graph);
+                               procedure.finished(graph);
                        }
 
         } else {
@@ -563,9 +495,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
                 
                 @Override
                 public void run(ReadGraphImpl graph) throws DatabaseException {
-                    
-                    forAssertions(graph, r1, r2, entry, procedure);
-                    
+                    forAssertions(graph, r1, r2, parent, procedure);
                 }
 
                 @Override
@@ -580,8 +510,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                             @Override
                             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                               if(entry != null) entry.addOrSet(r1, pred2, i);
-                               else procedure.execute(graph, r1, pred2, i);
+                               procedure.execute(graph, r1, pred2, i);
                             }
 
                             @Override
@@ -603,7 +532,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                         try {
                             
-                            IntSet result = QueryCache.resultSuperRelations(graph, pred2, entry, null);
+                            IntSet result = QueryCache.resultSuperRelations(graph, pred2, parent, null);
                             if(result.contains(r2)) {
 
                                 inc();
@@ -613,8 +542,7 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
 
                                     @Override
                                     public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                                        if(entry != null) entry.addOrSet(r1, pred2, i);
-                                        else procedure.execute(graph, r1, pred2, i);
+                                        procedure.execute(graph, r1, pred2, i);
 
                                     }
 
@@ -652,32 +580,19 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
         
     }
        
-    public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException {
-       
-       if(entry != null) {
-               assert(entry.isPending());
-       }
+    public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure_) throws DatabaseException {
+        
+        TripleIntProcedure procedure = entry != null ? entry : procedure_;
        
-       QueryCache.runnerRelationInfoQuery(graph, r2, entry, null, new InternalProcedure<RelationInfo>() {
-
-            @Override
-            public void execute(ReadGraphImpl graph, final RelationInfo ri) throws DatabaseException {
-               
-               graph.ensureLoaded(r1, r2);
-               if(ri.isFunctional) {
-                       computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               } else {
-                       computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
-               }
-                
-            }
-                       
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                               procedure.exception(graph, t);
-            }
+        RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null);
+        graph.ensureLoaded(r1, r2);
+        if(ri.isFunctional) {
+            computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
+        } else {
+            computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
+        }
 
-        });
+        if(entry != null) entry.performFromCache(graph, procedure_);
         
     }
     
@@ -789,5 +704,20 @@ final public class Statements extends CollectionBinaryQuery<TripleIntProcedure>
     boolean isImmutable(ReadGraphImpl graph) {
        return graph.processor.isImmutable(r1());
     }
+
+    @Override
+    public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
+        addOrSet(s, p, o);
+    }
+
+    @Override
+    public void finished(ReadGraphImpl graph) throws DatabaseException {
+        setReady();
+    }
+
+    @Override
+    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
+    }
     
 }
index b8d29b7e6ea995299e9baa11cc6c6c1be4a78c11..52781fb61f63f344e0f73bf445fe916b2003d41b 100644 (file)
@@ -21,7 +21,7 @@ import org.simantics.db.impl.procedure.InternalProcedure;
 import gnu.trove.procedure.TIntProcedure;
 import gnu.trove.set.hash.TIntHashSet;
 
-final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>> {
+final public class SuperRelations extends UnaryQueryP<IntSet> {
     
     SuperRelations(final int resource) {
         super(resource);
@@ -36,10 +36,7 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
                provider.cache.remove(this);
        }
        
-       static int histoCounter = 0;
-    static int counter = 0;
-    
-    class Koss {
+    static class Koss {
         
         private TIntHashSet set = null;
         public int single = 0;
@@ -69,10 +66,53 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
         
     }
 
-       //@Override
-       public Object compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+       public void compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+           computeForEach(graph, id, this, procedure);
+       }
+
+    static class DirectProcedure extends Koss implements IntProcedure, TIntProcedure, InternalProcedure<IntSet> {
+        
+        IntSet result;
+        InternalProcedure<IntSet> proc;
+        
+        public DirectProcedure(IntSet result, InternalProcedure<IntSet> proc) {
+            this.result = result;
+            this.proc = proc;
+        }
+        
+        @Override
+        final public boolean execute(int r) {
+            result.add(r);
+            return true;
+        }
+        @Override
+        final public void execute(ReadGraphImpl graph, int r) {
+            if(single == 0) {
+                single = r;
+                return;
+            }
+            add(r);
+        }
+        @Override
+        final public void execute(ReadGraphImpl graph, IntSet set) throws DatabaseException {
+            set.forEach(this);
+            proc.execute(graph, result);
+        }
+        @Override
+        public void finished(ReadGraphImpl graph) {
+        }
+        @Override
+        public void exception(ReadGraphImpl graph, Throwable t) {
+            throw new Error("Errors are not supported.", t);
+        }
 
-               QueryProcessor processor = graph.processor;
+    }
+
+       public static Object computeForEach(final ReadGraphImpl graph, int id, SuperRelations entry, final InternalProcedure<IntSet> procedure_) throws DatabaseException {
+           
+           InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
+
+           QueryProcessor processor = graph.processor;
                
                processor.querySupport.ensureLoaded(graph, id);
                
@@ -82,56 +122,19 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
 
            final IntSet result = new IntSet(processor.querySupport);
 
-           final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure, InternalProcedure<IntSet> {
-               @Override
-               final public boolean execute(int r) {
-                       result.add(r);
-                       return true;
-               }
-               @Override
-               final public void execute(ReadGraphImpl graph, int r) {
-                       if(single == 0) {
-                               single = r;
-                               return;
-                       }
-                       add(r);
-               }
-               @Override
-               final public void execute(ReadGraphImpl graph, IntSet set) throws DatabaseException {
-                       set.forEach(this);
-                       addOrSet(graph, result, processor);
-                       proc.execute(graph, result);
-               }
-               @Override
-               public void finished(ReadGraphImpl graph) {
-               }
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable t) {
-                       throw new Error("Errors are not supported.", t);
-               }
-
-           }
-
-           final DirectProcedure directProc = new DirectProcedure();
+           final DirectProcedure directProc = new DirectProcedure(result, proc);
 
           processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
 
            int size = directProc.size();
 
            if(size == 0) {
-
-               addOrSet(graph, IntSet.EMPTY, processor);
                proc.execute(graph, IntSet.EMPTY);
-
            } else if (size == 1) {
-
                result.add(directProc.single);
-               QueryCache.runnerSuperRelations(graph, directProc.single, SuperRelations.this, null, directProc);
-
+               QueryCache.runnerSuperRelations(graph, directProc.single, entry, null, directProc);
            } else {
 
-               //                  if((counter++ % 500) == 0) System.out.println("SR " + counter);
-
                final TIntProcedure addToResult = new TIntProcedure() {
                        @Override
                        public boolean execute(int r) {
@@ -162,14 +165,13 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
                                        result.add(arg0);
                                }
 
-                               QueryCache.runnerSuperRelations(graph, arg0, SuperRelations.this, null, new InternalProcedure<IntSet>() {
+                               QueryCache.runnerSuperRelations(graph, arg0, entry, null, new InternalProcedure<IntSet>() {
 
                                        @Override
                                        public void execute(ReadGraphImpl graph, IntSet set) throws DatabaseException {
                                                set.forEach(addToResult);
                                                int current = finishes.addAndGet(1);
                                                if(current == directProc.size()) {
-                                                       addOrSet(graph, result, processor);
                                                        proc.execute(graph, result);
                                                        return; 
                                                }
@@ -190,6 +192,8 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
 
            }
            
+           if(entry != null) entry.performFromCache(graph, procedure_);
+            
            return result;
         
     }
@@ -198,57 +202,5 @@ final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>>
     public String toString() {
        return "SuperRelations[" + id + "]";
     }
-
-    private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
-
-        assert(!isReady());
-
-        synchronized(this) {
-        
-            value.trim();
-            setResult(value);
-            setReady();
-        
-        }
-        
-    }
-    
-    @Override
-    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
-              
-        assert(isReady());
-
-       if(handleException(graph, procedure)) return null;
-        
-       IntSet result = getResult();
-       
-        procedure.execute(graph, result);
-        
-        return result;
-        
-    }
-
-    @Override
-    public void recompute(ReadGraphImpl graph) throws DatabaseException {
-        
-        compute(graph, new InternalProcedure<IntSet>() {
-
-               @Override
-               public void execute(ReadGraphImpl graph, IntSet result) {
-               }
-
-            @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
-                new Error("Error in recompute.", t).printStackTrace();
-            }
-
-        });
-        
-    }
-
-    @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(id);
-    }
     
 }
index a01c84699ce3181febae84ac376f7f93bf62e234..c7efade251d1f184968a368490d6a6938cbac031 100644 (file)
@@ -14,38 +14,29 @@ package org.simantics.db.impl.query;
 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>> {
+final public class SuperTypes extends UnaryQueryP<IntSet> {
        
     SuperTypes(final int resource) {
         super(resource);
     }
-    
-//    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
        final public void removeEntry(QueryProcessor provider) {
                provider.cache.remove(this);
        }
 
-       //@Override
-       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);
-               }
+       @Override
+       public void compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+               computeForEach(graph, id, this, procedure);
        }
        
-       public static Object computeForEach(ReadGraphImpl graph, int r, SuperTypes entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+       public static Object computeForEach(ReadGraphImpl graph, int r, SuperTypes entry, final InternalProcedure<IntSet> procedure_) throws DatabaseException {
 
+           InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
+           
                QueryProcessor provider = graph.processor;
 
         final int inherits = provider.getInherits();
@@ -66,17 +57,12 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
             
             @Override
             public void run(ReadGraphImpl graph) throws DatabaseException {
-
-                addOrSet(graph, entry, result);
                 procedure.execute(graph, result);
-                
             }
             
             @Override
             public void execute(ReadGraphImpl graph, final int i) throws DatabaseException {
                 
-//             assert(graph.parent == parent);
-               
                synchronized(result) {
                        result.add(i);
                }
@@ -109,65 +95,16 @@ final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
             
         });
         
-        return result;
-        
-    }
-    
-    @Override
-    public String toString() {
-       return "SuperTypes2[" + id + "]";
-    }
-
-    private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
-       
-        assert(!isReady());
-
-        synchronized(this) {
-        
-            value.trim();
-            setResult(value);
-            setReady();
-        
-        }
-        
-    }
-
-    @Override
-    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
-       
-       assert(isReady());
-
-       if(handleException(graph, procedure)) return null;
-       
-       IntSet result = getResult();
-       
-        procedure.execute(graph, result);
+        if(entry != null) entry.performFromCache(graph, procedure_);
         
         return result;
         
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph) throws DatabaseException {
-
-        compute(graph, new InternalProcedure<IntSet>() {
-
-               @Override
-               public void execute(ReadGraphImpl graph, IntSet result) {
-               }
-
-            @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
-                new Error("Error in recompute.", t).printStackTrace();
-            }
-
-        });
-        
+    public String toString() {
+       return "SuperTypes[" + id + "]";
     }
     
-    @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(id);
-    }
     
 }
index ad4581bb6a3e6045c877193dd9aecf8e6c316fd7..cc8ee07ded8fc2937bfa43926c6cb9e4c2b0b254 100644 (file)
@@ -18,7 +18,7 @@ import org.simantics.db.procedure.ListenerBase;
 
 import gnu.trove.procedure.TIntProcedure;
 
-final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
+final public class TypeHierarchy extends UnaryQueryP<IntSet> {
     
     TypeHierarchy(final int resource) {
         super(resource);
@@ -33,8 +33,14 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
                provider.cache.remove(this);
        }
 
-       //@Override
-       public IntSet compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+       @Override
+       public void compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+           computeForEach(graph, id, this, procedure);
+       }
+
+       public static IntSet computeForEach(ReadGraphImpl graph, int id, TypeHierarchy entry, final InternalProcedure<IntSet> procedure_) throws DatabaseException {
+           
+          InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
 
                QueryProcessor processor = graph.processor;
                
@@ -48,15 +54,12 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
             }
         };
         
-        QueryCache.runnerSuperTypes(graph, id, TypeHierarchy.this, null, new InternalProcedure<IntSet>() {
+        QueryCache.runnerSuperTypes(graph, id, entry, null, new InternalProcedure<IntSet>() {
 
                @Override
                public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
-
                        types.forEach(addToResult);
-                       addOrSet(graph, result, processor);
                        procedure.execute(graph, result); 
-
                }
 
                @Override
@@ -66,65 +69,15 @@ final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
 
         });
         
-        return result;
-                        
-    }
-    
-    @Override
-    public String toString() {
-       return "TypeHierarchy[" + id + "]";
-    }
-
-    private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
-       
-        assert(!isReady());
-
-        synchronized(this) {
-        
-            value.trim();
-            setResult(value);
-            setReady();
-        
-        }
-        
-    }
-
-    @Override
-    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
-       
-       assert(isReady());
-
-       if(handleException(graph, procedure)) return null;
-       
-       IntSet result = getResult();
-       
-        procedure.execute(graph, result);
+        if(entry != null) entry.performFromCache(graph, procedure_);
         
         return result;
-        
-    }
-    
-    @Override
-    public void recompute(ReadGraphImpl graph) throws DatabaseException {
-    
-        compute(graph, new InternalProcedure<IntSet>() {
-
-               @Override
-               public void execute(ReadGraphImpl graph, IntSet result) {
-               }
-
-            @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
-                new Error("Error in recompute.", t).printStackTrace();
-            }
 
-        });
-        
     }
     
     @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(id);
+    public String toString() {
+       return "TypeHierarchy[" + id + "]";
     }
 
 }
index 512045e17be63f354fd12a41a9d87cfd17d1afca..35c43885adc83161416041709b83e10f5abcb6d2 100644 (file)
@@ -19,154 +19,156 @@ import org.simantics.db.impl.procedure.InternalProcedure;
 
 import gnu.trove.procedure.TIntProcedure;
 
-final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
-       
+final public class Types extends UnaryQueryP<IntSet> {
+
     Types(final int resource) {
         super(resource);
     }
 
-       @Override
-       final public void removeEntry(QueryProcessor provider) {
-               provider.cache.remove(this);
-       }
+    @Override
+    final public void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
+    }
 
-       //@Override
-       public Object compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
-               computeForEach(graph, id, this, procedure);
-               return getResult();
-       }
+    @Override
+    public void compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+        computeForEach(graph, id, this, procedure);
+    }
 
-       public static void computeForEach(final ReadGraphImpl graph, int id, Types entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+    public static void computeForEach(final ReadGraphImpl graph, int id, Types entry,
+            final InternalProcedure<IntSet> procedure_) throws DatabaseException {
 
-               assert(procedure != null);
+        InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
+
+        computeForEach2(graph, id, entry, procedure);
+
+        if (entry != null)
+            entry.performFromCache(graph, procedure_);
+
+    }
 
-               QueryProcessor processor = graph.processor;
+    public static void computeForEach2(final ReadGraphImpl graph, int id, Types parent,
+            final InternalProcedure<IntSet> procedure) throws DatabaseException {
 
-               processor.querySupport.ensureLoaded(graph, id);
-               
-               int ret = processor.querySupport.getSingleInstance(id);
-               if(ret > 0) {
+        QueryProcessor processor = graph.processor;
 
-                       TypeHierarchy.queryEach(graph, ret, processor, entry, null, new InternalProcedure<IntSet>() {
+        processor.querySupport.ensureLoaded(graph, id);
 
-                               @Override
-                               public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
+        int ret = processor.querySupport.getSingleInstance(id);
+        if (ret > 0) {
 
-                                       if(entry != null) {
-                                               entry.addOrSet(graph, types, processor);
-                                               entry.finish();
-                                       }
-                                       procedure.execute(graph, types);
+            TypeHierarchy.queryEach(graph, ret, processor, parent, null, new InternalProcedure<IntSet>() {
 
-                               }
+                @Override
+                public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
+                    procedure.execute(graph, types);
+                }
 
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       procedure.exception(graph, t);
-                               }
+                @Override
+                public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                    procedure.exception(graph, t);
+                }
 
-                       });
-                       
-                       return;
+            });
 
-               }
-               
-               final int instanceOf = processor.getInstanceOf();
+            return;
+
+        }
+
+        final int instanceOf = processor.getInstanceOf();
         final int inherits = processor.getInherits();
         final int subrelationOf = processor.getSubrelationOf();
 
         final IntSet result = new IntSet(processor.querySupport);
-        
+
         final TIntProcedure addToResult = new TIntProcedure() {
             @Override
             public boolean execute(int r) {
-               synchronized(result) {
-                       result.add(r);
-               }
+                synchronized (result) {
+                    result.add(r);
+                }
                 return true;
             }
         };
-        
+
         final AtomicInteger finishes = new AtomicInteger(0);
-        
+
         SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
-            
+
             @Override
             public void run(ReadGraphImpl graph) throws DatabaseException {
-               
-                if(finishes.addAndGet(1) == 3) {
-                    if(entry != null) entry.addOrSet(graph, result, processor);
+
+                if (finishes.addAndGet(1) == 3) {
                     procedure.execute(graph, result);
-               }
-                
+                }
+
             }
-            
+
             @Override
             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                
-               result.add(i);
-                
+
+                result.add(i);
+
                 inc();
 
-                QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerSuperTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
 
                     @Override
                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
                         types.forEach(addToResult);
                         dec(graph);
                     }
-                               
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       procedure.exception(graph, t);
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                        procedure.exception(graph, t);
                         dec(graph);
-                   }
+                    }
 
                 });
-                
+
             }
 
             @Override
             public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
-            
-        }; 
-        
+
+        };
+
         SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
-            
+
             @Override
             public void run(ReadGraphImpl graph) throws DatabaseException {
 
-               int current = finishes.addAndGet(1);
-               if(current == 3) {
-                    if(entry != null) entry.addOrSet(graph, result, processor);
+                int current = finishes.addAndGet(1);
+                if (current == 3) {
                     procedure.execute(graph, result);
-               }
-                
+                }
+
             }
-            
+
             @Override
             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                
+
                 inc();
 
-                QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
 
                     @Override
                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
                         types.forEach(addToResult);
                         dec(graph);
                     }
-                               
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       procedure.exception(graph, t);
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                        procedure.exception(graph, t);
                         dec(graph);
-                   }
+                    }
 
                 });
-                
+
             }
 
             @Override
@@ -175,124 +177,67 @@ final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
                 dec(graph);
 
             }
-            
-        }; 
+
+        };
 
         SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
 
             @Override
             public void run(ReadGraphImpl graph) throws DatabaseException {
 
-               int current = finishes.addAndGet(1);
-               if(current == 3) {
-                    if(entry != null) entry.addOrSet(graph, result, processor);
+                int current = finishes.addAndGet(1);
+                if (current == 3) {
                     procedure.execute(graph, result);
-               }
-                
+                }
+
             }
-            
+
             @Override
             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
-                
+
                 inc();
-                
-                QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
+
+                QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
 
                     @Override
                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
 
                         types.forEach(addToResult);
                         dec(graph);
-                        
+
                     }
-                               
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                                       procedure.exception(graph, t);
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                        procedure.exception(graph, t);
                         dec(graph);
-                   }
+                    }
 
                 });
-                
+
             }
 
             @Override
             public void finished(ReadGraphImpl graph) throws DatabaseException {
-                
+
                 dec(graph);
 
             }
-            
-        }; 
-        
+
+        };
+
         processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
         instanceOfProcedure.finished(graph);
         processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
         inheritsProcedure.finished(graph);
         processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
         subrelationOfProcedure.finished(graph);
-        
-        if(entry != null) entry.finish();
-        
-    }
-    
-    @Override
-    public String toString() {
-       return "Types[" + id + "]";
-    }
-    
-    private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
-       
-        assert(!isReady());
 
-        setResult(value);
-        
     }
-    
-    void finish() {
 
-       IntSet result = getResult(); 
-       result.trim();
-       setReady();
-       
-    }
-    
-    @Override
-    final public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
-        
-       assert(isReady());
-       
-       if(handleException(graph, procedure)) return EXCEPTED;
-       
-       IntSet result = getResult();
-       
-        procedure.execute(graph, result);
-        
-        return result;
-        
-    }
-    
     @Override
-    public void recompute(ReadGraphImpl graph) throws DatabaseException {
-
-        compute(graph, new InternalProcedure<IntSet>() {
-
-               @Override
-               public void execute(ReadGraphImpl graph, IntSet result) {
-               }
-
-            @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
-                new Error("Error in recompute.", t).printStackTrace();
-            }
-
-        });
-        
+    public String toString() {
+        return "Types[" + id + "]";
     }
 
-    @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(id);
-    }
-    
 }
index 918f882f179c2acd4ba8b79677f473abacf67deb..547264f4b0c99fb75368e5c448d82965038994a1 100644 (file)
@@ -19,7 +19,7 @@ import org.simantics.db.exception.ResourceNotFoundException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 
-public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
+public class URIToResource extends StringQuery<InternalProcedure<Integer>> implements InternalProcedure<Integer> {
 
     URIToResource(final String id) {
         super(id);
@@ -36,12 +36,13 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
         return getResult();
     }
 
-    static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) throws DatabaseException {
+    static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure_) throws DatabaseException {
+        
+        InternalProcedure<Integer> procedure = entry != null ? entry : procedure_;
         
         if("http://".equals(id) || "http:/".equals(id)) {
             
                QueryProcessor processor = graph.processor;
-            if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary());
             procedure.execute(graph, processor.getRootLibrary());
 
         } else {
@@ -49,8 +50,6 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
             final String[] parts = URIStringUtils.splitURI(id);
             if (parts != null) {
 
-               //Integer parentId = QueryCache.resultURIToResource(graph, parts[0], entry, null);
-               
                QueryCache.runnerURIToResource(graph, parts[0], entry, null, new InternalProcedure<Integer>() {
 
                     @Override
@@ -58,31 +57,17 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
                         
                         ObjectResourceIdMap<String> map = QueryCache.resultChildMap(graph, parentId, entry, null);
                         assert(map != null);
-//                      if(map == null) {
-//                          throw new DatabaseException("Internal Error, contact application support.");
-//                            if(entry != null) entry.except(e);
-//                            procedure.exception(graph, e);
-////                            procedure.execute(graph, 0);
-////                            if(entry != null) entry.addOrSet(graph, graph.processor, 0);
-//                      } else {
-                            int result = map.getId(URIStringUtils.unescape(parts[1]));
-                            if(result == 0) {
-                                ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
-                                if(entry != null) entry.except(e);
-                                procedure.exception(graph, e);
-                            } else {
-                                if(entry != null) entry.addOrSet(graph, graph.processor, result);
-                                procedure.execute(graph, result);
-                            }
-//                      }
-                        
-                        // TODO Auto-generated method stub
-                        
+                        int result = map.getId(URIStringUtils.unescape(parts[1]));
+                        if (result == 0) {
+                            ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
+                            procedure.exception(graph, e);
+                        } else {
+                            procedure.execute(graph, result);
+                        }
                     }
 
                     @Override
                     public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
-                        if(entry != null) entry.except(throwable);
                         procedure.exception(graph, throwable);
                     }
                     
@@ -90,18 +75,17 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
                
                        
             } else {
-               
                 ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
-               if(entry != null) entry.except(e);
-               procedure.exception(graph, e);
-                
+                procedure.exception(graph, e);
             }
 
         }
         
+        if(entry != null) entry.performFromCache(graph, procedure_);
+        
     }
     
-    public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {
+    public void addOrSet(Integer result) {
 
         assert(isPending());
 
@@ -148,5 +132,18 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
         });
         
     }
+
+    @Override
+    public void execute(ReadGraphImpl graph, Integer result) throws DatabaseException {
+        synchronized(this) {
+            setResult(result);
+            setReady();
+        }
+    }
+
+    @Override
+    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
+    }
        
 }
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQueryP.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQueryP.java
new file mode 100644 (file)
index 0000000..08ebff9
--- /dev/null
@@ -0,0 +1,76 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.impl.query;
+
+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;
+
+abstract public class UnaryQueryP<R> extends UnaryQuery<InternalProcedure<R>> implements InternalProcedure<R> {
+
+    public UnaryQueryP(int r) {
+        super(r);
+    }
+
+    abstract public void compute(ReadGraphImpl graph, InternalProcedure<R> procedure) throws DatabaseException;
+    
+    @Override
+    final public void recompute(ReadGraphImpl graph) throws DatabaseException {
+
+        compute(graph, new InternalProcedure<R>() {
+
+            @Override
+            public void execute(ReadGraphImpl graph, R result) {
+            }
+
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) {
+                if (DebugException.DEBUG)
+                    new DebugException(t).printStackTrace();
+                throw new Error("Error in recompute.", t);
+            }
+
+        });
+
+    }
+
+    @Override
+    final public Object performFromCache(ReadGraphImpl graph, InternalProcedure<R> procedure)
+            throws DatabaseException {
+
+        if (handleException(graph, procedure))
+            return (Throwable) statusOrException;
+
+        procedure.execute(graph, (R)getResult());
+
+        return result;
+
+    }
+    
+    @Override
+    final public void execute(ReadGraphImpl graph, R result) throws DatabaseException {
+        setResult(result);
+        setReady();
+    }
+    
+    @Override
+    final public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
+    }
+    
+    @Override
+    final boolean isImmutable(ReadGraphImpl graph) {
+        return graph.processor.isImmutable(id);
+    }
+    
+}
index b96e13d42fc14d177779ef021564dfb1fdd22fed..e533ee30f4ee4f521316b56981c8a13f68c4c6af 100644 (file)
@@ -15,7 +15,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 
-final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {
+final public class ValueQuery extends UnaryQueryP<byte[]> {
     
     ValueQuery(final int resource) {
         super(resource);
@@ -25,59 +25,30 @@ final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {
        final public void removeEntry(QueryProcessor provider) {
                provider.cache.remove(this);
        }
+       
+       @Override
+       public void compute(ReadGraphImpl graph, InternalProcedure<byte[]> procedure) throws DatabaseException {
+           computeForEach(graph, id, this, procedure);
+       }
                
-       public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure<byte[]> procedure) throws DatabaseException {
+       public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure<byte[]> procedure_) throws DatabaseException {
+           
+           InternalProcedure<byte[]> procedure = entry != null ? entry : procedure_;
 
                graph.ensureLoaded(r);
                
                byte[] value = graph.getValue(r);
-               if(entry != null) {
-                       entry.setResult(value);
-                       entry.setReady();
-               }
-               if(procedure != null) {
-                       procedure.execute(graph, value);
-               }
+               if(procedure != null) procedure.execute(graph, value);
+               
+               if(entry != null && procedure_ != null) entry.performFromCache(graph, procedure_);
                
                return value;
                
        }
-
-//     public Object compute(ReadGraphImpl graph, final InternalProcedure<byte[]> procedure) throws DatabaseException {
-//             return computeForEach(graph, id, this, procedure);
-//    }
     
     @Override
     public String toString() {
        return "Value[" + id + "]";
     }
-
-    @Override
-    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<byte[]> procedure) throws DatabaseException {
-       return computeForEach(graph, id, this, procedure);
-    }
-    
-    @Override
-    public void recompute(ReadGraphImpl graph) throws DatabaseException {
-       
-       computeForEach(graph, id, this, new InternalProcedure<byte[]>() {
-
-            @Override
-            public void execute(ReadGraphImpl graph, byte[] result) {
-            }
-                       
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               throw new Error("Error in recompute.", t);
-            }
-
-        });
-        
-    }
-    
-    @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(id);
-    }
     
 }
index f30e6e156f13b0a4f8d37d3ad84b2ce3d4903a27..df3b6904f3beb27060b094e6a604f57449d0ce38 100644 (file)
@@ -826,7 +826,7 @@ public final class ClusterTable implements IClusterTable {
                 System.err.println("value " + cc.getValueIndex()[i] + " changed.");
         }
     }
-    final void refreshImportance(ClusterImpl c) {
+    final synchronized void refreshImportance(ClusterImpl c) {
 
         if (c.isWriteOnly())
             return;