package org.simantics.db.impl.query;
import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.common.GraphSemaphore;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.impl.DebugPolicy;
import org.simantics.db.impl.graph.ReadGraphImpl;
final public void addOrSet(AsyncReadGraph graph, Object item) {
+// System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + item);
+
assert(isPending());
synchronized(this) {
try {
+ GraphSemaphore s = new GraphSemaphore(graph, 0);
+
request.perform(graph , new AsyncProcedure<T>() {
@Override
public void execute(AsyncReadGraph graph, T result) {
addOrSet(graph, result);
+ s.release();
}
@Override
public void exception(AsyncReadGraph graph, Throwable t) {
except(t);
+ s.release();
}
});
+ s.waitFor(1);
+
} catch (Throwable t) {
except(t);
}
}
- @Override
+ //@Override
public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
- return graph.processor.cache.performQuery(graph, request, this, procedure);
+
+ 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();