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