]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectSuperRelations.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DirectSuperRelations.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 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 final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
23
24         DirectSuperRelations(final int resource) {
25                 super(resource);
26         }
27
28         @Override
29         final public void removeEntry(QueryProcessor provider) {
30                 provider.cache.remove(this);
31         }
32
33         class Koss {
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         //@Override
64         public Object compute(final ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
65
66                 QueryProcessor processor = graph.processor;
67                 
68                 processor.querySupport.ensureLoaded(graph, id);
69                 
70                 int single = processor.querySupport.getSingleSuperrelation(id);
71                 if(single > 0) {
72                         procedure.execute(graph, single);
73                         procedure.finished(graph);
74                         return single;
75                 }
76
77                 final int subrelationOf = processor.getSubrelationOf();
78
79                 final IntSet result = new IntSet(processor.querySupport);
80
81                 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
82                         @Override
83                         final public boolean execute(int r) {
84                                 result.add(r);
85                                 return true;
86                         }
87                         @Override
88                         final public void execute(ReadGraphImpl graph, int r) {
89                                 if(single == 0) {
90                                         single = r;
91                                         return;
92                                 }
93                                 add(r);
94                         }
95                         @Override
96                         public void finished(ReadGraphImpl graph) {
97                         }
98                         @Override
99                         public void exception(ReadGraphImpl graph, Throwable t) {
100                                 throw new Error("Errors are not supported.", t);
101                         }
102
103                 }
104
105                 final DirectProcedure directProc = new DirectProcedure();
106
107                 processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
108
109                 int size = directProc.size();
110
111                 if(size == 0) {
112
113                         procedure.finished(graph);
114
115                 } else if (size == 1) {
116
117                         procedure.execute(graph, directProc.single);
118                         procedure.finished(graph);
119
120                 } else {
121
122                         directProc.forEach(new TIntProcedure() {
123
124                                 @Override
125                                 public boolean execute(int arg0) {
126
127                                         try {
128                                                 procedure.execute(graph, arg0);
129                                         } catch (DatabaseException e) {
130                                                 Logger.defaultLogError(e);
131                                         }
132                                         return true;
133
134                                 }
135
136                         });
137
138                         procedure.finished(graph);
139
140                 }
141                 
142                 return getResult();
143
144
145         }
146
147         @Override
148         public String toString() {
149                 return "DirectSuperRelations[" + id + "]";
150         }
151
152         @Override
153         public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
154
155                 assert(isReady());
156
157                 return compute(graph, procedure);
158                 
159         }
160
161         @Override
162         public void recompute(ReadGraphImpl graph) throws DatabaseException {
163
164                 compute(graph, new IntProcedureAdapter() {
165
166                         @Override
167                         public void finished(ReadGraphImpl graph) {
168                         }
169
170                         @Override
171                         public void exception(ReadGraphImpl graph, Throwable t) {
172                                 new Error("Error in recompute.", t).printStackTrace();
173                         }
174
175                 });
176                 
177         }
178
179 }