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