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