X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FAsyncReadEntry.java;h=69a85220b954cac8d1dc0e24ef25ca07edd0c407;hb=de8b6e194da6d56c410101d9c72763078b0e8717;hp=a9726a75d6ac4eb5df323625ffd19da9369bcb9b;hpb=4aa615b51b887cd75b558a823f0d87b3174e5f5e;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java index a9726a75d..69a85220b 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java @@ -12,169 +12,204 @@ 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.exception.RuntimeDatabaseException; 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 extends CacheEntryBase { +final public class AsyncReadEntry extends CacheEntryBase> implements AsyncProcedure { protected AsyncRead request; - public AsyncReadEntry(AsyncRead request) { - this.request = request; - if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this); + AsyncReadEntry(AsyncRead request) { + this.request = request; + if (DebugPolicy.QUERY_STATE) + System.out.println("[QUERY STATE]: created " + this); } @Override int makeHash() { - return request.hashCode(); + return request.hashCode(); } - + @Override public Object getOriginalRequest() { return request; } - + @Override public void discard() { - super.discard(); - //request = null; - setResult(null); + super.discard(); + setResult(null); } - - final public void addOrSet(AsyncReadGraph graph, Object item) { - - assert(isPending()); - -// ArrayList> p = null; - - synchronized(this) { - - setResult(item); - setReady(); -// p = procs; -// procs = null; - - } -// if(p != null) -// for(AsyncProcedure 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> p = null; - synchronized(this) { - + assert (isPending()); + + synchronized (this) { except(t); -//// p = procs; -// procs = null; - } -// if(p != null) -// for(AsyncProcedure proc : p) { -// proc.exception(graph, t); -// } - } - - + @Override final public Query getQuery() { - + return new Query() { - @Override - public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) { - - QueryProcessor qp = (QueryProcessor)provider; + @Override + public void recompute(ReadGraphImpl graph) { - final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); + try { - try { + GraphSemaphore s = new GraphSemaphore(graph, 0); - request.perform(parentGraph , new AsyncProcedure() { + request.perform(graph, new AsyncProcedure() { @Override public void execute(AsyncReadGraph graph, T result) { - addOrSet(graph, result); + setResult(result); + setReady(); + s.release(); } - - @Override - public void exception(AsyncReadGraph graph, Throwable t) { - except(t); + + @Override + public void exception(AsyncReadGraph graph, Throwable t) { + except(t); + s.release(); } }); - } catch (Throwable t) { - except(t); + s.waitFor(1); + + } catch (Throwable t) { + except(t); } - - } - - @Override - public void removeEntry(QueryProcessor qp) { - qp.asyncReadMap.remove(request); - } - - @Override - public int type() { - return request.getFlags(); - } - - @Override - public String toString() { - if(request == null) return "DISCARDED"; - else if(isExcepted()) return request.toString() + " " + getResult(); - else return request.toString() + " " + statusOrException; - } - + + } + + @Override + public void removeEntry(QueryProcessor qp) { + qp.cache.remove(AsyncReadEntry.this); + } + + @Override + public int type() { + return request.getFlags(); + } + + @Override + public String toString() { + if (request == null) + return "DISCARDED"; + else if (isExcepted()) + return request.toString() + " " + getResult(); + else + return request.toString() + " " + statusOrException; + } + }; - + } - @SuppressWarnings("unchecked") - public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { - - AsyncProcedure proc = (AsyncProcedure)procedure; + @Override + public Object performFromCache(ReadGraphImpl graph, AsyncProcedure proc) { + + if (isExcepted()) { - if(isExcepted()) { - try { - proc.exception(graph, (Throwable)getResult()); + proc.exception(graph, (Throwable) getResult()); } catch (Throwable t) { t.printStackTrace(); } - + } else { - + try { - proc.execute(graph, (T)getResult()); + proc.execute(graph, (T) getResult()); } catch (Throwable t) { t.printStackTrace(); } - + + } + + return getResult(); + + } + + public static void computeForEach(ReadGraphImpl parentGraph, AsyncRead request, AsyncReadEntry entry, + AsyncProcedure procedure_) throws DatabaseException { + + AsyncProcedure procedure = entry != null ? entry : procedure_; + + GraphSemaphore s = new GraphSemaphore(parentGraph, 0); + + ReadGraphImpl queryGraph = parentGraph.withParent(entry); + + request.perform(queryGraph, new AsyncProcedure() { + + @Override + public void execute(AsyncReadGraph returnGraph, T result) { + try { + procedure.execute(parentGraph, result); + } catch (Throwable t) { + t.printStackTrace(); + } + s.release(); + } + + @Override + public void exception(AsyncReadGraph returnGraph, Throwable t) { + try { + procedure.exception(parentGraph, t); + } catch (Throwable t2) { + t2.printStackTrace(); + } + s.release(); + } + + @Override + public String toString() { + return procedure.toString(); + } + + }); + + try { + s.waitFor(1); + } catch (InterruptedException e) { + throw new RuntimeDatabaseException(e); } - - } - - @Override - public String toString() { - if(isDiscarded()) return "DISCARDED " + request.toString(); - else if(isExcepted()) return request.toString() + " " + getResult(); - else return request.toString() + " " + statusOrException; - } + + if (entry != null) + entry.performFromCache(parentGraph, procedure_); + + } + + @Override + public String toString() { + if (isDiscarded()) + return "DISCARDED " + request.toString(); + else if (isExcepted()) + return request.toString() + " " + getResult(); + else + return request.toString() + " " + statusOrException; + } + + @Override + public void execute(AsyncReadGraph graph, T result) { + setResult(result); + setReady(); + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + except(throwable); + } }