1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.ArrayList;
15 import java.util.LinkedList;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.DebugPolicy;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.procedure.AsyncProcedure;
21 import org.simantics.db.procedure.Procedure;
22 import org.simantics.db.request.ExternalRead;
23 import org.simantics.db.request.RequestFlags;
25 final public class ExternalReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
27 final LinkedList<T> items = new LinkedList<T>();
29 protected ExternalRead<T> request;
33 return request.hashCode();
37 public Object getOriginalRequest() {
42 public void clearResult(QuerySupport support) {
46 public void discard() {
47 request.unregistered();
53 public void setPending() {
54 if(result != NO_RESULT) {
55 //new Exception("result = " + result).printStackTrace();
57 statusOrException = PENDING;
58 result = REQUIRES_COMPUTATION;
61 public ExternalReadEntry(ExternalRead<T> request) {
62 assert request != null;
63 this.request = request;
66 final public void queue(T item) {
69 // TODO: implement flags/logic in ExternalRead to state that all but the latest request result can be evaporated
70 // In some cases where data is produced really fast this might be necessary but currently this queueing will do.
74 final public void addOrSet(QueryProcessor processor, Object item) {
80 ArrayList<Procedure<T>> p = null;
92 // for(Procedure proc : p) {
93 // proc.execute((T)item);
96 } catch (Throwable t) {
103 public void except(Throwable t) {
104 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: excepted " + this);
105 if(statusOrException != DISCARDED) {
106 statusOrException = EXCEPTED;
111 assert(isExcepted());
115 public void setResult(Object result) {
116 super.setResult(result);
117 assert(!(result instanceof Throwable));
118 assert(!isExcepted());
122 final public Query getQuery() {
127 public void recompute(ReadGraphImpl graph) {
129 synchronized(items) {
133 if(!items.isEmpty()) {
135 setResult(items.removeFirst());
138 if(!items.isEmpty()) {
139 graph.processor.updatePrimitive(request);
147 public void removeEntry(QueryProcessor processor) {
148 processor.cache.remove(ExternalReadEntry.this);
153 return RequestFlags.IMMEDIATE_UPDATE;
157 public String toString() {
158 if(request == null) return "DISCARDED ExternalRead";
159 else return request.toString();
167 public String toString() {
168 if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
169 else return request.toString() + " " + + System.identityHashCode(this);
173 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
175 AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
179 proc.exception(graph, (Throwable)getResult());
183 proc.execute(graph, (T)getResult());
192 void prepareRecompute(QuerySupport querySupport) {
193 // Do nothing - the state is already set and cannot be recomputed on demand
197 public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
198 return graph.processor.cache.performQuery(graph, request, this, procedure);