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