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 assert(assertPending());
59 final public Query getQuery() {
64 public void recompute(ReadGraphImpl graph) {
68 T result = request.perform(graph);
69 addOrSet(graph, result);
71 } catch (Throwable t) {
80 public void removeEntry(QueryProcessor processor) {
81 processor.cache.remove(ReadEntry.this);
86 if(request instanceof ReadExt) {
87 return ((ReadExt)request).getType();
89 return RequestFlags.INVALIDATE;
94 public String toString() {
95 if(request == null) return "DISCARDED";
96 else return request.toString() + statusOrException;
104 public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
106 ReadGraphImpl queryGraph = graph.withParent(this);
110 addOrSet(queryGraph, request.perform(queryGraph));
111 return get(queryGraph, procedure);
113 } catch (DatabaseException e) {
116 return get(graph, procedure);
118 } catch (Throwable t) {
120 except(new DatabaseException(t));
121 return get(graph, procedure);
127 public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
129 ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
133 T result = request.perform(queryGraph);
134 if(entry != null) entry.addOrSet(queryGraph, result);
135 procedure.execute(graph, result);
137 } catch (DatabaseException e) {
139 if(entry != null) entry.except(e);
140 procedure.exception(graph, e);
142 } catch (Throwable t) {
144 DatabaseException dbe = new DatabaseException(t);
145 if(entry != null) entry.except(dbe);
146 procedure.exception(graph, dbe);
152 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
154 AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
159 proc.exception(graph, (Throwable)getResult());
160 } catch (Throwable t) {
165 proc.execute(graph, (T)getResult());
166 } catch (Throwable t) {
172 return (T)getResult();
177 public String toString() {
178 if(request == null) return "DISCARDED";
179 else return request.toString() + " - " + statusOrException;
182 public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
183 if(procedure != null) performFromCache(graph, procedure);
189 boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
190 if(request instanceof ReadExt) {
191 return ((ReadExt)request).isImmutable(graph);