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.util.URIStringUtils;
19 import org.simantics.db.common.exception.DebugException;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.impl.procedure.InternalProcedure;
22 import org.simantics.db.procedure.ListenerBase;
24 public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
26 // public ArrayList<InternalProcedure<Integer>> procs = null;
28 private URIToResource(final String id) {
32 final static URIToResource entry(final QueryProcessor provider, final String id) {
33 return (URIToResource)provider.uriToResourceMap.get(id);
36 final static void runner(ReadGraphImpl graph, final String id, CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {
38 QueryProcessor processor = graph.processor;
40 URIToResource entry = (URIToResource)processor.uriToResourceMap.get(id);
43 entry = new URIToResource(id);
45 entry.clearResult(processor.querySupport);
46 entry.putEntry(processor);
48 processor.performForEach(graph, entry, parent, listener, procedure);
52 if(entry.isPending()) {
54 if(entry.isPending()) {
55 throw new IllegalStateException();
56 // if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<Integer>>();
57 // entry.procs.add(procedure);
58 // processor.registerDependencies(graph, entry, parent, listener, procedure, false);
64 processor.performForEach(graph, entry, parent, listener, procedure);
70 final public static void queryEach(ReadGraphImpl graph, final String id, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {
74 if(graph.parent == null && listener == null) {
75 URIToResource.computeForEach(graph, id, null, procedure);
77 runner(graph, id, parent, listener, procedure);
83 public URIToResource getEntry(QueryProcessor provider) {
84 return provider.uriToResourceMap.get(id);
88 public void putEntry(QueryProcessor provider) {
89 provider.uriToResourceMap.put(id, this);
93 final public void removeEntry(QueryProcessor provider) {
94 provider.uriToResourceMap.remove(id);
97 private void lookup(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure, final String namespace, final String name) {
99 NamespaceIndex.queryEach(graph, namespace, processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {
102 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
105 int result = index.get(name);
107 addOrSet(graph, processor, result);
108 procedure.execute(graph, result);
113 addOrSet(graph, processor, new Integer(0));
114 procedure.execute(graph, new Integer(0));
119 public void exception(ReadGraphImpl graph, Throwable t) {
121 procedure.exception(graph, t);
128 private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure<Integer> procedure, final String namespace, final String name) {
130 NamespaceIndex.queryEach(graph, namespace, graph.processor, entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {
133 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
136 int result = index.get(name);
138 if(entry != null) entry.addOrSet(graph, graph.processor, result);
139 procedure.execute(graph, result);
145 if(entry != null) entry.addOrSet(graph, graph.processor, zero);
146 procedure.execute(graph, zero);
151 public void exception(ReadGraphImpl graph, Throwable t) {
152 if(entry != null) entry.except(t);
153 procedure.exception(graph, t);
161 public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure) {
163 // new Exception("URIToResource " + id).printStackTrace();
165 if("http://".equals(id) || "http:/".equals(id)) {
167 addOrSet(graph, processor, processor.getRootLibrary());
168 procedure.execute(graph, processor.getRootLibrary());
172 final String[] parts = URIStringUtils.splitURI(id);
174 lookup(graph, processor, procedure, parts[0], parts[1]);
176 lookup(graph, processor, procedure, "http://", id.replaceFirst("http://", ""));
183 final private static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) {
185 if("http://".equals(id) || "http:/".equals(id)) {
187 QueryProcessor processor = graph.processor;
188 if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary());
189 procedure.execute(graph, processor.getRootLibrary());
193 final String[] parts = URIStringUtils.splitURI(id);
195 lookup(graph, entry, procedure, parts[0], parts[1]);
197 lookup(graph, entry, procedure, "http://", id.replaceFirst("http://", ""));
204 public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {
208 // ArrayList<InternalProcedure<Integer>> p = null;
220 // for(InternalProcedure<Integer> proc : p) proc.execute(graph, result);
225 public String toString() {
226 return "URIToResource[" + id + "]";
230 public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<Integer> procedure) {
234 if(handleException(graph, procedure)) return;
237 procedure.exception(graph, (Throwable)statusOrException);
239 procedure.execute(graph, (Integer)getResult());
245 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
247 final Semaphore s = new Semaphore(0);
249 computeForEach(graph, provider, new InternalProcedure<Integer>() {
252 public void execute(ReadGraphImpl graph, Integer result) {
257 public void exception(ReadGraphImpl graph, Throwable t) {
258 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
259 throw new Error("Error in recompute.", t);
264 while(!s.tryAcquire()) {
265 provider.resume(graph);