X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FReadEntry.java;h=56267cc12d39be24f0d245897977dafe0ec04dfd;hb=de8b6e194da6d56c410101d9c72763078b0e8717;hp=115a4a5a1509db803a88140b038d9f475b1e3305;hpb=880a8a4927805afbbf47005a807cef3f39406866;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java index 115a4a5a1..56267cc12 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java @@ -14,185 +14,167 @@ package org.simantics.db.impl.query; import org.simantics.db.AsyncReadGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; -import org.simantics.db.impl.graph.WriteGraphImpl; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.db.request.Read; import org.simantics.db.request.ReadExt; import org.simantics.db.request.RequestFlags; -final public class ReadEntry extends CacheEntryBase> { +final public class ReadEntry extends CacheEntryBase> implements AsyncProcedure { - protected Read request; + protected Read request; - public ReadEntry(Read request) { - this.request = request; + public ReadEntry(Read request) { + this.request = request; } - + @Override int makeHash() { - return request.hashCode(); + return request.hashCode(); } - + @Override public Object getOriginalRequest() { return request; } - + @Override public void discard() { - super.discard(); - setResult(null); - } - - final public Object addOrSet(AsyncReadGraph graph, Object result) { - -// System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + result); - - assert(assertPending()); - - setResult(result); - setReady(); - - return result; - + super.discard(); + setResult(null); } @Override final public Query getQuery() { - + return new Query() { - @Override - public void recompute(ReadGraphImpl graph) { - - try { + @Override + public void recompute(ReadGraphImpl graph) { + + try { + + T result = request.perform(graph); + setResult(result); + setReady(); + + } catch (Throwable t) { + + except(t); - T result = request.perform(graph); - addOrSet(graph, result); + } + + } - } catch (Throwable t) { + @Override + public void removeEntry(QueryProcessor processor) { + processor.cache.remove(ReadEntry.this); + } - except(t); - + @Override + public int type() { + if (request instanceof ReadExt) { + return ((ReadExt) request).getType(); + } else { + return RequestFlags.INVALIDATE; } - - } - - @Override - public void removeEntry(QueryProcessor processor) { - processor.cache.remove(ReadEntry.this); - } - - @Override - public int type() { - if(request instanceof ReadExt) { - return ((ReadExt)request).getType(); - } else { - return RequestFlags.INVALIDATE; - } - } - - @Override - public String toString() { - if(request == null) return "DISCARDED"; - else return request.toString() + statusOrException; - } - + } + + @Override + public String toString() { + if (request == null) + return "DISCARDED"; + else + return request.toString() + statusOrException; + } + }; - + } - - //@Override - public Object compute(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { - ReadGraphImpl queryGraph = graph.withParent(this); + public static void computeForEach(ReadGraphImpl graph, Read request, ReadEntry entry, + AsyncProcedure procedure_) throws DatabaseException { - try { + AsyncProcedure procedure = entry != null ? entry : procedure_; - addOrSet(queryGraph, request.perform(queryGraph)); - return get(queryGraph, procedure); + ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph; - } catch (DatabaseException e) { + try { - except(e); - return get(graph, procedure); + T result = request.perform(queryGraph); + procedure.execute(graph, result); - } catch (Throwable t) { + } catch (DatabaseException e) { - except(new DatabaseException(t)); - return get(graph, procedure); + procedure.exception(graph, e); - } - - } + } catch (Throwable t) { - public static void computeForEach(ReadGraphImpl graph, Read request, ReadEntry entry, AsyncProcedure procedure) throws DatabaseException { - - ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph; + DatabaseException dbe = new DatabaseException(t); + procedure.exception(graph, dbe); - try { + } - T result = request.perform(queryGraph); - if(entry != null) entry.addOrSet(queryGraph, result); - procedure.execute(graph, result); + if (entry != null) + entry.performFromCache(queryGraph, procedure_); - } catch (DatabaseException e) { + } - if(entry != null) entry.except(e); - procedure.exception(graph, e); + public Object performFromCache(ReadGraphImpl graph, AsyncProcedure procedure) { - } catch (Throwable t) { + AsyncProcedure proc = (AsyncProcedure) procedure; - DatabaseException dbe = new DatabaseException(t); - if(entry != null) entry.except(dbe); - procedure.exception(graph, dbe); + 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(); - } - } - - public Object performFromCache(ReadGraphImpl graph, AsyncProcedure procedure) { - - AsyncProcedure proc = (AsyncProcedure)procedure; - - 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(); - } - } + + @Override + public String toString() { + if (request == null) + return "DISCARDED"; + else + return request.toString() + " - " + statusOrException; + } + + public Object get(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { + if (procedure != null) + performFromCache(graph, procedure); + checkAndThrow(); + return getResult(); + } + + @Override + boolean isImmutable(ReadGraphImpl graph) throws DatabaseException { + if (request instanceof ReadExt) { + return ((ReadExt) request).isImmutable(graph); } - - return (T)getResult(); - - } - - @Override - public String toString() { - if(request == null) return "DISCARDED"; - else return request.toString() + " - " + statusOrException; - } - - public Object get(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { - if(procedure != null) performFromCache(graph, procedure); - checkAndThrow(); - return getResult(); - } - - @Override - boolean isImmutable(ReadGraphImpl graph) throws DatabaseException { - if(request instanceof ReadExt) { - return ((ReadExt)request).isImmutable(graph); - } - return false; - } + return false; + } + + @Override + public void execute(AsyncReadGraph graph, T result) { + setResult(result); + setReady(); + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + except(throwable); + } }