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.exception.RuntimeDatabaseException;
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>> implements AsyncProcedure<T> {
24 protected AsyncRead<T> request;
26 AsyncReadEntry(AsyncRead<T> request) {
27 this.request = request;
28 if (DebugPolicy.QUERY_STATE)
29 System.out.println("[QUERY STATE]: created " + this);
34 return request.hashCode();
38 public Object getOriginalRequest() {
43 public void discard() {
48 public void except(AsyncReadGraph graph, Throwable t) {
59 final public Query getQuery() {
64 public void recompute(ReadGraphImpl graph) {
68 GraphSemaphore s = new GraphSemaphore(graph, 0);
70 request.perform(graph, new AsyncProcedure<T>() {
73 public void execute(AsyncReadGraph graph, T result) {
80 public void exception(AsyncReadGraph graph, Throwable t) {
89 } catch (Throwable t) {
96 public void removeEntry(QueryProcessor qp) {
97 qp.cache.remove(AsyncReadEntry.this);
102 return request.getFlags();
106 public String toString() {
109 else if (isExcepted())
110 return request.toString() + " " + getResult();
112 return request.toString() + " " + statusOrException;
120 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
125 proc.exception(graph, (Throwable) getResult());
126 } catch (Throwable t) {
133 proc.execute(graph, (T) getResult());
134 } catch (Throwable t) {
144 public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry,
145 AsyncProcedure<T> procedure_) throws DatabaseException {
147 AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
149 GraphSemaphore s = new GraphSemaphore(parentGraph, 0);
151 ReadGraphImpl queryGraph = parentGraph.withParent(entry);
153 request.perform(queryGraph, new AsyncProcedure<T>() {
156 public void execute(AsyncReadGraph returnGraph, T result) {
158 procedure.execute(parentGraph, result);
159 } catch (Throwable t) {
166 public void exception(AsyncReadGraph returnGraph, Throwable t) {
168 procedure.exception(parentGraph, t);
169 } catch (Throwable t2) {
170 t2.printStackTrace();
176 public String toString() {
177 return procedure.toString();
184 } catch (InterruptedException e) {
185 throw new RuntimeDatabaseException(e);
189 entry.performFromCache(parentGraph, procedure_);
194 public String toString() {
196 return "DISCARDED " + request.toString();
197 else if (isExcepted())
198 return request.toString() + " " + getResult();
200 return request.toString() + " " + statusOrException;
204 public void execute(AsyncReadGraph graph, T result) {
210 public void exception(AsyncReadGraph graph, Throwable throwable) {