]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / URIToResource.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.util.URIStringUtils;\r
19 import org.simantics.db.common.exception.DebugException;\r
20 import org.simantics.db.impl.graph.ReadGraphImpl;\r
21 import org.simantics.db.impl.procedure.InternalProcedure;\r
22 import org.simantics.db.procedure.ListenerBase;\r
23 \r
24 public class URIToResource extends StringQuery<InternalProcedure<Integer>> {\r
25 \r
26 //    public ArrayList<InternalProcedure<Integer>> procs = null;\r
27     \r
28     private URIToResource(final String id) {\r
29         super(id);\r
30     }\r
31     \r
32     final static URIToResource entry(final QueryProcessor provider, final String id) {\r
33         return (URIToResource)provider.uriToResourceMap.get(id);\r
34     }\r
35 \r
36     final static void runner(ReadGraphImpl graph, final String id, CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {\r
37 \r
38         QueryProcessor processor = graph.processor;\r
39         \r
40         URIToResource entry = (URIToResource)processor.uriToResourceMap.get(id); \r
41         if(entry == null) {\r
42 \r
43                 entry = new URIToResource(id);\r
44                 entry.setPending();\r
45                 entry.clearResult(processor.querySupport);\r
46                 entry.putEntry(processor);\r
47 \r
48                 processor.performForEach(graph, entry, parent, listener, procedure);\r
49             \r
50         } else {\r
51                 \r
52             if(entry.isPending()) {\r
53                 synchronized(entry) {\r
54                     if(entry.isPending()) {\r
55                         throw new IllegalStateException();\r
56 //                      if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<Integer>>();\r
57 //                        entry.procs.add(procedure);\r
58 //                        processor.registerDependencies(graph, entry, parent, listener, procedure, false);\r
59 //                        return;\r
60                     }\r
61                 }\r
62             }\r
63             \r
64             processor.performForEach(graph, entry, parent, listener, procedure);\r
65             \r
66         }\r
67         \r
68     }\r
69     \r
70     final public static void queryEach(ReadGraphImpl graph, final String id, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {\r
71         \r
72         assert(id != null);\r
73         \r
74         if(graph.parent == null && listener == null) {\r
75                 URIToResource.computeForEach(graph, id, null, procedure);\r
76         } else {\r
77             runner(graph, id, parent, listener, procedure);\r
78         }\r
79          \r
80     }\r
81      \r
82     @Override\r
83     public URIToResource getEntry(QueryProcessor provider) {\r
84         return provider.uriToResourceMap.get(id);\r
85     }\r
86     \r
87     @Override\r
88     public void putEntry(QueryProcessor provider) {\r
89         provider.uriToResourceMap.put(id, this);\r
90     }\r
91 \r
92     @Override\r
93     final public void removeEntry(QueryProcessor provider) {\r
94         provider.uriToResourceMap.remove(id);\r
95     }\r
96 \r
97     private void lookup(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure, final String namespace, final String name) {\r
98         \r
99         NamespaceIndex.queryEach(graph, namespace, processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {\r
100 \r
101             @Override\r
102             public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {\r
103 \r
104                 if(index != null) {\r
105                     int result = index.get(name);\r
106                     if(result != 0) {\r
107                         addOrSet(graph, processor, result);\r
108                         procedure.execute(graph, result);\r
109                         return;\r
110                     }\r
111                 }\r
112                 \r
113                 addOrSet(graph, processor, new Integer(0));\r
114                 procedure.execute(graph, new Integer(0));\r
115 \r
116             }\r
117 \r
118             @Override\r
119             public void exception(ReadGraphImpl graph, Throwable t) {\r
120                 except(t);\r
121                 procedure.exception(graph, t);\r
122             }\r
123 \r
124         });\r
125 \r
126     }\r
127 \r
128     private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure<Integer> procedure, final String namespace, final String name) {\r
129         \r
130         NamespaceIndex.queryEach(graph, namespace, graph.processor, entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {\r
131 \r
132             @Override\r
133             public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {\r
134 \r
135                 if(index != null) {\r
136                     int result = index.get(name);\r
137                     if(result != 0) {\r
138                         if(entry != null) entry.addOrSet(graph, graph.processor, result);\r
139                         procedure.execute(graph, result);\r
140                         return;\r
141                     }\r
142                 }\r
143                 \r
144                 if(entry != null) entry.addOrSet(graph, graph.processor, new Integer(0));\r
145                 procedure.execute(graph, new Integer(0));\r
146 \r
147             }\r
148 \r
149             @Override\r
150             public void exception(ReadGraphImpl graph, Throwable t) {\r
151                 if(entry != null) entry.except(t);\r
152                 procedure.exception(graph, t);\r
153             }\r
154 \r
155         });\r
156 \r
157     }\r
158     \r
159     @Override\r
160     public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure) {\r
161         \r
162 //      new Exception("URIToResource " + id).printStackTrace();\r
163         \r
164         if("http://".equals(id) || "http:/".equals(id)) {\r
165             \r
166             addOrSet(graph, processor, processor.getRootLibrary());\r
167             procedure.execute(graph, processor.getRootLibrary());\r
168 \r
169         } else {\r
170             \r
171             final String[] parts = URIStringUtils.splitURI(id);\r
172             if (parts != null) {\r
173                 lookup(graph, processor, procedure, parts[0], parts[1]);\r
174             } else {\r
175                 lookup(graph, processor, procedure, "http://", id.replaceFirst("http://", ""));\r
176             }\r
177 \r
178         }\r
179         \r
180     }\r
181 \r
182     final private static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) {\r
183         \r
184         if("http://".equals(id) || "http:/".equals(id)) {\r
185             \r
186                 QueryProcessor processor = graph.processor;\r
187             if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary());\r
188             procedure.execute(graph, processor.getRootLibrary());\r
189 \r
190         } else {\r
191             \r
192             final String[] parts = URIStringUtils.splitURI(id);\r
193             if (parts != null) {\r
194                 lookup(graph, entry, procedure, parts[0], parts[1]);\r
195             } else {\r
196                 lookup(graph, entry, procedure, "http://", id.replaceFirst("http://", ""));\r
197             }\r
198 \r
199         }\r
200         \r
201     }\r
202     \r
203     public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {\r
204 \r
205         assert(isPending());\r
206 \r
207 //        ArrayList<InternalProcedure<Integer>> p = null;\r
208 \r
209         synchronized(this) {\r
210 \r
211             setResult(result);\r
212             setReady();\r
213 //            p = procs;\r
214 //            procs = null;\r
215             \r
216         }\r
217 \r
218 //        if(p != null)\r
219 //              for(InternalProcedure<Integer> proc : p) proc.execute(graph, result);\r
220         \r
221     }\r
222     \r
223     @Override\r
224     public String toString() {\r
225         return "URIToResource[" + id + "]";\r
226     }\r
227 \r
228     @Override\r
229     public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<Integer> procedure) {\r
230         \r
231         assert(isReady());\r
232         \r
233         if(handleException(graph, procedure)) return;\r
234         \r
235         if(isExcepted()) {\r
236                 procedure.exception(graph, (Throwable)statusOrException);\r
237         } else {\r
238             procedure.execute(graph, (Integer)getResult());\r
239         }\r
240         \r
241     }\r
242     \r
243     @Override\r
244     public void recompute(ReadGraphImpl graph, QueryProcessor provider) {\r
245         \r
246         final Semaphore s = new Semaphore(0);\r
247         \r
248         computeForEach(graph, provider, new InternalProcedure<Integer>() {\r
249 \r
250             @Override\r
251             public void execute(ReadGraphImpl graph, Integer result) {\r
252                 s.release();\r
253             }\r
254             \r
255             @Override\r
256             public void exception(ReadGraphImpl graph, Throwable t) {\r
257                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();\r
258                 throw new Error("Error in recompute.", t);\r
259             }\r
260 \r
261         });\r
262         \r
263         while(!s.tryAcquire()) {\r
264                 provider.resume(graph);\r
265         }\r
266         \r
267     }\r
268         \r
269 }\r