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