1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.query;
14 import gnu.trove.map.hash.TObjectIntHashMap;
16 import java.util.concurrent.Semaphore;
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.databoard.serialization.Serializer;
20 import org.simantics.databoard.util.URIStringUtils;
21 import org.simantics.db.common.WriteBindings;
22 import org.simantics.db.common.exception.DebugException;
23 import org.simantics.db.impl.graph.ReadGraphImpl;
24 import org.simantics.db.impl.procedure.InternalProcedure;
25 import org.simantics.db.procedure.ListenerBase;
27 final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectIntHashMap<String>>> {
29 private NamespaceIndex(final String id) {
33 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) {
35 NamespaceIndex entry = cached != null ? cached : (NamespaceIndex)provider.cache.namespaceIndexMap22.get(id);
38 entry = new NamespaceIndex(id);
40 entry.clearResult(provider.querySupport);
41 entry.putEntry(provider);
43 provider.performForEach(graph, entry, parent, listener, procedure);
47 if(entry.isPending()) {
49 if(entry.isPending()) {
50 throw new IllegalStateException();
51 // if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<TObjectIntHashMap<String>>>();
52 // entry.procs.add(procedure);
53 // provider.registerDependencies(graph, entry, parent, listener, procedure, false);
58 provider.performForEach(graph, entry, parent, listener, procedure);
63 final public static void queryEach(ReadGraphImpl graph, final String id, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
65 final NamespaceIndex entry = (NamespaceIndex)provider.cache.namespaceIndexMap22.get(id);
67 if(parent == null && listener == null && entry != null && entry.isReady()) {
68 entry.performFromCache(graph, provider, procedure);
72 runner(graph, id, provider, entry, parent, listener, procedure);
77 public NamespaceIndex getEntry(QueryProcessor provider) {
78 return provider.cache.namespaceIndexMap22.get(id);
82 public void putEntry(QueryProcessor provider) {
83 provider.cache.namespaceIndexMap22.put(id, this);
87 final public void removeEntry(QueryProcessor provider) {
88 provider.cache.namespaceIndexMap22.remove(id);
91 final private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
95 procedure.execute(graph, null);
96 // System.err.println("NamespaceIndex[" + id + "]->null");
100 final int consistsOf = provider.getConsistsOf();
101 final int hasName = provider.getHasName();
103 final TObjectIntHashMap<String> result = new TObjectIntHashMap<String>();
105 Objects.runner(graph, root, consistsOf, graph.parent, null, new SyncIntProcedure() {
108 public void run(ReadGraphImpl graph) {
112 procedure.execute(graph, result);
113 // System.err.println("NamespaceIndex[" + id + "]->" + result.size());
115 procedure.exception(graph, (Throwable)statusOrException);
121 public void finished(ReadGraphImpl graph) {
128 public void execute(ReadGraphImpl graph, final int obj) {
130 //System.out.println(id + " => " + obj);
134 Objects.runner(graph, obj, hasName, graph.parent, null, new IntProcedure() {
137 public void execute(ReadGraphImpl graph, int i) {
141 ValueQuery.queryEach(graph, i, NamespaceIndex.this, null, new InternalProcedure<byte[]>() {
144 public void execute(ReadGraphImpl graph, byte[] value) {
150 Binding b = WriteBindings.STRING;
151 Serializer serializer = b.serializer();
152 final String part = (String)serializer.deserialize(value);
154 synchronized(result) {
155 Object previous = result.put(URIStringUtils.escape(part), obj);
156 // TODO: this is not the most elegant solution
157 if(previous != null) previous = "";
160 } catch (Throwable e) {
161 if(DebugException.DEBUG) new DebugException(e).printStackTrace();
171 public void exception(ReadGraphImpl graph, Throwable t) {
181 public void finished(ReadGraphImpl graph) {
186 public void exception(ReadGraphImpl graph, Throwable t) {
200 public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
202 // System.err.println("NamespaceIndex " + id);
204 if("http://".equals(id) || "http:/".equals(id)) {
205 index(graph, processor, processor.getRootLibrary(), procedure);
207 final String[] parts = URIStringUtils.splitURI(id);
209 NamespaceIndex.queryEach(graph, parts[0], processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {
212 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
215 index(graph, processor, index.get(parts[1]), procedure);
218 procedure.execute(graph, null);
219 // System.err.println("NamespaceIndex[" + id + "]->null");
225 public void exception(ReadGraphImpl graph, Throwable t) {
226 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
228 procedure.exception(graph, t);
234 procedure.execute(graph, null);
235 // System.err.println("NamespaceIndex[" + id + "]->null");
243 public String toString() {
244 return "NamespaceIndex[" + id + "]";
247 synchronized private void add(TObjectIntHashMap<String> result) {
249 throw new Error("Not possible!");
253 private void add2(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
256 new Exception(""+hashCode()).printStackTrace();
261 // ArrayList<InternalProcedure<TObjectIntHashMap<String>>> p = null;
274 // for(InternalProcedure<TObjectIntHashMap<String>> proc : p) proc.execute(graph, result);
281 public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<TObjectIntHashMap<String>> procedure) {
285 if(handleException(graph, procedure)) return;
287 procedure.execute(graph, (TObjectIntHashMap<String>)getResult());
292 public synchronized void recompute(ReadGraphImpl graph, QueryProcessor provider) {
294 final Semaphore s = new Semaphore(0);
296 computeForEach(graph, provider, new InternalProcedure<TObjectIntHashMap<String>>() {
299 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
304 public void exception(ReadGraphImpl graph, Throwable t) {
305 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
306 throw new Error("Error in recompute.", t);
311 while(!s.tryAcquire()) {
312 provider.resume(graph);