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 java.util.Collection;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.DevelopmentKeys;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.impl.BlockingAsyncProcedure;
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>, IPending {
31 private static final Logger LOGGER = LoggerFactory.getLogger(AsyncReadEntry.class);
33 protected AsyncRead<T> id;
34 protected PendingTaskSupport pendingTaskSupport;
36 AsyncReadEntry(AsyncRead<T> request) {
38 if (Development.DEVELOPMENT) {
39 if(Development.<Boolean>getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) {
40 System.err.println("[QUERY STATE]: created " + this);
51 public Object getOriginalRequest() {
56 public void discard() {
61 public void except(AsyncReadGraph graph, Throwable t) {
72 final public Query getQuery() {
77 public void recompute(ReadGraphImpl graph) {
81 BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure(graph, AsyncReadEntry.this, new AsyncProcedure<T>() {
84 public void execute(AsyncReadGraph graph, T result) {
90 public void exception(AsyncReadGraph graph, Throwable t) {
96 id.perform(proc.queryGraph, proc);
101 } catch (Throwable t) {
108 public void removeEntry(QueryProcessor qp) {
109 qp.cache.remove(AsyncReadEntry.this);
114 return id.getFlags();
118 public String toString() {
121 else if (isExcepted())
122 return id.toString() + " " + getResult();
124 return id.toString() + " " + statusOrException;
132 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
137 proc.exception(graph, (Throwable) getResult());
138 } catch (Throwable t) {
139 LOGGER.error("performFromCache proc.exception failed", t);
145 T result = (T) getResult();
146 proc.execute(graph, result);
147 } catch (Throwable t) {
148 LOGGER.error("performFromCache proc.execute failed", t);
157 public static <T> T computeForEach(ReadGraphImpl callerGraph, AsyncRead<T> request, AsyncReadEntry<T> entry,
158 AsyncProcedure<T> procedure_, boolean needsToBlock) throws DatabaseException {
160 BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure(callerGraph, entry, procedure_, request, needsToBlock);
163 request.perform(proc.queryGraph, proc);
165 proc.queryGraph.asyncBarrier.dec();
178 public String toString() {
180 return "DISCARDED " + id.toString();
181 else if (isExcepted())
182 return id.toString() + " " + getResult();
184 return id.toString() + " " + statusOrException;
188 public void execute(AsyncReadGraph graph, T result) {
189 Collection<SessionTask> tasks = null;
193 if(pendingTaskSupport != null)
194 tasks = pendingTaskSupport.executePending();
197 for(SessionTask task : tasks)
198 ((ReadGraphImpl)graph).processor.scheduleNow(task);
202 public synchronized void exception(AsyncReadGraph graph, Throwable throwable) {
203 Collection<SessionTask> tasks = null;
206 if(pendingTaskSupport != null)
207 tasks = pendingTaskSupport.executePending();
210 for(SessionTask task : tasks)
211 ((ReadGraphImpl)graph).processor.scheduleNow(task);
214 public void executeWhenResultIsAvailable(QueryProcessor processor, SessionTask task) {
215 boolean ready = false;
217 if(pendingTaskSupport == null)
218 pendingTaskSupport = new PendingTaskSupport(this);
219 ready = pendingTaskSupport.executeWhenResultIsAvailable(task);
222 processor.scheduleNow(task);