-/*******************************************************************************\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 SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {\r
- \r
-// public ArrayList<InternalProcedure<IntSet>> procs = null;\r
- \r
- private SuperTypes(final int resource) {\r
- super(resource);\r
- }\r
- \r
- final static SuperTypes runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {\r
-\r
- SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);\r
- if(entry == null) {\r
- \r
- entry = new SuperTypes(r);\r
- entry.setPending();\r
- entry.clearResult(provider.querySupport);\r
- entry.putEntry(provider);\r
- \r
- provider.performForEach(graph, entry, parent, listener, procedure);\r
- \r
- return entry;\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 entry;\r
- }\r
- }\r
- }\r
- provider.performForEach(graph, entry, parent, listener, procedure);\r
- }\r
- \r
- return entry;\r
-\r
- }\r
- \r
- final public static SuperTypes 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
- SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);\r
- if(entry != null && entry.isReady()) { \r
- entry.performFromCache(graph, provider, procedure);\r
- return entry;\r
- }\r
- }\r
- \r
- return runner(graph, r, parent, provider, listener, procedure);\r
- \r
- }\r
- \r
- @Override\r
- public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {\r
- return provider.superTypesMap.get(id);\r
- }\r
- \r
- @Override\r
- public void putEntry(QueryProcessor provider) {\r
- provider.superTypesMap.put(id, this);\r
- }\r
-\r
- @Override\r
- final public void removeEntry(QueryProcessor provider) {\r
- provider.superTypesMap.remove(id);\r
- }\r
-\r
- @Override\r
- public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {\r
-\r
- final int inherits = provider.getInherits();\r
- \r
- final CacheEntry parent = graph.parent;\r
- \r
- assert(graph.parent != this);\r
-\r
- final IntSet result = new IntSet(provider.querySupport);\r
- \r
- final TIntProcedure addToResult = new TIntProcedure() {\r
- @Override\r
- public boolean execute(int r) {\r
- synchronized(result) {\r
- result.add(r);\r
- }\r
- return true;\r
- }\r
- };\r
-\r
- DirectObjects.queryEach(graph, id, inherits, provider, this, null, new SyncIntProcedure() {\r
- \r
- @Override\r
- public void run(ReadGraphImpl graph) {\r
-\r
- addOrSet(graph, result, provider);\r
- procedure.execute(graph, result);\r
- \r
- }\r
- \r
- @Override\r
- public void execute(ReadGraphImpl graph, final int i) {\r
- \r
- assert(graph.parent == parent);\r
- \r
- synchronized(result) {\r
- result.add(i);\r
- }\r
- \r
- inc();\r
-\r
- SuperTypes.queryEach(graph, i, provider, SuperTypes.this, null, new InternalProcedure<IntSet>() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, IntSet types) {\r
-\r
- types.forEach(addToResult);\r
- dec(graph);\r
- \r
- }\r
- \r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- procedure.exception(graph, t);\r
- }\r
-\r
- });\r
- \r
- }\r
-\r
- @Override\r
- public void finished(ReadGraphImpl graph) {\r
- dec(graph);\r
- }\r
- \r
- });\r
- \r
- return result;\r
- \r
- }\r
- \r
- @Override\r
- public String toString() {\r
- return "SuperTypes2[" + 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
-}\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 gnu.trove.procedure.TIntProcedure;
+
+public final class SuperTypes extends UnaryQueryPIntSet {
+
+ SuperTypes(int resource) {
+ super(resource);
+ }
+
+ @Override
+ public final void removeEntry(QueryProcessor provider) {
+ provider.cache.remove(this);
+ }
+
+ @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 {
+
+ InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
+
+ QueryProcessor provider = graph.processor;
+
+ final int inherits = provider.getInherits();
+
+ final IntSet result = new IntSet(provider.querySupport);
+
+ final TIntProcedure addToResult = new TIntProcedure() {
+ @Override
+ public boolean execute(int r) {
+ synchronized(result) {
+ result.add(r);
+ }
+ return true;
+ }
+ };
+
+ QueryCache.runnerDirectObjects(graph, r, inherits, entry, null, new SyncIntProcedure() {
+
+ @Override
+ public void run(ReadGraphImpl graph) throws DatabaseException {
+ procedure.execute(graph, result);
+ }
+
+ @Override
+ public void execute(ReadGraphImpl graph, final int i) throws DatabaseException {
+
+ synchronized(result) {
+ result.add(i);
+ }
+
+ inc();
+
+ QueryCache.runnerSuperTypes(graph, i, entry, 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 finished(ReadGraphImpl graph) throws DatabaseException {
+ dec(graph);
+ }
+
+ });
+
+ if(entry != null) entry.performFromCache(graph, procedure_);
+
+ return result;
+
+ }
+
+ @Override
+ public String toString() {
+ return "SuperTypes[" + id + "]";
+ }
+
+}