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;
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.common.exception.DebugException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.procedure.AsyncMultiProcedure;
20 import org.simantics.db.request.AsyncMultiRead;
21 import org.simantics.db.request.RequestFlags;
23 final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
25 // public ArrayList<AsyncMultiProcedure<T>> procs = null;
27 protected AsyncMultiRead<T> request;
29 public AsyncMultiReadEntry(AsyncMultiRead<T> request) {
30 this.request = request;
35 return request.hashCode();
39 public Object getOriginalRequest() {
44 public void discard() {
50 final synchronized public void finish(AsyncReadGraph graph) {
52 // new Exception("finish " + this).printStackTrace();
55 System.err.println("aff");
60 // ArrayList<AsyncMultiProcedure<T>> p = null;
71 // ArrayList<T> v = (ArrayList<T>)getResult();
73 // for(AsyncMultiProcedure<T> proc : p) {
74 // for(T t : v) proc.execute(graph, t);
78 // for(AsyncMultiProcedure<T> proc : p) proc.finished(graph);
83 final synchronized public void except(AsyncReadGraph graph, Throwable t) {
87 // ArrayList<AsyncMultiProcedure<T>> p = null;
98 // for(AsyncMultiProcedure<T> proc : p) proc.exception(graph, t);
103 final synchronized public void addOrSet(Object item) {
107 ArrayList<T> value = (ArrayList<T>)getResult();
113 public void clearResult(QuerySupport support) {
114 setResult(new ArrayList<T>());
118 final public Query getQuery() {
123 public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
125 QueryProcessor qp = (QueryProcessor)provider;
127 final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp);
131 request.perform(parentGraph , new AsyncMultiProcedure<T>() {
134 public void execute(AsyncReadGraph graph, T result) {
138 public void finished(AsyncReadGraph graph) {
143 public void exception(AsyncReadGraph graph, Throwable t) {
149 } catch (Throwable t) {
151 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
157 public void removeEntry(QueryProcessor processor) {
158 processor.asyncMultiReadMap.remove(request);
163 return RequestFlags.INVALIDATE;
167 public String toString() {
168 if(request == null) return "DISCARDED";
169 else return request.toString() + statusOrException;
177 public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
179 final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
184 proc.exception(graph, (Throwable)getResult());
185 } catch (Throwable t) {
192 final ArrayList<T> values = (ArrayList<T>)getResult();
193 for(T value : values) {
195 proc.execute(graph, value);
196 } catch (Throwable t) {
202 proc.finished(graph);
203 } catch (Throwable t) {
214 // public void performFromCache(int callerThread, Object provider,
215 // Object procedure) {
217 // QueryProvider2 queryProvider = (QueryProvider2)provider;
218 // ReadGraphImpl graph = ReadGraphImpl.forFromCache(callerThread, null, new ReadGraphSupportImpl(null, queryProvider, null));
219 // performFromCache(graph, provider, procedure);
224 public String toString() {
225 if(request == null) return "DISCARDED";
226 else return request.toString() + statusOrException;