/******************************************************************************* * 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 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ 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.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> implements Listener { final LinkedList items = new LinkedList(); protected ExternalRead id; protected ReadGraphImpl graph; protected boolean registered = false; @Override int makeHash() { return id.hashCode(); } @Override public Object getOriginalRequest() { return id; } @Override public void clearResult(QuerySupport support) { } @Override public void discard() { id.unregistered(); id = null; graph = null; super.discard(); } @Override public void setPending(QuerySupport querySupport) { //if(result != NO_RESULT) { //new Exception("result = " + result).printStackTrace(); //} statusOrException = PENDING; result = REQUIRES_COMPUTATION; } public ExternalReadEntry(ExternalRead request, ReadGraphImpl graph) { assert request != null; this.id = request; this.graph = graph; } @Override public void except(Throwable t) { 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 final public Query getQuery() { return new Query() { @Override public void recompute(ReadGraphImpl graph) { synchronized(items) { // Update if(!items.isEmpty()) { setReady(); setResult(items.removeFirst()); } // Reschedule if(!items.isEmpty()) { graph.processor.updatePrimitive(id); } } } @Override public void removeEntry(QueryProcessor processor) { processor.cache.remove(ExternalReadEntry.this); } @Override public int type() { return RequestFlags.IMMEDIATE_UPDATE; } @Override public String toString() { if(id == null) return "DISCARDED ExternalRead"; else return id.toString(); } }; } @Override public String toString() { if(id == null) return "DISCARDED ExternalRead " + System.identityHashCode(this); else return id.toString() + " " + + System.identityHashCode(this); } @Override public Object performFromCache(ReadGraphImpl graph, AsyncProcedure procedure) { AsyncProcedure proc = (AsyncProcedure)procedure; if(isExcepted()) { proc.exception(graph, (Throwable)getResult()); } else { proc.execute(graph, (T)getResult()); } return getResult(); } @Override void prepareRecompute(QuerySupport querySupport) { // Do nothing - the state is already set and cannot be recomputed on demand } public Object compute(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { try { ReadGraphImpl queryGraph = graph.withParent(this); 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); graph.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() || !graph.processor.isBound(this)); } }