import org.simantics.db.request.ReadExt;
import org.simantics.db.request.RequestFlags;
-final public class ReadEntry<T> extends CacheEntryBase {
+final public class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
protected Read<T> request;
setResult(null);
}
- final public void addOrSet(AsyncReadGraph graph, Object item) {
+ final public Object addOrSet(AsyncReadGraph graph, Object result) {
assert(assertPending());
-// ArrayList<Pair<AsyncProcedure<T>, AsyncBarrier>> p = null;
-
- synchronized(this) {
-
- setResult(item);
- setReady();
-// p = procs;
-// procs = null;
-
- }
-
-// if(p != null)
-// for(Pair<AsyncProcedure<T>, AsyncBarrier> proc : p) {
-// proc.first.execute(graph, (T)item);
-// proc.second.dec();
-// }
+ setResult(result);
+ setReady();
+
+ return result;
}
return new Query() {
@Override
- public void recompute(ReadGraphImpl graph_, Object provider, CacheEntry entry) {
-
- QueryProcessor qp = (QueryProcessor)provider;
+ public void recompute(ReadGraphImpl graph) {
- WriteGraphImpl write = qp.getCore().getSession().getService(WriteGraphImpl.class);
-
- ReadGraphImpl graph = write.newSync(entry);
-
try {
- entry.setPending();
T result = request.perform(graph);
addOrSet(graph, result);
@Override
public void removeEntry(QueryProcessor processor) {
- processor.readMap.remove(request);
+ processor.cache.remove(ReadEntry.this);
}
@Override
}
- public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
+ @Override
+ public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+
+ ReadGraphImpl queryGraph = graph.withParent(this);
+
+ try {
+
+ addOrSet(queryGraph, request.perform(queryGraph));
+ return get(queryGraph, procedure);
+
+ } catch (DatabaseException e) {
+
+ except(e);
+ return get(graph, procedure);
+
+ } catch (Throwable t) {
+
+ except(new DatabaseException(t));
+ return get(graph, procedure);
+
+ }
+
+ }
+
+ public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
+
+ ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
+
+ try {
+
+ T result = request.perform(queryGraph);
+ if(entry != null) entry.addOrSet(queryGraph, result);
+ procedure.execute(graph, result);
+
+ } catch (DatabaseException e) {
+
+ if(entry != null) entry.except(e);
+ procedure.exception(graph, e);
+
+ } catch (Throwable t) {
+
+ DatabaseException dbe = new DatabaseException(t);
+ if(entry != null) entry.except(dbe);
+ procedure.exception(graph, dbe);
+
+ }
+
+ }
+
+ public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
- if(isExcepted()) {
-
- try {
- proc.exception(graph, (Throwable)getResult());
- } catch (Throwable t) {
- t.printStackTrace();
- }
-
- } else {
-
- try {
- proc.execute(graph, (T)getResult());
- } catch (Throwable t) {
- t.printStackTrace();
- }
-
- }
+ if(proc != null) {
+ if(isExcepted()) {
+ try {
+ proc.exception(graph, (Throwable)getResult());
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ } else {
+ try {
+ proc.execute(graph, (T)getResult());
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ }
+ }
+ return (T)getResult();
+
}
@Override
else return request.toString() + " - " + statusOrException;
}
- public Object get(ReadGraphImpl graph, QueryProcessor processor, Object procedure) throws DatabaseException {
- if(procedure != null) performFromCache(graph, processor, procedure);
+ public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+ if(procedure != null) performFromCache(graph, procedure);
checkAndThrow();
return getResult();
}