1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.query;
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.AsyncReadGraph;
16 import org.simantics.db.DevelopmentKeys;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.BlockingAsyncProcedure;
19 import org.simantics.db.impl.graph.AsyncBarrierImpl;
20 import org.simantics.db.impl.graph.BarrierTracing;
21 import org.simantics.db.impl.graph.ReadGraphImpl;
22 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
23 import org.simantics.db.procedure.AsyncProcedure;
24 import org.simantics.db.request.AsyncRead;
25 import org.simantics.utils.Development;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
31 private static final Logger LOGGER = LoggerFactory.getLogger(AsyncReadEntry.class);
33 protected AsyncRead<T> id;
35 AsyncReadEntry(AsyncRead<T> request) {
37 if (Development.DEVELOPMENT) {
38 if(Development.<Boolean>getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) {
39 System.err.println("[QUERY STATE]: created " + this);
50 public Object getOriginalRequest() {
55 public void discard() {
60 public void except(AsyncReadGraph graph, Throwable t) {
71 final public Query getQuery() {
76 public void recompute(ReadGraphImpl graph) {
80 BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure<>(graph.asyncBarrier, graph, new AsyncProcedure<T>() {
83 public void execute(AsyncReadGraph graph, T result) {
89 public void exception(AsyncReadGraph graph, Throwable t) {
95 id.perform(graph, proc);
99 } catch (Throwable t) {
106 public void removeEntry(QueryProcessor qp) {
107 qp.cache.remove(AsyncReadEntry.this);
112 return id.getFlags();
116 public String toString() {
119 else if (isExcepted())
120 return id.toString() + " " + getResult();
122 return id.toString() + " " + statusOrException;
130 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
135 proc.exception(graph, (Throwable) getResult());
136 } catch (Throwable t) {
137 LOGGER.error("performFromCache proc.exception failed", t);
143 T result = (T) getResult();
144 proc.execute(graph, result);
145 } catch (Throwable t) {
146 LOGGER.error("performFromCache proc.execute failed", t);
155 public static <T> T computeForEach(ReadGraphImpl graph, AsyncRead<T> request, AsyncReadEntry<T> entry,
156 AsyncProcedure<T> procedure_, boolean needsToBlock) throws DatabaseException {
158 AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
160 ReadGraphImpl queryGraph = graph.withParent(entry);
161 queryGraph.asyncBarrier.inc();
163 BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure<>(queryGraph.asyncBarrier, graph, null, request);
165 class AsyncTask extends SessionTask {
169 DatabaseException exception;
171 public AsyncTask(ReadGraphImpl graph) {
176 public void run0(int thread) {
177 if(needsToBlock) proc.waitBarrier();
179 ReadGraphImpl executeGraph = graph.withParent(graph.parent);
180 executeGraph.asyncBarrier.inc();
182 result = (T)proc.get();
183 if(procedure != null) {
184 procedure.execute(executeGraph, result);
186 } catch (DatabaseException e) {
187 if(procedure != null) procedure.exception(executeGraph, e);
189 } catch (Throwable t) {
190 DatabaseException dbe = new DatabaseException(t);
191 if(procedure != null) procedure.exception(executeGraph, dbe);
195 // This does not throw
196 entry.performFromCache(executeGraph, procedure_);
198 executeGraph.asyncBarrier.dec();
199 executeGraph.asyncBarrier.waitBarrier(procedure, executeGraph);
202 if(counter++ > 10000) {
203 if(BarrierTracing.BOOKKEEPING) {
204 AsyncBarrierImpl.printReverse(queryGraph.asyncBarrier, 2);
205 AsyncBarrierImpl caller = queryGraph.asyncBarrier.caller;
206 while(caller != null) {
207 System.err.println("called by " + AsyncBarrierImpl.report(caller));
208 caller = caller.caller;
210 for(AsyncBarrierImpl ab : BarrierTracing.debuggerMap.keySet()) {
211 AsyncBarrierImpl.printReverse(ab, 2);
214 throw new IllegalStateException("Eternal loop in queries.");
216 graph.processor.schedule(new AsyncTask(graph));
223 request.perform(queryGraph, proc);
225 queryGraph.asyncBarrier.dec();
228 AsyncTask task = new AsyncTask(graph);
230 if(needsToBlock) task.run(0);
231 else if (proc.isDone()) task.run(0);
233 graph.processor.schedule(task);
237 if(task.exception != null) throw task.exception;
238 else return task.result;
243 public String toString() {
245 return "DISCARDED " + id.toString();
246 else if (isExcepted())
247 return id.toString() + " " + getResult();
249 return id.toString() + " " + statusOrException;
253 public void execute(AsyncReadGraph graph, T result) {
259 public void exception(AsyncReadGraph graph, Throwable throwable) {