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