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 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.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;
import org.simantics.utils.Development;
import org.simantics.utils.datastructures.Pair;
import org.simantics.utils.datastructures.collections.CollectionUtils;
+import org.simantics.utils.threads.logger.ITask;
+import org.simantics.utils.threads.logger.ThreadLogger;
import org.slf4j.LoggerFactory;
import gnu.trove.map.hash.TObjectIntHashMap;
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 (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
+
+ ITask task = ThreadLogger.task(request);
+ T result = (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
+ task.finish();
+ return result;
+
}
@Override
assert (request != null);
+ ITask task = ThreadLogger.task(request);
ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
-
- return QueryCache.resultReadEntry(this, request, parent, listener, procedure);
+ T result = QueryCache.resultReadEntry(this, request, parent, listener, procedure);
+ task.finish();
+ return result;
}
assert (request != null);
+ ITask task = ThreadLogger.task(request);
ListenerBase listener = getListenerBase(procedure);
-
-// BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
- return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
-// return ap.get();
+ T result = (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
+ task.finish();
+ return result;
}
// 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(this) {
+ processor.scheduleNow(new SessionTask(this) {
@Override
public void run0(int thread) {
assert (request != null);
assert (procedure != null);
- processor.schedule(new SessionTask(this) {
+ ITask task = ThreadLogger.task(request);
+
+ processor.scheduleNow(new SessionTask(this) {
@Override
public void run0(int thread) {
try {
final ListenerBase listener = getListenerBase(procedure);
- QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
+ QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure<T>() {
+
+ @Override
+ public void execute(AsyncReadGraph graph, T result) {
+ task.finish();
+ procedure.execute(graph, result);
+ }
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ task.finish();
+ procedure.exception(graph, throwable);
+ }
+
+ }, false);
} catch (DatabaseException e) {
Logger.defaultLogError(e);
}
}
Serializer serializer = binding.serializer();
byte[] bytes = serializer.serialize(initialValue);
+ // In case the file has been previously accessed and was larger we set the correct size now
+ rd.binaryFile.setLength(bytes.length);
rd.binaryFile.write(bytes);
ravs.put(resource, rd);
return rd;
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 <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
if(Development.DEVELOPMENT) {
- String error = L0Validations.checkValueType(this, subject, relation);
- if(error != null) {
- Logger.defaultLogError(new ValidationException(error));
- //throw new ValidationException(error);
- new ValidationException(error).printStackTrace();
+ if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
+ String error = L0Validations.checkValueType(this, subject, relation);
+ if(error != null) {
+ Logger.defaultLogError(new ValidationException(error));
+ throw new ValidationException(error);
+ }
}
}
return getValue2(getSingleObject(subject, relation), context);
@Override
public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
if(Development.DEVELOPMENT) {
- String error = L0Validations.checkValueType(this, subject, relation);
- if(error != null) {
- Logger.defaultLogError(new ValidationException(error));
- //throw new ValidationException(error);
- new ValidationException(error).printStackTrace();
- }
+ if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
+ String error = L0Validations.checkValueType(this, subject, relation);
+ if(error != null) {
+ Logger.defaultLogError(new ValidationException(error));
+ throw new ValidationException(error);
+ }
+ }
}
return getVariantValue2(getSingleObject(subject, relation), context);
}
public boolean performPending() {
return processor.performPending(this);
}
-
+
public Set<ReadGraphImpl> ancestorSet() {
HashSet<ReadGraphImpl> result = new HashSet<>();
ReadGraphImpl g = this;
}
return result;
}
+
+ public int getLevel() {
+ return getLevelStatic(this);
+ }
+
+ private static int getLevelStatic(ReadGraphImpl impl) {
+ if(impl == null) return 0;
+ else return 1 + getLevelStatic(impl.parentGraph);
+ }
+
+ public ReadGraphImpl getTopLevelGraph() {
+ return getTopLevelGraphStatic(this);
+ }
+
+ private static ReadGraphImpl getTopLevelGraphStatic(ReadGraphImpl impl) {
+ if(impl.parentGraph == null) return impl;
+ else return getTopLevelGraphStatic(impl.parentGraph);
+ }
}