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.graph.ReadGraphImpl;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.db.procedure.Procedure;
21 import org.simantics.db.request.ExternalRead;
22 import org.simantics.db.request.RequestFlags;
24 final public class ExternalReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
26 final LinkedList<T> items = new LinkedList<T>();
28 protected ExternalRead<T> request;
32 return request.hashCode();
36 public Object getOriginalRequest() {
41 public void clearResult(QuerySupport support) {
45 public void discard() {
46 request.unregistered();
51 public ExternalReadEntry(ExternalRead<T> request) {
52 assert request != null;
53 this.request = request;
56 final public void queue(T item) {
59 // TODO: implement flags/logic in ExternalRead to state that all but the latest request result can be evaporated
60 // In some cases where data is produced really fast this might be necessary but currently this queueing will do.
64 final public void addOrSet(QueryProcessor processor, Object item) {
70 ArrayList<Procedure<T>> p = null;
82 // for(Procedure proc : p) {
83 // proc.execute((T)item);
86 } catch (Throwable t) {
93 final public Query getQuery() {
98 public void recompute(ReadGraphImpl graph) {
100 synchronized(items) {
104 if(!items.isEmpty()) {
105 setResult(items.removeFirst());
108 if(!items.isEmpty()) {
109 graph.processor.updatePrimitive(request);
117 public void removeEntry(QueryProcessor processor) {
118 processor.cache.remove(ExternalReadEntry.this);
123 return RequestFlags.IMMEDIATE_UPDATE;
127 public String toString() {
128 if(request == null) return "DISCARDED ExternalRead";
129 else return request.toString();
137 public String toString() {
138 if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
139 else return request.toString() + " " + + System.identityHashCode(this);
143 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
145 AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
149 proc.exception(graph, (Throwable)getResult());
153 proc.execute(graph, (T)getResult());
162 void prepareRecompute(QuerySupport querySupport) {
163 // Do nothing - the state is already set and cannot be recomputed on demand
167 public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
168 return graph.processor.cache.performQuery(graph, request, this, procedure);