]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java
Generate parts of db client query code
[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 org.simantics.databoard.binding.Binding;
15 import org.simantics.databoard.serialization.Serializer;
16 import org.simantics.databoard.util.URIStringUtils;
17 import org.simantics.db.common.WriteBindings;
18 import org.simantics.db.common.exception.DebugException;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.impl.procedure.InternalProcedure;
22
23 import gnu.trove.map.hash.TObjectIntHashMap;
24
25 final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectIntHashMap<String>>> {
26         
27     NamespaceIndex(final String id) {
28         super(id);
29     }
30     
31         @Override
32         final public void removeEntry(QueryProcessor provider) {
33                 provider.cache.remove(this);
34         }
35         
36         final private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
37                 
38                 if(root == 0) {
39             add2(graph, null);
40             procedure.execute(graph, null);
41 //            System.err.println("NamespaceIndex[" + id + "]->null");
42             return;
43                 }
44
45                 final int consistsOf = provider.getConsistsOf();
46                 final int hasName = provider.getHasName();
47         
48         final TObjectIntHashMap<String> result = new TObjectIntHashMap<String>();
49         
50         QueryCache.runnerObjects(graph, root, consistsOf, graph.parent, null, new SyncIntProcedure() {
51                 
52                 @Override
53                         public void run(ReadGraphImpl graph) throws DatabaseException {
54                         
55                         if(isPending()) { 
56                     add2(graph, result);
57                     procedure.execute(graph, result);
58 //                    System.err.println("NamespaceIndex[" + id + "]->" + result.size());
59                         } else {
60                                 procedure.exception(graph, (Throwable)statusOrException);
61                         }
62                                 
63                         }
64
65                         @Override
66                         public void finished(ReadGraphImpl graph) throws DatabaseException {
67                                 dec(graph);
68                         }
69
70                 @Override
71                 public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
72                         
73                         //System.out.println(id + " => " + obj);
74
75                         inc();
76                         
77                         QueryCache.runnerObjects(graph, obj, hasName, graph.parent, null, new IntProcedure() {
78                         
79                         @Override
80                         public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
81
82                                 inc();
83
84                                 QueryCache.runnerValueQuery(graph, i, NamespaceIndex.this, null, new InternalProcedure<byte[]>() {
85                                         
86                                         @Override
87                                         public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException {
88                                                 
89                                                 if(value != null) {
90
91                                         try {
92
93                                                 Binding b = WriteBindings.STRING;
94                                             Serializer serializer = b.serializer();
95                                             final String part = (String)serializer.deserialize(value);
96         
97                                             synchronized(result) {
98                                                 Object previous = result.put(URIStringUtils.escape(part), obj);
99                                                 // TODO: this is not the most elegant solution
100                                                 if(previous != null) previous = "";
101                                             }
102                                             
103                                                 } catch (Throwable e) {
104                                             if(DebugException.DEBUG) new DebugException(e).printStackTrace();
105                                         }
106                                                 
107                                                 }
108                                                 
109                                         dec(graph);
110                                         
111                                         }
112                                         
113                                         @Override
114                                         public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
115                                                         except(t);
116                                                 dec(graph);
117                             }
118
119                                 });
120                                 
121                         }
122                         
123                         @Override
124                         public void finished(ReadGraphImpl graph) throws DatabaseException {
125                                 dec(graph);
126                         }
127                                 
128                                 @Override
129                                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
130                                                 except(t);
131                         dec(graph);
132                     }
133
134                 });
135
136                 }
137                 
138         });
139         
140     }
141
142     @Override
143         public Object compute(ReadGraphImpl graph, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
144         
145         QueryProcessor processor = graph.processor;
146         
147 //      System.err.println("NamespaceIndex " + id);
148         
149         if("http://".equals(id) || "http:/".equals(id)) {
150             index(graph, processor, processor.getRootLibrary(), procedure);
151         } else {
152             final String[] parts = URIStringUtils.splitURI(id);
153             if(parts != null) {
154                 QueryCache.runnerNamespaceIndex(graph, parts[0], this, null, new InternalProcedure<TObjectIntHashMap<String>>() {
155     
156                     @Override
157                     public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) throws DatabaseException {
158     
159                         if(index != null) {
160                             index(graph, processor, index.get(parts[1]), procedure);
161                         } else {
162                             add2(graph, null);
163                             procedure.execute(graph, null);
164 //                            System.err.println("NamespaceIndex[" + id + "]->null");
165                         }
166                         
167                     }
168     
169                     @Override
170                     public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
171                         if(DebugException.DEBUG) new DebugException(t).printStackTrace();
172                         except(t);
173                         procedure.exception(graph, t);
174                     }
175     
176                 });
177             } else {
178                 add2(graph, null);
179                 procedure.execute(graph, null);
180 //                System.err.println("NamespaceIndex[" + id + "]->null");
181             }
182
183         }
184         
185         return getResult();
186         
187     }
188
189     @Override
190     public String toString() {
191         return "NamespaceIndex[" + id + "]";
192     }
193
194     synchronized private void add(TObjectIntHashMap<String> result) {
195         
196         throw new Error("Not possible!");
197         
198     }
199
200     private void add2(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
201         
202         if(!isPending()) {
203                 new Exception(""+hashCode()).printStackTrace();
204         }
205         
206         assert(isPending());
207
208 //        ArrayList<InternalProcedure<TObjectIntHashMap<String>>> p = null;
209
210         synchronized(this) {
211
212             setResult(result);
213                 setReady();
214 //            p = procs;
215 //            procs = null; 
216         
217         }
218         
219 //        if(p != null) {
220 //        
221 //              for(InternalProcedure<TObjectIntHashMap<String>> proc : p) proc.execute(graph, result);
222 //              
223 //        }
224         
225     }
226     
227     @Override
228     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
229         
230         assert(isReady());
231         
232         if(handleException(graph, procedure)) return (Throwable)statusOrException;
233         
234         TObjectIntHashMap<String> result = (TObjectIntHashMap<String>)getResult();
235         
236         procedure.execute(graph, result);
237         
238         return result;
239         
240     }
241     
242     @Override
243     public void recompute(ReadGraphImpl graph) throws DatabaseException {
244         
245         compute(graph, new InternalProcedure<TObjectIntHashMap<String>>() {
246
247             @Override
248             public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
249             }
250                         
251                         @Override
252                         public void exception(ReadGraphImpl graph, Throwable t) {
253                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
254                                 throw new Error("Error in recompute.", t);
255             }
256
257         });
258         
259     }
260     
261 }
262