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 private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
40 procedure.execute(graph, null);
41 // System.err.println("NamespaceIndex[" + id + "]->null");
45 final int consistsOf = provider.getConsistsOf();
46 final int hasName = provider.getHasName();
48 final TObjectIntHashMap<String> result = new TObjectIntHashMap<String>();
50 QueryCache.runnerObjects(graph, root, consistsOf, graph.parent, null, new SyncIntProcedure() {
53 public void run(ReadGraphImpl graph) throws DatabaseException {
57 procedure.execute(graph, result);
58 // System.err.println("NamespaceIndex[" + id + "]->" + result.size());
60 procedure.exception(graph, (Throwable)statusOrException);
66 public void finished(ReadGraphImpl graph) throws DatabaseException {
71 public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
73 //System.out.println(id + " => " + obj);
77 QueryCache.runnerObjects(graph, obj, hasName, graph.parent, null, new IntProcedure() {
80 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
84 QueryCache.runnerValueQuery(graph, i, NamespaceIndex.this, null, new InternalProcedure<byte[]>() {
87 public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException {
93 Binding b = WriteBindings.STRING;
94 Serializer serializer = b.serializer();
95 final String part = (String)serializer.deserialize(value);
97 synchronized(result) {
98 Object previous = result.put(URIStringUtils.escape(part), obj);
99 // TODO: this is not the most elegant solution
100 if(previous != null) previous = "";
103 } catch (Throwable e) {
104 if(DebugException.DEBUG) new DebugException(e).printStackTrace();
114 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
124 public void finished(ReadGraphImpl graph) throws DatabaseException {
129 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
143 public Object compute(ReadGraphImpl graph, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
145 QueryProcessor processor = graph.processor;
147 // System.err.println("NamespaceIndex " + id);
149 if("http://".equals(id) || "http:/".equals(id)) {
150 index(graph, processor, processor.getRootLibrary(), procedure);
152 final String[] parts = URIStringUtils.splitURI(id);
154 QueryCache.runnerNamespaceIndex(graph, parts[0], this, null, new InternalProcedure<TObjectIntHashMap<String>>() {
157 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) throws DatabaseException {
160 index(graph, processor, index.get(parts[1]), procedure);
163 procedure.execute(graph, null);
164 // System.err.println("NamespaceIndex[" + id + "]->null");
170 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
171 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
173 procedure.exception(graph, t);
179 procedure.execute(graph, null);
180 // System.err.println("NamespaceIndex[" + id + "]->null");
190 public String toString() {
191 return "NamespaceIndex[" + id + "]";
194 synchronized private void add(TObjectIntHashMap<String> result) {
196 throw new Error("Not possible!");
200 private void add2(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
203 new Exception(""+hashCode()).printStackTrace();
208 // ArrayList<InternalProcedure<TObjectIntHashMap<String>>> p = null;
221 // for(InternalProcedure<TObjectIntHashMap<String>> proc : p) proc.execute(graph, result);
228 public Object performFromCache(ReadGraphImpl graph, InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
232 if(handleException(graph, procedure)) return (Throwable)statusOrException;
234 TObjectIntHashMap<String> result = (TObjectIntHashMap<String>)getResult();
236 procedure.execute(graph, result);
243 public void recompute(ReadGraphImpl graph) throws DatabaseException {
245 compute(graph, new InternalProcedure<TObjectIntHashMap<String>>() {
248 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
252 public void exception(ReadGraphImpl graph, Throwable t) {
253 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
254 throw new Error("Error in recompute.", t);