]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncMultiReadEntry.java
8a8b18b7dda5641113207102d8c43f54c5608716
[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     final synchronized public void addOrSet(Object item) {
104
105         assert(isPending());
106         
107         ArrayList<T> value = (ArrayList<T>)getResult(); 
108         value.add((T)item);
109         
110     }
111     
112     @Override
113     public void clearResult(QuerySupport support) {
114         setResult(new ArrayList<T>());
115     }
116     
117     @Override
118     final public Query getQuery() {
119         
120         return new Query() {
121
122                         @Override
123                         public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
124                                 
125                                 QueryProcessor qp = (QueryProcessor)provider;
126
127                                 final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); 
128
129                                 try {
130
131                                     request.perform(parentGraph , new AsyncMultiProcedure<T>() {
132
133                         @Override
134                         public void execute(AsyncReadGraph graph, T result) {
135                             addOrSet(result);
136                         }
137                         
138                         public void finished(AsyncReadGraph graph) {
139                                 finish(graph);
140                         };
141                                         
142                                         @Override
143                                         public void exception(AsyncReadGraph graph, Throwable t) {
144                             except(t);
145                             }
146
147                     });
148
149                                 } catch (Throwable t) {
150                     except(t);
151                     if(DebugException.DEBUG) new DebugException(t).printStackTrace();
152                 }
153                                 
154                         }
155
156                         @Override
157                         public void removeEntry(QueryProcessor processor) {
158                         processor.asyncMultiReadMap.remove(request);
159                         }
160
161                         @Override
162                         public int type() {
163                                 return RequestFlags.INVALIDATE;
164                         }
165                         
166                         @Override
167                         public String toString() {
168                                 if(request == null) return "DISCARDED";
169                                 else return request.toString() + statusOrException;
170                         }
171                 
172         };
173         
174     }
175
176         @Override
177         public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
178                 
179                 final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
180
181         if(isExcepted()) {
182
183             try {
184                 proc.exception(graph, (Throwable)getResult());
185             } catch (Throwable t) {
186                 t.printStackTrace();
187             }
188             
189             
190         } else {
191             
192             final ArrayList<T> values = (ArrayList<T>)getResult();
193             for(T value : values) {
194                 try {
195                     proc.execute(graph, value);
196                 } catch (Throwable t) {
197                     t.printStackTrace();
198                 }
199             }
200
201             try {
202                 proc.finished(graph);
203             } catch (Throwable t) {
204                 t.printStackTrace();
205             }
206
207         }
208                 
209                 
210                 
211         }
212
213 //      @Override
214 //      public void performFromCache(int callerThread, Object provider,
215 //                      Object procedure) {
216 //
217 //          QueryProvider2 queryProvider = (QueryProvider2)provider;
218 //          ReadGraphImpl graph = ReadGraphImpl.forFromCache(callerThread, null, new ReadGraphSupportImpl(null, queryProvider, null));
219 //              performFromCache(graph, provider, procedure);
220 //              
221 //      }
222         
223         @Override
224         public String toString() {
225                 if(request == null) return "DISCARDED";
226                 else return request.toString() + statusOrException;
227         }
228
229 }