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=72f132288bf79990c5bf56ce591463993ccd8acd;hb=159d04234f7fbf7554910a154b29a5dd7bbc6068;hp=e4ff1ea874bb2c3b9f8e288d413eaedc49f51c51;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;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 e4ff1ea87..72f132288 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * Copyright (c) 2007, 2018 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -14,153 +14,203 @@ 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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -final public class ReadEntry extends CacheEntryBase { +public final class ReadEntry extends CacheEntryBase> implements AsyncProcedure { - protected Read request; + private static final Logger LOGGER = LoggerFactory.getLogger(ReadEntry.class); - public ReadEntry(Read request) { - this.request = request; + protected Read id; + + public ReadEntry(Read request) { + this.id = request; } - + @Override int makeHash() { - return request.hashCode(); + return id.hashCode(); } - + @Override public Object getOriginalRequest() { - return request; + return id; } - + @Override public void discard() { - super.discard(); - setResult(null); - } - - final public void addOrSet(AsyncReadGraph graph, Object item) { - - assert(assertPending()); - -// ArrayList, AsyncBarrier>> p = null; - - synchronized(this) { - - setResult(item); - setReady(); -// p = procs; -// procs = null; - - } - -// if(p != null) -// for(Pair, AsyncBarrier> proc : p) { -// proc.first.execute(graph, (T)item); -// proc.second.dec(); -// } - + super.discard(); + setResult(null); } @Override final public Query getQuery() { - + return new Query() { - @Override - public void recompute(ReadGraphImpl graph_, Object provider, CacheEntry entry) { - - QueryProcessor qp = (QueryProcessor)provider; - - WriteGraphImpl write = qp.getCore().getSession().getService(WriteGraphImpl.class); - - ReadGraphImpl graph = write.newSync(entry); + @Override + public void recompute(ReadGraphImpl graph) { - try { + try { - entry.setPending(); - T result = request.perform(graph); - addOrSet(graph, result); + T result = id.perform(graph); + setResult(result); + setReady(); - } catch (Throwable t) { + } catch (Throwable t) { + + except(t); + + } + + } + + @Override + public void removeEntry(QueryProcessor processor) { + processor.cache.remove(ReadEntry.this); + } - except(t); - + @Override + public int type() { + if (id instanceof ReadExt) { + return ((ReadExt) id).getType(); + } else { + return RequestFlags.INVALIDATE; } - - } - - @Override - public void removeEntry(QueryProcessor processor) { - processor.readMap.remove(request); - } - - @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 (id == null) + return "DISCARDED"; + else + return id.toString() + statusOrException; + } + }; - + } - - public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { - - AsyncProcedure proc = (AsyncProcedure)procedure; - if(isExcepted()) { + public static T computeForEach(ReadGraphImpl graph, Read request, ReadEntry entry, + AsyncProcedure procedure_, boolean needsToBlock) throws DatabaseException { + + AsyncProcedure procedure = entry != null ? entry : procedure_; + + ReadGraphImpl queryGraph = graph.withParent(entry); + queryGraph.asyncBarrier.inc(); + + ReadGraphImpl executeGraph = graph.withParent(graph.parent); + executeGraph.asyncBarrier.inc(); + + try { + + // This throws + T result = request.perform(queryGraph); + + if(procedure != null) procedure.execute(executeGraph, result); + return (T)result; + + } catch (DatabaseException e) { + + if(procedure != null) procedure.exception(executeGraph, e); + throw e; + + } catch (Throwable t) { + + DatabaseException dbe = new DatabaseException(t); + if(procedure != null) procedure.exception(executeGraph, dbe); + throw dbe; + + } finally { + + queryGraph.asyncBarrier.dec(); try { - proc.exception(graph, (Throwable)getResult()); - } catch (Throwable t) { - t.printStackTrace(); - } - - } else { - - try { - proc.execute(graph, (T)getResult()); - } catch (Throwable t) { - t.printStackTrace(); + + if (entry != null) { + // This also throws so must dec barrier finally + entry.performFromCache(executeGraph, procedure_); + } + + } finally { + + executeGraph.asyncBarrier.dec(); + executeGraph.asyncBarrier.waitBarrier(procedure, executeGraph); + } + + + } + + } + + public Object performFromCache(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { + + AsyncProcedure proc = (AsyncProcedure) procedure; + + if (isExcepted()) { + if(proc != null) { + try { + proc.exception(graph, (Throwable) getResult()); + } catch (Throwable t) { + LOGGER.error("performFromCache proc.exception failed", t); + } + } + Throwable t = (Throwable) getResult(); + if(t instanceof DatabaseException) { + throw (DatabaseException)t; + } else { + throw new DatabaseException(t); + } + } else { + if(proc != null) { + try { + proc.execute(graph, (T) getResult()); + } catch (Throwable t) { + LOGGER.error("performFromCache proc.execute failed", t); + } + } + return (T)getResult(); + } - } - - } - - @Override - public String toString() { - if(request == null) return "DISCARDED"; - else return request.toString() + " - " + statusOrException; - } - - public Object get(ReadGraphImpl graph, QueryProcessor processor, Object procedure) throws DatabaseException { - if(procedure != null) performFromCache(graph, processor, procedure); - checkAndThrow(); - return getResult(); - } - - @Override - boolean isImmutable(ReadGraphImpl graph) throws DatabaseException { - if(request instanceof ReadExt) { - return ((ReadExt)request).isImmutable(graph); - } - return false; - } + } + + @Override + public String toString() { + if (id == null) + return "DISCARDED"; + else + return id.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 (id instanceof ReadExt) { + return ((ReadExt) id).isImmutable(graph); + } + return false; + } + + @Override + public void execute(AsyncReadGraph graph, T result) { + setResult(result); + setReady(); + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + except(throwable); + } }