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.graph.ReadGraphImpl;
17 import org.simantics.db.impl.graph.WriteGraphImpl;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.db.request.Read;
20 import org.simantics.db.request.ReadExt;
21 import org.simantics.db.request.RequestFlags;
23 final public class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
25 protected Read<T> request;
27 public ReadEntry(Read<T> request) {
28 this.request = request;
33 return request.hashCode();
37 public Object getOriginalRequest() {
42 public void discard() {
47 final public Object addOrSet(AsyncReadGraph graph, Object result) {
49 // System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + result);
51 assert(assertPending());
61 final public Query getQuery() {
66 public void recompute(ReadGraphImpl graph) {
70 T result = request.perform(graph);
71 addOrSet(graph, result);
73 } catch (Throwable t) {
82 public void removeEntry(QueryProcessor processor) {
83 processor.cache.remove(ReadEntry.this);
88 if(request instanceof ReadExt) {
89 return ((ReadExt)request).getType();
91 return RequestFlags.INVALIDATE;
96 public String toString() {
97 if(request == null) return "DISCARDED";
98 else return request.toString() + statusOrException;
106 public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
108 ReadGraphImpl queryGraph = graph.withParent(this);
112 addOrSet(queryGraph, request.perform(queryGraph));
113 return get(queryGraph, procedure);
115 } catch (DatabaseException e) {
118 return get(graph, procedure);
120 } catch (Throwable t) {
122 except(new DatabaseException(t));
123 return get(graph, procedure);
129 public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
131 ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
135 T result = request.perform(queryGraph);
136 if(entry != null) entry.addOrSet(queryGraph, result);
137 procedure.execute(graph, result);
139 } catch (DatabaseException e) {
141 if(entry != null) entry.except(e);
142 procedure.exception(graph, e);
144 } catch (Throwable t) {
146 DatabaseException dbe = new DatabaseException(t);
147 if(entry != null) entry.except(dbe);
148 procedure.exception(graph, dbe);
154 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
156 AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
161 proc.exception(graph, (Throwable)getResult());
162 } catch (Throwable t) {
167 proc.execute(graph, (T)getResult());
168 } catch (Throwable t) {
174 return (T)getResult();
179 public String toString() {
180 if(request == null) return "DISCARDED";
181 else return request.toString() + " - " + statusOrException;
184 public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
185 if(procedure != null) performFromCache(graph, procedure);
191 boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
192 if(request instanceof ReadExt) {
193 return ((ReadExt)request).isImmutable(graph);