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.impl.query.QueryProcessor.AsyncBarrier;
20 import org.simantics.db.procedure.AsyncMultiProcedure;
21 import org.simantics.db.request.MultiRead;
22 import org.simantics.db.request.RequestFlags;
23 import org.simantics.utils.datastructures.Pair;
25 final public class MultiReadEntry<T> extends CacheEntryBase {
27 // public ArrayList<Pair<AsyncMultiProcedure<T>, AsyncBarrier>> procs;
29 protected MultiRead<T> request;
31 public MultiReadEntry(MultiRead<T> request) {
32 this.request = request;
37 return request.hashCode();
41 public Object getOriginalRequest() {
46 public void discard() {
52 synchronized public void finish(AsyncReadGraph graph) {
56 ArrayList<Pair<AsyncMultiProcedure<T>, AsyncBarrier>> p = null;
68 // ArrayList<T> v = (ArrayList<T>)getResult();
70 // for(Pair<AsyncMultiProcedure<T>, AsyncBarrier> pair : p) {
71 // for(T t : v) pair.first.execute(graph, t);
74 // for(Pair<AsyncMultiProcedure<T>, AsyncBarrier> pair : p) {
75 // pair.first.finished(graph);
83 final public void clearResult(QuerySupport support) {
84 setResult(new ArrayList<T>());
87 final synchronized public void addOrSet(Object item) {
91 ArrayList<T> value = (ArrayList<T>)getResult();
97 final public Query getQuery() {
102 public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
104 QueryProcessor qp = (QueryProcessor)provider;
106 final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp);
107 // parentGraph.state.barrier.inc();
111 request.perform(parentGraph , new AsyncMultiProcedure<T>() {
114 public void execute(AsyncReadGraph graph, T result) {
116 // parentGraph.state.barrier.dec();
119 public void finished(AsyncReadGraph graph) {
121 // parentGraph.state.barrier.dec();
125 public void exception(AsyncReadGraph graph, Throwable t) {
127 // parentGraph.state.barrier.dec();
132 // parentGraph.waitAsync(request);
134 } catch (Throwable t) {
136 // parentGraph.state.barrier.dec();
137 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
143 public void removeEntry(QueryProcessor processor) {
144 processor.cache.multiReadMap.remove(request);
149 return RequestFlags.INVALIDATE;
153 public String toString() {
154 if(request == null) return "DISCARDED";
155 else return request.toString() + statusOrException;
162 public void performFromCache(AsyncReadGraph graph, Object provider, Object procedure) {
164 AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
169 proc.exception(graph, (Throwable)getResult());
170 } catch (Throwable t) {
173 // parentBarrier.dec();
177 final ArrayList<T> values = (ArrayList<T>)getResult();
178 for(T value : values) {
180 proc.execute(graph, value);
181 } catch (Throwable t) {
187 proc.finished(graph);
188 } catch (Throwable t) {
191 // parentBarrier.dec();
198 public Object performFromCache(ReadGraphImpl graph, Object provider,
201 final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
206 proc.exception(graph, (Throwable)getResult());
207 } catch (Throwable t) {
213 final ArrayList<T> values = (ArrayList<T>)getResult();
214 for(T value : values) {
216 proc.execute(graph, value);
217 } catch (Throwable t) {
223 proc.finished(graph);
224 } catch (Throwable t) {
235 public String toString() {
236 if(request == null) return "DISCARDED";
237 else return request.toString() + statusOrException;