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.procedure.AsyncProcedure;
18 import org.simantics.db.request.Read;
19 import org.simantics.db.request.ReadExt;
20 import org.simantics.db.request.RequestFlags;
22 final public class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
24 protected Read<T> request;
26 public ReadEntry(Read<T> request) {
27 this.request = request;
32 return request.hashCode();
36 public Object getOriginalRequest() {
41 public void discard() {
47 final public Query getQuery() {
52 public void recompute(ReadGraphImpl graph) {
56 T result = request.perform(graph);
60 } catch (Throwable t) {
69 public void removeEntry(QueryProcessor processor) {
70 processor.cache.remove(ReadEntry.this);
75 if (request instanceof ReadExt) {
76 return ((ReadExt) request).getType();
78 return RequestFlags.INVALIDATE;
83 public String toString() {
87 return request.toString() + statusOrException;
94 public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry,
95 AsyncProcedure<T> procedure_) throws DatabaseException {
97 AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
99 ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
103 T result = request.perform(queryGraph);
104 procedure.execute(graph, result);
106 } catch (DatabaseException e) {
108 procedure.exception(graph, e);
110 } catch (Throwable t) {
112 DatabaseException dbe = new DatabaseException(t);
113 procedure.exception(graph, dbe);
118 entry.performFromCache(queryGraph, procedure_);
122 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
124 AsyncProcedure<T> proc = (AsyncProcedure<T>) procedure;
129 proc.exception(graph, (Throwable) getResult());
130 } catch (Throwable t) {
135 proc.execute(graph, (T) getResult());
136 } catch (Throwable t) {
142 return (T) getResult();
147 public String toString() {
151 return request.toString() + " - " + statusOrException;
154 public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
155 if (procedure != null)
156 performFromCache(graph, procedure);
162 boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
163 if (request instanceof ReadExt) {
164 return ((ReadExt) request).isImmutable(graph);
170 public void execute(AsyncReadGraph graph, T result) {
176 public void exception(AsyncReadGraph graph, Throwable throwable) {