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.exception.DatabaseException;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.impl.query.QueryProcessor.AsyncBarrier;
21 import org.simantics.db.procedure.AsyncMultiProcedure;
22 import org.simantics.db.request.MultiRead;
23 import org.simantics.db.request.RequestFlags;
24 import org.simantics.utils.datastructures.Pair;
26 final public class MultiReadEntry<T> extends CacheEntryBase<AsyncMultiProcedure<T>> {
28 protected MultiRead<T> request;
30 MultiReadEntry(MultiRead<T> request) {
31 this.request = request;
36 return request.hashCode();
40 public Object getOriginalRequest() {
45 public void discard() {
51 synchronized public void finish(AsyncReadGraph graph) {
55 ArrayList<Pair<AsyncMultiProcedure<T>, AsyncBarrier>> p = null;
64 final public void clearResult(QuerySupport support) {
65 setResult(new ArrayList<T>());
68 final synchronized public void addOrSet(Object item) {
72 ArrayList<T> value = (ArrayList<T>)getResult();
78 final public Query getQuery() {
83 public void recompute(ReadGraphImpl graph) {
87 request.perform(graph , new AsyncMultiProcedure<T>() {
90 public void execute(AsyncReadGraph graph, T result) {
94 public void finished(AsyncReadGraph graph) {
99 public void exception(AsyncReadGraph graph, Throwable t) {
105 } catch (Throwable t) {
107 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
113 public void removeEntry(QueryProcessor processor) {
114 processor.cache.remove(MultiReadEntry.this);
119 return RequestFlags.INVALIDATE;
123 public String toString() {
124 if(request == null) return "DISCARDED";
125 else return request.toString() + statusOrException;
132 public void performFromCache(AsyncReadGraph graph, Object provider, Object procedure) {
134 AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
139 proc.exception(graph, (Throwable)getResult());
140 } catch (Throwable t) {
143 // parentBarrier.dec();
147 final ArrayList<T> values = (ArrayList<T>)getResult();
148 for(T value : values) {
150 proc.execute(graph, value);
151 } catch (Throwable t) {
157 proc.finished(graph);
158 } catch (Throwable t) {
161 // parentBarrier.dec();
168 public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure<T> proc) {
173 proc.exception(graph, (Throwable)getResult());
174 } catch (Throwable t) {
180 final ArrayList<T> values = (ArrayList<T>)getResult();
181 for(T value : values) {
183 proc.execute(graph, value);
184 } catch (Throwable t) {
190 proc.finished(graph);
191 } catch (Throwable t) {
202 public String toString() {
203 if(request == null) return "DISCARDED";
204 else return request.toString() + statusOrException;
208 public Object compute(ReadGraphImpl graph, AsyncMultiProcedure<T> procedure) throws DatabaseException {
209 return graph.processor.cache.performQuery(graph, request, this, procedure);