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