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 java.util.ArrayList;
16 import org.simantics.db.AsyncReadGraph;
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 {
24 protected AsyncRead<T> request;
26 public AsyncReadEntry(AsyncRead<T> request) {
27 this.request = request;
28 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
33 return request.hashCode();
37 public Object getOriginalRequest() {
42 public void discard() {
48 final public void addOrSet(AsyncReadGraph graph, Object item) {
52 // ArrayList<AsyncProcedure<T>> p = null;
64 // for(AsyncProcedure<T> proc : p) {
65 // proc.execute(graph, (T)item);
66 //// proc.first.execute(graph, (T)item);
67 //// proc.second.dec();
73 public void except(AsyncReadGraph graph, Throwable t) {
77 // ArrayList<AsyncProcedure<T>> p = null;
88 // for(AsyncProcedure<T> proc : p) {
89 // proc.exception(graph, t);
96 final public Query getQuery() {
101 public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
103 QueryProcessor qp = (QueryProcessor)provider;
105 final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp);
109 request.perform(parentGraph , new AsyncProcedure<T>() {
112 public void execute(AsyncReadGraph graph, T result) {
113 addOrSet(graph, result);
117 public void exception(AsyncReadGraph graph, Throwable t) {
123 } catch (Throwable t) {
130 public void removeEntry(QueryProcessor qp) {
131 qp.asyncReadMap.remove(request);
136 return request.getFlags();
140 public String toString() {
141 if(request == null) return "DISCARDED";
142 else if(isExcepted()) return request.toString() + " " + getResult();
143 else return request.toString() + " " + statusOrException;
150 public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
152 AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
157 proc.exception(graph, (Throwable)getResult());
158 } catch (Throwable t) {
165 proc.execute(graph, (T)getResult());
166 } catch (Throwable t) {
175 public String toString() {
176 if(isDiscarded()) return "DISCARDED " + request.toString();
177 else if(isExcepted()) return request.toString() + " " + getResult();
178 else return request.toString() + " " + statusOrException;