+ LOGGER.error("performFromCache proc.execute failed", t);
+ }
+
+ }
+
+ return getResult();
+
+ }
+
+ public static <T> T computeForEach(ReadGraphImpl graph, AsyncRead<T> request, AsyncReadEntry<T> entry,
+ AsyncProcedure<T> procedure_, boolean needsToBlock) throws DatabaseException {
+
+ AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
+
+ ReadGraphImpl queryGraph = graph.withParent(entry);
+ queryGraph.asyncBarrier.inc();
+
+ BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure<>(queryGraph.asyncBarrier, graph, null, request);
+
+ class AsyncTask extends SessionTask {
+
+ int counter = 0;
+ T result;
+ DatabaseException exception;
+
+ public AsyncTask(ReadGraphImpl graph) {
+ this(graph, 1);
+ }
+
+ public AsyncTask(ReadGraphImpl graph, int pos) {
+ super(graph);
+ this.position = pos;
+ if(this.position < 1024)
+ this.position *= 2;
+ }
+
+ @Override
+ public void run0(int thread) {
+ if(needsToBlock) proc.waitBarrier();
+ if(proc.isDone()) {
+ ReadGraphImpl executeGraph = graph.withParent(graph.parent);
+ executeGraph.asyncBarrier.inc();
+ try {
+ result = (T)proc.get();
+ if(procedure != null) {
+ procedure.execute(executeGraph, result);
+ }
+ } catch (DatabaseException e) {
+ if(procedure != null) procedure.exception(executeGraph, e);
+ exception = e;
+ } catch (Throwable t) {
+ DatabaseException dbe = new DatabaseException(t);
+ if(procedure != null) procedure.exception(executeGraph, dbe);
+ exception = dbe;
+ } finally {
+ if (entry != null) {
+ // This does not throw
+ entry.performFromCache(executeGraph, procedure_);
+ }
+ executeGraph.asyncBarrier.dec();
+ executeGraph.asyncBarrier.waitBarrier(procedure, executeGraph);
+ }
+ } else {
+ if(counter++ > 10000) {
+ if(BarrierTracing.BOOKKEEPING) {
+ AsyncBarrierImpl.printReverse(queryGraph.asyncBarrier, 2);
+ AsyncBarrierImpl caller = queryGraph.asyncBarrier.caller;
+ while(caller != null) {
+ System.err.println("called by " + AsyncBarrierImpl.report(caller));
+ caller = caller.caller;
+ }
+ for(AsyncBarrierImpl ab : BarrierTracing.debuggerMap.keySet()) {
+ AsyncBarrierImpl.printReverse(ab, 2);
+ }
+ }
+ throw new IllegalStateException("Eternal loop in queries.");
+ }
+ graph.processor.scheduleLater(new AsyncTask(graph, position));
+ }
+ }
+
+ @Override
+ public boolean maybeReady() {
+ return proc.isDone();