X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fgraph%2FReadGraphImpl.java;h=f0147e047e001cf6f82d809a269aec65e100ed13;hb=68ce0966a57f5153b133c6283fdbae10f683b745;hp=e3e7d50ecbc04998d62a650a9695ce939b6667ac;hpb=ded784594eb0e1fb318fbb931135288152691cf2;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 e3e7d50ec..f0147e047 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 @@ -88,6 +88,7 @@ import org.simantics.db.common.primitiverequest.UniqueAdapter; 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.BlockingAsyncProcedure; import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter; import org.simantics.db.common.procedure.adapter.ProcedureAdapter; import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener; @@ -112,6 +113,7 @@ import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure; import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure; import org.simantics.db.common.request.AdaptValue; import org.simantics.db.common.request.ResourceRead; +import org.simantics.db.common.utils.Functions; import org.simantics.db.common.utils.Logger; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.common.validation.L0Validations; @@ -135,13 +137,16 @@ import org.simantics.db.impl.ResourceImpl; import org.simantics.db.impl.internal.RandomAccessValueSupport; import org.simantics.db.impl.internal.ResourceData; import org.simantics.db.impl.procedure.CallWrappedSingleQueryProcedure4; +import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.impl.procedure.ResultCallWrappedQueryProcedure4; import org.simantics.db.impl.procedure.ResultCallWrappedSingleQueryProcedure4; import org.simantics.db.impl.query.CacheEntry; import org.simantics.db.impl.query.QueryCache; +import org.simantics.db.impl.query.QueryCacheBase; import org.simantics.db.impl.query.QueryProcessor; import org.simantics.db.impl.query.QuerySupport; import org.simantics.db.impl.query.TripleIntProcedure; +import org.simantics.db.impl.query.QueryProcessor.SessionTask; import org.simantics.db.impl.support.ResourceSupport; import org.simantics.db.procedure.AsyncListener; import org.simantics.db.procedure.AsyncMultiListener; @@ -179,6 +184,7 @@ import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException; import org.simantics.scl.reflection.ReflectionUtils; import org.simantics.scl.reflection.ValueNotFoundException; +import org.simantics.scl.runtime.function.Function1; import org.simantics.scl.runtime.function.Function3; import org.simantics.utils.DataContainer; import org.simantics.utils.Development; @@ -280,8 +286,17 @@ public class ReadGraphImpl implements ReadGraph { try { - return syncRequest(new org.simantics.db.common.primitiverequest.Resource( - id)); +// assert (id != null); +// assert (procedure != null); +// +// processor.forResource(this, id, procedure); +// +//// return syncRequest(new org.simantics.db.common.primitiverequest.Resource( +//// id)); + + Integer rid = QueryCache.resultURIToResource(this, id, parent, null); + if(rid == 0) throw new ResourceNotFoundException(id); + return processor.querySupport.getResource(rid); } catch (ResourceNotFoundException e) { @@ -311,8 +326,10 @@ public class ReadGraphImpl implements ReadGraph { try { - return syncRequest(new org.simantics.db.common.primitiverequest.Resource( - id)); + return getResource(id); + +// return syncRequest(new org.simantics.db.common.primitiverequest.Resource( +// id)); } catch (ResourceNotFoundException e) { @@ -2031,7 +2048,7 @@ public class ReadGraphImpl implements ReadGraph { throw (DatabaseException) exception; else throw new DatabaseException( - "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)", + "Unexpected exception in ReadGraph.syncRequest(AsyncRead)", exception); } } @@ -2047,10 +2064,12 @@ public class ReadGraphImpl implements ReadGraph { throws DatabaseException { assert (request != null); - AsyncReadProcedure procedure = new AsyncReadProcedure(); - syncRequest(request, procedure); - procedure.checkAndThrow(); - return procedure.result; +// AsyncReadProcedure procedure = new AsyncReadProcedure(); + BlockingAsyncProcedure ap = new BlockingAsyncProcedure<>(null, request); + syncRequest(request, ap); + return ap.get(); +// procedure.checkAndThrow(); +// return procedure.result; // return syncRequest(request, new AsyncProcedureAdapter()); @@ -2082,14 +2101,18 @@ public class ReadGraphImpl implements ReadGraph { ListenerBase listener = getListenerBase(procedure); - final ResultCallWrappedSingleQueryProcedure4 wrapper = new ResultCallWrappedSingleQueryProcedure4( - procedure, request); + BlockingAsyncProcedure ap = new BlockingAsyncProcedure<>(procedure, request); - QueryCache.runnerAsyncReadEntry(this, request, parent, listener, wrapper); +// final ResultCallWrappedSingleQueryProcedure4 wrapper = new ResultCallWrappedSingleQueryProcedure4( +// procedure, request); + + QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true); //processor.query(this, request, parent, wrapper, listener); - return wrapper.getResult(); + return ap.get(); + +// return wrapper.getResult(); // if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) { // @@ -2157,10 +2180,14 @@ public class ReadGraphImpl implements ReadGraph { ListenerBase listener = getListenerBase(procedure); assert(listener == null); + BlockingAsyncProcedure ap = new BlockingAsyncProcedure<>(procedure, request); + // final ResultCallWrappedSingleQueryProcedure4 wrapper = new ResultCallWrappedSingleQueryProcedure4( // procedure, request); - QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure); + QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true); + + ap.get(); } @@ -2526,28 +2553,38 @@ public class ReadGraphImpl implements ReadGraph { final public T syncRequest(final ExternalRead request, final Procedure procedure) throws DatabaseException { - assert (request != null); - - ListenerBase listener = getListenerBase(procedure); - - final DataContainer exception = new DataContainer(); - final DataContainer result = new DataContainer(); - - processor.query(this, request, parent, new Procedure() { - - @Override - public void exception(Throwable throwable) { - exception.set(throwable); - procedure.exception(throwable); - } + assert (request != null); - @Override - public void execute(T t) { - result.set(t); - procedure.execute(t); - } + ListenerBase listener = procedure != null ? getListenerBase(procedure) : null; + return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure); - }, listener); +// +// ListenerBase listener = getListenerBase(procedure); +// +// return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure); + +// assert (request != null); +// +// ListenerBase listener = getListenerBase(procedure); +// +// final DataContainer exception = new DataContainer(); +// final DataContainer result = new DataContainer(); +// +// processor.query(this, request, parent, new Procedure() { +// +// @Override +// public void exception(Throwable throwable) { +// exception.set(throwable); +// procedure.exception(throwable); +// } +// +// @Override +// public void execute(T t) { +// result.set(t); +// procedure.execute(t); +// } +// +// }, listener); @@ -2581,18 +2618,18 @@ public class ReadGraphImpl implements ReadGraph { // } // // } - - Throwable t = exception.get(); - if (t != null) { - if (t instanceof DatabaseException) - throw (DatabaseException) t; - else - throw new DatabaseException( - "Unexpected exception in ReadGraph.syncRequest(Read)", - t); - } - - return result.get(); +// +// Throwable t = exception.get(); +// if (t != null) { +// if (t instanceof DatabaseException) +// throw (DatabaseException) t; +// else +// throw new DatabaseException( +// "Unexpected exception in ReadGraph.syncRequest(Read)", +// t); +// } +// +// return result.get(); } @@ -5387,47 +5424,64 @@ public class ReadGraphImpl implements ReadGraph { assert (request != null); assert (procedure != null); + + processor.schedule(Integer.MIN_VALUE, new SessionTask(request, processor.THREAD_MASK+1, -1) { - final ListenerBase listener = getListenerBase(procedure); - - if (parent != null || listener != null) { - - try { - QueryCache.runnerReadEntry(this, request, parent, listener, procedure); - //processor.query(this, request, parent, procedure,listener); - } catch (DatabaseException e) { - Logger.defaultLogError(e); - // This throwable has already been transferred to procedure at this point - do nothing about it - // - } - - } else { - -// final ReadGraphImpl newGraph = newSync(); - - try { - - T result = request.perform(this); - - try { - procedure.execute(this, result); - } catch (Throwable t) { - Logger.defaultLogError(t); - } - - } catch (Throwable t) { - + @Override + public void run(int thread) { try { - procedure.exception(this, t); - } catch (Throwable t2) { - Logger.defaultLogError(t2); + final ListenerBase listener = getListenerBase(procedure); + QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false); + } catch (DatabaseException e) { + Logger.defaultLogError(e); } - - } finally { - } + + }); - } + +// quer +// +// final ListenerBase listener = getListenerBase(procedure); +// +// if (parent != null || listener != null) { +// +// try { +// QueryCache.runnerReadEntry(this, request, parent, listener, procedure); +// //processor.query(this, request, parent, procedure,listener); +// } catch (DatabaseException e) { +// Logger.defaultLogError(e); +// // This throwable has already been transferred to procedure at this point - do nothing about it +// // +// } +// +// } else { +// +//// final ReadGraphImpl newGraph = newSync(); +// +// try { +// +// T result = request.perform(this); +// +// try { +// procedure.execute(this, result); +// } catch (Throwable t) { +// Logger.defaultLogError(t); +// } +// +// } catch (Throwable t) { +// +// try { +// procedure.exception(this, t); +// } catch (Throwable t2) { +// Logger.defaultLogError(t2); +// } +// +// } finally { +// +// } +// +// } } @@ -5499,38 +5553,55 @@ public class ReadGraphImpl implements ReadGraph { assert (request != null); assert (procedure != null); - final ListenerBase listener = getListenerBase(procedure); + //final ListenerBase listener = getListenerBase(procedure); - if (parent != null || listener != null) { + processor.schedule(Integer.MIN_VALUE, new SessionTask(request, processor.THREAD_MASK+1, -1) { - try { - QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure); - //processor.query(this, request, parent, procedure, listener); - } catch (DatabaseException e) { - Logger.defaultLogError(e); + @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); + } } + + }); - } else { - - try { - - request.perform(this, new CallWrappedSingleQueryProcedure4(procedure, request)); - - } catch (Throwable t) { - - if (t instanceof DatabaseException) - procedure.exception(this, t); - else - procedure - .exception( - this, - new DatabaseException( - "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)", - t)); + - } - } +// if (parent != null || listener != null) { +// +// try { +// QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure); +// //processor.query(this, request, parent, procedure, listener); +// } catch (DatabaseException e) { +// Logger.defaultLogError(e); +// } +// +// } else { +// +// try { +// +// request.perform(this, new CallWrappedSingleQueryProcedure4(procedure, request)); +// +// } catch (Throwable t) { +// +// if (t instanceof DatabaseException) +// procedure.exception(this, t); +// else +// procedure +// .exception( +// this, +// new DatabaseException( +// "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)", +// t)); +// +// } +// +// } } @@ -5761,56 +5832,125 @@ public class ReadGraphImpl implements ReadGraph { final public void asyncRequest(final ExternalRead request, final Procedure procedure) { - assert (request != null); - assert (procedure != null); - - ListenerBase listener = getListenerBase(procedure); - - if (parent != null || listener != null) { - - processor.query(this, request, parent, procedure, listener); + + assert (request != null); + assert (procedure != null); - } else { + final ListenerBase listener = getListenerBase(procedure); - try { + if (parent != null || listener != null) { - request.register(this, new Listener() { - - @Override - public void execute(T result) { - procedure.execute(result); - } - - @Override - public void exception(Throwable t) { - procedure.exception(t); - } + try { + QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure); + //processor.query(this, request, parent, procedure,listener); + } catch (DatabaseException e) { + Logger.defaultLogError(e); + // This throwable has already been transferred to procedure at this point - do nothing about it + // + } + + } else { - @Override - public String toString() { - return "asyncRequest(PrimitiveRead) -> " + request; - } + request.register(this, new Listener() { + // + @Override + public void execute(T result) { + procedure.execute(result); + } - @Override - public boolean isDisposed() { - return true; - } + @Override + public void exception(Throwable t) { + procedure.exception(t); + } - }); + @Override + public String toString() { + return "asyncRequest(PrimitiveRead) -> " + request; + } - } catch (Throwable t) { + @Override + public boolean isDisposed() { + return true; + } - if (t instanceof DatabaseException) - procedure.exception(t); - else - procedure - .exception(new DatabaseException( - "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)", - t)); + }); + +// final ReadGraphImpl newGraph = newSync(); - } +// try { +// +// T result = request.perform(this); +// +// try { +// procedure.execute(this, result); +// } catch (Throwable t) { +// Logger.defaultLogError(t); +// } +// +// } catch (Throwable t) { +// +// try { +// procedure.exception(this, t); +// } catch (Throwable t2) { +// Logger.defaultLogError(t2); +// } +// +// } finally { +// +// } - } + } + +// assert (request != null); +// assert (procedure != null); +// +// ListenerBase listener = getListenerBase(procedure); +// +// if (parent != null || listener != null) { +// +// processor.query(this, request, parent, procedure, listener); +// +// } else { +// +// try { +// +// request.register(this, new Listener() { +// +// @Override +// public void execute(T result) { +// procedure.execute(result); +// } +// +// @Override +// public void exception(Throwable t) { +// procedure.exception(t); +// } +// +// @Override +// public String toString() { +// return "asyncRequest(PrimitiveRead) -> " + request; +// } +// +// @Override +// public boolean isDisposed() { +// return true; +// } +// +// }); +// +// } catch (Throwable t) { +// +// if (t instanceof DatabaseException) +// procedure.exception(t); +// else +// procedure +// .exception(new DatabaseException( +// "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)", +// t)); +// +// } +// +// } } @@ -6371,6 +6511,9 @@ public class ReadGraphImpl implements ReadGraph { return getValue(r); } } + else if(types.contains(L0.Function)) { + return Functions.exec(this, r, this, r, context); + } else if(types.contains(L0.ExternalValue)) { return (T)syncRequest(new AdaptValue(r), TransientCacheListener.instance()); } @@ -6401,6 +6544,14 @@ public class ReadGraphImpl implements ReadGraph { return getVariantValue(r); } } + else if(types.contains(L0.Function)) { + Object value = Functions.exec(this, r, this, r, context); + try { + return new Variant(Bindings.OBJECT.getContentBinding(value), value); + } catch ( org.simantics.databoard.binding.error.BindingException e ) { + throw new BindingException( "No binding found for class " + value.getClass().getName(), e ); + } + } else if(types.contains(L0.ExternalValue)) { Object value = syncRequest(new AdaptValue(r), TransientCacheListener.instance()); try { @@ -6511,6 +6662,21 @@ public class ReadGraphImpl implements ReadGraph { } else { return getValue(r, binding); } + } else if(types.contains(L0.Function)) { + Object value = Functions.exec(this, r, this, r, context); + if(binding.isInstance(value)) return (T)value; + try { + Binding srcBinding = Bindings.OBJECT.getContentBinding(value); + return (T)Bindings.adapt(value, srcBinding, binding); + } catch(RuntimeException e) { + DatabaseException dte = findPossibleRootException(e); + if(dte != null) throw dte; + else throw new DatabaseException(e); + } catch (AdaptException e) { + throw new DatabaseException(e); + } catch (org.simantics.databoard.binding.error.BindingException e) { + throw new DatabaseException(e); + } } else if(types.contains(L0.ExternalValue)) { try { ExternalValue ev = adapt(r, ExternalValue.class); @@ -6662,16 +6828,22 @@ public class ReadGraphImpl implements ReadGraph { } } + private static ThreadLocal syncGraph = new ThreadLocal() { + protected Boolean initialValue() { + return true; + } + }; + @Override public boolean setSynchronous(boolean value) { - boolean old = processor.synch; - processor.synch = value; + boolean old = getSynchronous(); + syncGraph.set(value); return old; } @Override public boolean getSynchronous() { - return processor.synch; + return syncGraph.get(); } public void ensureLoaded(int resource) {