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