X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FExternalReadEntry.java;h=2980b9b15f72e9a54b00a96fe06a397ef60b59b6;hp=d61049744a3cbdbdb09c85040833ffc0e8329d91;hb=HEAD;hpb=0d9b90834ce56b292c00b1a39850ed842c3e4d42 diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java index d61049744..2980b9b15 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java @@ -13,27 +13,32 @@ package org.simantics.db.impl.query; import java.util.LinkedList; +import org.simantics.databoard.Bindings; +import org.simantics.db.DevelopmentKeys; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.impl.DebugPolicy; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.procedure.AsyncProcedure; +import org.simantics.db.procedure.Listener; import org.simantics.db.request.ExternalRead; import org.simantics.db.request.RequestFlags; +import org.simantics.utils.Development; -final public class ExternalReadEntry extends CacheEntryBase> { +final public class ExternalReadEntry extends CacheEntryBase> implements Listener { final LinkedList items = new LinkedList(); - protected ExternalRead request; + protected ExternalRead id; + protected QueryProcessor processor; + protected boolean registered = false; @Override int makeHash() { - return request.hashCode(); + return id.hashCode(); } @Override public Object getOriginalRequest() { - return request; + return id; } @Override @@ -42,13 +47,14 @@ final public class ExternalReadEntry extends CacheEntryBase @Override public void discard() { - request.unregistered(); - request = null; + id.unregistered(); + id = null; + processor = null; super.discard(); } @Override - public void setPending() { + public void setPending(QuerySupport querySupport) { //if(result != NO_RESULT) { //new Exception("result = " + result).printStackTrace(); //} @@ -56,64 +62,44 @@ final public class ExternalReadEntry extends CacheEntryBase result = REQUIRES_COMPUTATION; } - public ExternalReadEntry(ExternalRead request) { + public ExternalReadEntry(ExternalRead request, ReadGraphImpl graph) { assert request != null; - this.request = request; - } - - final public void queue(T item) { - synchronized(items) { - items.addLast(item); - // TODO: implement flags/logic in ExternalRead to state that all but the latest request result can be evaporated - // In some cases where data is produced really fast this might be necessary but currently this queueing will do. - } - } - - final public void addOrSet(QueryProcessor processor, Object item) { - - try { - - assert(isPending()); - - //ArrayList> p = null; - - synchronized(this) { - - setResult(item); - setReady(); -// p = procs; -// procs = null; - - } - -// if(p != null) -// for(Procedure proc : p) { -// proc.execute((T)item); -// } - - } catch (Throwable t) { - t.printStackTrace(); - } - + this.id = request; + this.processor = graph.processor; } @Override public void except(Throwable t) { - if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: excepted " + this); + + if (Development.DEVELOPMENT) { + if(Development.getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) { + System.err.println("[QUERY STATE]: excepted " + this); + } + } + if(statusOrException != DISCARDED) { statusOrException = EXCEPTED; result = t; } else { result = t; } + assert(isExcepted()); + } @Override public void setResult(Object result) { + super.setResult(result); assert(!(result instanceof Throwable)); assert(!isExcepted()); + + } + + @Override + public void setReady() { + super.setReady(); } @Override @@ -126,7 +112,6 @@ final public class ExternalReadEntry extends CacheEntryBase synchronized(items) { - // Update if(!items.isEmpty()) { setReady(); @@ -134,7 +119,7 @@ final public class ExternalReadEntry extends CacheEntryBase } // Reschedule if(!items.isEmpty()) { - graph.processor.updatePrimitive(request); + graph.processor.updatePrimitive(id); } } @@ -153,8 +138,8 @@ final public class ExternalReadEntry extends CacheEntryBase @Override public String toString() { - if(request == null) return "DISCARDED ExternalRead"; - else return request.toString(); + if(id == null) return "DISCARDED ExternalRead"; + else return id.toString(); } }; @@ -163,8 +148,8 @@ final public class ExternalReadEntry extends CacheEntryBase @Override public String toString() { - if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this); - else return request.toString() + " " + + System.identityHashCode(this); + if(id == null) return "DISCARDED ExternalRead " + System.identityHashCode(this); + else return id.toString() + " " + + System.identityHashCode(this); } @Override @@ -192,7 +177,64 @@ final public class ExternalReadEntry extends CacheEntryBase } public Object compute(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { - return graph.processor.cache.performQuery(graph, request, this, procedure); + + try { + + ReadGraphImpl queryGraph = graph.withParent(this, null, true); + + if(!registered) { + id.register(graph, this); + registered = true; + } + + queryGraph.asyncBarrier.waitBarrier(id, graph); + + } catch (Throwable t) { + + except(t); + + } + + performFromCache(graph, procedure); + + return getResult(); + + } + + @Override + public void execute(T result) { + + if(this.result == REQUIRES_COMPUTATION) { + + setResult(result); + setReady(); + + } else { + + synchronized(items) { + items.addLast(result); + processor.updatePrimitive(id); + // TODO: implement flags/logic in ExternalRead to state that all but the latest request result can be evaporated + // In some cases where data is produced really fast this might be necessary but currently this queueing will do. + } + + } + + } + + @Override + public void exception(Throwable t) { + except(t); + } + + @Override + public boolean isDisposed() { + return registered && (isDiscarded() || !processor.isBound(this)); + } + + @Override + public String classId() { + return null; } }