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