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.databoard.Bindings;
17 import org.simantics.db.DevelopmentKeys;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.procedure.AsyncProcedure;
21 import org.simantics.db.procedure.Listener;
22 import org.simantics.db.request.ExternalRead;
23 import org.simantics.db.request.RequestFlags;
24 import org.simantics.utils.Development;
26 final public class ExternalReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements Listener<T> {
28 final LinkedList<T> items = new LinkedList<T>();
30 protected ExternalRead<T> id;
31 protected ReadGraphImpl graph;
32 protected boolean registered = false;
40 public Object getOriginalRequest() {
45 public void clearResult(QuerySupport support) {
49 public void discard() {
57 public void setPending(QuerySupport querySupport) {
58 //if(result != NO_RESULT) {
59 //new Exception("result = " + result).printStackTrace();
61 statusOrException = PENDING;
62 result = REQUIRES_COMPUTATION;
65 public ExternalReadEntry(ExternalRead<T> request, ReadGraphImpl graph) {
66 assert request != null;
72 public void except(Throwable t) {
74 if (Development.DEVELOPMENT) {
75 if(Development.<Boolean>getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) {
76 System.err.println("[QUERY STATE]: excepted " + this);
80 if(statusOrException != DISCARDED) {
81 statusOrException = EXCEPTED;
92 public void setResult(Object result) {
94 super.setResult(result);
95 assert(!(result instanceof Throwable));
96 assert(!isExcepted());
101 public void setReady() {
106 final public Query getQuery() {
111 public void recompute(ReadGraphImpl graph) {
113 synchronized(items) {
116 if(!items.isEmpty()) {
118 setResult(items.removeFirst());
121 if(!items.isEmpty()) {
122 graph.processor.updatePrimitive(id);
130 public void removeEntry(QueryProcessor processor) {
131 processor.cache.remove(ExternalReadEntry.this);
136 return RequestFlags.IMMEDIATE_UPDATE;
140 public String toString() {
141 if(id == null) return "DISCARDED ExternalRead";
142 else return id.toString();
150 public String toString() {
151 if(id == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
152 else return id.toString() + " " + + System.identityHashCode(this);
156 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
158 AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
162 proc.exception(graph, (Throwable)getResult());
166 proc.execute(graph, (T)getResult());
175 void prepareRecompute(QuerySupport querySupport) {
176 // Do nothing - the state is already set and cannot be recomputed on demand
179 public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
183 ReadGraphImpl queryGraph = graph.withParent(this, null, true);
186 id.register(graph, this);
190 queryGraph.asyncBarrier.waitBarrier(id, graph);
192 } catch (Throwable t) {
198 performFromCache(graph, procedure);
205 public void execute(T result) {
207 if(this.result == REQUIRES_COMPUTATION) {
214 synchronized(items) {
215 items.addLast(result);
216 graph.processor.updatePrimitive(id);
217 // TODO: implement flags/logic in ExternalRead to state that all but the latest request result can be evaporated
218 // In some cases where data is produced really fast this might be necessary but currently this queueing will do.
226 public void exception(Throwable t) {
231 public boolean isDisposed() {
232 return registered && (isDiscarded() || !graph.processor.isBound(this));
236 public String classId() {