]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Types.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.concurrent.atomic.AtomicInteger;
15
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
18 import org.simantics.db.impl.procedure.InternalProcedure;
19
20 import gnu.trove.procedure.TIntProcedure;
21
22 final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
23         
24     Types(final int resource) {
25         super(resource);
26     }
27
28         @Override
29         final public void removeEntry(QueryProcessor provider) {
30                 provider.cache.remove(this);
31         }
32
33         @Override
34         public Object compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
35                 computeForEach(graph, id, this, procedure);
36                 return getResult();
37         }
38
39         public static void computeForEach(final ReadGraphImpl graph, int id, Types entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
40
41                 if(entry != null)
42                         if(entry.isReady())
43                                 System.err.println("asd");
44                 
45                 assert(procedure != null);
46
47                 QueryProcessor processor = graph.processor;
48
49                 processor.querySupport.ensureLoaded(graph, id);
50                 
51                 int ret = processor.querySupport.getSingleInstance(id);
52                 if(ret > 0) {
53
54                         TypeHierarchy.queryEach(graph, ret, processor, entry, null, new InternalProcedure<IntSet>() {
55
56                                 @Override
57                                 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
58
59                                         if(entry != null) entry.addOrSet(graph, types, processor);
60                                         procedure.execute(graph, types);
61
62                                 }
63
64                                 @Override
65                                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
66                                         procedure.exception(graph, t);
67                                 }
68
69                         });
70
71                 }
72                 
73                 final int instanceOf = processor.getInstanceOf();
74         final int inherits = processor.getInherits();
75         final int subrelationOf = processor.getSubrelationOf();
76
77         final IntSet result = new IntSet(processor.querySupport);
78         
79         final TIntProcedure addToResult = new TIntProcedure() {
80             @Override
81             public boolean execute(int r) {
82                 synchronized(result) {
83                         result.add(r);
84                 }
85                 return true;
86             }
87         };
88         
89         final AtomicInteger finishes = new AtomicInteger(0);
90         
91         SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
92             
93             @Override
94             public void run(ReadGraphImpl graph) throws DatabaseException {
95                 
96                 if(finishes.addAndGet(1) == 3) {
97                     if(entry != null) entry.addOrSet(graph, result, processor);
98                     procedure.execute(graph, result);
99                 }
100                 
101             }
102             
103             @Override
104             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
105                 
106                 synchronized(result) {
107                         result.add(i);
108                 }
109                 
110                 inc();
111
112                 QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
113
114                     @Override
115                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
116                         types.forEach(addToResult);
117                         dec(graph);
118                     }
119                                 
120                                 @Override
121                                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
122                                         procedure.exception(graph, t);
123                         dec(graph);
124                     }
125
126                 });
127                 
128             }
129
130             @Override
131             public void finished(ReadGraphImpl graph) throws DatabaseException {
132                 dec(graph);
133             }
134             
135         }; 
136         
137         SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
138             
139             @Override
140             public void run(ReadGraphImpl graph) throws DatabaseException {
141
142                 int current = finishes.addAndGet(1);
143                 if(current == 3) {
144                     if(entry != null) entry.addOrSet(graph, result, processor);
145                     procedure.execute(graph, result);
146                 }
147                 
148             }
149             
150             @Override
151             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
152                 
153                 inc();
154
155                 QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
156
157                     @Override
158                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
159                         types.forEach(addToResult);
160                         dec(graph);
161                     }
162                                 
163                                 @Override
164                                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
165                                         procedure.exception(graph, t);
166                         dec(graph);
167                     }
168
169                 });
170                 
171             }
172
173             @Override
174             public void finished(ReadGraphImpl graph) throws DatabaseException {
175
176                 dec(graph);
177
178             }
179             
180         }; 
181
182         SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
183
184             @Override
185             public void run(ReadGraphImpl graph) throws DatabaseException {
186
187                 int current = finishes.addAndGet(1);
188                 if(current == 3) {
189                     if(entry != null) entry.addOrSet(graph, result, processor);
190                     procedure.execute(graph, result);
191                 }
192                 
193             }
194             
195             @Override
196             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
197                 
198                 inc();
199                 
200                 QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
201
202                     @Override
203                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
204
205                         types.forEach(addToResult);
206                         dec(graph);
207                         
208                     }
209                                 
210                                 @Override
211                                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
212                                         procedure.exception(graph, t);
213                         dec(graph);
214                     }
215
216                 });
217                 
218             }
219
220             @Override
221             public void finished(ReadGraphImpl graph) throws DatabaseException {
222                 
223                 dec(graph);
224
225             }
226             
227         }; 
228         
229         processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
230         instanceOfProcedure.finished(graph);
231         processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
232         inheritsProcedure.finished(graph);
233         processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
234         subrelationOfProcedure.finished(graph);
235         
236         if(entry != null) entry.finish();
237         
238     }
239     
240     @Override
241     public String toString() {
242         return "Types[" + id + "]";
243     }
244     
245     private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
246         
247         assert(!isReady());
248
249         setResult(value);
250         
251     }
252     
253     void finish() {
254
255         IntSet result = getResult(); 
256         result.trim();
257         setReady();
258         
259     }
260     
261     @Override
262     final public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
263         
264         assert(isReady());
265         
266         if(handleException(graph, procedure)) return EXCEPTED;
267         
268         IntSet result = getResult();
269         
270         procedure.execute(graph, result);
271         
272         return result;
273         
274     }
275     
276     @Override
277     public void recompute(ReadGraphImpl graph) throws DatabaseException {
278
279         compute(graph, new InternalProcedure<IntSet>() {
280
281                 @Override
282                 public void execute(ReadGraphImpl graph, IntSet result) {
283                 }
284
285             @Override
286             public void exception(ReadGraphImpl graph, Throwable t) {
287                 new Error("Error in recompute.", t).printStackTrace();
288             }
289
290         });
291         
292     }
293
294     @Override
295     boolean isImmutable(ReadGraphImpl graph) {
296         return graph.processor.isImmutable(id);
297     }
298     
299 }