import org.simantics.db.layer0.variable.Variables;
import org.simantics.db.request.Read;
import org.simantics.db.service.ClusterControl;
+import org.simantics.db.service.QueryControl;
import org.simantics.db.service.SerialisationSupport;
import org.simantics.db.service.VirtualGraphSupport;
import org.simantics.layer0.utils.triggers.IActivationManager;
import org.simantics.scl.runtime.SCLContext;
import org.simantics.scl.runtime.function.Function;
import org.simantics.scl.runtime.function.Function1;
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;
import org.simantics.scl.runtime.tuple.Tuple;
import org.simantics.scl.runtime.tuple.Tuple0;
import org.simantics.utils.DataContainer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
@SuppressWarnings({"rawtypes", "unchecked"})
public class SCLFunctions {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SCLFunctions.class);
+
public static final String GRAPH = "graph";
public static <T> T safeExec(final Function f) {
try {
return (T)f.apply(Tuple0.INSTANCE);
} catch (Throwable t) {
- t.printStackTrace();
+ LOGGER.error("safeExec caught exception", t);
return null;
}
}
public static void asyncWrite(final Function f) throws DatabaseException {
SCLContext context = SCLContext.createDerivedContext();
- Simantics.getSession().asyncRequest(new WriteRequest() {
- @Override
- public void perform(WriteGraph graph) throws DatabaseException {
- SCLContext.push(context);
- context.put(GRAPH, graph);
- try {
- f.apply(Tuple0.INSTANCE);
- } finally {
- SCLContext.pop();
+ if (Simantics.peekSession() != null) {
+ Simantics.getSession().asyncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ SCLContext.push(context);
+ context.put(GRAPH, graph);
+ try {
+ f.apply(Tuple0.INSTANCE);
+ } finally {
+ SCLContext.pop();
+ }
}
- }
- });
+ });
+ } else {
+ LOGGER.warn("No session available for asynchronous write requests");
+ }
}
public static <T> T syncWrite(final Function f) throws DatabaseException {
if (graph != null) {
return (T)f.apply(Tuple0.INSTANCE);
} else {
+ final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
return Simantics.getSession().syncRequest(new WriteResultRequest<T>() {
@Override
public T perform(WriteGraph graph) throws DatabaseException {
SCLContext.push(context);
+ SCLReportingHandler oldPrinter = (SCLReportingHandler)context.put(SCLReportingHandler.REPORTING_HANDLER, printer);
ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
try {
return (T)f.apply(Tuple0.INSTANCE);
} finally {
context.put(GRAPH, oldGraph);
+ context.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
SCLContext.pop();
}
}
}
public static void subqueryL(ReadGraph graph, Function query, Function executeCallback, Function1<Throwable, Tuple> exceptionCallback, Function1<Tuple0, Boolean> isDisposedCallback) throws DatabaseException {
- graph.asyncRequest(new Subquery(query), new SyncListenerAdapter<Object>() {
+ graph.syncRequest(new Subquery(query), new SyncListenerAdapter<Object>() {
@Override
public void execute(ReadGraph graph, Object result) throws DatabaseException {
Simantics.applySCLRead(graph, executeCallback, result);
return value;
}
+ public static void restrictQueries(ReadGraph graph, int amount, int step, int maxTimeInMs) {
+
+ QueryControl qc = graph.getService(QueryControl.class);
+ long start = System.currentTimeMillis();
+ while(true) {
+ int current = qc.count();
+ if(current < amount) return;
+ qc.gc(graph, step);
+ long duration = System.currentTimeMillis() - start;
+ if(duration > maxTimeInMs) return;
+ }
+
+ }
+
+ public static int countQueries(ReadGraph graph) {
+
+ QueryControl qc = graph.getService(QueryControl.class);
+ return qc.count();
+
+ }
+
}