]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/MultiReadEntry.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / MultiReadEntry.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 java.util.ArrayList;
15
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.common.exception.DebugException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.impl.query.QueryProcessor.AsyncBarrier;
20 import org.simantics.db.procedure.AsyncMultiProcedure;
21 import org.simantics.db.request.MultiRead;
22 import org.simantics.db.request.RequestFlags;
23 import org.simantics.utils.datastructures.Pair;
24
25 final public class MultiReadEntry<T> extends CacheEntryBase {
26
27 //    public ArrayList<Pair<AsyncMultiProcedure<T>, AsyncBarrier>> procs;
28
29     protected MultiRead<T> request;
30     
31     public MultiReadEntry(MultiRead<T> request) {
32         this.request = request;
33     }
34
35     @Override
36     int makeHash() {
37         return request.hashCode();
38     }
39     
40     @Override
41     public Object getOriginalRequest() {
42         return request;
43     }
44     
45     @Override
46     public void discard() {
47         super.discard();
48         request = null;
49         setResult(null);
50     }
51     
52     synchronized public void finish(AsyncReadGraph graph) {
53         
54         assert(isPending());
55
56         ArrayList<Pair<AsyncMultiProcedure<T>, AsyncBarrier>> p = null;
57
58         synchronized(this) {
59
60                 setReady();
61                 
62 //            p = procs;
63 //            procs = null; 
64         
65         }
66         
67 //        if(p != null) {
68 //              ArrayList<T> v = (ArrayList<T>)getResult();
69 //              if(v != null) {
70 //                      for(Pair<AsyncMultiProcedure<T>, AsyncBarrier> pair : p) {
71 //                              for(T t : v) pair.first.execute(graph, t);
72 //                      }
73 //              }
74 //              for(Pair<AsyncMultiProcedure<T>, AsyncBarrier> pair : p) {
75 //                      pair.first.finished(graph);
76 //                      pair.second.dec();
77 //              }
78 //        }
79         
80     }
81
82     @Override
83     final public void clearResult(QuerySupport support) {
84         setResult(new ArrayList<T>());
85     }
86
87     final synchronized public void addOrSet(Object item) {
88
89         assert(isPending());
90
91         ArrayList<T> value = (ArrayList<T>)getResult(); 
92         value.add((T)item);
93         
94     }
95     
96     @Override
97     final public Query getQuery() {
98         
99         return new Query() {
100
101                         @Override
102                         public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
103                                 
104                                 QueryProcessor qp = (QueryProcessor)provider;
105
106                                 final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); 
107 //                parentGraph.state.barrier.inc();
108
109                                 try {
110
111                                     request.perform(parentGraph , new AsyncMultiProcedure<T>() {
112
113                         @Override
114                         public void execute(AsyncReadGraph graph, T result) {
115                             addOrSet(result);
116 //                            parentGraph.state.barrier.dec();
117                         }
118                         
119                         public void finished(AsyncReadGraph graph) {
120                                 finish(graph);
121 //                            parentGraph.state.barrier.dec();
122                         };
123                                         
124                                         @Override
125                                         public void exception(AsyncReadGraph graph, Throwable t) {
126                             except(t);
127 //                            parentGraph.state.barrier.dec();
128                             }
129
130                     });
131
132 //                                      parentGraph.waitAsync(request);
133
134                                 } catch (Throwable t) {
135                     except(t);
136 //                    parentGraph.state.barrier.dec();
137                     if(DebugException.DEBUG) new DebugException(t).printStackTrace();
138                 }
139                                 
140                         }
141
142                         @Override
143                         public void removeEntry(QueryProcessor processor) {
144                         processor.multiReadMap.remove(request);
145                         }
146
147                         @Override
148                         public int type() {
149                                 return RequestFlags.INVALIDATE;
150                         }
151                         
152                         @Override
153                         public String toString() {
154                                 if(request == null) return "DISCARDED";
155                                 else return request.toString() + statusOrException;
156                         }
157                 
158         };
159         
160     }
161
162     public void performFromCache(AsyncReadGraph graph, Object provider, Object procedure) {
163         
164         AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
165
166         if(isExcepted()) {
167             
168             try {
169                 proc.exception(graph, (Throwable)getResult());
170             } catch (Throwable t) {
171                 t.printStackTrace();
172             }
173 //            parentBarrier.dec();
174             
175         } else {
176             
177             final ArrayList<T> values = (ArrayList<T>)getResult();
178             for(T value : values) {
179                 try {
180                     proc.execute(graph, value);
181                 } catch (Throwable t) {
182                     t.printStackTrace();
183                 }
184             }
185
186             try {
187                 proc.finished(graph);
188             } catch (Throwable t) {
189                 t.printStackTrace();
190             }
191 //            parentBarrier.dec();
192
193         }
194         
195     }
196     
197         @Override
198         public void performFromCache(ReadGraphImpl graph, Object provider,
199                         Object procedure) {
200                 
201                 final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
202
203         if(isExcepted()) {
204             
205             try {
206                 proc.exception(graph, (Throwable)getResult());
207             } catch (Throwable t) {
208                 t.printStackTrace();
209             }
210             
211         } else {
212             
213             final ArrayList<T> values = (ArrayList<T>)getResult();
214             for(T value : values) {
215                 try {
216                     proc.execute(graph, value);
217                 } catch (Throwable t) {
218                     t.printStackTrace();
219                 }
220             }
221
222             try {
223                 proc.finished(graph);
224             } catch (Throwable t) {
225                 t.printStackTrace();
226             }
227
228         }
229                 
230                 
231                 
232         }
233         
234         @Override
235         public String toString() {
236                 if(request == null) return "DISCARDED";
237                 else return request.toString() + statusOrException;
238         }
239
240 }