]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / TypeHierarchy.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 import org.simantics.db.procedure.ListenerBase;
18
19 import gnu.trove.procedure.TIntProcedure;
20
21 public final class TypeHierarchy extends UnaryQueryPIntSet {
22
23     TypeHierarchy(int resource) {
24         super(resource);
25     }
26
27     public static final void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
28         QueryCache.runnerTypeHierarchy(graph, r, parent, listener, procedure);
29     }
30
31     @Override
32     public final void removeEntry(QueryProcessor provider) {
33         provider.cache.remove(this);
34     }
35
36     @Override
37     public void compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
38         computeForEach(graph, id, this, procedure);
39     }
40
41     public static IntSet computeForEach(ReadGraphImpl graph, int id, TypeHierarchy entry, final InternalProcedure<IntSet> procedure_) throws DatabaseException {
42
43         InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
44
45         QueryProcessor processor = graph.processor;
46
47         final IntSet result = new IntSet(processor.querySupport, id);
48
49         final TIntProcedure addToResult = new TIntProcedure() {
50             @Override
51             public boolean execute(int r) {
52                 result.add(r);
53                 return true;
54             }
55         };
56
57         QueryCache.runnerSuperTypes(graph, id, entry, null, new InternalProcedure<IntSet>() {
58
59             @Override
60             public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
61                 types.forEach(addToResult);
62                 procedure.execute(graph, result); 
63             }
64
65             @Override
66             public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
67                 procedure.exception(graph, t);
68             }
69
70         });
71
72         if(entry != null) entry.performFromCache(graph, procedure_);
73
74         return result;
75
76     }
77
78     @Override
79     public String toString() {
80         return "TypeHierarchy[" + id + "]";
81     }
82
83 }