import org.simantics.db.impl.query.AsyncReadEntry;
import org.simantics.db.impl.query.PendingTaskSupport;
import org.simantics.db.procedure.AsyncProcedure;
-
+import org.simantics.db.request.AsyncRead;
public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
private static final Object NO_RESULT = new Object();
- public final Object key;
- public final ReadGraphImpl queryGraph;
- public final ReadGraphImpl callerGraph;
- public final AsyncProcedure<Result> procedure;
- public PendingTaskSupport pendingTaskSupport;
- public final boolean needsToBlock;
- public Object result = NO_RESULT;
- public Throwable exception = null;
+ private final Object key;
+ private final ReadGraphImpl queryGraph;
+ private final ReadGraphImpl callerGraph;
+ private final AsyncProcedure<Result> procedure;
+ private PendingTaskSupport pendingTaskSupport;
+ private final boolean needsToBlock;
+ private Object result = NO_RESULT;
+ private Throwable exception = null;
private ReadGraphImpl queryGraph() {
return queryGraph;
}
}
+
+ public void print() {
+ System.err.println("BlockingAsyncProcedure");
+ System.err.println("-key: " + key);
+ System.err.println("-queryGraph: " + queryGraph);
+ System.err.println("-callerGraph: " + callerGraph);
+ System.err.println("-procedure: " + procedure);
+ System.err.println("-pendingTaskSupport: " + pendingTaskSupport);
+ System.err.println("-result: " + result);
+ System.err.println("-exception: " + exception);
+ }
+
+ public Result performSync(AsyncRead<Result> request) throws DatabaseException {
+ try {
+ request.perform(queryGraph, this);
+ } finally {
+ dec();
+ }
+ return get();
+ }
+
+ public void performAsync(AsyncRead<Result> request) throws DatabaseException {
+ try {
+ request.perform(queryGraph, this);
+ } finally {
+ dec();
+ }
+ }
}
synchronized public static void printBAPS() {
for(BlockingAsyncProcedure bap : baps.keySet()) {
- Throwable e = baps.get(bap);
- System.err.println("BlockingAsyncProcedure");
- System.err.println("-key: " + bap.key);
- System.err.println("-queryGraph: " + bap.queryGraph);
- System.err.println("-callerGraph: " + bap.callerGraph);
- System.err.println("-procedure: " + bap.procedure);
- System.err.println("-pendingTaskSupport: " + bap.pendingTaskSupport);
- System.err.println("-result: " + bap.result);
- System.err.println("-exception: " + bap.exception);
- e.printStackTrace();
+ bap.print();
+ Throwable t = baps.get(bap);
+ if(t != null)
+ t.printStackTrace();
}
}
return new ReadGraphImpl(this, parent, callback, needsToBlock);
}
-// public ReadGraphImpl withParent(CacheEntry parent) {
-// return withParent(parent, null);
-// }
-
- public ReadGraphImpl syncWithParent(CacheEntry parent) {
- return new ReadGraphImpl(this, parent, processor, null);
- }
-
public ReadGraphImpl forRecompute(CacheEntry parent) {
return new ReadGraphImpl(null, parent, processor);
}
- public static ReadGraphImpl create(QueryProcessor support) {
- ReadGraphImpl result = new ReadGraphImpl(null, null, support);
- return result;
- }
+ public static ReadGraphImpl create(QueryProcessor support) {
+ ReadGraphImpl result = new ReadGraphImpl(null, null, support);
+ return result;
+ }
public ReadGraphImpl newRestart(ReadGraphImpl impl) {
}
}, id, true);
-
- id.perform(proc.queryGraph, proc);
- proc.dec();
- proc.get();
+ proc.performSync(id);
} catch (Throwable t) {
except(t);
AsyncProcedure<T> procedure_, boolean needsToBlock) throws DatabaseException {
BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure(callerGraph, entry, procedure_, request, needsToBlock);
-
- try {
- request.perform(proc.queryGraph, proc);
- } finally {
- proc.queryGraph.asyncBarrier.dec();
- }
-
if(needsToBlock) {
- proc.waitBarrier();
- return proc.get();
+ return proc.performSync(request);
} else {
+ proc.performAsync(request);
return null;
}
if (listener != null) {
try {
- QueryCacheBase.resultAsyncReadEntry(newGraph, request, null, listener, procedure);
- } catch (DatabaseException e) {
- Logger.defaultLogError(e);
- }
+ QueryCacheBase.resultAsyncReadEntry(newGraph, request, null, listener, procedure);
+ } catch (DatabaseException e) {
+ LOGGER.error("Unhandled query exception", e);
+ }
} else {
- BlockingAsyncProcedure<T> wrap = new BlockingAsyncProcedure<T>(newGraph, null, procedure, request, true) {
-
- public void execute(AsyncReadGraph graph_, T result) {
- task.finish();
- super.execute(graph_, result);
- }
-
- public void exception(AsyncReadGraph graph_, Throwable t) {
- task.finish();
- super.exception(graph_, t);
- }
-
- };
-
- try {
+ BlockingAsyncProcedure<T> wrap = new BlockingAsyncProcedure<T>(newGraph, null, procedure, request, true) {
- request.perform(newGraph, wrap);
- wrap.dec();
- wrap.get();
+ public void execute(AsyncReadGraph graph_, T result) {
+ task.finish();
+ super.execute(graph_, result);
+ }
- } catch (DatabaseException e) {
+ public void exception(AsyncReadGraph graph_, Throwable t) {
+ task.finish();
+ super.exception(graph_, t);
+ }
- Logger.defaultLogError(e);
+ };
+ try {
+ wrap.performSync(request);
+ } catch (DatabaseException e) {
+ LOGGER.error("Unhandled query exception", e);
}
}
@Override
public void perform(AsyncReadGraph graph, final AsyncProcedure<ElementClass> procedure) {
-// System.out.println("NodeClassRequest " + data.getResourceId());
-// graph.asyncRequest(new SafeName(data), new Procedure<String>() {
-// @Override
-// public void exception(Throwable t) {
-// }
-// @Override
-// public void execute(String result) {
-// System.out.println("NodeClassRequest " + result);
-// debug = result;
-// }
-// });
-
graph.forAdapted(data, ElementFactory.class, new AsyncProcedure<ElementFactory>() {
@Override
@Override
public void execute(AsyncReadGraph graph, final ElementFactory factory) {
-// System.out.println("NodeClassRequest factory " + data.getResourceId() + " = " + factory.getClass().getName());
-// graph.asyncRequest(new SafeName(data), new Procedure<String>() {
-// @Override
-// public void exception(Throwable t) {
-// }
-// @Override
-// public void execute(String result) {
-// System.out.println("NodeClassRequest " + result + " " + factory.getClass().getName());
-// debug = result;
-// }
-// });
-
if (factory == null) {
// Should never happen since we are using forAdapted
System.out.println("No factory in NodeClassRequest! " + this);