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