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) {
48 // System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + item);
60 public void except(AsyncReadGraph graph, Throwable t) {
72 final public Query getQuery() {
77 public void recompute(ReadGraphImpl graph) {
81 request.perform(graph , new AsyncProcedure<T>() {
84 public void execute(AsyncReadGraph graph, T result) {
85 addOrSet(graph, result);
89 public void exception(AsyncReadGraph graph, Throwable t) {
95 } catch (Throwable t) {
102 public void removeEntry(QueryProcessor qp) {
103 qp.cache.remove(AsyncReadEntry.this);
108 return request.getFlags();
112 public String toString() {
113 if(request == null) return "DISCARDED";
114 else if(isExcepted()) return request.toString() + " " + getResult();
115 else return request.toString() + " " + statusOrException;
123 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
128 proc.exception(graph, (Throwable)getResult());
129 } catch (Throwable t) {
136 proc.execute(graph, (T)getResult());
137 } catch (Throwable t) {
148 public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
150 ReadGraphImpl queryGraph = graph.withParent(this);
152 request.perform(queryGraph, new AsyncProcedure<T>() {
155 public void execute(AsyncReadGraph returnGraph, T result) {
156 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
157 AsyncReadEntry.this.addOrSet(graph, result);
159 procedure.execute(graph, result);
160 } catch (Throwable t) {
163 // parentBarrier.dec(query);
167 public void exception(AsyncReadGraph returnGraph, Throwable t) {
168 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
169 // AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
170 AsyncReadEntry.this.except(graph, t);
172 procedure.exception(graph, t);
173 } catch (Throwable t2) {
174 t2.printStackTrace();
176 // parentBarrier.dec(query);
180 public String toString() {
181 return procedure.toString();
190 public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
192 ReadGraphImpl queryGraph = parentGraph.withParent(entry);
194 request.perform(queryGraph, new AsyncProcedure<T>() {
197 public void execute(AsyncReadGraph returnGraph, T result) {
198 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
199 if(entry != null) entry.addOrSet(parentGraph, result);
201 procedure.execute(parentGraph, result);
202 } catch (Throwable t) {
208 public void exception(AsyncReadGraph returnGraph, Throwable t) {
209 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
210 if(entry != null) entry.except(parentGraph, t);
212 procedure.exception(parentGraph, t);
213 } catch (Throwable t2) {
214 t2.printStackTrace();
219 public String toString() {
220 return procedure.toString();
229 public String toString() {
230 if(isDiscarded()) return "DISCARDED " + request.toString();
231 else if(isExcepted()) return request.toString() + " " + getResult();
232 else return request.toString() + " " + statusOrException;