]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncMultiReadEntry.java
Working towards multiple readers.
[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.exception.DatabaseException;
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<AsyncMultiProcedure<T>> {
24
25     protected AsyncMultiRead<T> request;
26     
27     AsyncMultiReadEntry(AsyncMultiRead<T> request) {
28         this.request = request;
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 synchronized public void finish(AsyncReadGraph graph) {
49         
50         assert(isPending());
51
52         synchronized(this) {
53                 setReady();
54         }
55         
56     }
57
58     final synchronized public void except(AsyncReadGraph graph, Throwable t) {
59
60         assert(isPending());
61
62         synchronized(this) {
63                 except(t);
64         }
65         
66     }
67
68     @SuppressWarnings("unchecked")
69         final synchronized public void addOrSet(Object item) {
70
71         assert(isPending());
72         
73         ArrayList<T> value = (ArrayList<T>)getResult(); 
74         value.add((T)item);
75         
76     }
77     
78     @Override
79     public void clearResult(QuerySupport support) {
80         setResult(new ArrayList<T>());
81     }
82     
83     @Override
84     final public Query getQuery() {
85         
86         return new Query() {
87
88                         @Override
89                         public void recompute(ReadGraphImpl graph) {
90
91                                 try {
92
93                                         GraphSemaphore s = new GraphSemaphore(graph, 0);
94
95                                         request.perform(graph , new AsyncMultiProcedure<T>() {
96
97                                                 @Override
98                                                 public void execute(AsyncReadGraph graph, T result) {
99                                                         addOrSet(result);
100                                                 }
101
102                                                 public void finished(AsyncReadGraph graph) {
103                                                         finish(graph);
104                                                         s.release();
105                                                 };
106
107                                                 @Override
108                                                 public void exception(AsyncReadGraph graph, Throwable t) {
109                                                         except(t);
110                                                         s.release();
111                                                 }
112
113                                         });
114
115                                         s.waitFor(1);
116
117                                 } catch (Throwable t) {
118                                         
119                                         except(t);
120                                         
121                                 }
122                                 
123                         }
124
125                         @Override
126                         public void removeEntry(QueryProcessor processor) {
127                         processor.cache.remove(AsyncMultiReadEntry.this);
128                         }
129
130                         @Override
131                         public int type() {
132                                 return RequestFlags.INVALIDATE;
133                         }
134                         
135                         @Override
136                         public String toString() {
137                                 if(request == null) return "DISCARDED";
138                                 else return request.toString() + statusOrException;
139                         }
140                 
141         };
142         
143     }
144
145         @SuppressWarnings("unchecked")
146         @Override
147         public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure<T> proc) {
148
149         if(isExcepted()) {
150
151             try {
152                 proc.exception(graph, (Throwable)getResult());
153             } catch (Throwable t) {
154                 t.printStackTrace();
155             }
156             
157             
158         } else {
159             
160             final ArrayList<T> values = (ArrayList<T>)getResult();
161             for(T value : values) {
162                 try {
163                     proc.execute(graph, value);
164                 } catch (Throwable t) {
165                     t.printStackTrace();
166                 }
167             }
168
169             try {
170                 proc.finished(graph);
171             } catch (Throwable t) {
172                 t.printStackTrace();
173             }
174
175         }
176                 
177                 return getResult();
178                 
179         }
180         
181         @Override
182         public String toString() {
183                 if(request == null) return "DISCARDED";
184                 else return request.toString() + statusOrException;
185         }
186
187         //@Override
188         public Object compute(ReadGraphImpl graph, AsyncMultiProcedure<T> procedure) throws DatabaseException {
189         return graph.processor.cache.performQuery(graph, request, this, procedure);
190         }
191
192 }