X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=inline;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fgraph%2FReadGraphImpl.java;h=ba39c57dfe65f5db752d9a1977a4bdd5cefa36db;hb=63bb6d595c37b3a2fb55e07fb810779cae3b4d03;hp=2ef40c29459d4be20009b30a73f54bd85b6fb08f;hpb=1efdd1cea0fcef60df0267f23e4ffda9fab5b23d;p=simantics%2Fplatform.git 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 2ef40c294..ba39c57df 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 @@ -188,6 +188,8 @@ import org.simantics.utils.DataContainer; 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; @@ -1923,7 +1925,12 @@ public class ReadGraphImpl implements AsyncReadGraph { @Override public T syncRequest(final Read 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 @@ -1943,9 +1950,11 @@ public class ReadGraphImpl implements AsyncReadGraph { 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; } @@ -2029,11 +2038,11 @@ public class ReadGraphImpl implements AsyncReadGraph { assert (request != null); + //ITask task = ThreadLogger.task(request); ListenerBase listener = getListenerBase(procedure); - -// BlockingAsyncProcedure 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; } @@ -5151,8 +5160,12 @@ public class ReadGraphImpl implements AsyncReadGraph { assert (request != null); assert (procedure != null); - - processor.schedule(new SessionTask(this) { + + AsyncBarrierImpl barrier = asyncBarrier; + if(barrier != null) + barrier.inc(); + + processor.scheduleNow(new SessionTask(this) { @Override public void run0(int thread) { @@ -5161,6 +5174,9 @@ public class ReadGraphImpl implements AsyncReadGraph { QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false); } catch (DatabaseException e) { Logger.defaultLogError(e); + } finally { + if(barrier != null) + barrier.dec(); } } @@ -5232,13 +5248,43 @@ public class ReadGraphImpl implements AsyncReadGraph { assert (request != null); assert (procedure != null); - processor.schedule(new SessionTask(this) { + //ITask task = ThreadLogger.task(request); + + AsyncBarrierImpl barrier = asyncBarrier; + if(barrier != null) + barrier.inc(); + + processor.scheduleNow(new SessionTask(this) { @Override public void run0(int thread) { - try { + + if(barrier != null) + barrier.inc(); + + try { final ListenerBase listener = getListenerBase(procedure); - QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false); + QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure() { + + @Override + public void execute(AsyncReadGraph graph, T result) { + //task.finish(); + procedure.execute(graph, result); + if(barrier != null) + barrier.dec(); + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + //task.finish(); + procedure.exception(graph, throwable); + if(barrier != null) + barrier.dec(); + } + + }, false); + if(barrier != null) + barrier.dec(); } catch (DatabaseException e) { Logger.defaultLogError(e); } @@ -5626,27 +5672,47 @@ public class ReadGraphImpl implements AsyncReadGraph { this.parentGraph = parentGraph; this.parent = parent; this.processor = support; - this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent); + this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, null); } + 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) { + this(parentGraph, parent, parentGraph.processor, new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, callback)); + } + ReadGraphImpl(ReadGraphImpl graph) { this(graph, graph.parent); } - public ReadGraphImpl withParent(CacheEntry parent) { - return new ReadGraphImpl(this, parent); + public ReadGraphImpl withParent(CacheEntry parent, Runnable callback) { + return new ReadGraphImpl(this, parent, callback); } + public ReadGraphImpl withParent(CacheEntry parent) { + return withParent(parent, null); + } + + public ReadGraphImpl syncWithParent(CacheEntry parent) { + return new ReadGraphImpl(this, parent, processor, null); + } + public ReadGraphImpl forRecompute(CacheEntry parent) { return new ReadGraphImpl(null, parent, processor); } public static ReadGraphImpl create(QueryProcessor support) { - return new ReadGraphImpl(null, null, support); + ReadGraphImpl result = new ReadGraphImpl(null, null, support); + return result; } public ReadGraphImpl newRestart(ReadGraphImpl impl) { @@ -5914,6 +5980,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; @@ -6179,11 +6247,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); @@ -6192,12 +6261,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); } @@ -6308,7 +6378,7 @@ public class ReadGraphImpl implements AsyncReadGraph { public boolean performPending() { return processor.performPending(this); } - + public Set ancestorSet() { HashSet result = new HashSet<>(); ReadGraphImpl g = this; @@ -6318,5 +6388,29 @@ public class ReadGraphImpl implements AsyncReadGraph { } 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); + } }