1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.impl.query;
\r
14 import java.util.ArrayList;
\r
16 import org.simantics.db.AsyncReadGraph;
\r
17 import org.simantics.db.impl.DebugPolicy;
\r
18 import org.simantics.db.impl.graph.ReadGraphImpl;
\r
19 import org.simantics.db.procedure.AsyncProcedure;
\r
20 import org.simantics.db.request.AsyncRead;
\r
22 final public class AsyncReadEntry<T> extends CacheEntryBase {
\r
24 protected AsyncRead<T> request;
\r
26 public AsyncReadEntry(AsyncRead<T> request) {
\r
27 this.request = request;
\r
28 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
\r
33 return request.hashCode();
\r
37 public Object getOriginalRequest() {
\r
42 public void discard() {
\r
48 final public void addOrSet(AsyncReadGraph graph, Object item) {
\r
50 assert(isPending());
\r
52 // ArrayList<AsyncProcedure<T>> p = null;
\r
54 synchronized(this) {
\r
64 // for(AsyncProcedure<T> proc : p) {
\r
65 // proc.execute(graph, (T)item);
\r
66 //// proc.first.execute(graph, (T)item);
\r
67 //// proc.second.dec();
\r
73 public void except(AsyncReadGraph graph, Throwable t) {
\r
75 assert(isPending());
\r
77 // ArrayList<AsyncProcedure<T>> p = null;
\r
79 synchronized(this) {
\r
88 // for(AsyncProcedure<T> proc : p) {
\r
89 // proc.exception(graph, t);
\r
96 final public Query getQuery() {
\r
98 return new Query() {
\r
101 public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
\r
103 QueryProcessor qp = (QueryProcessor)provider;
\r
105 final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp);
\r
109 request.perform(parentGraph , new AsyncProcedure<T>() {
\r
112 public void execute(AsyncReadGraph graph, T result) {
\r
113 addOrSet(graph, result);
\r
117 public void exception(AsyncReadGraph graph, Throwable t) {
\r
123 } catch (Throwable t) {
\r
130 public void removeEntry(QueryProcessor qp) {
\r
131 qp.asyncReadMap.remove(request);
\r
135 public int type() {
\r
136 return request.getFlags();
\r
140 public String toString() {
\r
141 if(request == null) return "DISCARDED";
\r
142 else if(isExcepted()) return request.toString() + " " + getResult();
\r
143 else return request.toString() + " " + statusOrException;
\r
150 public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
\r
152 AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
\r
157 proc.exception(graph, (Throwable)getResult());
\r
158 } catch (Throwable t) {
\r
159 t.printStackTrace();
\r
165 proc.execute(graph, (T)getResult());
\r
166 } catch (Throwable t) {
\r
167 t.printStackTrace();
\r
175 public String toString() {
\r
176 if(isDiscarded()) return "DISCARDED " + request.toString();
\r
177 else if(isExcepted()) return request.toString() + " " + getResult();
\r
178 else return request.toString() + " " + statusOrException;
\r