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.exception.DatabaseException;
16 import org.simantics.db.impl.DebugPolicy;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.db.request.AsyncRead;
21 final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
23 protected AsyncRead<T> request;
25 AsyncReadEntry(AsyncRead<T> request) {
26 this.request = request;
27 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
32 return request.hashCode();
36 public Object getOriginalRequest() {
41 public void discard() {
46 final public void addOrSet(AsyncReadGraph graph, Object item) {
58 public void except(AsyncReadGraph graph, Throwable t) {
70 final public Query getQuery() {
75 public void recompute(ReadGraphImpl graph) {
79 request.perform(graph , new AsyncProcedure<T>() {
82 public void execute(AsyncReadGraph graph, T result) {
83 addOrSet(graph, result);
87 public void exception(AsyncReadGraph graph, Throwable t) {
93 } catch (Throwable t) {
100 public void removeEntry(QueryProcessor qp) {
101 qp.cache.remove(AsyncReadEntry.this);
106 return request.getFlags();
110 public String toString() {
111 if(request == null) return "DISCARDED";
112 else if(isExcepted()) return request.toString() + " " + getResult();
113 else return request.toString() + " " + statusOrException;
121 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
126 proc.exception(graph, (Throwable)getResult());
127 } catch (Throwable t) {
134 proc.execute(graph, (T)getResult());
135 } catch (Throwable t) {
146 public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
148 ReadGraphImpl queryGraph = graph.withParent(this);
150 request.perform(queryGraph, new AsyncProcedure<T>() {
153 public void execute(AsyncReadGraph returnGraph, T result) {
154 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
155 AsyncReadEntry.this.addOrSet(graph, result);
157 procedure.execute(graph, result);
158 } catch (Throwable t) {
161 // parentBarrier.dec(query);
165 public void exception(AsyncReadGraph returnGraph, Throwable t) {
166 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
167 // AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
168 AsyncReadEntry.this.except(graph, t);
170 procedure.exception(graph, t);
171 } catch (Throwable t2) {
172 t2.printStackTrace();
174 // parentBarrier.dec(query);
178 public String toString() {
179 return procedure.toString();
188 public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
190 ReadGraphImpl queryGraph = parentGraph.withParent(entry);
192 request.perform(queryGraph, new AsyncProcedure<T>() {
195 public void execute(AsyncReadGraph returnGraph, T result) {
196 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
197 if(entry != null) entry.addOrSet(parentGraph, result);
199 procedure.execute(parentGraph, result);
200 } catch (Throwable t) {
206 public void exception(AsyncReadGraph returnGraph, Throwable t) {
207 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
208 if(entry != null) entry.except(parentGraph, t);
210 procedure.exception(parentGraph, t);
211 } catch (Throwable t2) {
212 t2.printStackTrace();
217 public String toString() {
218 return procedure.toString();
227 public String toString() {
228 if(isDiscarded()) return "DISCARDED " + request.toString();
229 else if(isExcepted()) return request.toString() + " " + getResult();
230 else return request.toString() + " " + statusOrException;