1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.impl.query;
\r
14 import java.util.ArrayList;
\r
16 import org.simantics.db.AsyncReadGraph;
\r
17 import org.simantics.db.common.exception.DebugException;
\r
18 import org.simantics.db.impl.graph.ReadGraphImpl;
\r
19 import org.simantics.db.procedure.AsyncMultiProcedure;
\r
20 import org.simantics.db.request.AsyncMultiRead;
\r
21 import org.simantics.db.request.RequestFlags;
\r
23 final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
\r
25 // public ArrayList<AsyncMultiProcedure<T>> procs = null;
\r
27 protected AsyncMultiRead<T> request;
\r
29 public AsyncMultiReadEntry(AsyncMultiRead<T> request) {
\r
30 this.request = request;
\r
35 return request.hashCode();
\r
39 public Object getOriginalRequest() {
\r
44 public void discard() {
\r
50 final synchronized public void finish(AsyncReadGraph graph) {
\r
52 // new Exception("finish " + this).printStackTrace();
\r
55 System.err.println("aff");
\r
58 assert(isPending());
\r
60 // ArrayList<AsyncMultiProcedure<T>> p = null;
\r
62 synchronized(this) {
\r
71 // ArrayList<T> v = (ArrayList<T>)getResult();
\r
73 // for(AsyncMultiProcedure<T> proc : p) {
\r
74 // for(T t : v) proc.execute(graph, t);
\r
78 // for(AsyncMultiProcedure<T> proc : p) proc.finished(graph);
\r
83 final synchronized public void except(AsyncReadGraph graph, Throwable t) {
\r
85 assert(isPending());
\r
87 // ArrayList<AsyncMultiProcedure<T>> p = null;
\r
89 synchronized(this) {
\r
98 // for(AsyncMultiProcedure<T> proc : p) proc.exception(graph, t);
\r
103 final synchronized public void addOrSet(Object item) {
\r
105 assert(isPending());
\r
107 ArrayList<T> value = (ArrayList<T>)getResult();
\r
108 value.add((T)item);
\r
113 public void clearResult(QuerySupport support) {
\r
114 setResult(new ArrayList<T>());
\r
118 final public Query getQuery() {
\r
120 return new Query() {
\r
123 public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
\r
125 QueryProcessor qp = (QueryProcessor)provider;
\r
127 final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp);
\r
131 request.perform(parentGraph , new AsyncMultiProcedure<T>() {
\r
134 public void execute(AsyncReadGraph graph, T result) {
\r
138 public void finished(AsyncReadGraph graph) {
\r
143 public void exception(AsyncReadGraph graph, Throwable t) {
\r
149 } catch (Throwable t) {
\r
151 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
\r
157 public void removeEntry(QueryProcessor processor) {
\r
158 processor.asyncMultiReadMap.remove(request);
\r
162 public int type() {
\r
163 return RequestFlags.INVALIDATE;
\r
167 public String toString() {
\r
168 if(request == null) return "DISCARDED";
\r
169 else return request.toString() + statusOrException;
\r
177 public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
\r
179 final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
\r
184 proc.exception(graph, (Throwable)getResult());
\r
185 } catch (Throwable t) {
\r
186 t.printStackTrace();
\r
192 final ArrayList<T> values = (ArrayList<T>)getResult();
\r
193 for(T value : values) {
\r
195 proc.execute(graph, value);
\r
196 } catch (Throwable t) {
\r
197 t.printStackTrace();
\r
202 proc.finished(graph);
\r
203 } catch (Throwable t) {
\r
204 t.printStackTrace();
\r
214 // public void performFromCache(int callerThread, Object provider,
\r
215 // Object procedure) {
\r
217 // QueryProvider2 queryProvider = (QueryProvider2)provider;
\r
218 // ReadGraphImpl graph = ReadGraphImpl.forFromCache(callerThread, null, new ReadGraphSupportImpl(null, queryProvider, null));
\r
219 // performFromCache(graph, provider, procedure);
\r
224 public String toString() {
\r
225 if(request == null) return "DISCARDED";
\r
226 else return request.toString() + statusOrException;
\r