]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java
a9726a75d6ac4eb5df323625ffd19da9369bcb9b
[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.impl.DebugPolicy;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.procedure.AsyncProcedure;
18 import org.simantics.db.request.AsyncRead;
19
20 final public class AsyncReadEntry<T> extends CacheEntryBase {
21
22     protected AsyncRead<T> request;
23
24     public AsyncReadEntry(AsyncRead<T> request) {
25         this.request = request;
26         if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
27     }
28
29     @Override
30     int makeHash() {
31         return request.hashCode();
32     }
33     
34     @Override
35     public Object getOriginalRequest() {
36         return request;
37     }
38     
39     @Override
40     public void discard() {
41         super.discard();
42         //request = null;
43         setResult(null);
44     }
45     
46     final public void addOrSet(AsyncReadGraph graph, Object item) {
47
48         assert(isPending());
49         
50 //        ArrayList<AsyncProcedure<T>> p = null;
51
52         synchronized(this) {
53                 
54             setResult(item);
55                 setReady();
56 //            p = procs;
57 //            procs = null;
58             
59         }
60
61 //        if(p != null)
62 //            for(AsyncProcedure<T> proc : p) {
63 //              proc.execute(graph, (T)item);
64 ////                proc.first.execute(graph, (T)item);
65 ////                proc.second.dec();
66 //            }
67         
68     }
69     
70     
71     public void except(AsyncReadGraph graph, Throwable t) {
72         
73         assert(isPending());
74         
75 //        ArrayList<AsyncProcedure<T>> p = null;
76
77         synchronized(this) {
78                 
79             except(t);
80 ////            p = procs;
81 //            procs = null;
82             
83         }
84
85 //        if(p != null)
86 //            for(AsyncProcedure<T> proc : p) {
87 //              proc.exception(graph, t);
88 //            }
89         
90     }
91     
92     
93     @Override
94     final public Query getQuery() {
95         
96         return new Query() {
97
98                         @Override
99                         public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
100                                 
101                                 QueryProcessor qp = (QueryProcessor)provider;
102
103                                 final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); 
104
105                                 try {
106
107                                     request.perform(parentGraph , new AsyncProcedure<T>() {
108
109                         @Override
110                         public void execute(AsyncReadGraph graph, T result) {
111                             addOrSet(graph, result);
112                         }
113                                 
114                                 @Override
115                                 public void exception(AsyncReadGraph graph, Throwable t) {
116                                     except(t);
117                         }
118
119                     });
120
121                                 } catch (Throwable t) {
122                                     except(t);
123                 }
124                                 
125                         }
126
127                         @Override
128                         public void removeEntry(QueryProcessor qp) {
129                         qp.asyncReadMap.remove(request);
130                         }
131
132                         @Override
133                         public int type() {
134                                 return request.getFlags();
135                         }
136                         
137                         @Override
138                         public String toString() {
139                                 if(request == null) return "DISCARDED";
140                                 else if(isExcepted()) return request.toString() + " " + getResult();
141                                 else return request.toString() + " " + statusOrException;
142                         }
143                 
144         };
145         
146     }
147
148         @SuppressWarnings("unchecked")
149         public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
150                 
151         AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
152
153             if(isExcepted()) {
154             
155             try {
156                 proc.exception(graph, (Throwable)getResult());
157             } catch (Throwable t) {
158                 t.printStackTrace();
159             }
160             
161         } else {
162             
163             try {
164                 proc.execute(graph, (T)getResult());
165             } catch (Throwable t) {
166                 t.printStackTrace();
167             }
168             
169         }
170                 
171         }
172
173         @Override
174         public String toString() {
175                 if(isDiscarded()) return "DISCARDED " + request.toString();
176                 else if(isExcepted()) return request.toString() + " " + getResult();
177                 else return request.toString() + " " + statusOrException;
178         }
179
180 }