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.GraphSemaphore;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.procedure.AsyncMultiProcedure;
21 import org.simantics.db.request.AsyncMultiRead;
22 import org.simantics.db.request.RequestFlags;
24 final public class AsyncMultiReadEntry<T> extends CacheEntryBase<AsyncMultiProcedure<T>> {
26 protected AsyncMultiRead<T> request;
28 AsyncMultiReadEntry(AsyncMultiRead<T> request) {
29 this.request = request;
34 return request.hashCode();
38 public Object getOriginalRequest() {
43 public void discard() {
49 final synchronized public void finish(AsyncReadGraph graph) {
59 final synchronized public void except(AsyncReadGraph graph, Throwable t) {
69 @SuppressWarnings("unchecked")
70 final synchronized public void addOrSet(Object item) {
74 ArrayList<T> value = (ArrayList<T>)getResult();
80 public void clearResult(QuerySupport support) {
81 setResult(new ArrayList<T>());
85 final public Query getQuery() {
90 public void recompute(ReadGraphImpl graph) {
94 GraphSemaphore s = new GraphSemaphore(graph, 0);
96 request.perform(graph , new AsyncMultiProcedure<T>() {
99 public void execute(AsyncReadGraph graph, T result) {
103 public void finished(AsyncReadGraph graph) {
109 public void exception(AsyncReadGraph graph, Throwable t) {
118 } catch (Throwable t) {
127 public void removeEntry(QueryProcessor processor) {
128 processor.cache.remove(AsyncMultiReadEntry.this);
133 return RequestFlags.INVALIDATE;
137 public String toString() {
138 if(request == null) return "DISCARDED";
139 else return request.toString() + statusOrException;
146 @SuppressWarnings("unchecked")
148 public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure<T> proc) {
153 proc.exception(graph, (Throwable)getResult());
154 } catch (Throwable t) {
161 final ArrayList<T> values = (ArrayList<T>)getResult();
162 for(T value : values) {
164 proc.execute(graph, value);
165 } catch (Throwable t) {
171 proc.finished(graph);
172 } catch (Throwable t) {
183 public String toString() {
184 if(request == null) return "DISCARDED";
185 else return request.toString() + statusOrException;
189 public Object compute(ReadGraphImpl graph, AsyncMultiProcedure<T> procedure) throws DatabaseException {
190 return graph.processor.cache.performQuery(graph, request, this, procedure);