]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PossibleSuperRelation.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / PossibleSuperRelation.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 gnu.trove.procedure.TIntProcedure;
15 import gnu.trove.set.hash.TIntHashSet;
16
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.utils.Logger;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.procedure.AsyncProcedure;
22 import org.simantics.db.procedure.ListenerBase;
23
24 final public class PossibleSuperRelation extends UnaryQuery<IntProcedure> {
25
26         private PossibleSuperRelation(final int resource) {
27                 super(resource);
28         }
29
30         final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure<Resource> procedure) {
31
32                 int result = provider.querySupport.getSingleSuperrelation(r);
33                 if(result != 0) {
34                         procedure.execute(graph, provider.querySupport.getResource(result));
35                 } else {
36                         procedure.execute(graph, null);
37                 }
38
39         }
40
41         @Override
42         final public void removeEntry(QueryProcessor provider) {
43         }
44
45         class Koss {
46
47                 private TIntHashSet set = null;
48                 public int single = 0;
49
50                 public boolean add(int val) {
51                         if(single == val) return false;
52                         if(single == 0) {
53                                 single = val;
54                                 return true;
55                         }
56                         if(set == null) set = new TIntHashSet(4);
57                         return set.add(val);
58                 }
59
60                 public int size() {
61                         if(single == 0) return 0;
62                         if(set == null) return 1;
63                         return set.size() + 1;
64                 }
65
66                 public void forEach(TIntProcedure proc) {
67                         if(single > 0) proc.execute(single);
68                         if(set != null) set.forEach(proc);
69                 }
70
71         }
72
73         //@Override
74         public Object compute(final ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
75
76                 QueryProcessor processor = graph.processor;
77                 
78                 processor.querySupport.ensureLoaded(graph, id);
79                 int single = processor.querySupport.getSingleSuperrelation(id);
80                 if(single > 0) {
81                         procedure.execute(graph, single);
82                         procedure.finished(graph);
83                         return single;
84                 }
85
86                 final int subrelationOf = processor.getSubrelationOf();
87
88                 final IntSet result = new IntSet(processor.querySupport);
89
90                 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
91                         @Override
92                         final public boolean execute(int r) {
93                                 result.add(r);
94                                 return true;
95                         }
96                         @Override
97                         final public void execute(ReadGraphImpl graph, int r) {
98                                 if(single == 0) {
99                                         single = r;
100                                         return;
101                                 }
102                                 add(r);
103                         }
104                         @Override
105                         public void finished(ReadGraphImpl graph) {
106                         }
107                         @Override
108                         public void exception(ReadGraphImpl graph, Throwable t) {
109                                 throw new Error("Errors are not supported.", t);
110                         }
111
112                 }
113
114                 final DirectProcedure directProc = new DirectProcedure();
115
116                 processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
117
118                 int size = directProc.size();
119
120                 if(size == 0) {
121
122                         procedure.finished(graph);
123
124                 } else if (size == 1) {
125
126                         procedure.execute(graph, directProc.single);
127                         procedure.finished(graph);
128
129                 } else {
130
131                         directProc.forEach(new TIntProcedure() {
132
133                                 @Override
134                                 public boolean execute(int arg0) {
135
136                                         try {
137                                                 procedure.execute(graph, arg0);
138                                         } catch (DatabaseException e) {
139                                                 Logger.defaultLogError(e);
140                                         }
141                                         return true;
142
143                                 }
144
145                         });
146
147                         procedure.finished(graph);
148
149                 }
150                 
151                 return result;
152
153         }
154
155         @Override
156         public String toString() {
157                 return "PossibleSuperRelation[" + id + "]";
158         }
159
160         @Override
161         public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) {
162                 throw new UnsupportedOperationException();
163         }
164
165         @Override
166         public void recompute(ReadGraphImpl graph) {
167         }
168
169 }