]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java
Still working for multiple 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 //      System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + item);
49         
50         assert(isPending());
51
52         synchronized(this) {
53             setResult(item);
54                 setReady();
55         }
56         
57     }
58     
59     
60     public void except(AsyncReadGraph graph, Throwable t) {
61         
62         assert(isPending());
63
64         synchronized(this) {
65             except(t);
66         }
67         
68     }
69     
70     
71     @Override
72     final public Query getQuery() {
73         
74         return new Query() {
75
76                         @Override
77                         public void recompute(ReadGraphImpl graph) {
78
79                                 try {
80
81                                     request.perform(graph , new AsyncProcedure<T>() {
82
83                         @Override
84                         public void execute(AsyncReadGraph graph, T result) {
85                             addOrSet(graph, result);
86                         }
87                                 
88                                 @Override
89                                 public void exception(AsyncReadGraph graph, Throwable t) {
90                                     except(t);
91                         }
92
93                     });
94
95                                 } catch (Throwable t) {
96                                     except(t);
97                 }
98                                 
99                         }
100
101                         @Override
102                         public void removeEntry(QueryProcessor qp) {
103                         qp.cache.remove(AsyncReadEntry.this);
104                         }
105
106                         @Override
107                         public int type() {
108                                 return request.getFlags();
109                         }
110                         
111                         @Override
112                         public String toString() {
113                                 if(request == null) return "DISCARDED";
114                                 else if(isExcepted()) return request.toString() + " " + getResult();
115                                 else 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                 t.printStackTrace();
131             }
132             
133         } else {
134             
135             try {
136                 proc.execute(graph, (T)getResult());
137             } catch (Throwable t) {
138                 t.printStackTrace();
139             }
140             
141         }
142                 
143             return getResult();
144             
145         }
146
147     //@Override
148     public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
149
150         ReadGraphImpl queryGraph = graph.withParent(this);
151
152         request.perform(queryGraph, new AsyncProcedure<T>() {
153
154                 @Override
155                 public void execute(AsyncReadGraph returnGraph, T result) {
156                         ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
157                         AsyncReadEntry.this.addOrSet(graph, result);
158                         try {
159                                 procedure.execute(graph, result);
160                         } catch (Throwable t) {
161                                 t.printStackTrace();
162                         }
163                         //                                      parentBarrier.dec(query);
164                 }
165
166                 @Override
167                 public void exception(AsyncReadGraph returnGraph, Throwable t) {
168                         ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
169                         //                                      AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
170                         AsyncReadEntry.this.except(graph, t);
171                         try {
172                                 procedure.exception(graph, t);
173                         } catch (Throwable t2) {
174                                 t2.printStackTrace();
175                         }
176                         //                                      parentBarrier.dec(query);
177                 }
178
179                 @Override
180                 public String toString() {
181                         return procedure.toString();
182                 }
183
184         });
185
186                 return getResult();
187         
188     }
189     
190     public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
191
192                 ReadGraphImpl queryGraph = parentGraph.withParent(entry);
193
194                 request.perform(queryGraph, new AsyncProcedure<T>() {
195
196                         @Override
197                         public void execute(AsyncReadGraph returnGraph, T result) {
198                                 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
199                                 if(entry != null) entry.addOrSet(parentGraph, result);
200                                 try {
201                                         procedure.execute(parentGraph, result);
202                                 } catch (Throwable t) {
203                                         t.printStackTrace();
204                                 }
205                         }
206
207                         @Override
208                         public void exception(AsyncReadGraph returnGraph, Throwable t) {
209                                 ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
210                                 if(entry != null) entry.except(parentGraph, t);
211                                 try {
212                                         procedure.exception(parentGraph, t);
213                                 } catch (Throwable t2) {
214                                         t2.printStackTrace();
215                                 }
216                         }
217
218                         @Override
219                         public String toString() {
220                                 return procedure.toString();
221                         }
222
223                 });
224         
225     }
226     
227     
228         @Override
229         public String toString() {
230                 if(isDiscarded()) return "DISCARDED " + request.toString();
231                 else if(isExcepted()) return request.toString() + " " + getResult();
232                 else return request.toString() + " " + statusOrException;
233         }
234
235 }