]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java
00abbebf65859e76d0e8dad481d01097cab04621
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AsyncReadEntry.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
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;
24
25 final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
26
27     private static final Logger LOGGER = LoggerFactory.getLogger(AsyncReadEntry.class);
28
29     protected AsyncRead<T> request;
30
31     AsyncReadEntry(AsyncRead<T> request) {
32         this.request = request;
33         if (DebugPolicy.QUERY_STATE)
34             System.out.println("[QUERY STATE]: created " + this);
35     }
36
37     @Override
38     int makeHash() {
39         return request.hashCode();
40     }
41
42     @Override
43     public Object getOriginalRequest() {
44         return request;
45     }
46
47     @Override
48     public void discard() {
49         super.discard();
50         setResult(null);
51     }
52
53     public void except(AsyncReadGraph graph, Throwable t) {
54
55         assert (isPending());
56
57         synchronized (this) {
58             except(t);
59         }
60
61     }
62
63     @Override
64     final public Query getQuery() {
65
66         return new Query() {
67
68             @Override
69             public void recompute(ReadGraphImpl graph) {
70
71                 try {
72
73                     BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure<>(graph, new AsyncProcedure<T>() {
74
75                         @Override
76                         public void execute(AsyncReadGraph graph, T result) {
77                             setResult(result);
78                             setReady();
79                         }
80
81                         @Override
82                         public void exception(AsyncReadGraph graph, Throwable t) {
83                             except(t);
84                         }
85
86                     }, request);
87
88                     request.perform(graph, proc);
89
90                     proc.get();
91
92                 } catch (Throwable t) {
93                     except(t);
94                 }
95
96             }
97
98             @Override
99             public void removeEntry(QueryProcessor qp) {
100                 qp.cache.remove(AsyncReadEntry.this);
101             }
102
103             @Override
104             public int type() {
105                 return request.getFlags();
106             }
107
108             @Override
109             public String toString() {
110                 if (request == null)
111                     return "DISCARDED";
112                 else if (isExcepted())
113                     return request.toString() + " " + getResult();
114                 else
115                     return request.toString() + " " + statusOrException;
116             }
117
118         };
119
120     }
121
122     @Override
123     public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
124
125         if (isExcepted()) {
126
127             try {
128                 proc.exception(graph, (Throwable) getResult());
129             } catch (Throwable t) {
130                 LOGGER.error("performFromCache proc.exception failed", t);
131             }
132
133         } else {
134
135             try {
136                 proc.execute(graph, (T) getResult());
137             } catch (Throwable t) {
138                 LOGGER.error("performFromCache proc.execute failed", t);
139             }
140
141         }
142
143         return getResult();
144
145     }
146
147     public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry,
148             AsyncProcedure<T> procedure_) throws DatabaseException {
149
150         AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
151
152         ReadGraphImpl queryGraph = parentGraph.withParent(entry);
153         
154         BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure<>(queryGraph, new AsyncProcedure<T>() {
155
156             @Override
157             public void execute(AsyncReadGraph returnGraph, T result) {
158                 try {
159                     procedure.execute(parentGraph, result);
160                 } catch (Throwable t) {
161                     LOGGER.error("computeForEach procedure.execute failed", t);
162                 }
163             }
164
165             @Override
166             public void exception(AsyncReadGraph returnGraph, Throwable t) {
167                 try {
168                     procedure.exception(parentGraph, t);
169                 } catch (Throwable t2) {
170                     LOGGER.error("computeForEach procedure.exception failed", t2);
171                 }
172             }
173
174             @Override
175             public String toString() {
176                 return procedure.toString();
177             }
178
179         }, request);
180
181         request.perform(queryGraph, proc);
182         
183         proc.get();
184
185         if (entry != null)
186             entry.performFromCache(parentGraph, procedure_);
187
188     }
189
190     @Override
191     public String toString() {
192         if (isDiscarded())
193             return "DISCARDED " + request.toString();
194         else if (isExcepted())
195             return request.toString() + " " + getResult();
196         else
197             return request.toString() + " " + statusOrException;
198     }
199
200     @Override
201     public void execute(AsyncReadGraph graph, T result) {
202         setResult(result);
203         setReady();
204     }
205
206     @Override
207     public void exception(AsyncReadGraph graph, Throwable throwable) {
208         except(throwable);
209     }
210
211 }