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.ReadGraph;
18 import org.simantics.db.common.exception.DebugException;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.procedure.AsyncMultiProcedure;
22 import org.simantics.db.procedure.SyncMultiProcedure;
23 import org.simantics.db.request.MultiRead;
24 import org.simantics.db.request.RequestFlags;
26 final public class MultiReadEntry<T> extends CacheEntryBase<SyncMultiProcedure<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(ReadGraph graph) {
62 final public void clearResult(QuerySupport support) {
63 setResult(new ArrayList<T>());
66 final synchronized public void addOrSet(Object item) {
70 ArrayList<T> value = (ArrayList<T>)getResult();
76 final public Query getQuery() {
81 public void recompute(ReadGraphImpl graph) {
85 request.perform(graph , new SyncMultiProcedure<T>() {
88 public void execute(ReadGraph graph, T result) {
92 public void finished(ReadGraph graph) {
97 public void exception(ReadGraph graph, Throwable t) {
103 } catch (Throwable t) {
105 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
111 public void removeEntry(QueryProcessor processor) {
112 processor.cache.remove(MultiReadEntry.this);
117 return RequestFlags.INVALIDATE;
121 public String toString() {
122 if(request == null) return "DISCARDED";
123 else return request.toString() + statusOrException;
130 public void performFromCache(AsyncReadGraph graph, Object provider, Object procedure) {
132 AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
137 proc.exception(graph, (Throwable)getResult());
138 } catch (Throwable t) {
141 // parentBarrier.dec();
145 final ArrayList<T> values = (ArrayList<T>)getResult();
146 for(T value : values) {
148 proc.execute(graph, value);
149 } catch (Throwable t) {
155 proc.finished(graph);
156 } catch (Throwable t) {
159 // parentBarrier.dec();
166 public Object performFromCache(ReadGraphImpl graph, SyncMultiProcedure<T> proc) {
171 proc.exception(graph, (Throwable)getResult());
172 } catch (Throwable t) {
178 final ArrayList<T> values = (ArrayList<T>)getResult();
179 for(T value : values) {
181 proc.execute(graph, value);
182 } catch (Throwable t) {
188 proc.finished(graph);
189 } catch (Throwable t) {
200 public String toString() {
201 if(request == null) return "DISCARDED";
202 else return request.toString() + statusOrException;
206 public Object compute(ReadGraphImpl graph, SyncMultiProcedure<T> procedure) throws DatabaseException {
207 return graph.processor.cache.performQuery(graph, request, this, procedure);