]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java
Fixed asynchronous recompute problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AsyncReadEntry.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.common.GraphSemaphore;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.DebugPolicy;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.db.request.AsyncRead;
21
22 final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
23
24     protected AsyncRead<T> request;
25
26     AsyncReadEntry(AsyncRead<T> request) {
27         this.request = request;
28         if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
29     }
30
31     @Override
32     int makeHash() {
33         return request.hashCode();
34     }
35     
36     @Override
37     public Object getOriginalRequest() {
38         return request;
39     }
40     
41     @Override
42     public void discard() {
43         super.discard();
44         setResult(null);
45     }
46     
47     final public void addOrSet(AsyncReadGraph graph, Object item) {
48
49 //      System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + item);
50         
51         assert(isPending());
52
53         synchronized(this) {
54             setResult(item);
55                 setReady();
56         }
57         
58     }
59     
60     
61     public void except(AsyncReadGraph graph, Throwable t) {
62         
63         assert(isPending());
64
65         synchronized(this) {
66             except(t);
67         }
68         
69     }
70     
71     
72     @Override
73     final public Query getQuery() {
74         
75         return new Query() {
76
77                         @Override
78                         public void recompute(ReadGraphImpl graph) {
79
80                                 try {
81
82                                         GraphSemaphore s = new GraphSemaphore(graph, 0);
83                                         
84                                     request.perform(graph , new AsyncProcedure<T>() {
85
86                         @Override
87                         public void execute(AsyncReadGraph graph, T result) {
88                             addOrSet(graph, result);
89                             s.release();
90                         }
91                                 
92                                 @Override
93                                 public void exception(AsyncReadGraph graph, Throwable t) {
94                                     except(t);
95                             s.release();
96                         }
97
98                     });
99
100                     s.waitFor(1);
101
102                                 } catch (Throwable t) {
103                                     except(t);
104                 }
105                                 
106                         }
107
108                         @Override
109                         public void removeEntry(QueryProcessor qp) {
110                         qp.cache.remove(AsyncReadEntry.this);
111                         }
112
113                         @Override
114                         public int type() {
115                                 return request.getFlags();
116                         }
117                         
118                         @Override
119                         public String toString() {
120                                 if(request == null) return "DISCARDED";
121                                 else if(isExcepted()) return request.toString() + " " + getResult();
122                                 else return request.toString() + " " + statusOrException;
123                         }
124                 
125         };
126         
127     }
128
129     @Override
130         public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
131                 
132             if(isExcepted()) {
133             
134             try {
135                 proc.exception(graph, (Throwable)getResult());
136             } catch (Throwable t) {
137                 t.printStackTrace();
138             }
139             
140         } else {
141             
142             try {
143                 proc.execute(graph, (T)getResult());
144             } catch (Throwable t) {
145                 t.printStackTrace();
146             }
147             
148         }
149                 
150             return getResult();
151             
152         }
153
154     //@Override
155     public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
156
157         ReadGraphImpl queryGraph = graph.withParent(this);
158
159         request.perform(queryGraph, new AsyncProcedure<T>() {
160
161                 @Override
162                 public void execute(AsyncReadGraph returnGraph, T result) {
163                         ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
164                         AsyncReadEntry.this.addOrSet(graph, result);
165                         try {
166                                 procedure.execute(graph, result);
167                         } catch (Throwable t) {
168                                 t.printStackTrace();
169                         }
170                         //                                      parentBarrier.dec(query);
171                 }
172
173                 @Override
174                 public void exception(AsyncReadGraph returnGraph, Throwable t) {
175                         ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
176                         //                                      AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
177                         AsyncReadEntry.this.except(graph, t);
178                         try {
179                                 procedure.exception(graph, t);
180                         } catch (Throwable t2) {
181                                 t2.printStackTrace();
182                         }
183                         //                                      parentBarrier.dec(query);
184                 }
185
186                 @Override
187                 public String toString() {
188                         return procedure.toString();
189                 }
190
191         });
192
193                 return getResult();
194         
195     }
196     
197     public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
198
199                 ReadGraphImpl queryGraph = parentGraph.withParent(entry);
200
201                 request.perform(queryGraph, new AsyncProcedure<T>() {
202
203                         @Override
204                         public void execute(AsyncReadGraph returnGraph, T result) {
205                                 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
206                                 if(entry != null) entry.addOrSet(parentGraph, result);
207                                 try {
208                                         procedure.execute(parentGraph, result);
209                                 } catch (Throwable t) {
210                                         t.printStackTrace();
211                                 }
212                         }
213
214                         @Override
215                         public void exception(AsyncReadGraph returnGraph, Throwable t) {
216                                 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
217                                 if(entry != null) entry.except(parentGraph, t);
218                                 try {
219                                         procedure.exception(parentGraph, t);
220                                 } catch (Throwable t2) {
221                                         t2.printStackTrace();
222                                 }
223                         }
224
225                         @Override
226                         public String toString() {
227                                 return procedure.toString();
228                         }
229
230                 });
231         
232     }
233     
234     
235         @Override
236         public String toString() {
237                 if(isDiscarded()) return "DISCARDED " + request.toString();
238                 else if(isExcepted()) return request.toString() + " " + getResult();
239                 else return request.toString() + " " + statusOrException;
240         }
241
242 }