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 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;
23 import gnu.trove.map.hash.TObjectIntHashMap;
25 final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectIntHashMap<String>>> {
27 NamespaceIndex(final String id) {
32 final public void removeEntry(QueryProcessor provider) {
33 provider.cache.remove(this);
36 final static private void index(ReadGraphImpl graph, int root, NamespaceIndex entry, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
40 entry.add2(graph, null);
41 procedure.execute(graph, null);
42 // System.err.println("NamespaceIndex[" + id + "]->null");
46 QueryProcessor processor = graph.processor;
48 final int consistsOf = processor.getConsistsOf();
49 final int hasName = processor.getHasName();
51 final TObjectIntHashMap<String> result = new TObjectIntHashMap<String>();
53 QueryCache.runnerObjects(graph, root, consistsOf, entry, null, new SyncIntProcedure() {
56 public void run(ReadGraphImpl graph) throws DatabaseException {
58 if(entry != null) entry.add2(graph, result);
59 procedure.execute(graph, result);
64 public void finished(ReadGraphImpl graph) throws DatabaseException {
69 public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
71 //System.out.println(id + " => " + obj);
75 QueryCache.runnerObjects(graph, obj, hasName, entry, null, new IntProcedure() {
78 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
82 QueryCache.runnerValueQuery(graph, i, entry, null, new InternalProcedure<byte[]>() {
85 public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException {
91 Binding b = WriteBindings.STRING;
92 Serializer serializer = b.serializer();
93 final String part = (String)serializer.deserialize(value);
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 = "";
101 } catch (Throwable e) {
102 if(DebugException.DEBUG) new DebugException(e).printStackTrace();
112 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
113 if(entry != null) entry.except(t);
122 public void finished(ReadGraphImpl graph) throws DatabaseException {
127 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
128 if(entry != null) entry.except(t);
142 public Object compute(ReadGraphImpl graph, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
143 computeForEach(graph, id, this, procedure);
147 public static void computeForEach(ReadGraphImpl graph, final String id, final NamespaceIndex entry, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
149 QueryProcessor processor = graph.processor;
151 // System.err.println("NamespaceIndex " + id);
153 if("http://".equals(id) || "http:/".equals(id)) {
154 index(graph, processor.getRootLibrary(), entry, procedure);
156 final String[] parts = URIStringUtils.splitURI(id);
158 QueryCache.runnerNamespaceIndex(graph, parts[0], entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {
161 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) throws DatabaseException {
164 index(graph, index.get(parts[1]), entry, procedure);
166 if(entry != null) entry.add2(graph, null);
167 procedure.execute(graph, null);
168 // System.err.println("NamespaceIndex[" + id + "]->null");
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);
182 if(entry != null) entry.add2(graph, null);
183 procedure.execute(graph, null);
184 // System.err.println("NamespaceIndex[" + id + "]->null");
192 public String toString() {
193 return "NamespaceIndex[" + id + "]";
196 private void add2(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
199 new Exception(""+hashCode()).printStackTrace();
214 public Object performFromCache(ReadGraphImpl graph, InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
218 if(handleException(graph, procedure)) return (Throwable)statusOrException;
220 TObjectIntHashMap<String> result = (TObjectIntHashMap<String>)getResult();
222 procedure.execute(graph, result);
229 public void recompute(ReadGraphImpl graph) throws DatabaseException {
231 compute(graph, new InternalProcedure<TObjectIntHashMap<String>>() {
234 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
238 public void exception(ReadGraphImpl graph, Throwable t) {
239 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
240 throw new Error("Error in recompute.", t);