-/*******************************************************************************\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 java.util.concurrent.Semaphore;\r
-\r
-import org.simantics.db.common.exception.DebugException;\r
-import org.simantics.db.impl.graph.ReadGraphImpl;\r
-import org.simantics.db.impl.procedure.IntProcedureAdapter;\r
-import org.simantics.db.procedure.ListenerBase;\r
-\r
-final public class DirectPredicates extends CollectionUnaryQuery<IntProcedure> {\r
-\r
- private DirectPredicates(final int resource) {\r
- super(resource);\r
- }\r
-\r
- final static DirectPredicates entry(final QueryProcessor provider, final int r) {\r
-\r
- return (DirectPredicates)provider.directPredicatesMap.get(r);\r
-\r
- }\r
-\r
- final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {\r
-\r
- DirectPredicates entry = (DirectPredicates)provider.directPredicatesMap.get(r);\r
- if(entry == null) {\r
-\r
- entry = new DirectPredicates(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
- provider.registerDependencies(graph, entry, parent, listener, procedure, false);\r
- entry.computeForEach(graph, provider, procedure, true);\r
- return;\r
- }\r
- }\r
- }\r
- \r
- provider.performForEach(graph, entry, parent, listener, procedure);\r
- \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 IntProcedure procedure) {\r
-\r
- if(parent == null && listener == null) {\r
- DirectPredicates entry = (DirectPredicates)provider.directPredicatesMap.get(r);\r
- if(entry != null && entry.isReady()) { \r
- entry.performFromCache(graph, provider, procedure);\r
- return;\r
- } else {\r
- computeForEach(graph, r, null, procedure, false);\r
- return;\r
- }\r
- }\r
-\r
- runner(graph, r, provider, parent, listener, procedure);\r
-\r
- }\r
-\r
- \r
- @Override\r
- public void clearResult(QuerySupport support) {\r
- // The cached result is never used\r
- setResult(INVALID_RESULT);\r
- }\r
- \r
- @Override\r
- public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {\r
- return provider.directPredicatesMap.get(id);\r
- }\r
-\r
- @Override\r
- public void putEntry(QueryProcessor provider) {\r
- provider.directPredicatesMap.put(id, this);\r
- }\r
-\r
- @Override\r
- final public void removeEntry(QueryProcessor provider) {\r
- provider.directPredicatesMap.remove(id);\r
- }\r
-\r
- @Override\r
- public Object computeForEach(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure, boolean store) {\r
- return computeForEach(graph, id, this, procedure, store);\r
- }\r
- \r
- public static Object computeForEach(ReadGraphImpl graph, int id, final DirectPredicates entry, final IntProcedure procedure, final boolean store) {\r
-\r
- graph.processor.querySupport.ensureLoaded(graph, id);\r
- \r
- final IntArray list = new IntArray();\r
-\r
- graph.processor.querySupport.getPredicates(graph, id, new IntProcedure() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, int i) {\r
- list.add(i);\r
- }\r
-\r
- @Override\r
- public void finished(ReadGraphImpl graph) {\r
- }\r
-\r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- if(DebugException.DEBUG) new DebugException(t).printStackTrace();\r
- }\r
-\r
- });\r
-\r
- if(entry != null)\r
- entry.finish(graph, graph.processor);\r
-\r
- if(list.data == null) {\r
- if(list.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, list.sizeOrData);\r
- } else {\r
- for(int i = 0;i < list.sizeOrData ; i++) procedure.execute(graph, list.data[i]);\r
- }\r
-\r
- procedure.finished(graph);\r
- \r
- return list;\r
-\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- return "DirectPredicates[" + id + "]";\r
- }\r
-\r
- @Override\r
- public void setReady() {\r
- statusOrException = READY;\r
- }\r
- \r
- final private void finish(ReadGraphImpl graph, QueryProcessor provider) {\r
-\r
- setReady();\r
-\r
- }\r
-\r
- @Override\r
- public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {\r
-\r
- assert(isReady());\r
-\r
- return computeForEach(graph, provider, procedure, false);\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 IntProcedureAdapter() {\r
-\r
- @Override\r
- public void finished(ReadGraphImpl graph) {\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
-\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.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.impl.procedure.InternalProcedure;
+
+public final class DirectPredicates extends UnaryQueryP<IntSet> {
+
+ DirectPredicates(final int resource) {
+ super(resource);
+ }
+
+ @Override
+ public final void removeEntry(QueryProcessor provider) {
+ provider.cache.remove(this);
+ }
+
+ @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 {
+
+ InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
+
+ graph.processor.querySupport.ensureLoaded(graph, id);
+
+ final IntSet list = new IntSet(graph.processor.querySupport);
+
+ graph.processor.querySupport.getPredicates(graph, id, new IntProcedure() {
+
+ @Override
+ public void execute(ReadGraphImpl graph, int i) {
+ list.add(i);
+ }
+
+ @Override
+ public void finished(ReadGraphImpl graph) {
+ }
+
+ @Override
+ public void exception(ReadGraphImpl graph, Throwable t) {
+ if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+ }
+
+ });
+
+ procedure.execute(graph, list);
+
+ if(entry != null) entry.performFromCache(graph, procedure_);
+
+ return list;
+
+ }
+
+ @Override
+ public String toString() {
+ return "DirectPredicates[" + id + "]";
+ }
+
+}