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.LinkedList;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.DebugPolicy;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.db.procedure.Listener;
21 import org.simantics.db.request.ExternalRead;
22 import org.simantics.db.request.RequestFlags;
24 final public class ExternalReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements Listener<T> {
26 final LinkedList<T> items = new LinkedList<T>();
28 protected ExternalRead<T> request;
29 protected ReadGraphImpl graph;
30 protected boolean registered = false;
34 return request.hashCode();
38 public Object getOriginalRequest() {
43 public void clearResult(QuerySupport support) {
47 public void discard() {
48 request.unregistered();
55 public void setPending() {
56 //if(result != NO_RESULT) {
57 //new Exception("result = " + result).printStackTrace();
59 statusOrException = PENDING;
60 result = REQUIRES_COMPUTATION;
63 public ExternalReadEntry(ExternalRead<T> request, ReadGraphImpl graph) {
64 assert request != null;
65 this.request = request;
70 public void except(Throwable t) {
72 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: excepted " + this);
73 if(statusOrException != DISCARDED) {
74 statusOrException = EXCEPTED;
85 public void setResult(Object result) {
87 super.setResult(result);
88 assert(!(result instanceof Throwable));
89 assert(!isExcepted());
94 public void setReady() {
99 final public Query getQuery() {
104 public void recompute(ReadGraphImpl graph) {
106 synchronized(items) {
109 if(!items.isEmpty()) {
111 setResult(items.removeFirst());
114 if(!items.isEmpty()) {
115 graph.processor.updatePrimitive(request);
123 public void removeEntry(QueryProcessor processor) {
124 processor.cache.remove(ExternalReadEntry.this);
129 return RequestFlags.IMMEDIATE_UPDATE;
133 public String toString() {
134 if(request == null) return "DISCARDED ExternalRead";
135 else return request.toString();
143 public String toString() {
144 if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
145 else return request.toString() + " " + + System.identityHashCode(this);
149 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
151 AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
155 proc.exception(graph, (Throwable)getResult());
159 proc.execute(graph, (T)getResult());
168 void prepareRecompute(QuerySupport querySupport) {
169 // Do nothing - the state is already set and cannot be recomputed on demand
172 public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
176 ReadGraphImpl queryGraph = graph.withParent(this);
179 request.register(graph, this);
183 queryGraph.asyncBarrier.waitBarrier(request, graph);
185 } catch (Throwable t) {
191 performFromCache(graph, procedure);
198 public void execute(T result) {
200 if(this.result == REQUIRES_COMPUTATION) {
207 synchronized(items) {
208 items.addLast(result);
209 graph.processor.updatePrimitive(request);
210 // TODO: implement flags/logic in ExternalRead to state that all but the latest request result can be evaporated
211 // In some cases where data is produced really fast this might be necessary but currently this queueing will do.
219 public void exception(Throwable t) {
224 public boolean isDisposed() {
225 return registered && (isDiscarded() || !graph.processor.isBound(this));