import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
+import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
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;
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.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;
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) {
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) {
}
}
+
+ @Override
+ public Map<String, Resource> getChildren(Resource resource) throws ValidationException, ServiceException {
+
+ assert (resource != null);
+
+ try {
+
+ int rId = processor.querySupport.getId(resource);
+ return QueryCache.resultChildMap(this, rId, parent, null);
+
+ } catch (ValidationException e) {
+
+ throw new ValidationException(e);
+
+ } catch (ServiceException e) {
+
+ throw new ServiceException(e);
+
+ } catch (DatabaseException e) {
+
+ throw new ServiceException(INTERNAL_ERROR_STRING, e);
+
+ }
+
+ }
final public Resource getRootLibrary() {
return processor.getRootLibraryResource();
throws DatabaseException {
assert (request != null);
- AsyncReadProcedure<T> procedure = new AsyncReadProcedure<T>();
- syncRequest(request, procedure);
- procedure.checkAndThrow();
- return procedure.result;
+// AsyncReadProcedure<T> procedure = new AsyncReadProcedure<T>();
+ BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, null, request);
+ syncRequest(request, ap);
+ return ap.get();
+// procedure.checkAndThrow();
+// return procedure.result;
// return syncRequest(request, new AsyncProcedureAdapter<T>());
ListenerBase listener = getListenerBase(procedure);
- final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
- procedure, request);
+ BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
+
+// final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
+// procedure, request);
- QueryCache.runnerAsyncReadEntry(this, request, parent, listener, wrapper);
+ 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)) {
//
ListenerBase listener = getListenerBase(procedure);
assert(listener == null);
+ BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
+
// final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
// procedure, request);
- QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure);
+ QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
+
+ ap.get();
}
return processor.getSession();
}
-// @Override
-// final public Builtins getBuiltins() {
-// return processor.getSession().getBuiltins();
-// }
-
@Override
public <T> void asyncRequest(final Read<T> request) {
assert (request != null);
assert (procedure != null);
+
+ processor.schedule(new SessionTask(false) {
- 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 {
-
}
-
- }
+
+ });
}
return new ReadGraphImpl(null, support);
}
- public static ReadGraphImpl forRecompute(CacheEntry entry, QueryProcessor support) {
- return new ReadGraphImpl(entry, support);
- }
-
@Override
public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
assert (request != null);
assert (procedure != null);
- final ListenerBase listener = getListenerBase(procedure);
-
- 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<T>(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));
+ processor.schedule(new SessionTask(false) {
+ @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);
+ }
}
-
- }
+
+ });
}
return thread == Integer.MIN_VALUE;
}
-// final private boolean isSync(int thread) {
-// return thread < -1 && thread > Integer.MIN_VALUE;
-// }
-
ReadGraphImpl(ReadGraphImpl graph) {
this(graph.parent, graph.processor);
}
ReadGraphImpl(CacheEntry parent, QueryProcessor support) {
-// this.state = new ReadGraphState(barrier, support);
this.parent = parent;
this.processor = support;
}
- ReadGraphImpl(final QueryProcessor support) {
-
-// this.state = state;
- this.processor = support;
- this.parent = null;
-
- }
-
-// public static ReadGraphImpl createSync(int syncThread, Object syncParent,
-// ReadGraphSupportImpl support) {
-// return new ReadGraphImpl(syncThread, syncThread, syncParent, null,
-// support, new AsyncBarrierImpl(null));
-// }
-
public static ReadGraphImpl create(QueryProcessor support) {
- return new ReadGraphImpl(support);
- }
-
-// public ReadGraphImpl newAsync() {
-// return this;
-//// if(!state.synchronizedExecution) {
-//// return this;
-//// } else {
-//// return new ReadGraphImpl(false, parent, state.support, state.barrier);
-//// }
-// }
-
-// public ReadGraphImpl newSync() {
-// return new ReadGraphImpl(parent, processor);
-// }
-
- public ReadGraphImpl newSync(CacheEntry parentEntry) {
- return new ReadGraphImpl(parentEntry, processor);
+ return new ReadGraphImpl(null, support);
}
public ReadGraphImpl newRestart(ReadGraphImpl impl) {
WriteGraphImpl write = processor.getSession().getService(
WriteGraphImpl.class);
-// if (write.callerThread != impl.callerThread)
-// return new WriteGraphImpl(impl.callerThread, parent, state.support, write.writeSupport, write.provider, write.state.barrier);
return write;
}
-// public ReadGraphImpl newSync(Object parentRequest) {
-// return new ReadGraphImpl(callerThread, state.parent, state.support, new AsyncBarrierImpl(state.barrier));
-// }
-
-// public ReadGraphImpl newSync(final int callerThread, Object parentRequest) {
-// assert (state.syncThread == callerThread || (state.syncThread == Integer.MIN_VALUE && callerThread != Integer.MIN_VALUE));
-// return new ReadGraphImpl(callerThread, callerThread, parentRequest,
-// state.parent, state.support, new AsyncBarrierImpl(state.barrier));
-// }
-//
-// public ReadGraphImpl newSyncAsync(Object parentRequest) {
-//// assert (callerThread < 0);
-// return new ReadGraphImpl(callerThread, state.syncThread, parentRequest,
-// state.parent, state.support, new AsyncBarrierImpl(state.barrier));
-// }
-//
-// public ReadGraphImpl newSyncAsync(final int callerThread,
-// Object parentRequest) {
-//// assert (callerThread < 0);
-// // assert(state.syncThread == callerThread || (state.syncThread == Integer.MIN_VALUE
-// // && callerThread != Integer.MIN_VALUE) );
-// return new ReadGraphImpl(callerThread, callerThread, parentRequest,
-// state.parent, state.support, new AsyncBarrierImpl(state.barrier));
-// }
-
- public ReadGraphImpl withAsyncParent(CacheEntry parent) {
- return new ReadGraphImpl(parent, processor);
- }
-
public ReadGraphImpl withParent(CacheEntry parent) {
if(parent == this.parent) return this;
else return new ReadGraphImpl(parent, processor);
assert(procedure.done());
-// while (!procedure.done()) {
-//
-// boolean executed = processor.resumeTasks(callerThread, null, null);
-// if (!executed) {
-// try {
-// Thread.sleep(1);
-// // sema.tryAcquire(1, TimeUnit.MILLISECONDS);
-// } catch (InterruptedException e) {
-// e.printStackTrace();
-// }
-// }
-//
-// }
-
}
public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
assert(procedure.done());
-// while (!procedure.done()) {
-//
-// boolean executed = processor.processor.resume(this);
-// if (!executed) {
-// try {
-// Thread.sleep(1);
-// // sema.tryAcquire(1, TimeUnit.MILLISECONDS);
-// } catch (InterruptedException e) {
-// e.printStackTrace();
-// }
-// }
-//
-// }
-
}
-
-// public void waitAsync(Object request) {
-// try {
-// state.barrier.waitBarrier(request, this);
-// } catch (Throwable t) {
-// t.printStackTrace();
-// processor.scanPending();
-// processor.querySupport.checkTasks();
-// throw new RuntimeDatabaseException(t);
-// }
-// }
-
-// public void restart() {
-// state.barrier.restart();
-// }
public boolean resumeTasks() {
return processor.resumeTasks(this);
public Object getModificationCounter() {
return processor.getSession().getModificationCounter();
}
+
+ @Override
+ public boolean performPending() {
+ return processor.performPending(processor.thread.get());
+ }
}