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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.impl.query;
\r
14 import gnu.trove.map.hash.TObjectIntHashMap;
\r
16 import java.util.concurrent.Semaphore;
\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
27 final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectIntHashMap<String>>> {
\r
29 // public ArrayList<InternalProcedure<TObjectIntHashMap<String>>> procs = null;
\r
31 private NamespaceIndex(final String id) {
\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
37 NamespaceIndex entry = cached != null ? cached : (NamespaceIndex)provider.namespaceIndexMap22.get(id);
\r
40 entry = new NamespaceIndex(id);
\r
42 entry.clearResult(provider.querySupport);
\r
43 entry.putEntry(provider);
\r
45 provider.performForEach(graph, entry, parent, listener, procedure);
\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
60 provider.performForEach(graph, entry, parent, listener, procedure);
\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
67 final NamespaceIndex entry = (NamespaceIndex)provider.namespaceIndexMap22.get(id);
\r
69 if(parent == null && listener == null && entry != null && entry.isReady()) {
\r
70 entry.performFromCache(graph, provider, procedure);
\r
74 runner(graph, id, provider, entry, parent, listener, procedure);
\r
79 public NamespaceIndex getEntry(QueryProcessor provider) {
\r
80 return provider.namespaceIndexMap22.get(id);
\r
84 public void putEntry(QueryProcessor provider) {
\r
85 provider.namespaceIndexMap22.put(id, this);
\r
89 final public void removeEntry(QueryProcessor provider) {
\r
90 provider.namespaceIndexMap22.remove(id);
\r
93 final private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
\r
97 procedure.execute(graph, null);
\r
98 // System.err.println("NamespaceIndex[" + id + "]->null");
\r
102 final int consistsOf = provider.getConsistsOf();
\r
103 final int hasName = provider.getHasName();
\r
105 final TObjectIntHashMap<String> result = new TObjectIntHashMap<String>();
\r
107 Objects.runner(graph, root, consistsOf, graph.parent, null, new SyncIntProcedure() {
\r
110 public void run(ReadGraphImpl graph) {
\r
113 add2(graph, result);
\r
114 procedure.execute(graph, result);
\r
115 // System.err.println("NamespaceIndex[" + id + "]->" + result.size());
\r
117 procedure.exception(graph, (Throwable)statusOrException);
\r
123 public void finished(ReadGraphImpl graph) {
\r
130 public void execute(ReadGraphImpl graph, final int obj) {
\r
132 //System.out.println(id + " => " + obj);
\r
136 Objects.runner(graph, obj, hasName, graph.parent, null, new IntProcedure() {
\r
139 public void execute(ReadGraphImpl graph, int i) {
\r
143 ValueQuery.queryEach(graph, i, NamespaceIndex.this, null, new InternalProcedure<byte[]>() {
\r
146 public void execute(ReadGraphImpl graph, byte[] value) {
\r
148 if(value != null) {
\r
152 Binding b = WriteBindings.STRING;
\r
153 Serializer serializer = b.serializer();
\r
154 final String part = (String)serializer.deserialize(value);
\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
162 } catch (Throwable e) {
\r
163 if(DebugException.DEBUG) new DebugException(e).printStackTrace();
\r
173 public void exception(ReadGraphImpl graph, Throwable t) {
\r
183 public void finished(ReadGraphImpl graph) {
\r
188 public void exception(ReadGraphImpl graph, Throwable t) {
\r
202 public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
\r
204 // System.err.println("NamespaceIndex " + id);
\r
206 if("http://".equals(id) || "http:/".equals(id)) {
\r
207 index(graph, processor, processor.getRootLibrary(), procedure);
\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
214 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
\r
216 if(index != null) {
\r
217 index(graph, processor, index.get(parts[1]), procedure);
\r
220 procedure.execute(graph, null);
\r
221 // System.err.println("NamespaceIndex[" + id + "]->null");
\r
227 public void exception(ReadGraphImpl graph, Throwable t) {
\r
228 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
\r
230 procedure.exception(graph, t);
\r
236 procedure.execute(graph, null);
\r
237 // System.err.println("NamespaceIndex[" + id + "]->null");
\r
245 public String toString() {
\r
246 return "NamespaceIndex[" + id + "]";
\r
249 synchronized private void add(TObjectIntHashMap<String> result) {
\r
251 throw new Error("Not possible!");
\r
255 private void add2(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
\r
258 new Exception(""+hashCode()).printStackTrace();
\r
261 assert(isPending());
\r
263 // ArrayList<InternalProcedure<TObjectIntHashMap<String>>> p = null;
\r
265 synchronized(this) {
\r
276 // for(InternalProcedure<TObjectIntHashMap<String>> proc : p) proc.execute(graph, result);
\r
283 public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<TObjectIntHashMap<String>> procedure) {
\r
287 if(handleException(graph, procedure)) return;
\r
289 procedure.execute(graph, (TObjectIntHashMap<String>)getResult());
\r
294 public synchronized void recompute(ReadGraphImpl graph, QueryProcessor provider) {
\r
296 final Semaphore s = new Semaphore(0);
\r
298 computeForEach(graph, provider, new InternalProcedure<TObjectIntHashMap<String>>() {
\r
301 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
\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
313 while(!s.tryAcquire()) {
\r
314 provider.resume(graph);
\r