]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / SuperTypes.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 org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.impl.procedure.InternalProcedure;
17 import org.simantics.db.procedure.ListenerBase;
18
19 import gnu.trove.procedure.TIntProcedure;
20
21 final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
22         
23     SuperTypes(final int resource) {
24         super(resource);
25     }
26     
27 //    final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
28 //        return QueryCache.runnerSuperTypes(graph, r, parent, listener, procedure);
29 //    }
30         
31         @Override
32         final public void removeEntry(QueryProcessor provider) {
33                 provider.cache.remove(this);
34         }
35
36         @Override
37         public Object compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
38                 return computeForEach(graph, id, this, procedure);
39         }
40         
41         private static void addOrSet(ReadGraphImpl graph, SuperTypes entry, IntSet value) {
42                 if(entry != null) {
43                         entry.addOrSet(graph, value, graph.processor);
44                 }
45         }
46         
47         public static Object computeForEach(ReadGraphImpl graph, int r, SuperTypes entry, final InternalProcedure<IntSet> procedure) throws DatabaseException {
48
49                 QueryProcessor provider = graph.processor;
50
51         final int inherits = provider.getInherits();
52
53         final IntSet result = new IntSet(provider.querySupport);
54         
55         final TIntProcedure addToResult = new TIntProcedure() {
56             @Override
57             public boolean execute(int r) {
58                 synchronized(result) {
59                         result.add(r);
60                 }
61                 return true;
62             }
63         };
64
65         QueryCache.runnerDirectObjects(graph, r, inherits, entry, null, new SyncIntProcedure() {
66             
67             @Override
68             public void run(ReadGraphImpl graph) throws DatabaseException {
69
70                 addOrSet(graph, entry, result);
71                 procedure.execute(graph, result);
72                 
73             }
74             
75             @Override
76             public void execute(ReadGraphImpl graph, final int i) throws DatabaseException {
77                 
78 //              assert(graph.parent == parent);
79                 
80                 synchronized(result) {
81                         result.add(i);
82                 }
83                 
84                 inc();
85
86                 QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
87
88                     @Override
89                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
90
91                         types.forEach(addToResult);
92                         dec(graph);
93                         
94                     }
95                                 
96                                 @Override
97                                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
98                                         procedure.exception(graph, t);
99                     }
100
101                 });
102                 
103             }
104
105             @Override
106             public void finished(ReadGraphImpl graph) throws DatabaseException {
107                 dec(graph);
108             }
109             
110         });
111         
112         return result;
113         
114     }
115     
116     @Override
117     public String toString() {
118         return "SuperTypes2[" + id + "]";
119     }
120
121     private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
122         
123         assert(!isReady());
124
125         synchronized(this) {
126         
127             value.trim();
128             setResult(value);
129             setReady();
130         
131         }
132         
133     }
134
135     @Override
136     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
137         
138         assert(isReady());
139
140         if(handleException(graph, procedure)) return null;
141         
142         IntSet result = getResult();
143         
144         procedure.execute(graph, result);
145         
146         return result;
147         
148     }
149     
150     @Override
151     public void recompute(ReadGraphImpl graph) throws DatabaseException {
152
153         compute(graph, new InternalProcedure<IntSet>() {
154
155                 @Override
156                 public void execute(ReadGraphImpl graph, IntSet result) {
157                 }
158
159             @Override
160             public void exception(ReadGraphImpl graph, Throwable t) {
161                 new Error("Error in recompute.", t).printStackTrace();
162             }
163
164         });
165         
166     }
167     
168     @Override
169     boolean isImmutable(ReadGraphImpl graph) {
170         return graph.processor.isImmutable(id);
171     }
172     
173 }