/*******************************************************************************
- * 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
import java.util.ArrayList;
import java.util.Iterator;
+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.impl.procedure.InternalProcedure;
+import org.simantics.utils.Development;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-abstract public class CacheEntryBase extends CacheEntry {
+public abstract class CacheEntryBase<Procedure> extends CacheEntry<Procedure> {
private static final Logger LOGGER = LoggerFactory.getLogger(CacheEntryBase.class);
final public static CacheEntryBase[] NONE = new CacheEntryBase[0];
- static private Object NO_RESULT = new Object();
- static protected Object INVALID_RESULT = new Object();
+ static Object NO_RESULT = new Object() { public String toString() { return "NO_RESULT"; }};
+ static protected Object INVALID_RESULT = new Object() { public String toString() { return "INVALID_RESULT"; }};
- // Just created
- static protected Object FRESH = new Object() { public String toString() { return "CREATED"; }};
+// // Just created
+// static protected Object FRESH = new Object() { public String toString() { return "CREATED"; }};
// Result is computed - no exception
static protected Object READY = new Object() { public String toString() { return "READY"; }};
// Computation is under way
// Entry is discarded and is waiting for garbage collect
static protected Object DISCARDED = new Object() { public String toString() { return "DISCARDED"; }};
// The result has been invalidated
- static protected Object REFUTED = new Object() { public String toString() { return "REFUTED"; }};
+ static protected Object REQUIRES_COMPUTATION = new Object() { public String toString() { return "REFUTED"; }};
// The computation has excepted - the exception is in the result
static protected Object EXCEPTED = new Object() { public String toString() { return "EXCEPTED"; }};
// This indicates the status of the entry
- public Object statusOrException = FRESH;
+ public Object statusOrException = REQUIRES_COMPUTATION;
private CacheEntry p1 = null;
private Object p2OrParents = null;
abstract int makeHash();
// This can be tested to see if the result is finished
- private Object result = NO_RESULT;
+ Object result = NO_RESULT;
final public boolean isFresh() {
- return FRESH == statusOrException;
+ return REQUIRES_COMPUTATION == statusOrException;
}
public void setReady() {
+ assert(result != NO_RESULT);
statusOrException = READY;
}
@Override
public void discard() {
- if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: discarded " + this);
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) {
+ System.err.println("[QUERY STATE]: discarded " + this);
+ }
+ }
statusOrException = DISCARDED;
}
@Override
final public void refute() {
- if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: refuted " + this);
- statusOrException = REFUTED;
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) {
+ System.err.println("[QUERY STATE]: refuted " + this);
+ }
+ }
+ statusOrException = REQUIRES_COMPUTATION;
}
@Override
final public boolean isRefuted() {
- return REFUTED == statusOrException;
+ return REQUIRES_COMPUTATION == statusOrException;
}
@Override
- final public void except(Throwable t) {
- if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: excepted " + this);
+ public void except(Throwable throwable) {
+ if (Development.DEVELOPMENT) {
+ if(Development.<Boolean>getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) {
+ System.err.println("[QUERY STATE]: excepted " + this);
+ }
+ }
if(statusOrException != DISCARDED) {
statusOrException = EXCEPTED;
- result = t;
+ result = throwable;
} else {
- LOGGER.warn("Cache entry got excepted status after being discarded: " + getClass().getSimpleName(), t);
- result = t;
+ LOGGER.warn("Cache entry got excepted status after being discarded: " + getClass().getSimpleName(), throwable);
+ result = throwable;
}
}
}
@Override
- final public void setPending() {
- statusOrException = PENDING;
+ public void setPending(QuerySupport querySupport) {
+ statusOrException = PENDING;
+ clearResult(querySupport);
}
@Override
return PENDING == statusOrException;
}
+ final public boolean requiresComputation() {
+ return REQUIRES_COMPUTATION == statusOrException;
+ }
+
final public boolean assertPending() {
boolean result = isPending();
if(!result) {
this.result = result;
}
+ @SuppressWarnings("unchecked")
@Override
final public <T> T getResult() {
assert(statusOrException != DISCARDED);
}
@Override
- final public Iterable<CacheEntry> getParents(QueryProcessor processor) {
+ final public Iterable<CacheEntry<?>> getParents(QueryProcessor processor) {
- ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
+ ArrayList<CacheEntry<?>> result = new ArrayList<CacheEntry<?>>();
if(p1 != null) result.add(p1);
if(p2OrParents != null) {
if(p2OrParents instanceof QueryIdentityHashSet) {
}
- protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry> result) {
-
+ protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {
}
protected String internalError() {
}
- protected boolean handleException(ReadGraphImpl graph, IntProcedure procedure) {
+ protected boolean handleException(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
if(isExcepted()) {
procedure.exception(graph, (Throwable)getResult());
return true;
}
}
- protected boolean handleException(ReadGraphImpl graph, TripleIntProcedure procedure) {
+ protected boolean handleException(ReadGraphImpl graph, TripleIntProcedure procedure) throws DatabaseException {
if(isExcepted()) {
procedure.exception(graph, (Throwable)getResult());
return true;
}
}
- protected <T> boolean handleException(ReadGraphImpl graph, InternalProcedure<T> procedure) {
+ protected <T> boolean handleException(ReadGraphImpl graph, InternalProcedure<T> procedure) throws DatabaseException {
if(isExcepted()) {
procedure.exception(graph, (Throwable)getResult());
return true;
@Override
void prepareRecompute(QuerySupport querySupport) {
- setPending();
- clearResult(querySupport);
+ setPending(querySupport);
}
- /*
- *
- *
- */
@Override
int getGCStatus() {
return GCStatus;
// This is the original request for all built-in queries
return getQuery();
}
-
+
+ public CacheEntryBase() {
+ }
+
}