import java.io.PrintStream;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
-import java.nio.BufferUnderflowException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import org.simantics.databoard.util.binary.BinaryFile;
import org.simantics.databoard.util.binary.RandomAccessBinary;
import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ComputationalValue;
import org.simantics.db.DevelopmentKeys;
import org.simantics.db.ExternalValueSupport;
import org.simantics.db.ReadGraph;
import org.simantics.db.common.primitiverequest.ValueImplied;
import org.simantics.db.common.primitiverequest.VariantValueImplied;
import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
+import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
import org.simantics.db.exception.ResourceNotFoundException;
import org.simantics.db.exception.ServiceException;
import org.simantics.db.exception.ValidationException;
-import org.simantics.db.impl.BlockingAsyncProcedure;
import org.simantics.db.impl.RelationContextImpl;
import org.simantics.db.impl.ResourceImpl;
import org.simantics.db.impl.internal.RandomAccessValueSupport;
final static boolean EMPTY_RESOURCE_CHECK = false;
final public CacheEntry parent;
+ public final ReadGraphImpl parentGraph;
final public QueryProcessor processor;
- public AsyncBarrierImpl asyncBarrier = null;
+ public final AsyncBarrierImpl asyncBarrier;
final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
assert (subject != null);
+ byte[] bytes = null;
try {
- byte[] bytes = processor.getValue(this, subject);
+ bytes = processor.getValue(this, subject);
if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
Serializer serializer = getSerializer(binding);
throw new DoesNotContainValueException(e);
- } catch (IOException e) {
-
- throw new ServiceException(e);
-
- } catch (DatabaseException e) {
-
- throw new ServiceException(e);
-
- } catch (BufferUnderflowException e) {
- // This is sometimes thrown when deserialize fails because wrong format.
- // For callers of this method this is just an service exception.
- throw new ServiceException(e);
- }
-
+ } catch (Throwable t) {
+ throw new ServiceException("Could not getValue for subject " + debugString(subject) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(bytes), t);
+ }
}
@Override
@Override
public <T> T syncRequest(final Read<T> request) throws DatabaseException {
-
assert (request != null);
-
- return QueryCache.resultReadEntry(this, request, parent, null, null);
-
+ return (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
}
@Override
throws DatabaseException {
assert (request != null);
- asyncBarrier = new AsyncBarrierImpl(null);
- BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, null, request);
- syncRequest(request, ap);
- return ap.get();
+ return syncRequest(request, new AsyncProcedureAdapter<>() );
}
ListenerBase listener = getListenerBase(procedure);
- BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
-
- QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
-
- return ap.get();
-
- }
-
- final private <T> void syncRequest(final AsyncRead<T> request, final AsyncReadProcedure<T> procedure) throws DatabaseException {
-
- assert (request != null);
-
- ListenerBase listener = getListenerBase(procedure);
- assert(listener == null);
-
- BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
-
- QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
-
- ap.get();
+// BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
+ return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
+// return ap.get();
}
// else
procedure.execute(graph, (T) obj);
- } catch (IOException e) {
- procedure.exception(graph, e);
- } catch (BufferUnderflowException e) {
- procedure.exception(graph, e);
} catch (Throwable t) {
- procedure.exception(graph, t);
+ procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
}
-
}
@Override
});
}
+
+ private static String safeArrayToString(byte[] a) {
+ if (a == null)
+ return "null";
+ int iMax = a.length - 1;
+ if (iMax == -1)
+ return "[]";
+
+ StringBuilder b = new StringBuilder();
+ b.append('[');
+ for (int i = 0; i < 100; i++) { // limit to first 100 items
+ b.append(a[i]);
+ if (i == iMax)
+ return b.append(']').toString();
+ b.append(", ");
+ }
+ return b.append(", ... (" + a.length + ")]").toString();
+ }
@Override
public <T> void forValue(Resource subject, Binding binding,
else
procedure.execute(graph, (T) obj);
- } catch (IOException e) {
- procedure.exception(graph, e);
- } catch (BufferUnderflowException e) {
- procedure.exception(graph, e);
} catch (Throwable t) {
- procedure.exception(graph, t);
+ procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
}
-
}
@Override
assert (request != null);
assert (procedure != null);
- processor.schedule(new SessionTask(false) {
+ processor.schedule(new SessionTask(this) {
@Override
- public void run(int thread) {
+ public void run0(int thread) {
try {
final ListenerBase listener = getListenerBase(procedure);
QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
}
public static ReadGraphImpl createAsync(QueryProcessor support) {
- return new ReadGraphImpl(null, support);
+ return new ReadGraphImpl(null, null, support);
}
@Override
assert (request != null);
assert (procedure != null);
- processor.schedule(new SessionTask(false) {
+ processor.schedule(new SessionTask(this) {
@Override
- public void run(int thread) {
+ public void run0(int thread) {
try {
final ListenerBase listener = getListenerBase(procedure);
QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
* Internal routines
*/
- protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error. Contact application support.";
+ protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
/*
* callerThread is the currently running thread state.syncThread is blocking for
* this execution state.syncParent is the blocking request
*/
- final private boolean isExternal(int thread) {
- return thread == Integer.MIN_VALUE;
+ ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
+ this.parentGraph = parentGraph;
+ this.parent = parent;
+ this.processor = support;
+ this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent);
+ }
+
+ ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
+ this(graph, parent, graph.processor);
}
ReadGraphImpl(ReadGraphImpl graph) {
- this(graph.parent, graph.processor);
+ this(graph, graph.parent);
}
- ReadGraphImpl(CacheEntry parent, QueryProcessor support) {
- this.parent = parent;
- this.processor = support;
+ public ReadGraphImpl withParent(CacheEntry parent) {
+ return new ReadGraphImpl(this, parent);
}
- public static ReadGraphImpl create(QueryProcessor support) {
- return new ReadGraphImpl(null, support);
+ public ReadGraphImpl forRecompute(CacheEntry parent) {
+ return new ReadGraphImpl(null, parent, processor);
}
- public static ReadGraphImpl newAsync(ReadGraphImpl parent) {
- ReadGraphImpl result = new ReadGraphImpl(parent);
- result.asyncBarrier = new AsyncBarrierImpl(parent.asyncBarrier);
- return result;
+ public static ReadGraphImpl create(QueryProcessor support) {
+ return new ReadGraphImpl(null, null, support);
}
public ReadGraphImpl newRestart(ReadGraphImpl impl) {
}
- public ReadGraphImpl withParent(CacheEntry parent) {
- if(parent == this.parent) return this;
- else return new ReadGraphImpl(parent, processor);
- }
-
final private ListenerBase getListenerBase(final Object procedure) {
if (procedure instanceof ListenerBase)
return (ListenerBase) procedure;
return getValue(r, binding);
}
} else if(types.contains(L0.ExternalValue)) {
- try {
- return (T)ReflectionUtils.getValue(getURI(r)).getValue();
- } catch(ValueNotFoundException e) {
- throw new DatabaseException(e);
- } catch(ClassCastException e) {
- throw new DatabaseException(e);
+ ComputationalValue cv = syncRequest(new PossibleAdapter<ComputationalValue>(r, ComputationalValue.class), TransientCacheAsyncListener.instance());
+ if(cv != null) {
+ return cv.getValue(this, r);
+ } else {
+ // This should not even be possible since L0 defines an adapter for all values
+ try {
+ return (T)ReflectionUtils.getValue(getURI(r)).getValue();
+ } catch(ValueNotFoundException e) {
+ throw new DatabaseException(e);
+ } catch(ClassCastException e) {
+ throw new DatabaseException(e);
+ }
}
}
else {
@Override
public boolean performPending() {
- return processor.performPending(processor.thread.get());
+ return processor.performPending(this);
+ }
+
+ public Set<ReadGraphImpl> ancestorSet() {
+ HashSet<ReadGraphImpl> result = new HashSet<>();
+ ReadGraphImpl g = this;
+ while(g != null) {
+ result.add(g);
+ g = g.parentGraph;
+ }
+ return result;
}
}