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 // public ArrayList<InternalProcedure<TObjectIntHashMap<String>>> procs = null;
31 private NamespaceIndex(final String id) {
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) {
37 NamespaceIndex entry = cached != null ? cached : (NamespaceIndex)provider.namespaceIndexMap22.get(id);
40 entry = new NamespaceIndex(id);
42 entry.clearResult(provider.querySupport);
43 entry.putEntry(provider);
45 provider.performForEach(graph, entry, parent, listener, procedure);
49 if(entry.isPending()) {
51 if(entry.isPending()) {
52 throw new IllegalStateException();
53 // if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<TObjectIntHashMap<String>>>();
54 // entry.procs.add(procedure);
55 // provider.registerDependencies(graph, entry, parent, listener, procedure, false);
60 provider.performForEach(graph, entry, parent, listener, procedure);
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) {
67 final NamespaceIndex entry = (NamespaceIndex)provider.namespaceIndexMap22.get(id);
69 if(parent == null && listener == null && entry != null && entry.isReady()) {
70 entry.performFromCache(graph, provider, procedure);
74 runner(graph, id, provider, entry, parent, listener, procedure);
79 public NamespaceIndex getEntry(QueryProcessor provider) {
80 return provider.namespaceIndexMap22.get(id);
84 public void putEntry(QueryProcessor provider) {
85 provider.namespaceIndexMap22.put(id, this);
89 final public void removeEntry(QueryProcessor provider) {
90 provider.namespaceIndexMap22.remove(id);
93 final private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
97 procedure.execute(graph, null);
98 // System.err.println("NamespaceIndex[" + id + "]->null");
102 final int consistsOf = provider.getConsistsOf();
103 final int hasName = provider.getHasName();
105 final TObjectIntHashMap<String> result = new TObjectIntHashMap<String>();
107 Objects.runner(graph, root, consistsOf, graph.parent, null, new SyncIntProcedure() {
110 public void run(ReadGraphImpl graph) {
114 procedure.execute(graph, result);
115 // System.err.println("NamespaceIndex[" + id + "]->" + result.size());
117 procedure.exception(graph, (Throwable)statusOrException);
123 public void finished(ReadGraphImpl graph) {
130 public void execute(ReadGraphImpl graph, final int obj) {
132 //System.out.println(id + " => " + obj);
136 Objects.runner(graph, obj, hasName, graph.parent, null, new IntProcedure() {
139 public void execute(ReadGraphImpl graph, int i) {
143 ValueQuery.queryEach(graph, i, NamespaceIndex.this, null, new InternalProcedure<byte[]>() {
146 public void execute(ReadGraphImpl graph, byte[] value) {
152 Binding b = WriteBindings.STRING;
153 Serializer serializer = b.serializer();
154 final String part = (String)serializer.deserialize(value);
156 synchronized(result) {
157 Object previous = result.put(URIStringUtils.escape(part), obj);
158 // TODO: this is not the most elegant solution
159 if(previous != null) previous = "";
162 } catch (Throwable e) {
163 if(DebugException.DEBUG) new DebugException(e).printStackTrace();
173 public void exception(ReadGraphImpl graph, Throwable t) {
183 public void finished(ReadGraphImpl graph) {
188 public void exception(ReadGraphImpl graph, Throwable t) {
202 public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
204 // System.err.println("NamespaceIndex " + id);
206 if("http://".equals(id) || "http:/".equals(id)) {
207 index(graph, processor, processor.getRootLibrary(), procedure);
209 final String[] parts = URIStringUtils.splitURI(id);
211 NamespaceIndex.queryEach(graph, parts[0], processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {
214 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
217 index(graph, processor, index.get(parts[1]), procedure);
220 procedure.execute(graph, null);
221 // System.err.println("NamespaceIndex[" + id + "]->null");
227 public void exception(ReadGraphImpl graph, Throwable t) {
228 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
230 procedure.exception(graph, t);
236 procedure.execute(graph, null);
237 // System.err.println("NamespaceIndex[" + id + "]->null");
245 public String toString() {
246 return "NamespaceIndex[" + id + "]";
249 synchronized private void add(TObjectIntHashMap<String> result) {
251 throw new Error("Not possible!");
255 private void add2(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
258 new Exception(""+hashCode()).printStackTrace();
263 // ArrayList<InternalProcedure<TObjectIntHashMap<String>>> p = null;
276 // for(InternalProcedure<TObjectIntHashMap<String>> proc : p) proc.execute(graph, result);
283 public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<TObjectIntHashMap<String>> procedure) {
287 if(handleException(graph, procedure)) return;
289 procedure.execute(graph, (TObjectIntHashMap<String>)getResult());
294 public synchronized void recompute(ReadGraphImpl graph, QueryProcessor provider) {
296 final Semaphore s = new Semaphore(0);
298 computeForEach(graph, provider, new InternalProcedure<TObjectIntHashMap<String>>() {
301 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
306 public void exception(ReadGraphImpl graph, Throwable t) {
307 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
308 throw new Error("Error in recompute.", t);
313 while(!s.tryAcquire()) {
314 provider.resume(graph);