]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Types.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.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 public final class Types extends UnaryQueryPIntSet {
23
24     Types(int resource) {
25         super(resource);
26     }
27
28     @Override
29     public final void removeEntry(QueryProcessor provider) {
30         provider.cache.remove(this);
31     }
32
33     @Override
34     public void compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
35         computeForEach(graph, id, this, procedure);
36     }
37
38     public static void computeForEach(final ReadGraphImpl graph, int id, Types entry,
39             final InternalProcedure<IntSet> procedure_) throws DatabaseException {
40
41         InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
42
43         computeForEach2(graph, id, entry, procedure);
44
45         if (entry != null)
46             entry.performFromCache(graph, procedure_);
47
48     }
49
50     public static void computeForEach2(final ReadGraphImpl graph, int id, Types parent,
51             final InternalProcedure<IntSet> procedure) throws DatabaseException {
52
53         QueryProcessor processor = graph.processor;
54
55         processor.querySupport.ensureLoaded(graph, id);
56
57         int ret = processor.querySupport.getSingleInstance(id);
58         if (ret > 0) {
59
60             TypeHierarchy.queryEach(graph, ret, processor, parent, null, new InternalProcedure<IntSet>() {
61
62                 @Override
63                 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
64                     procedure.execute(graph, types);
65                 }
66
67                 @Override
68                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
69                     procedure.exception(graph, t);
70                 }
71
72             });
73
74             return;
75
76         }
77
78         final int instanceOf = processor.getInstanceOf();
79         final int inherits = processor.getInherits();
80         final int subrelationOf = processor.getSubrelationOf();
81
82         final IntSet result = new IntSet(processor.querySupport);
83
84         final TIntProcedure addToResult = new TIntProcedure() {
85             @Override
86             public boolean execute(int r) {
87                 synchronized (result) {
88                     result.add(r);
89                 }
90                 return true;
91             }
92         };
93
94         final AtomicInteger finishes = new AtomicInteger(0);
95
96         SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
97
98             @Override
99             public void run(ReadGraphImpl graph) throws DatabaseException {
100
101                 if (finishes.addAndGet(1) == 3) {
102                     procedure.execute(graph, result);
103                 }
104
105             }
106
107             @Override
108             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
109
110                 result.add(i);
111
112                 inc();
113
114                 QueryCache.runnerSuperTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
115
116                     @Override
117                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
118                         types.forEach(addToResult);
119                         dec(graph);
120                     }
121
122                     @Override
123                     public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
124                         procedure.exception(graph, t);
125                         dec(graph);
126                     }
127
128                 });
129
130             }
131
132             @Override
133             public void finished(ReadGraphImpl graph) throws DatabaseException {
134                 dec(graph);
135             }
136
137         };
138
139         SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
140
141             @Override
142             public void run(ReadGraphImpl graph) throws DatabaseException {
143
144                 int current = finishes.addAndGet(1);
145                 if (current == 3) {
146                     procedure.execute(graph, result);
147                 }
148
149             }
150
151             @Override
152             public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
153
154                 inc();
155
156                 QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
157
158                     @Override
159                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
160                         types.forEach(addToResult);
161                         dec(graph);
162                     }
163
164                     @Override
165                     public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
166                         procedure.exception(graph, t);
167                         dec(graph);
168                     }
169
170                 });
171
172             }
173
174             @Override
175             public void finished(ReadGraphImpl graph) throws DatabaseException {
176
177                 dec(graph);
178
179             }
180
181         };
182
183         SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
184
185             @Override
186             public void run(ReadGraphImpl graph) throws DatabaseException {
187
188                 int current = finishes.addAndGet(1);
189                 if (current == 3) {
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, parent, 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     }
237
238     @Override
239     public String toString() {
240         return "Types[" + id + "]";
241     }
242
243 }