]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java
New splash.bmp with version 1.36.0
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / NamespaceIndex.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.map.hash.TObjectIntHashMap;
15
16 import java.util.concurrent.Semaphore;
17
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.databoard.serialization.Serializer;
20 import org.simantics.databoard.util.URIStringUtils;
21 import org.simantics.db.common.WriteBindings;
22 import org.simantics.db.common.exception.DebugException;
23 import org.simantics.db.impl.graph.ReadGraphImpl;
24 import org.simantics.db.impl.procedure.InternalProcedure;
25 import org.simantics.db.procedure.ListenerBase;
26
27 final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectIntHashMap<String>>> {
28     
29 //      public ArrayList<InternalProcedure<TObjectIntHashMap<String>>> procs = null;
30         
31     private NamespaceIndex(final String id) {
32         super(id);
33     }
34     
35     final static void runner(ReadGraphImpl graph, final String id, final QueryProcessor provider, NamespaceIndex cached, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
36
37         NamespaceIndex entry = cached != null ? cached : (NamespaceIndex)provider.namespaceIndexMap22.get(id); 
38         if(entry == null) {
39                 
40                 entry = new NamespaceIndex(id);
41                 entry.setPending();
42                 entry.clearResult(provider.querySupport);
43                 entry.putEntry(provider);
44
45             provider.performForEach(graph, entry, parent, listener, procedure);
46             
47         } else {
48
49             if(entry.isPending()) {
50                 synchronized(entry) {
51                     if(entry.isPending()) {
52                         throw new IllegalStateException();
53 //                      if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<TObjectIntHashMap<String>>>(); 
54 //                      entry.procs.add(procedure);
55 //                        provider.registerDependencies(graph, entry, parent, listener, procedure, false);
56 //                      return;
57                     }
58                 }
59             }
60             provider.performForEach(graph, entry, parent, listener, procedure);
61         }
62         
63     }
64     
65     final public static void queryEach(ReadGraphImpl graph, final String id, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
66
67         final NamespaceIndex entry = (NamespaceIndex)provider.namespaceIndexMap22.get(id);
68         
69         if(parent == null && listener == null && entry != null && entry.isReady()) {
70                 entry.performFromCache(graph, provider, procedure);
71                 return;
72         }
73         
74         runner(graph, id, provider, entry, parent, listener, procedure);
75         
76     }
77      
78         @Override
79         public NamespaceIndex getEntry(QueryProcessor provider) {
80         return provider.namespaceIndexMap22.get(id);
81         }
82         
83         @Override
84         public void putEntry(QueryProcessor provider) {
85         provider.namespaceIndexMap22.put(id, this);
86         }
87
88         @Override
89         final public void removeEntry(QueryProcessor provider) {
90                 provider.namespaceIndexMap22.remove(id);
91         }
92         
93         final private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
94                 
95                 if(root == 0) {
96             add2(graph, null);
97             procedure.execute(graph, null);
98 //            System.err.println("NamespaceIndex[" + id + "]->null");
99             return;
100                 }
101
102                 final int consistsOf = provider.getConsistsOf();
103                 final int hasName = provider.getHasName();
104         
105         final TObjectIntHashMap<String> result = new TObjectIntHashMap<String>();
106         
107         Objects.runner(graph, root, consistsOf, graph.parent, null, new SyncIntProcedure() {
108                 
109                 @Override
110                         public void run(ReadGraphImpl graph) {
111                         
112                         if(isPending()) { 
113                     add2(graph, result);
114                     procedure.execute(graph, result);
115 //                    System.err.println("NamespaceIndex[" + id + "]->" + result.size());
116                         } else {
117                                 procedure.exception(graph, (Throwable)statusOrException);
118                         }
119                                 
120                         }
121
122                         @Override
123                         public void finished(ReadGraphImpl graph) {
124                             
125                                 dec(graph);
126                                 
127                         }
128
129                 @Override
130                 public void execute(ReadGraphImpl graph, final int obj) {
131                         
132                         //System.out.println(id + " => " + obj);
133
134                         inc();
135                         
136                         Objects.runner(graph, obj, hasName, graph.parent, null, new IntProcedure() {
137                         
138                         @Override
139                         public void execute(ReadGraphImpl graph, int i) {
140
141                                 inc();
142
143                                 ValueQuery.queryEach(graph, i, NamespaceIndex.this, null, new InternalProcedure<byte[]>() {
144                                         
145                                         @Override
146                                         public void execute(ReadGraphImpl graph, byte[] value) {
147                                                 
148                                                 if(value != null) {
149
150                                         try {
151
152                                                 Binding b = WriteBindings.STRING;
153                                             Serializer serializer = b.serializer();
154                                             final String part = (String)serializer.deserialize(value);
155         
156                                             synchronized(result) {
157                                                 Object previous = result.put(URIStringUtils.escape(part), obj);
158                                                 // TODO: this is not the most elegant solution
159                                                 if(previous != null) previous = "";
160                                             }
161                                             
162                                                 } catch (Throwable e) {
163                                             if(DebugException.DEBUG) new DebugException(e).printStackTrace();
164                                         }
165                                                 
166                                                 }
167                                                 
168                                         dec(graph);
169                                         
170                                         }
171                                         
172                                         @Override
173                                         public void exception(ReadGraphImpl graph, Throwable t) {
174                                                         except(t);
175                                                 dec(graph);
176                             }
177
178                                 });
179                                 
180                         }
181                         
182                         @Override
183                         public void finished(ReadGraphImpl graph) {
184                                 dec(graph);
185                         }
186                                 
187                                 @Override
188                                 public void exception(ReadGraphImpl graph, Throwable t) {
189                                                 except(t);
190                         dec(graph);
191                     }
192
193                 });
194
195                 }
196                 
197         });
198         
199     }
200
201     @Override
202         public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
203         
204 //      System.err.println("NamespaceIndex " + id);
205         
206         if("http://".equals(id) || "http:/".equals(id)) {
207             index(graph, processor, processor.getRootLibrary(), procedure);
208         } else {
209             final String[] parts = URIStringUtils.splitURI(id);
210             if(parts != null) {
211                 NamespaceIndex.queryEach(graph, parts[0], processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {
212     
213                     @Override
214                     public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
215     
216                         if(index != null) {
217                             index(graph, processor, index.get(parts[1]), procedure);
218                         } else {
219                             add2(graph, null);
220                             procedure.execute(graph, null);
221 //                            System.err.println("NamespaceIndex[" + id + "]->null");
222                         }
223                         
224                     }
225     
226                     @Override
227                     public void exception(ReadGraphImpl graph, Throwable t) {
228                         if(DebugException.DEBUG) new DebugException(t).printStackTrace();
229                         except(t);
230                         procedure.exception(graph, t);
231                     }
232     
233                 });
234             } else {
235                 add2(graph, null);
236                 procedure.execute(graph, null);
237 //                System.err.println("NamespaceIndex[" + id + "]->null");
238             }
239
240         }
241         
242     }
243
244     @Override
245     public String toString() {
246         return "NamespaceIndex[" + id + "]";
247     }
248
249     synchronized private void add(TObjectIntHashMap<String> result) {
250         
251         throw new Error("Not possible!");
252         
253     }
254
255     private void add2(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
256         
257         if(!isPending()) {
258                 new Exception(""+hashCode()).printStackTrace();
259         }
260         
261         assert(isPending());
262
263 //        ArrayList<InternalProcedure<TObjectIntHashMap<String>>> p = null;
264
265         synchronized(this) {
266
267             setResult(result);
268                 setReady();
269 //            p = procs;
270 //            procs = null; 
271         
272         }
273         
274 //        if(p != null) {
275 //        
276 //              for(InternalProcedure<TObjectIntHashMap<String>> proc : p) proc.execute(graph, result);
277 //              
278 //        }
279         
280     }
281     
282     @Override
283     public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<TObjectIntHashMap<String>> procedure) {
284         
285         assert(isReady());
286         
287         if(handleException(graph, procedure)) return;
288         
289         procedure.execute(graph, (TObjectIntHashMap<String>)getResult());
290         
291     }
292     
293     @Override
294     public synchronized void recompute(ReadGraphImpl graph, QueryProcessor provider) {
295         
296         final Semaphore s = new Semaphore(0);
297         
298         computeForEach(graph, provider, new InternalProcedure<TObjectIntHashMap<String>>() {
299
300             @Override
301             public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
302                 s.release();
303             }
304                         
305                         @Override
306                         public void exception(ReadGraphImpl graph, Throwable t) {
307                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
308                                 throw new Error("Error in recompute.", t);
309             }
310
311         });
312         
313         while(!s.tryAcquire()) {
314                 provider.resume(graph);
315         }
316         
317     }
318     
319 }
320