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%2Fgraph%2FReadGraphImpl.java;h=2309af76440b10bc2a3e9d336cf2fc4bdfc14819;hp=b5da0b92f7bae6521487e2fa0907ca2b96a3c09b;hb=e10760b08746837538ad4c2b4c37736aa34c7a47;hpb=0d9b90834ce56b292c00b1a39850ed842c3e4d42 diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java index b5da0b92f..2309af764 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java @@ -18,10 +18,10 @@ import java.io.IOException; 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; @@ -45,10 +45,12 @@ import org.simantics.databoard.type.Datatype; 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.RelationContext; +import org.simantics.db.RelationInfo; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.Statement; @@ -89,6 +91,7 @@ import org.simantics.db.common.primitiverequest.Value; 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; @@ -133,7 +136,6 @@ import org.simantics.db.exception.NoSingleResultException; 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; @@ -198,9 +200,10 @@ public class ReadGraphImpl implements AsyncReadGraph { 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); @@ -1104,9 +1107,10 @@ public class ReadGraphImpl implements AsyncReadGraph { 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); @@ -1116,20 +1120,9 @@ public class ReadGraphImpl implements AsyncReadGraph { 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 @@ -1357,7 +1350,10 @@ public class ReadGraphImpl implements AsyncReadGraph { Statement stm = getSingleStatement(resource, relation); - return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz); + Object o = adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz); + if (clazz.isInstance(o)) + return (T)o; + throw new AdaptionException("Returned value is not expected class , got " + o.getClass().getName()+ " , expected " + clazz.getName()); } @@ -1519,15 +1515,15 @@ public class ReadGraphImpl implements AsyncReadGraph { try { - int result = processor.getSingleObject(this, subject, relation); - if(result == 0) return null; - - return processor.querySupport.getResource(result); + int result = processor.getSingleObject(this, subject, relation); + if(result == 0) return null; + + return processor.querySupport.getResource(result); + + } catch (ManyObjectsForFunctionalRelationException e) { - } catch (ManyObjectsForFunctionalRelationException e) { + throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation); - throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e); - } catch (DatabaseException e) { throw new ServiceException(e); @@ -1854,13 +1850,42 @@ public class ReadGraphImpl implements AsyncReadGraph { try { - Collection objects = getObjects(subject, relation); - return !objects.isEmpty(); + RelationInfo rinfo = processor.getRelationInfo(this, relation); + Collection predicates = getPredicates(subject); + + if (rinfo.isFinal) { + + return predicates.contains(relation); + + } else if (rinfo.isFunctional) { + + try { + return processor.getSingleObject(this, subject, relation) != 0; + } catch (ManyObjectsForFunctionalRelationException e) { + return true; + } catch (DatabaseException e) { + throw new ServiceException(e); + } + + } else { + + for (Resource predicate : getPredicates(subject)) { + if (isSubrelationOf(predicate, relation)) + return true; + } + + } + + return false; } catch (ServiceException e) { throw new ServiceException(e); + } catch (DatabaseException e) { + + throw new ServiceException(INTERNAL_ERROR_STRING, e); + } } @@ -1928,14 +1953,11 @@ public class ReadGraphImpl implements AsyncReadGraph { * Implementation of the interface RequestProcessor */ - @Override - public T syncRequest(final Read request) throws DatabaseException { - - assert (request != null); - - return QueryCache.resultReadEntry(this, request, parent, null, null); - - } + @Override + public T syncRequest(final Read request) throws DatabaseException { + assert (request != null); + return (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true); + } @Override public T syncRequest(Read request, SyncListener procedure) @@ -1949,16 +1971,12 @@ public class ReadGraphImpl implements AsyncReadGraph { return syncRequest(request, new NoneToAsyncListener(procedure)); } - @Override - public T syncRequest(final Read request, final AsyncProcedure procedure) throws DatabaseException { - - assert (request != null); - - ListenerBase listener = procedure != null ? getListenerBase(procedure) : null; - - return QueryCache.resultReadEntry(this, request, parent, listener, procedure); - - } + @Override + public T syncRequest(final Read request, final AsyncProcedure procedure) throws DatabaseException { + assert (request != null); + ListenerBase listener = procedure != null ? getListenerBase(procedure) : null; + return (T)QueryCache.runnerReadEntry(this, request, parent, listener, procedure, true); + } @Override public T syncRequest(final Read request, @@ -2012,10 +2030,7 @@ public class ReadGraphImpl implements AsyncReadGraph { throws DatabaseException { assert (request != null); - asyncBarrier = new AsyncBarrierImpl(null); - BlockingAsyncProcedure ap = new BlockingAsyncProcedure<>(this, null, request); - syncRequest(request, ap); - return ap.get(); + return syncRequest(request, new AsyncProcedureAdapter<>() ); } @@ -2037,36 +2052,13 @@ public class ReadGraphImpl implements AsyncReadGraph { return syncRequest(request, new NoneToAsyncListener(procedure)); } - @Override - final public T syncRequest(final AsyncRead request, - final AsyncProcedure procedure) throws DatabaseException { - - assert (request != null); - - ListenerBase listener = getListenerBase(procedure); - - BlockingAsyncProcedure ap = new BlockingAsyncProcedure<>(this, procedure, request); - - QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true); - - return ap.get(); - - } - - final private void syncRequest(final AsyncRead request, final AsyncReadProcedure procedure) throws DatabaseException { - - assert (request != null); - - ListenerBase listener = getListenerBase(procedure); - assert(listener == null); - - BlockingAsyncProcedure ap = new BlockingAsyncProcedure<>(this, procedure, request); - - QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true); - - ap.get(); - - } + @Override + final public T syncRequest(final AsyncRead request, + final AsyncProcedure procedure) throws DatabaseException { + assert (request != null); + ListenerBase listener = getListenerBase(procedure); + return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true); + } @Override public T syncRequest(AsyncRead request, @@ -3564,14 +3556,9 @@ public class ReadGraphImpl implements AsyncReadGraph { // 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 @@ -3591,6 +3578,24 @@ public class ReadGraphImpl implements AsyncReadGraph { }); } + + 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 void forValue(Resource subject, Binding binding, @@ -4285,14 +4290,9 @@ public class ReadGraphImpl implements AsyncReadGraph { 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 @@ -5174,16 +5174,23 @@ public class ReadGraphImpl implements AsyncReadGraph { assert (request != null); assert (procedure != null); - - processor.schedule(new SessionTask(false) { + + AsyncBarrierImpl barrier = asyncBarrier; + if(barrier != null) + barrier.inc(); + + processor.scheduleNow(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); } catch (DatabaseException e) { Logger.defaultLogError(e); + } finally { + if(barrier != null) + barrier.dec(); } } @@ -5192,7 +5199,7 @@ public class ReadGraphImpl implements AsyncReadGraph { } public static ReadGraphImpl createAsync(QueryProcessor support) { - return new ReadGraphImpl(null, support); + return new ReadGraphImpl(null, null, support); } @Override @@ -5248,28 +5255,55 @@ public class ReadGraphImpl implements AsyncReadGraph { asyncRequest(request, new NoneToAsyncListener(procedure)); } - @Override - final public void asyncRequest(final AsyncRead request, - final AsyncProcedure procedure) { + @Override + final public void asyncRequest(final AsyncRead request, + final AsyncProcedure procedure) { - assert (request != null); - assert (procedure != null); + assert (request != null); + assert (procedure != null); - processor.schedule(new SessionTask(false) { + AsyncBarrierImpl barrier = asyncBarrier; + if(barrier != null) + barrier.inc(); - @Override - public void run(int thread) { - try { - final ListenerBase listener = getListenerBase(procedure); - QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false); - } catch (DatabaseException e) { - Logger.defaultLogError(e); - } - } - - }); + processor.scheduleNow(new SessionTask(this) { - } + @Override + public void run0(int thread) { + + if(barrier != null) + barrier.inc(); + + try { + final ListenerBase listener = getListenerBase(procedure); + QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure() { + + @Override + public void execute(AsyncReadGraph graph, T result) { + procedure.execute(graph, result); + if(barrier != null) + barrier.dec(); + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + procedure.exception(graph, throwable); + if(barrier != null) + barrier.dec(); + } + + }, false); + } catch (DatabaseException e) { + LOGGER.error("Error while executing async request", e); + } finally { + if(barrier != null) + barrier.dec(); + } + } + + }); + + } @Override public void asyncRequest(AsyncRead request, @@ -5638,35 +5672,55 @@ public class ReadGraphImpl implements AsyncReadGraph { * 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 = prepareBarrier(parentGraph, parent, null, false); + } + + ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support, AsyncBarrierImpl asyncBarrier) { + this.parentGraph = parentGraph; + this.parent = parent; + this.processor = support; + this.asyncBarrier = asyncBarrier; + } + + ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) { + this(graph, parent, graph.processor); } + ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) { + this(parentGraph, parent, parentGraph.processor, prepareBarrier(parentGraph, parent, callback, needsToBlock)); + } + + static AsyncBarrierImpl prepareBarrier(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) { + return new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, callback, needsToBlock); + } + 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, Runnable callback, boolean needsToBlock) { + return new ReadGraphImpl(this, parent, callback, needsToBlock); } - 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) { + ReadGraphImpl result = new ReadGraphImpl(null, null, support); + return result; + } public ReadGraphImpl newRestart(ReadGraphImpl impl) { @@ -5677,11 +5731,6 @@ public class ReadGraphImpl implements AsyncReadGraph { } - 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; @@ -5938,6 +5987,8 @@ public class ReadGraphImpl implements AsyncReadGraph { } 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; @@ -6127,12 +6178,18 @@ public class ReadGraphImpl implements AsyncReadGraph { 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(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 { @@ -6197,11 +6254,12 @@ public class ReadGraphImpl implements AsyncReadGraph { @Override public 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.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); @@ -6210,12 +6268,13 @@ public class ReadGraphImpl implements AsyncReadGraph { @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.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); } @@ -6324,7 +6383,47 @@ public class ReadGraphImpl implements AsyncReadGraph { @Override public boolean performPending() { - return processor.performPending(processor.thread.get()); + return processor.performPending(this); + } + + public Set ancestorSet() { + HashSet result = new HashSet<>(); + ReadGraphImpl g = this; + while(g != null) { + result.add(g); + g = g.parentGraph; + } + 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 boolean isParent(ReadGraphImpl impl) { + if(impl == null) return false; + if(this == impl) return true; + return isParent(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); + } + + @SuppressWarnings("unchecked") + @Override + public T l0() { + return (T) processor.getL0(); } }