package org.simantics.db.impl.query;
import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.exception.DatabaseException;
import org.simantics.db.impl.DebugPolicy;
import org.simantics.db.impl.graph.ReadGraphImpl;
import org.simantics.db.procedure.AsyncProcedure;
import org.simantics.db.request.AsyncRead;
-final public class AsyncReadEntry<T> extends CacheEntryBase {
+final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
protected AsyncRead<T> request;
- public AsyncReadEntry(AsyncRead<T> request) {
+ AsyncReadEntry(AsyncRead<T> request) {
this.request = request;
if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
}
@Override
public void discard() {
super.discard();
- //request = null;
setResult(null);
}
final public void addOrSet(AsyncReadGraph graph, Object item) {
assert(isPending());
-
-// ArrayList<AsyncProcedure<T>> p = null;
synchronized(this) {
-
setResult(item);
setReady();
-// p = procs;
-// procs = null;
-
}
-
-// if(p != null)
-// for(AsyncProcedure<T> proc : p) {
-// proc.execute(graph, (T)item);
-//// proc.first.execute(graph, (T)item);
-//// proc.second.dec();
-// }
}
public void except(AsyncReadGraph graph, Throwable t) {
assert(isPending());
-
-// ArrayList<AsyncProcedure<T>> p = null;
synchronized(this) {
-
except(t);
-//// p = procs;
-// procs = null;
-
}
-
-// if(p != null)
-// for(AsyncProcedure<T> proc : p) {
-// proc.exception(graph, t);
-// }
}
return new Query() {
@Override
- public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-
- QueryProcessor qp = (QueryProcessor)provider;
-
- final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp);
+ public void recompute(ReadGraphImpl graph) {
try {
- request.perform(parentGraph , new AsyncProcedure<T>() {
+ request.perform(graph , new AsyncProcedure<T>() {
@Override
public void execute(AsyncReadGraph graph, T result) {
@Override
public void removeEntry(QueryProcessor qp) {
- qp.asyncReadMap.remove(request);
+ qp.cache.remove(AsyncReadEntry.this);
}
@Override
}
- @SuppressWarnings("unchecked")
- public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
+ @Override
+ public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
- AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
-
if(isExcepted()) {
try {
}
+ return getResult();
+
}
+ @Override
+ public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+
+ ReadGraphImpl queryGraph = graph.withParent(this);
+
+ request.perform(queryGraph, new AsyncProcedure<T>() {
+
+ @Override
+ public void execute(AsyncReadGraph returnGraph, T result) {
+ ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+ AsyncReadEntry.this.addOrSet(graph, result);
+ try {
+ procedure.execute(graph, result);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ // parentBarrier.dec(query);
+ }
+
+ @Override
+ public void exception(AsyncReadGraph returnGraph, Throwable t) {
+ ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+ // AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
+ AsyncReadEntry.this.except(graph, t);
+ try {
+ procedure.exception(graph, t);
+ } catch (Throwable t2) {
+ t2.printStackTrace();
+ }
+ // parentBarrier.dec(query);
+ }
+
+ @Override
+ public String toString() {
+ return procedure.toString();
+ }
+
+ });
+
+ return getResult();
+
+ }
+
+ public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
+
+ ReadGraphImpl queryGraph = parentGraph.withParent(entry);
+
+ request.perform(queryGraph, new AsyncProcedure<T>() {
+
+ @Override
+ public void execute(AsyncReadGraph returnGraph, T result) {
+ ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+ if(entry != null) entry.addOrSet(parentGraph, result);
+ try {
+ procedure.execute(parentGraph, result);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ }
+
+ @Override
+ public void exception(AsyncReadGraph returnGraph, Throwable t) {
+ ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
+ if(entry != null) entry.except(parentGraph, t);
+ try {
+ procedure.exception(parentGraph, t);
+ } catch (Throwable t2) {
+ t2.printStackTrace();
+ }
+ }
+
+ @Override
+ public String toString() {
+ return procedure.toString();
+ }
+
+ });
+
+ }
+
+
@Override
public String toString() {
if(isDiscarded()) return "DISCARDED " + request.toString();