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.request.ExternalRead;
21 import org.simantics.db.request.RequestFlags;
23 final public class ExternalReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
25 final LinkedList<T> items = new LinkedList<T>();
27 protected ExternalRead<T> request;
31 return request.hashCode();
35 public Object getOriginalRequest() {
40 public void clearResult(QuerySupport support) {
44 public void discard() {
45 request.unregistered();
51 public void setPending() {
52 //if(result != NO_RESULT) {
53 //new Exception("result = " + result).printStackTrace();
55 statusOrException = PENDING;
56 result = REQUIRES_COMPUTATION;
59 public ExternalReadEntry(ExternalRead<T> request) {
60 assert request != null;
61 this.request = request;
64 final public void queue(T item) {
67 // TODO: implement flags/logic in ExternalRead to state that all but the latest request result can be evaporated
68 // In some cases where data is produced really fast this might be necessary but currently this queueing will do.
72 final public void addOrSet(QueryProcessor processor, Object item) {
78 //ArrayList<Procedure<T>> p = null;
90 // for(Procedure proc : p) {
91 // proc.execute((T)item);
94 } catch (Throwable t) {
101 public void except(Throwable t) {
102 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: excepted " + this);
103 if(statusOrException != DISCARDED) {
104 statusOrException = EXCEPTED;
109 assert(isExcepted());
113 public void setResult(Object result) {
114 super.setResult(result);
115 assert(!(result instanceof Throwable));
116 assert(!isExcepted());
120 final public Query getQuery() {
125 public void recompute(ReadGraphImpl graph) {
127 synchronized(items) {
131 if(!items.isEmpty()) {
133 setResult(items.removeFirst());
136 if(!items.isEmpty()) {
137 graph.processor.updatePrimitive(request);
145 public void removeEntry(QueryProcessor processor) {
146 processor.cache.remove(ExternalReadEntry.this);
151 return RequestFlags.IMMEDIATE_UPDATE;
155 public String toString() {
156 if(request == null) return "DISCARDED ExternalRead";
157 else return request.toString();
165 public String toString() {
166 if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
167 else return request.toString() + " " + + System.identityHashCode(this);
171 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
173 AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
177 proc.exception(graph, (Throwable)getResult());
181 proc.execute(graph, (T)getResult());
190 void prepareRecompute(QuerySupport querySupport) {
191 // Do nothing - the state is already set and cannot be recomputed on demand
194 public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
195 return graph.processor.cache.performQuery(graph, request, this, procedure);