]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java
Merge "Multiple reader thread support for db client"
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / TypeHierarchy.java
index a8a3ea3179867c3bb81319e8bbcfbabcb28e3e30..5c2be0c64e6dca1908b182ee340073de9405bfa3 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.impl.query;\r
-\r
-import gnu.trove.procedure.TIntProcedure;\r
-\r
-import java.util.concurrent.Semaphore;\r
-\r
-import org.simantics.db.impl.graph.ReadGraphImpl;\r
-import org.simantics.db.impl.procedure.InternalProcedure;\r
-import org.simantics.db.procedure.ListenerBase;\r
-\r
-final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {\r
-    \r
-//     public ArrayList<InternalProcedure<IntSet>> procs = null;\r
-       \r
-    private TypeHierarchy(final int resource) {\r
-        super(resource);\r
-    }\r
-    \r
-    final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {\r
-\r
-       TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r);\r
-        if(entry == null) {\r
-\r
-               entry = new TypeHierarchy(r);\r
-               entry.setPending();\r
-               entry.clearResult(provider.querySupport);\r
-               entry.putEntry(provider);\r
-               \r
-            provider.performForEach(graph, entry, parent, listener, procedure);\r
-            \r
-        } else {\r
-               \r
-            if(!entry.isReady()) {\r
-               synchronized(entry) {\r
-                    if(!entry.isReady()) {\r
-                        throw new IllegalStateException();\r
-//                     if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<IntSet>>();\r
-//                     entry.procs.add(procedure);\r
-//                                             provider.registerDependencies(graph, entry, parent, listener, procedure, false);\r
-//                     return;\r
-                    }\r
-               }\r
-            }\r
-            provider.performForEach(graph, entry, parent, listener, procedure);\r
-        }\r
-\r
-    }\r
-    \r
-    final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {\r
-        \r
-        if(parent == null && listener == null) {\r
-               TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r);\r
-               if(entry != null && entry.isReady()) { \r
-                       entry.performFromCache(graph, provider, procedure);\r
-                       return;\r
-               }\r
-        }\r
-        \r
-        runner(graph, r, parent, provider, listener, procedure);\r
-         \r
-    }\r
-       \r
-       @Override\r
-       public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {\r
-        return provider.typeHierarchyMap.get(id);\r
-       }\r
-       \r
-       @Override\r
-       public void putEntry(QueryProcessor provider) {\r
-        provider.typeHierarchyMap.put(id, this);\r
-       }\r
-\r
-       @Override\r
-       final public void removeEntry(QueryProcessor provider) {\r
-               provider.typeHierarchyMap.remove(id);\r
-       }\r
-\r
-       @Override\r
-       public IntSet computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {\r
-\r
-        final IntSet result = new IntSet(provider.querySupport, id);\r
-        \r
-        final TIntProcedure addToResult = new TIntProcedure() {\r
-            @Override\r
-            public boolean execute(int r) {\r
-               result.add(r);\r
-                return true;\r
-            }\r
-        };\r
-        \r
-        SuperTypes.queryEach(graph, id, provider, TypeHierarchy.this, null, new InternalProcedure<IntSet>() {\r
-\r
-               @Override\r
-               public void execute(ReadGraphImpl graph, IntSet types) {\r
-\r
-                       types.forEach(addToResult);\r
-                       addOrSet(graph, result, provider);\r
-                       procedure.execute(graph, result); \r
-\r
-               }\r
-\r
-               @Override\r
-               public void exception(ReadGraphImpl graph, Throwable t) {\r
-                       procedure.exception(graph, t);\r
-               }\r
-\r
-        });\r
-        \r
-        return result;\r
-                        \r
-    }\r
-    \r
-    @Override\r
-    public String toString() {\r
-       return "TypeHierarchy[" + id + "]";\r
-    }\r
-\r
-    private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {\r
-       \r
-        assert(!isReady());\r
-\r
-//        ArrayList<InternalProcedure<IntSet>> p = null;\r
-\r
-        synchronized(this) {\r
-        \r
-            value.trim();\r
-            setResult(value);\r
-            setReady();\r
-//            p = procs;\r
-//            procs = null; \r
-        \r
-        }\r
-\r
-//        if(p != null) {\r
-//             IntSet v = (IntSet)getResult();\r
-//             if(v != null) {\r
-//                 for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);\r
-//             }\r
-//        }\r
-        \r
-    }\r
-\r
-    @Override\r
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {\r
-       \r
-       assert(isReady());\r
-\r
-       if(handleException(graph, procedure)) return null;\r
-       \r
-       IntSet result = getResult();\r
-       \r
-        procedure.execute(graph, result);\r
-        \r
-        return result;\r
-        \r
-    }\r
-    \r
-    @Override\r
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {\r
-        \r
-        final Semaphore s = new Semaphore(0);\r
-\r
-        computeForEach(graph, provider, new InternalProcedure<IntSet>() {\r
-\r
-               @Override\r
-               public void execute(ReadGraphImpl graph, IntSet result) {\r
-                s.release();\r
-               }\r
-\r
-            @Override\r
-            public void exception(ReadGraphImpl graph, Throwable t) {\r
-               s.release();\r
-                new Error("Error in recompute.", t).printStackTrace();\r
-            }\r
-\r
-        }, true);\r
-\r
-        while(!s.tryAcquire()) {\r
-               provider.resume(graph);\r
-        }\r
-        \r
-    }\r
-    \r
-    @Override\r
-    boolean isImmutable(ReadGraphImpl graph) {\r
-       return graph.processor.isImmutable(id);\r
-    }    \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2018 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.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;
+
+public final class TypeHierarchy extends UnaryQueryP<IntSet> {
+
+    TypeHierarchy(int resource) {
+        super(resource);
+    }
+
+    public static final void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+        QueryCache.runnerTypeHierarchy(graph, r, parent, listener, procedure);
+    }
+
+    @Override
+    public final void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
+    }
+
+    @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;
+
+        final IntSet result = new IntSet(processor.querySupport, id);
+
+        final TIntProcedure addToResult = new TIntProcedure() {
+            @Override
+            public boolean execute(int r) {
+               result.add(r);
+                return true;
+            }
+        };
+
+        QueryCache.runnerSuperTypes(graph, id, entry, null, new InternalProcedure<IntSet>() {
+
+            @Override
+            public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
+                types.forEach(addToResult);
+                procedure.execute(graph, result); 
+            }
+
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                procedure.exception(graph, t);
+            }
+
+        });
+
+        if(entry != null) entry.performFromCache(graph, procedure_);
+
+        return result;
+
+    }
+
+    @Override
+    public String toString() {
+        return "TypeHierarchy[" + id + "]";
+    }
+
+}