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.exception.RuntimeDatabaseException;
17 import org.simantics.db.impl.BlockingAsyncProcedure;
18 import org.simantics.db.impl.DebugPolicy;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.procedure.AsyncProcedure;
21 import org.simantics.db.request.AsyncRead;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
27 private static final Logger LOGGER = LoggerFactory.getLogger(AsyncReadEntry.class);
29 protected AsyncRead<T> request;
31 AsyncReadEntry(AsyncRead<T> request) {
32 this.request = request;
33 if (DebugPolicy.QUERY_STATE)
34 System.out.println("[QUERY STATE]: created " + this);
39 return request.hashCode();
43 public Object getOriginalRequest() {
48 public void discard() {
53 public void except(AsyncReadGraph graph, Throwable t) {
64 final public Query getQuery() {
69 public void recompute(ReadGraphImpl graph) {
73 BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure<>(graph, new AsyncProcedure<T>() {
76 public void execute(AsyncReadGraph graph, T result) {
82 public void exception(AsyncReadGraph graph, Throwable t) {
88 request.perform(graph, proc);
92 } catch (Throwable t) {
99 public void removeEntry(QueryProcessor qp) {
100 qp.cache.remove(AsyncReadEntry.this);
105 return request.getFlags();
109 public String toString() {
112 else if (isExcepted())
113 return request.toString() + " " + getResult();
115 return request.toString() + " " + statusOrException;
123 public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
128 proc.exception(graph, (Throwable) getResult());
129 } catch (Throwable t) {
130 LOGGER.error("performFromCache proc.exception failed", t);
136 proc.execute(graph, (T) getResult());
137 } catch (Throwable t) {
138 LOGGER.error("performFromCache proc.execute failed", t);
147 public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry,
148 AsyncProcedure<T> procedure_) throws DatabaseException {
150 AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
152 ReadGraphImpl queryGraph = parentGraph.withParent(entry);
154 BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure<>(queryGraph, new AsyncProcedure<T>() {
157 public void execute(AsyncReadGraph returnGraph, T result) {
159 procedure.execute(parentGraph, result);
160 } catch (Throwable t) {
161 LOGGER.error("computeForEach procedure.execute failed", t);
166 public void exception(AsyncReadGraph returnGraph, Throwable t) {
168 procedure.exception(parentGraph, t);
169 } catch (Throwable t2) {
170 LOGGER.error("computeForEach procedure.exception failed", t2);
175 public String toString() {
176 return procedure.toString();
181 request.perform(queryGraph, proc);
186 entry.performFromCache(parentGraph, procedure_);
191 public String toString() {
193 return "DISCARDED " + request.toString();
194 else if (isExcepted())
195 return request.toString() + " " + getResult();
197 return request.toString() + " " + statusOrException;
201 public void execute(AsyncReadGraph graph, T result) {
207 public void exception(AsyncReadGraph graph, Throwable throwable) {