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;fp=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FAsyncReadEntry.java;h=69a85220b954cac8d1dc0e24ef25ca07edd0c407;hb=de8b6e194da6d56c410101d9c72763078b0e8717;hp=75169d02e9946ca0a1dea3f1e03a3dba2ce49b97;hpb=880a8a4927805afbbf47005a807cef3f39406866;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 75169d02e..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 @@ -14,84 +14,72 @@ 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; AsyncReadEntry(AsyncRead request) { - this.request = request; - if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this); + 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(); - setResult(null); + super.discard(); + setResult(null); } - - final public void addOrSet(AsyncReadGraph graph, Object item) { -// System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + item); - - assert(isPending()); - - synchronized(this) { - setResult(item); - setReady(); - } - - } - - public void except(AsyncReadGraph graph, Throwable t) { - - assert(isPending()); - synchronized(this) { + assert (isPending()); + + synchronized (this) { except(t); } - + } - - + @Override final public Query getQuery() { - + return new Query() { - @Override - public void recompute(ReadGraphImpl graph) { + @Override + public void recompute(ReadGraphImpl graph) { - try { + try { - GraphSemaphore s = new GraphSemaphore(graph, 0); - - request.perform(graph , new AsyncProcedure() { + GraphSemaphore s = new GraphSemaphore(graph, 0); + + 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(); } @@ -99,144 +87,129 @@ final public class AsyncReadEntry extends CacheEntryBase> { s.waitFor(1); - } catch (Throwable t) { - except(t); + } catch (Throwable t) { + except(t); } - - } - - @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; - } - + + } + + @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; + } + }; - + } @Override - public Object performFromCache(ReadGraphImpl graph, AsyncProcedure proc) { - - if(isExcepted()) { - + public Object performFromCache(ReadGraphImpl graph, AsyncProcedure proc) { + + 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); } - - return getResult(); - - } - - //@Override - public Object compute(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { - - ReadGraphImpl queryGraph = graph.withParent(this); - - request.perform(queryGraph, new AsyncProcedure() { - - @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(); - + + 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; } - - public static void computeForEach(ReadGraphImpl parentGraph, AsyncRead request, AsyncReadEntry entry, AsyncProcedure procedure) throws DatabaseException { - - ReadGraphImpl queryGraph = parentGraph.withParent(entry); - - request.perform(queryGraph, new AsyncProcedure() { - - @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 void execute(AsyncReadGraph graph, T result) { + setResult(result); + setReady(); + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + except(throwable); } - - - @Override - public String toString() { - if(isDiscarded()) return "DISCARDED " + request.toString(); - else if(isExcepted()) return request.toString() + " " + getResult(); - else return request.toString() + " " + statusOrException; - } }