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) {
98 } catch (Throwable t) {
105 public void removeEntry(QueryProcessor qp) {
106 qp.cache.remove(AsyncReadEntry.this);
111 return id.getFlags();
115 public String toString() {
118 else if (isExcepted())
119 return id.toString() + " " + getResult();
121 return id.toString() + " " + statusOrException;
129 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
134 proc.exception(graph, (Throwable) getResult());
135 } catch (Throwable t) {
136 LOGGER.error("performFromCache proc.exception failed", t);
142 T result = (T) getResult();
143 proc.execute(graph, result);
144 } catch (Throwable t) {
145 LOGGER.error("performFromCache proc.execute failed", t);
154 public static <T> T computeForEach(ReadGraphImpl callerGraph, AsyncRead<T> request, AsyncReadEntry<T> entry,
155 AsyncProcedure<T> procedure_, boolean needsToBlock) throws DatabaseException {
157 BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure(callerGraph, entry, procedure_, request, needsToBlock);
159 return proc.performSync(request);
161 proc.performAsync(request);
168 public String toString() {
170 return "DISCARDED " + id.toString();
171 else if (isExcepted())
172 return id.toString() + " " + getResult();
174 return id.toString() + " " + statusOrException;
178 public void execute(AsyncReadGraph graph, T result) {
179 Collection<SessionTask> tasks = null;
183 if(pendingTaskSupport != null)
184 tasks = pendingTaskSupport.executePending();
187 for(SessionTask task : tasks)
188 ((ReadGraphImpl)graph).processor.scheduleNow(task);
192 public synchronized void exception(AsyncReadGraph graph, Throwable throwable) {
193 Collection<SessionTask> tasks = null;
196 if(pendingTaskSupport != null)
197 tasks = pendingTaskSupport.executePending();
200 for(SessionTask task : tasks)
201 ((ReadGraphImpl)graph).processor.scheduleNow(task);
204 public void executeWhenResultIsAvailable(QueryProcessor processor, SessionTask task) {
205 boolean ready = false;
207 if(pendingTaskSupport == null)
208 pendingTaskSupport = new PendingTaskSupport(this);
209 ready = pendingTaskSupport.executeWhenResultIsAvailable(task);
212 processor.scheduleNow(task);