1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 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;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public final class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
26 private static final Logger LOGGER = LoggerFactory.getLogger(ReadEntry.class);
28 protected Read<T> request;
30 public ReadEntry(Read<T> request) {
31 this.request = request;
36 return request.hashCode();
40 public Object getOriginalRequest() {
45 public void discard() {
51 final public Query getQuery() {
56 public void recompute(ReadGraphImpl graph) {
60 T result = request.perform(graph);
64 } catch (Throwable t) {
73 public void removeEntry(QueryProcessor processor) {
74 processor.cache.remove(ReadEntry.this);
79 if (request instanceof ReadExt) {
80 return ((ReadExt) request).getType();
82 return RequestFlags.INVALIDATE;
87 public String toString() {
91 return request.toString() + statusOrException;
98 public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry,
99 AsyncProcedure<T> procedure_) throws DatabaseException {
101 AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
103 ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
107 T result = request.perform(queryGraph);
108 procedure.execute(graph, result);
110 } catch (DatabaseException e) {
112 procedure.exception(graph, e);
114 } catch (Throwable t) {
116 DatabaseException dbe = new DatabaseException(t);
117 procedure.exception(graph, dbe);
122 entry.performFromCache(queryGraph, procedure_);
126 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
128 AsyncProcedure<T> proc = (AsyncProcedure<T>) procedure;
133 proc.exception(graph, (Throwable) getResult());
134 } catch (Throwable t) {
135 LOGGER.error("performFromCache proc.exception failed", t);
139 proc.execute(graph, (T) getResult());
140 } catch (Throwable t) {
141 LOGGER.error("performFromCache proc.execute failed", t);
146 return (T) getResult();
151 public String toString() {
155 return request.toString() + " - " + statusOrException;
158 public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
159 if (procedure != null)
160 performFromCache(graph, procedure);
166 boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
167 if (request instanceof ReadExt) {
168 return ((ReadExt) request).isImmutable(graph);
174 public void execute(AsyncReadGraph graph, T result) {
180 public void exception(AsyncReadGraph graph, Throwable throwable) {