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.db.AsyncReadGraph;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.exception.RuntimeDatabaseException;
17 import org.simantics.db.impl.BlockingAsyncProcedure;
18 import org.simantics.db.impl.DebugPolicy;
19 import org.simantics.db.impl.graph.AsyncBarrierImpl;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
22 import org.simantics.db.procedure.AsyncProcedure;
23 import org.simantics.db.request.AsyncRead;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
29 private static final Logger LOGGER = LoggerFactory.getLogger(AsyncReadEntry.class);
31 protected AsyncRead<T> request;
33 AsyncReadEntry(AsyncRead<T> request) {
34 this.request = request;
35 if (DebugPolicy.QUERY_STATE)
36 System.out.println("[QUERY STATE]: created " + this);
41 return request.hashCode();
45 public Object getOriginalRequest() {
50 public void discard() {
55 public void except(AsyncReadGraph graph, Throwable t) {
66 final public Query getQuery() {
71 public void recompute(ReadGraphImpl graph) {
75 BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure<>(graph, new AsyncProcedure<T>() {
78 public void execute(AsyncReadGraph graph, T result) {
84 public void exception(AsyncReadGraph graph, Throwable t) {
90 request.perform(graph, proc);
94 } catch (Throwable t) {
101 public void removeEntry(QueryProcessor qp) {
102 qp.cache.remove(AsyncReadEntry.this);
107 return request.getFlags();
111 public String toString() {
114 else if (isExcepted())
115 return request.toString() + " " + getResult();
117 return request.toString() + " " + statusOrException;
125 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
130 proc.exception(graph, (Throwable) getResult());
131 } catch (Throwable t) {
132 LOGGER.error("performFromCache proc.exception failed", t);
138 T result = (T) getResult();
139 proc.execute(graph, result);
140 } catch (Throwable t) {
141 LOGGER.error("performFromCache proc.execute failed", t);
150 public static <T> T computeForEach(ReadGraphImpl graph, AsyncRead<T> request, AsyncReadEntry<T> entry,
151 AsyncProcedure<T> procedure_, boolean needsToBlock) throws DatabaseException {
153 AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
155 ReadGraphImpl queryGraph = graph.withParent(entry);
157 BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure<>(queryGraph, null, request);
159 class AsyncTask extends SessionTask {
162 DatabaseException exception;
164 public AsyncTask(ReadGraphImpl graph) {
169 public void run(int thread) {
170 if(needsToBlock) proc.waitBarrier();
173 result = (T)proc.get();
174 if(procedure != null) procedure.execute(graph, result);
175 } catch (DatabaseException e) {
176 if(procedure != null) procedure.exception(graph, e);
178 } catch (Throwable t) {
179 DatabaseException dbe = new DatabaseException(t);
180 if(procedure != null) procedure.exception(graph, dbe);
184 entry.performFromCache(queryGraph, procedure_);
187 graph.processor.schedule(this);
193 request.perform(queryGraph, proc);
195 AsyncTask task = new AsyncTask(graph);
197 if(needsToBlock) task.run(0);
198 else if (proc.isDone()) task.run(0);
200 graph.processor.schedule(task);
204 if(task.exception != null) throw task.exception;
205 else return task.result;
210 public String toString() {
212 return "DISCARDED " + request.toString();
213 else if (isExcepted())
214 return request.toString() + " " + getResult();
216 return request.toString() + " " + statusOrException;
220 public void execute(AsyncReadGraph graph, T result) {
226 public void exception(AsyncReadGraph graph, Throwable throwable) {