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 org.simantics.db.AsyncReadGraph;
15 import org.simantics.db.common.GraphSemaphore;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.DebugPolicy;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.db.request.AsyncRead;
22 final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
24 protected AsyncRead<T> request;
26 AsyncReadEntry(AsyncRead<T> request) {
27 this.request = request;
28 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
33 return request.hashCode();
37 public Object getOriginalRequest() {
42 public void discard() {
47 final public void addOrSet(AsyncReadGraph graph, Object item) {
49 // System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + item);
61 public void except(AsyncReadGraph graph, Throwable t) {
73 final public Query getQuery() {
78 public void recompute(ReadGraphImpl graph) {
82 GraphSemaphore s = new GraphSemaphore(graph, 0);
84 request.perform(graph , new AsyncProcedure<T>() {
87 public void execute(AsyncReadGraph graph, T result) {
88 addOrSet(graph, result);
93 public void exception(AsyncReadGraph graph, Throwable t) {
102 } catch (Throwable t) {
109 public void removeEntry(QueryProcessor qp) {
110 qp.cache.remove(AsyncReadEntry.this);
115 return request.getFlags();
119 public String toString() {
120 if(request == null) return "DISCARDED";
121 else if(isExcepted()) return request.toString() + " " + getResult();
122 else return request.toString() + " " + statusOrException;
130 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
135 proc.exception(graph, (Throwable)getResult());
136 } catch (Throwable t) {
143 proc.execute(graph, (T)getResult());
144 } catch (Throwable t) {
155 public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
157 ReadGraphImpl queryGraph = graph.withParent(this);
159 request.perform(queryGraph, new AsyncProcedure<T>() {
162 public void execute(AsyncReadGraph returnGraph, T result) {
163 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
164 AsyncReadEntry.this.addOrSet(graph, result);
166 procedure.execute(graph, result);
167 } catch (Throwable t) {
170 // parentBarrier.dec(query);
174 public void exception(AsyncReadGraph returnGraph, Throwable t) {
175 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
176 // AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
177 AsyncReadEntry.this.except(graph, t);
179 procedure.exception(graph, t);
180 } catch (Throwable t2) {
181 t2.printStackTrace();
183 // parentBarrier.dec(query);
187 public String toString() {
188 return procedure.toString();
197 public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
199 ReadGraphImpl queryGraph = parentGraph.withParent(entry);
201 request.perform(queryGraph, new AsyncProcedure<T>() {
204 public void execute(AsyncReadGraph returnGraph, T result) {
205 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
206 if(entry != null) entry.addOrSet(parentGraph, result);
208 procedure.execute(parentGraph, result);
209 } catch (Throwable t) {
215 public void exception(AsyncReadGraph returnGraph, Throwable t) {
216 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
217 if(entry != null) entry.except(parentGraph, t);
219 procedure.exception(parentGraph, t);
220 } catch (Throwable t2) {
221 t2.printStackTrace();
226 public String toString() {
227 return procedure.toString();
236 public String toString() {
237 if(isDiscarded()) return "DISCARDED " + request.toString();
238 else if(isExcepted()) return request.toString() + " " + getResult();
239 else return request.toString() + " " + statusOrException;