]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncMultiReadEntry.java
Merge "Trying to wait for procedures"
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AsyncMultiReadEntry.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 java.util.ArrayList;
15
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;
25
26 final public class AsyncMultiReadEntry<T> extends CacheEntryBase<AsyncMultiProcedure<T>> {
27
28     private static final Logger LOGGER = LoggerFactory.getLogger(AsyncMultiReadEntry.class);
29
30     protected AsyncMultiRead<T> request;
31     
32     AsyncMultiReadEntry(AsyncMultiRead<T> request) {
33         this.request = request;
34     }
35     
36     @Override
37     int makeHash() {
38         return request.hashCode();
39     }
40     
41     @Override
42     public Object getOriginalRequest() {
43         return request;
44     }
45     
46     @Override
47     public void discard() {
48         super.discard();
49         request = null;
50         setResult(null);
51     }
52     
53     final synchronized public void finish(AsyncReadGraph graph) {
54         
55         assert(isPending());
56
57         synchronized(this) {
58                 setReady();
59         }
60         
61     }
62
63     final synchronized public void except(AsyncReadGraph graph, Throwable t) {
64
65         assert(isPending());
66
67         synchronized(this) {
68                 except(t);
69         }
70         
71     }
72
73     @SuppressWarnings("unchecked")
74         final synchronized public void addOrSet(Object item) {
75
76         assert(isPending());
77         
78         ArrayList<T> value = (ArrayList<T>)getResult(); 
79         value.add((T)item);
80         
81     }
82     
83     @Override
84     public void clearResult(QuerySupport support) {
85         setResult(new ArrayList<T>());
86     }
87     
88     @Override
89     final public Query getQuery() {
90         
91         return new Query() {
92
93                         @Override
94                         public void recompute(ReadGraphImpl graph) {
95
96                                 try {
97
98                                         BlockingAsyncMultiProcedure<T> proc = new BlockingAsyncMultiProcedure<>(graph.asyncBarrier, graph, new AsyncMultiProcedure<T>() {
99
100                                                 @Override
101                                                 public void execute(AsyncReadGraph graph, T result) {
102                                                         addOrSet(result);
103                                                 }
104
105                                                 public void finished(AsyncReadGraph graph) {
106                                                         finish(graph);
107                                                 };
108
109                                                 @Override
110                                                 public void exception(AsyncReadGraph graph, Throwable t) {
111                                                         except(t);
112                                                 }
113
114                                         }, request);
115
116                                         request.perform(graph , proc);
117
118                                         proc.get();
119
120                                 } catch (Throwable t) {
121                                         
122                                         except(t);
123                                         
124                                 }
125                                 
126                         }
127
128                         @Override
129                         public void removeEntry(QueryProcessor processor) {
130                         processor.cache.remove(AsyncMultiReadEntry.this);
131                         }
132
133                         @Override
134                         public int type() {
135                                 return RequestFlags.INVALIDATE;
136                         }
137                         
138                         @Override
139                         public String toString() {
140                                 if(request == null) return "DISCARDED";
141                                 else return request.toString() + statusOrException;
142                         }
143                 
144         };
145         
146     }
147
148         @SuppressWarnings("unchecked")
149         @Override
150         public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure<T> proc) {
151
152         if(isExcepted()) {
153
154             try {
155                 proc.exception(graph, (Throwable)getResult());
156             } catch (Throwable t) {
157                 LOGGER.error("performFromCache proc.exception failed", t);
158             }
159
160         } else {
161
162             final ArrayList<T> values = (ArrayList<T>)getResult();
163             for(T value : values) {
164                 try {
165                     proc.execute(graph, value);
166                 } catch (Throwable t) {
167                     LOGGER.error("performFromCache proc.execute failed", t);
168                 }
169             }
170
171             try {
172                 proc.finished(graph);
173             } catch (Throwable t) {
174                 LOGGER.error("performFromCache proc.finished failed", t);
175             }
176
177         }
178                 
179                 return getResult();
180                 
181         }
182         
183         @Override
184         public String toString() {
185                 if(request == null) return "DISCARDED";
186                 else return request.toString() + statusOrException;
187         }
188
189         public Object compute(ReadGraphImpl graph, AsyncMultiProcedure<T> procedure) throws DatabaseException {
190                 return graph.processor.cache.performQuery(graph, request, this, procedure);
191         }
192
193 }