]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java
0489546c55d6caa5fdd50f5537bf321302db4f7f
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / SuperTypes.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
16 import java.util.concurrent.Semaphore;
17
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.impl.procedure.InternalProcedure;
20 import org.simantics.db.procedure.ListenerBase;
21
22 final public class SuperTypes extends UnaryQuery<InternalProcedure<IntSet>> {
23     
24 //      public ArrayList<InternalProcedure<IntSet>> procs = null;
25         
26     private SuperTypes(final int resource) {
27         super(resource);
28     }
29     
30     final static SuperTypes runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
31
32         SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);
33         if(entry == null) {
34                 
35                 entry = new SuperTypes(r);
36                 entry.setPending();
37                 entry.clearResult(provider.querySupport);
38                 entry.putEntry(provider);
39                 
40             provider.performForEach(graph, entry, parent, listener, procedure);
41             
42             return entry;
43             
44         } else {
45                 
46             if(!entry.isReady()) {
47                 synchronized(entry) {
48                     if(!entry.isReady()) {
49                         throw new IllegalStateException();
50 //                      if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<IntSet>>();
51 //                      entry.procs.add(procedure);
52 //                                              provider.registerDependencies(graph, entry, parent, listener, procedure, false);
53 //                      return entry;
54                     }
55                 }
56             }
57             provider.performForEach(graph, entry, parent, listener, procedure);
58         }
59         
60         return entry;
61
62     }
63     
64     final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
65     
66         if(parent == null && listener == null) {
67                 SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r);
68                 if(entry != null && entry.isReady()) { 
69                         entry.performFromCache(graph, provider, procedure);
70                         return entry;
71                 }
72         }
73         
74         return runner(graph, r, parent, provider, listener, procedure);
75          
76     }
77         
78         @Override
79         public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
80         return provider.superTypesMap.get(id);
81         }
82         
83         @Override
84         public void putEntry(QueryProcessor provider) {
85         provider.superTypesMap.put(id, this);
86         }
87
88         @Override
89         final public void removeEntry(QueryProcessor provider) {
90                 provider.superTypesMap.remove(id);
91         }
92
93         @Override
94         public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, boolean store) {
95
96         final int inherits = provider.getInherits();
97         
98         final CacheEntry parent = graph.parent;
99         
100         assert(graph.parent != this);
101
102         final IntSet result = new IntSet(provider.querySupport);
103         
104         final TIntProcedure addToResult = new TIntProcedure() {
105             @Override
106             public boolean execute(int r) {
107                 synchronized(result) {
108                         result.add(r);
109                 }
110                 return true;
111             }
112         };
113
114         DirectObjects.queryEach(graph, id, inherits, provider, this, null, new SyncIntProcedure() {
115             
116             @Override
117             public void run(ReadGraphImpl graph) {
118
119                 addOrSet(graph, result, provider);
120                 procedure.execute(graph, result);
121                 
122             }
123             
124             @Override
125             public void execute(ReadGraphImpl graph, final int i) {
126                 
127                 assert(graph.parent == parent);
128                 
129                 synchronized(result) {
130                         result.add(i);
131                 }
132                 
133                 inc();
134
135                 SuperTypes.queryEach(graph, i, provider, SuperTypes.this, null, new InternalProcedure<IntSet>() {
136
137                     @Override
138                     public void execute(ReadGraphImpl graph, IntSet types) {
139
140                         types.forEach(addToResult);
141                         dec(graph);
142                         
143                     }
144                                 
145                                 @Override
146                                 public void exception(ReadGraphImpl graph, Throwable t) {
147                                         procedure.exception(graph, t);
148                     }
149
150                 });
151                 
152             }
153
154             @Override
155             public void finished(ReadGraphImpl graph) {
156                 dec(graph);
157             }
158             
159         });
160         
161         return result;
162         
163     }
164     
165     @Override
166     public String toString() {
167         return "SuperTypes2[" + id + "]";
168     }
169
170     private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
171         
172         assert(!isReady());
173
174 //        ArrayList<InternalProcedure<IntSet>> p = null;
175
176         synchronized(this) {
177         
178             value.trim();
179             setResult(value);
180             setReady();
181 //            p = procs;
182 //            procs = null; 
183         
184         }
185
186 //        if(p != null) {
187 //              IntSet v = (IntSet)getResult();
188 //              if(v != null) {
189 //                  for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
190 //              }
191 //        }
192         
193     }
194
195     @Override
196     public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
197         
198         assert(isReady());
199
200         if(handleException(graph, procedure)) return null;
201         
202         IntSet result = getResult();
203         
204         procedure.execute(graph, result);
205         
206         return result;
207         
208     }
209     
210     @Override
211     public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
212         
213         final Semaphore s = new Semaphore(0);
214
215         computeForEach(graph, provider, new InternalProcedure<IntSet>() {
216
217                 @Override
218                 public void execute(ReadGraphImpl graph, IntSet result) {
219                 s.release();
220                 }
221
222             @Override
223             public void exception(ReadGraphImpl graph, Throwable t) {
224                 s.release();
225                 new Error("Error in recompute.", t).printStackTrace();
226             }
227
228         }, true);
229
230         while(!s.tryAcquire()) {
231                 provider.resume(graph);
232         }
233         
234     }
235     
236     @Override
237     boolean isImmutable(ReadGraphImpl graph) {
238         return graph.processor.isImmutable(id);
239     }
240     
241 }