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.impl.graph.ReadGraphImpl;
18 import org.simantics.db.procedure.Procedure;
19 import org.simantics.db.request.ExternalRead;
20 import org.simantics.db.request.RequestFlags;
22 final public class ExternalReadEntry<T> extends CacheEntryBase {
24 final LinkedList<T> items = new LinkedList<T>();
26 protected ExternalRead<T> request;
30 return request.hashCode();
34 public Object getOriginalRequest() {
39 public void clearResult(QuerySupport support) {
43 public void discard() {
44 request.unregistered();
49 public ExternalReadEntry(ExternalRead<T> request) {
50 assert request != null;
51 this.request = request;
54 final public void queue(T item) {
57 // TODO: implement flags/logic in ExternalRead to state that all but the latest request result can be evaporated
58 // In some cases where data is produced really fast this might be necessary but currently this queueing will do.
62 final public void addOrSet(QueryProcessor processor, Object item) {
68 ArrayList<Procedure<T>> p = null;
80 // for(Procedure proc : p) {
81 // proc.execute((T)item);
84 } catch (Throwable t) {
91 final public Query getQuery() {
96 public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
98 final QueryProcessor qp = (QueryProcessor)provider;
101 if(entry.isExcepted()) {
103 // Exception persists
108 if(!items.isEmpty()) {
109 setResult(items.removeFirst());
112 if(!items.isEmpty()) {
113 qp.updatePrimitive(request);
125 public void removeEntry(QueryProcessor processor) {
126 processor.cache.externalReadMap.remove(request);
131 return RequestFlags.IMMEDIATE_UPDATE;
135 public String toString() {
136 if(request == null) return "DISCARDED ExternalRead";
137 else return request.toString();
144 public Object performFromCache(Object procedure) {
146 Procedure<T> proc = (Procedure<T>)procedure;
150 proc.exception((Throwable)getResult());
154 proc.execute((T)getResult());
163 public String toString() {
164 if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
165 else return request.toString() + " " + + System.identityHashCode(this);
169 public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
170 return performFromCache(procedure);
174 public void setReady() {
179 void prepareRecompute(QuerySupport querySupport) {
180 // Do nothing - the state is already set and cannot be recomputed on demand