- }
-
- }
-
- @Override
- public String toString() {
- if(request == null) return "DISCARDED";
- else return request.toString() + " - " + statusOrException;
- }
-
- public Object get(ReadGraphImpl graph, QueryProcessor processor, Object procedure) throws DatabaseException {
- if(procedure != null) performFromCache(graph, processor, procedure);
- checkAndThrow();
- return getResult();
- }
-
- @Override
- boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
- if(request instanceof ReadExt) {
- return ((ReadExt)request).isImmutable(graph);
- }
- return false;
- }
+ };
+
+ }
+
+ public static <T> T computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry,
+ AsyncProcedure<T> procedure_) throws DatabaseException {
+
+ AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
+
+ ReadGraphImpl queryGraph = graph.withParent(entry);
+
+ try {
+
+ T result = request.perform(queryGraph);
+ if(procedure != null) procedure.execute(graph, result);
+ return (T)result;
+
+ } catch (DatabaseException e) {
+
+ if(procedure != null) procedure.exception(graph, e);
+ throw e;
+
+ } catch (Throwable t) {
+
+ DatabaseException dbe = new DatabaseException(t);
+ if(procedure != null) procedure.exception(graph, dbe);
+ throw dbe;
+
+ } finally {
+
+ if (entry != null)
+ entry.performFromCache(queryGraph, procedure_);
+
+ }
+
+ }
+
+ public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+
+ AsyncProcedure<T> proc = (AsyncProcedure<T>) procedure;
+
+ if (isExcepted()) {
+ if(proc != null) {
+ try {
+ proc.exception(graph, (Throwable) getResult());
+ } catch (Throwable t) {
+ LOGGER.error("performFromCache proc.exception failed", t);
+ }
+ }
+ Throwable t = (Throwable) getResult();
+ if(t instanceof DatabaseException) {
+ throw (DatabaseException)t;
+ } else {
+ throw new DatabaseException(t);
+ }
+ } else {
+ if(proc != null) {
+ try {
+ proc.execute(graph, (T) getResult());
+ } catch (Throwable t) {
+ LOGGER.error("performFromCache proc.execute failed", t);
+ }
+ }
+ return (T)getResult();
+ }
+
+ }
+
+ @Override
+ public String toString() {
+ if (request == null)
+ return "DISCARDED";
+ else
+ return request.toString() + " - " + statusOrException;
+ }
+
+ public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+ if (procedure != null)
+ performFromCache(graph, procedure);
+ checkAndThrow();
+ return getResult();
+ }
+
+ @Override
+ boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
+ if (request instanceof ReadExt) {
+ return ((ReadExt) request).isImmutable(graph);
+ }
+ return false;
+ }
+
+ @Override
+ public void execute(AsyncReadGraph graph, T result) {
+ setResult(result);
+ setReady();
+ }
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ except(throwable);
+ }