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.ReadGraph;
18 import org.simantics.db.common.exception.DebugException;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.procedure.AsyncMultiProcedure;
22 import org.simantics.db.procedure.SyncMultiProcedure;
23 import org.simantics.db.request.MultiRead;
24 import org.simantics.db.request.RequestFlags;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public final class MultiReadEntry<T> extends CacheEntryBase<SyncMultiProcedure<T>> {
30 private static final Logger LOGGER = LoggerFactory.getLogger(MultiReadEntry.class);
32 protected MultiRead<T> request;
34 MultiReadEntry(MultiRead<T> request) {
35 this.request = request;
40 return request.hashCode();
44 public Object getOriginalRequest() {
49 public void discard() {
55 synchronized public void finish(ReadGraph graph) {
66 final public void clearResult(QuerySupport support) {
67 setResult(new ArrayList<T>());
70 final synchronized public void addOrSet(Object item) {
74 ArrayList<T> value = (ArrayList<T>)getResult();
80 final public Query getQuery() {
85 public void recompute(ReadGraphImpl graph) {
89 request.perform(graph , new SyncMultiProcedure<T>() {
92 public void execute(ReadGraph graph, T result) {
96 public void finished(ReadGraph graph) {
101 public void exception(ReadGraph graph, Throwable t) {
107 } catch (Throwable t) {
109 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
115 public void removeEntry(QueryProcessor processor) {
116 processor.cache.remove(MultiReadEntry.this);
121 return RequestFlags.INVALIDATE;
125 public String toString() {
126 if(request == null) return "DISCARDED";
127 else return request.toString() + statusOrException;
134 public void performFromCache(AsyncReadGraph graph, Object provider, Object procedure) {
136 AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
141 proc.exception(graph, (Throwable)getResult());
142 } catch (Throwable t) {
143 LOGGER.error("performFromCache proc.exception failed", t);
145 // parentBarrier.dec();
149 final ArrayList<T> values = (ArrayList<T>)getResult();
150 for(T value : values) {
152 proc.execute(graph, value);
153 } catch (Throwable t) {
154 LOGGER.error("performFromCache proc.execute failed", t);
159 proc.finished(graph);
160 } catch (Throwable t) {
161 LOGGER.error("performFromCache proc.finished failed", t);
163 // parentBarrier.dec();
170 public Object performFromCache(ReadGraphImpl graph, SyncMultiProcedure<T> proc) {
175 proc.exception(graph, (Throwable)getResult());
176 } catch (Throwable t) {
177 LOGGER.error("performFromCache(Sync) proc.exception failed", t);
182 final ArrayList<T> values = (ArrayList<T>)getResult();
183 for(T value : values) {
185 proc.execute(graph, value);
186 } catch (Throwable t) {
187 LOGGER.error("performFromCache(Sync) proc.execute failed", t);
192 proc.finished(graph);
193 } catch (Throwable t) {
194 LOGGER.error("performFromCache(Sync) proc.finished failed", t);
204 public String toString() {
205 if(request == null) return "DISCARDED";
206 else return request.toString() + statusOrException;
209 public Object compute(ReadGraphImpl graph, SyncMultiProcedure<T> procedure) throws DatabaseException {
210 return graph.processor.cache.performQuery(graph, request, this, procedure);