/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.db.impl.query; import org.simantics.databoard.util.URIStringUtils; import org.simantics.db.common.exception.DebugException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.procedure.ListenerBase; import gnu.trove.map.hash.TObjectIntHashMap; public class URIToResource extends StringQuery> { URIToResource(final String id) { super(id); } @Override final public void removeEntry(QueryProcessor provider) { provider.cache.remove(this); } private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure procedure, final String namespace, final String name) throws DatabaseException { QueryCache.runnerNamespaceIndex(graph, namespace, entry, null, new InternalProcedure>() { @Override public void execute(ReadGraphImpl graph, TObjectIntHashMap index) throws DatabaseException { if(index != null) { int result = index.get(name); if(result != 0) { if(entry != null) entry.addOrSet(graph, graph.processor, result); procedure.execute(graph, result); return; } } Integer zero = 0; if(entry != null) entry.addOrSet(graph, graph.processor, zero); procedure.execute(graph, zero); } @Override public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { if(entry != null) entry.except(t); procedure.exception(graph, t); } }); } @Override public Object compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { computeForEach(graph, id, this, procedure); return getResult(); } static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure procedure) throws DatabaseException { if("http://".equals(id) || "http:/".equals(id)) { QueryProcessor processor = graph.processor; if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary()); procedure.execute(graph, processor.getRootLibrary()); } else { final String[] parts = URIStringUtils.splitURI(id); if (parts != null) { lookup(graph, entry, procedure, parts[0], parts[1]); } else { lookup(graph, entry, procedure, "http://", id.replaceFirst("http://", "")); } } } public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) { assert(isPending()); synchronized(this) { setResult(result); setReady(); } } @Override public String toString() { return "URIToResource[" + id + "]"; } @Override public Object performFromCache(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { assert(isReady()); if(handleException(graph, procedure)) return (Throwable)statusOrException; Integer result = (Integer)getResult(); procedure.execute(graph, result); return result; } @Override public void recompute(ReadGraphImpl graph) throws DatabaseException { compute(graph, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, Integer result) { } @Override public void exception(ReadGraphImpl graph, Throwable t) { if(DebugException.DEBUG) new DebugException(t).printStackTrace(); throw new Error("Error in recompute.", t); } }); } }