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