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