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.exception.RuntimeDatabaseException;
18 import org.simantics.db.impl.DebugPolicy;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.procedure.AsyncProcedure;
21 import org.simantics.db.request.AsyncRead;
23 final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
25 protected AsyncRead<T> request;
27 AsyncReadEntry(AsyncRead<T> request) {
28 this.request = request;
29 if (DebugPolicy.QUERY_STATE)
30 System.out.println("[QUERY STATE]: created " + this);
35 return request.hashCode();
39 public Object getOriginalRequest() {
44 public void discard() {
49 public void except(AsyncReadGraph graph, Throwable t) {
60 final public Query getQuery() {
65 public void recompute(ReadGraphImpl graph) {
69 GraphSemaphore s = new GraphSemaphore(graph, 0);
71 request.perform(graph, new AsyncProcedure<T>() {
74 public void execute(AsyncReadGraph graph, T result) {
81 public void exception(AsyncReadGraph graph, Throwable t) {
90 } catch (Throwable t) {
97 public void removeEntry(QueryProcessor qp) {
98 qp.cache.remove(AsyncReadEntry.this);
103 return request.getFlags();
107 public String toString() {
110 else if (isExcepted())
111 return request.toString() + " " + getResult();
113 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) {
145 public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry,
146 AsyncProcedure<T> procedure_) throws DatabaseException {
148 AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
150 GraphSemaphore s = new GraphSemaphore(parentGraph, 0);
152 ReadGraphImpl queryGraph = parentGraph.withParent(entry);
154 request.perform(queryGraph, new AsyncProcedure<T>() {
157 public void execute(AsyncReadGraph returnGraph, T result) {
159 procedure.execute(parentGraph, result);
160 } catch (Throwable t) {
167 public void exception(AsyncReadGraph returnGraph, Throwable t) {
169 procedure.exception(parentGraph, t);
170 } catch (Throwable t2) {
171 t2.printStackTrace();
177 public String toString() {
178 return procedure.toString();
185 } catch (InterruptedException e) {
186 throw new RuntimeDatabaseException(e);
190 entry.performFromCache(parentGraph, procedure_);
195 public String toString() {
197 return "DISCARDED " + request.toString();
198 else if (isExcepted())
199 return request.toString() + " " + getResult();
201 return request.toString() + " " + statusOrException;
205 public void execute(AsyncReadGraph graph, T result) {
211 public void exception(AsyncReadGraph graph, Throwable throwable) {