-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.impl.query;\r
-\r
-import gnu.trove.map.hash.TObjectIntHashMap;\r
-\r
-import java.util.concurrent.Semaphore;\r
-\r
-import org.simantics.databoard.util.URIStringUtils;\r
-import org.simantics.db.common.exception.DebugException;\r
-import org.simantics.db.impl.graph.ReadGraphImpl;\r
-import org.simantics.db.impl.procedure.InternalProcedure;\r
-import org.simantics.db.procedure.ListenerBase;\r
-\r
-public class URIToResource extends StringQuery<InternalProcedure<Integer>> {\r
-\r
-// public ArrayList<InternalProcedure<Integer>> procs = null;\r
- \r
- private URIToResource(final String id) {\r
- super(id);\r
- }\r
- \r
- final static URIToResource entry(final QueryProcessor provider, final String id) {\r
- return (URIToResource)provider.uriToResourceMap.get(id);\r
- }\r
-\r
- final static void runner(ReadGraphImpl graph, final String id, CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {\r
-\r
- QueryProcessor processor = graph.processor;\r
- \r
- URIToResource entry = (URIToResource)processor.uriToResourceMap.get(id); \r
- if(entry == null) {\r
-\r
- entry = new URIToResource(id);\r
- entry.setPending();\r
- entry.clearResult(processor.querySupport);\r
- entry.putEntry(processor);\r
-\r
- processor.performForEach(graph, entry, parent, listener, procedure);\r
- \r
- } else {\r
- \r
- if(entry.isPending()) {\r
- synchronized(entry) {\r
- if(entry.isPending()) {\r
- throw new IllegalStateException();\r
-// if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<Integer>>();\r
-// entry.procs.add(procedure);\r
-// processor.registerDependencies(graph, entry, parent, listener, procedure, false);\r
-// return;\r
- }\r
- }\r
- }\r
- \r
- processor.performForEach(graph, entry, parent, listener, procedure);\r
- \r
- }\r
- \r
- }\r
- \r
- final public static void queryEach(ReadGraphImpl graph, final String id, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<Integer> procedure) {\r
- \r
- assert(id != null);\r
- \r
- if(graph.parent == null && listener == null) {\r
- URIToResource.computeForEach(graph, id, null, procedure);\r
- } else {\r
- runner(graph, id, parent, listener, procedure);\r
- }\r
- \r
- }\r
- \r
- @Override\r
- public URIToResource getEntry(QueryProcessor provider) {\r
- return provider.uriToResourceMap.get(id);\r
- }\r
- \r
- @Override\r
- public void putEntry(QueryProcessor provider) {\r
- provider.uriToResourceMap.put(id, this);\r
- }\r
-\r
- @Override\r
- final public void removeEntry(QueryProcessor provider) {\r
- provider.uriToResourceMap.remove(id);\r
- }\r
-\r
- private void lookup(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure, final String namespace, final String name) {\r
- \r
- NamespaceIndex.queryEach(graph, namespace, processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {\r
-\r
- if(index != null) {\r
- int result = index.get(name);\r
- if(result != 0) {\r
- addOrSet(graph, processor, result);\r
- procedure.execute(graph, result);\r
- return;\r
- }\r
- }\r
- \r
- addOrSet(graph, processor, new Integer(0));\r
- procedure.execute(graph, new Integer(0));\r
-\r
- }\r
-\r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- except(t);\r
- procedure.exception(graph, t);\r
- }\r
-\r
- });\r
-\r
- }\r
-\r
- private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure<Integer> procedure, final String namespace, final String name) {\r
- \r
- NamespaceIndex.queryEach(graph, namespace, graph.processor, entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {\r
-\r
- if(index != null) {\r
- int result = index.get(name);\r
- if(result != 0) {\r
- if(entry != null) entry.addOrSet(graph, graph.processor, result);\r
- procedure.execute(graph, result);\r
- return;\r
- }\r
- }\r
- \r
- if(entry != null) entry.addOrSet(graph, graph.processor, new Integer(0));\r
- procedure.execute(graph, new Integer(0));\r
-\r
- }\r
-\r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- if(entry != null) entry.except(t);\r
- procedure.exception(graph, t);\r
- }\r
-\r
- });\r
-\r
- }\r
- \r
- @Override\r
- public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<Integer> procedure) {\r
- \r
-// new Exception("URIToResource " + id).printStackTrace();\r
- \r
- if("http://".equals(id) || "http:/".equals(id)) {\r
- \r
- addOrSet(graph, processor, processor.getRootLibrary());\r
- procedure.execute(graph, processor.getRootLibrary());\r
-\r
- } else {\r
- \r
- final String[] parts = URIStringUtils.splitURI(id);\r
- if (parts != null) {\r
- lookup(graph, processor, procedure, parts[0], parts[1]);\r
- } else {\r
- lookup(graph, processor, procedure, "http://", id.replaceFirst("http://", ""));\r
- }\r
-\r
- }\r
- \r
- }\r
-\r
- final private static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) {\r
- \r
- if("http://".equals(id) || "http:/".equals(id)) {\r
- \r
- QueryProcessor processor = graph.processor;\r
- if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary());\r
- procedure.execute(graph, processor.getRootLibrary());\r
-\r
- } else {\r
- \r
- final String[] parts = URIStringUtils.splitURI(id);\r
- if (parts != null) {\r
- lookup(graph, entry, procedure, parts[0], parts[1]);\r
- } else {\r
- lookup(graph, entry, procedure, "http://", id.replaceFirst("http://", ""));\r
- }\r
-\r
- }\r
- \r
- }\r
- \r
- public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {\r
-\r
- assert(isPending());\r
-\r
-// ArrayList<InternalProcedure<Integer>> p = null;\r
-\r
- synchronized(this) {\r
-\r
- setResult(result);\r
- setReady();\r
-// p = procs;\r
-// procs = null;\r
- \r
- }\r
-\r
-// if(p != null)\r
-// for(InternalProcedure<Integer> proc : p) proc.execute(graph, result);\r
- \r
- }\r
- \r
- @Override\r
- public String toString() {\r
- return "URIToResource[" + id + "]";\r
- }\r
-\r
- @Override\r
- public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<Integer> procedure) {\r
- \r
- assert(isReady());\r
- \r
- if(handleException(graph, procedure)) return;\r
- \r
- if(isExcepted()) {\r
- procedure.exception(graph, (Throwable)statusOrException);\r
- } else {\r
- procedure.execute(graph, (Integer)getResult());\r
- }\r
- \r
- }\r
- \r
- @Override\r
- public void recompute(ReadGraphImpl graph, QueryProcessor provider) {\r
- \r
- final Semaphore s = new Semaphore(0);\r
- \r
- computeForEach(graph, provider, new InternalProcedure<Integer>() {\r
-\r
- @Override\r
- public void execute(ReadGraphImpl graph, Integer result) {\r
- s.release();\r
- }\r
- \r
- @Override\r
- public void exception(ReadGraphImpl graph, Throwable t) {\r
- if(DebugException.DEBUG) new DebugException(t).printStackTrace();\r
- throw new Error("Error in recompute.", t);\r
- }\r
-\r
- });\r
- \r
- while(!s.tryAcquire()) {\r
- provider.resume(graph);\r
- }\r
- \r
- }\r
- \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2018 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.ObjectResourceIdMap;
+import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ResourceNotFoundException;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.impl.procedure.InternalProcedure;
+
+public class URIToResource extends StringQuery<InternalProcedure<Integer>> implements InternalProcedure<Integer> {
+
+ URIToResource(String id) {
+ super(id);
+ }
+
+ @Override
+ public final void removeEntry(QueryProcessor provider) {
+ provider.cache.remove(this);
+ }
+
+ public Object compute(ReadGraphImpl graph, final InternalProcedure<Integer> procedure) throws DatabaseException {
+ computeForEach(graph, id, this, procedure);
+ return getResult();
+ }
+
+ static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure_) throws DatabaseException {
+
+ InternalProcedure<Integer> procedure = entry != null ? entry : procedure_;
+
+ if("http://".equals(id) || "http:/".equals(id)) {
+
+ QueryProcessor processor = graph.processor;
+ procedure.execute(graph, processor.getRootLibrary());
+
+ } else {
+
+ final String[] parts = URIStringUtils.splitURI(id);
+ if (parts != null) {
+
+ QueryCache.runnerURIToResource(graph, parts[0], entry, null, new InternalProcedure<Integer>() {
+
+ @Override
+ public void execute(ReadGraphImpl graph, Integer parentId) throws DatabaseException {
+
+ ObjectResourceIdMap<String> map = QueryCache.resultChildMap(graph, parentId, entry, null);
+ assert(map != null);
+ int result = map.getId(URIStringUtils.unescape(parts[1]));
+ if (result == 0) {
+ ResourceNotFoundException e = new ResourceNotFoundException(id);
+ procedure.exception(graph, e);
+ } else {
+ procedure.execute(graph, result);
+ }
+ }
+
+ @Override
+ public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+ procedure.exception(graph, throwable);
+ }
+
+ });
+
+ } else {
+ ResourceNotFoundException e = new ResourceNotFoundException(id);
+ procedure.exception(graph, e);
+ }
+
+ }
+
+ if(entry != null) entry.performFromCache(graph, procedure_);
+
+ }
+
+ public void addOrSet(Integer result) {
+
+ assert(isPending());
+
+ synchronized(this) {
+ setResult(result);
+ setReady();
+ }
+
+ }
+
+ @Override
+ public String toString() {
+ return "URIToResource[" + id + "]";
+ }
+
+ @Override
+ public Object performFromCache(ReadGraphImpl graph, InternalProcedure<Integer> procedure) throws DatabaseException {
+
+ assert(isReady());
+
+ if(handleException(graph, procedure)) return (Throwable)getResult();
+
+ Integer result = (Integer)getResult();
+ procedure.execute(graph, result);
+ return result;
+
+ }
+
+ @Override
+ public void recompute(ReadGraphImpl graph) throws DatabaseException {
+
+ compute(graph, new InternalProcedure<Integer>() {
+
+ @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);
+ }
+
+ });
+
+ }
+
+ @Override
+ public void execute(ReadGraphImpl graph, Integer result) throws DatabaseException {
+ synchronized(this) {
+ setResult(result);
+ setReady();
+ }
+ }
+
+ @Override
+ public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+ except(throwable);
+ }
+
+}