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 java.util.ArrayList;
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.BlockingAsyncMultiProcedure;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.procedure.AsyncMultiProcedure;
21 import org.simantics.db.request.AsyncMultiRead;
22 import org.simantics.db.request.RequestFlags;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 final public class AsyncMultiReadEntry<T> extends CacheEntryBase<AsyncMultiProcedure<T>> {
28 private static final Logger LOGGER = LoggerFactory.getLogger(AsyncMultiReadEntry.class);
30 protected AsyncMultiRead<T> request;
32 AsyncMultiReadEntry(AsyncMultiRead<T> request) {
33 this.request = request;
38 return request.hashCode();
42 public Object getOriginalRequest() {
47 public void discard() {
53 final synchronized public void finish(AsyncReadGraph graph) {
63 final synchronized public void except(AsyncReadGraph graph, Throwable t) {
73 @SuppressWarnings("unchecked")
74 final synchronized public void addOrSet(Object item) {
78 ArrayList<T> value = (ArrayList<T>)getResult();
84 public void clearResult(QuerySupport support) {
85 setResult(new ArrayList<T>());
89 final public Query getQuery() {
94 public void recompute(ReadGraphImpl graph) {
98 BlockingAsyncMultiProcedure<T> proc = new BlockingAsyncMultiProcedure<>(graph.asyncBarrier, graph, new AsyncMultiProcedure<T>() {
101 public void execute(AsyncReadGraph graph, T result) {
105 public void finished(AsyncReadGraph graph) {
110 public void exception(AsyncReadGraph graph, Throwable t) {
116 request.perform(graph , proc);
120 } catch (Throwable t) {
129 public void removeEntry(QueryProcessor processor) {
130 processor.cache.remove(AsyncMultiReadEntry.this);
135 return RequestFlags.INVALIDATE;
139 public String toString() {
140 if(request == null) return "DISCARDED";
141 else return request.toString() + statusOrException;
148 @SuppressWarnings("unchecked")
150 public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure<T> proc) {
155 proc.exception(graph, (Throwable)getResult());
156 } catch (Throwable t) {
157 LOGGER.error("performFromCache proc.exception failed", t);
162 final ArrayList<T> values = (ArrayList<T>)getResult();
163 for(T value : values) {
165 proc.execute(graph, value);
166 } catch (Throwable t) {
167 LOGGER.error("performFromCache proc.execute failed", t);
172 proc.finished(graph);
173 } catch (Throwable t) {
174 LOGGER.error("performFromCache proc.finished failed", t);
184 public String toString() {
185 if(request == null) return "DISCARDED";
186 else return request.toString() + statusOrException;
189 public Object compute(ReadGraphImpl graph, AsyncMultiProcedure<T> procedure) throws DatabaseException {
190 return graph.processor.cache.performQuery(graph, request, this, procedure);