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;
-import org.simantics.db.procedure.ListenerBase;
-import gnu.trove.map.hash.TObjectIntHashMap;
-
-public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
+public class URIToResource extends StringQuery<InternalProcedure<Integer>> implements InternalProcedure<Integer> {
URIToResource(final String id) {
super(id);
final public void removeEntry(QueryProcessor provider) {
provider.cache.remove(this);
}
-
- private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure<Integer> procedure, final String namespace, final String name) throws DatabaseException {
-
- QueryCache.runnerNamespaceIndex(graph, namespace, entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {
-
- @Override
- public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> 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
+ //@Override
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 {
+ 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;
- 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]);
+
+ 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("No resource for URI: " + 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 {
- lookup(graph, entry, procedure, "http://", id.replaceFirst("http://", ""));
+ ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
+ procedure.exception(graph, e);
}
}
+ if(entry != null) entry.performFromCache(graph, procedure_);
+
}
- public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {
+ public void addOrSet(Integer result) {
assert(isPending());
assert(isReady());
- if(handleException(graph, procedure)) return (Throwable)statusOrException;
+ if(handleException(graph, procedure)) return (Throwable)getResult();
Integer result = (Integer)getResult();
procedure.execute(graph, result);
});
}
+
+ @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);
+ }
}