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