]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / SuperTypes.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 org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.impl.procedure.InternalProcedure;
17
18 import gnu.trove.procedure.TIntProcedure;
19
20 public final class SuperTypes extends UnaryQueryPIntSet {
21
22     SuperTypes(int resource) {
23         super(resource);
24     }
25
26     @Override
27     public final void removeEntry(QueryProcessor provider) {
28         provider.cache.remove(this);
29     }
30
31     @Override
32     public void compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
33         computeForEach(graph, id, this, procedure);
34     }
35
36     public static Object computeForEach(ReadGraphImpl graph, int r, SuperTypes entry, final InternalProcedure<IntSet> procedure_) throws DatabaseException {
37
38         InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
39
40         QueryProcessor provider = graph.processor;
41
42         final int inherits = provider.getInherits();
43
44         final IntSet result = new IntSet(provider.querySupport);
45
46         final TIntProcedure addToResult = new TIntProcedure() {
47             @Override
48             public boolean execute(int r) {
49                 synchronized(result) {
50                     result.add(r);
51                 }
52                 return true;
53             }
54         };
55
56         QueryCache.runnerDirectObjects(graph, r, inherits, entry, null, new SyncIntProcedure() {
57
58             @Override
59             public void run(ReadGraphImpl graph) throws DatabaseException {
60                 procedure.execute(graph, result);
61             }
62
63             @Override
64             public void execute(ReadGraphImpl graph, final int i) throws DatabaseException {
65
66                 synchronized(result) {
67                     result.add(i);
68                 }
69
70                 inc();
71
72                 QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure<IntSet>() {
73
74                     @Override
75                     public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
76
77                         types.forEach(addToResult);
78                         dec(graph);
79                         
80                     }
81
82                     @Override
83                     public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
84                         procedure.exception(graph, t);
85                     }
86
87                 });
88
89             }
90
91             @Override
92             public void finished(ReadGraphImpl graph) throws DatabaseException {
93                 dec(graph);
94             }
95
96         });
97
98         if(entry != null) entry.performFromCache(graph, procedure_);
99
100         return result;
101
102     }
103
104     @Override
105     public String toString() {
106         return "SuperTypes[" + id + "]";
107     }
108
109 }