From 9d0403a59f44e89cf7def60fe7439923f786befb Mon Sep 17 00:00:00 2001 From: Jonathan Shook Date: Fri, 15 May 2020 11:26:24 -0500 Subject: [PATCH] polyglot mode now does full type introspection --- .../core/PolyglotScenarioController.java | 130 ++++++++++++++++-- 1 file changed, 118 insertions(+), 12 deletions(-) diff --git a/engine-core/src/main/java/io/nosqlbench/engine/core/PolyglotScenarioController.java b/engine-core/src/main/java/io/nosqlbench/engine/core/PolyglotScenarioController.java index b94f6a2a7..222f35988 100644 --- a/engine-core/src/main/java/io/nosqlbench/engine/core/PolyglotScenarioController.java +++ b/engine-core/src/main/java/io/nosqlbench/engine/core/PolyglotScenarioController.java @@ -4,6 +4,7 @@ import io.nosqlbench.engine.api.activityimpl.ActivityDef; import org.graalvm.polyglot.Value; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.xml.sax.helpers.AttributesImpl; import java.security.InvalidParameterException; import java.util.Map; @@ -23,11 +24,45 @@ public class PolyglotScenarioController { // int timeout = vtimeout.asInt(); // run(timeout, vspec); // } - public synchronized void run(Value v) { - run(Integer.MAX_VALUE, v); +// +// public synchronized void run(Map map) { +// controller.run(map); +// } +// +// public synchronized void run(int timeout, Map map) { +// controller.run(timeout, map); +// } +// + + public synchronized void run(Object o) { + if (o instanceof Value) { + runValue((Value) o); + } else if (o instanceof Map) { + controller.run((Map) o); + } else if (o instanceof String) { + controller.run(o.toString()); + } else { + throw new RuntimeException("Unrecognized type: " + o.getClass().getCanonicalName()); + } } - public synchronized void run(int timeout, Value spec) { + public synchronized void run(int timeout, Object o) { + if (o instanceof Value) { + runValue(timeout, (Value) o); + } else if (o instanceof Map) { + controller.run(timeout, (Map) o); + } else if (o instanceof String) { + controller.run(timeout, o.toString()); + } else { + throw new RuntimeException("Uncrecognized type: " + o.getClass().getCanonicalName()); + } + } + + private synchronized void runValue(Value v) { + runValue(Integer.MAX_VALUE, v); + } + + private synchronized void runValue(int timeout, Value spec) { logger.debug("run(Value) called with:" + spec); if (spec.isHostObject()) { controller.run(timeout, (ActivityDef) spec.asHostObject()); @@ -47,7 +82,20 @@ public class PolyglotScenarioController { } } - public synchronized void start(Value spec) { + + public synchronized void start(Object o) { + if (o instanceof Value) { + startValue((Value) o); + } else if (o instanceof Map) { + controller.start((Map) o); + } else if (o instanceof String) { + controller.start(o.toString()); + } else { + throw new RuntimeException("unrecognized type " + o.getClass().getCanonicalName()); + } + } + + private synchronized void startValue(Value spec) { if (spec.isHostObject()) { controller.start((ActivityDef) spec.asHostObject()); } else if (spec.isString()) { @@ -59,7 +107,19 @@ public class PolyglotScenarioController { } } - public synchronized void stop(Value spec) { + public synchronized void stop(Object o) { + if (o instanceof Value) { + stopValue((Value) o); + } else if (o instanceof Map) { + controller.stop((Map) o); + } else if (o instanceof String) { + controller.stop(o.toString()); + } else { + throw new RuntimeException("unknown type " + o.getClass().getCanonicalName()); + } + } + + private synchronized void stopValue(Value spec) { if (spec.isHostObject()) { controller.stop((ActivityDef) spec.asHostObject()); } else if (spec.isString()) { @@ -71,15 +131,37 @@ public class PolyglotScenarioController { } } - public synchronized void apply(Value spec) { + public synchronized void apply(Object o) { + if (o instanceof Value) { + applyValue((Value) o); + } else if (o instanceof Map) { + controller.apply((Map) o); + } else { + throw new RuntimeException("unknown type: " + o.getClass().getCanonicalName()); + } + } + + private synchronized void applyValue(Value spec) { Map map = spec.as(Map.class); controller.apply(map); } - public synchronized void await(Value spec) { - awaitActivity(spec); + public synchronized void awaitActivity(Object o) { + this.await(o); } - public synchronized void awaitActivity(Value spec) { + public synchronized void await(Object o) { + if (o instanceof String) { + controller.await(o.toString()); + } else if (o instanceof Value) { + awaitValue((Value) o); + } else if (o instanceof Map) { + controller.await((Map) o); + } else { + throw new RuntimeException("unknown type: " + o.getClass().getCanonicalName()); + } + } + + private synchronized void awaitValue(Value spec) { if (spec.isHostObject()) { controller.await((ActivityDef) spec.asHostObject()); } else if (spec.hasMembers()) { @@ -91,10 +173,22 @@ public class PolyglotScenarioController { } } - public synchronized void waitMillis(Value spec) { + public synchronized void waitMillis(Object o) { + if (o instanceof Value) { + waitMillisValue((Value) o); + } else if (o instanceof Integer) { + controller.waitMillis((Integer) o); + } else if (o instanceof Long) { + controller.waitMillis((Long) o); + } else { + throw new RuntimeException("unknown type: " + o.getClass().getCanonicalName()); + } + } + + private synchronized void waitMillisValue(Value spec) { if (spec.isString()) { controller.waitMillis(Long.parseLong(spec.asString())); - } else if (spec.isNumber()){ + } else if (spec.isNumber()) { controller.waitMillis(spec.asLong()); } else { throw new InvalidParameterException( @@ -102,7 +196,19 @@ public class PolyglotScenarioController { } } - public synchronized boolean isRunningActivity(Value spec) { + public synchronized boolean isRunningActivity(Object o) { + if (o instanceof Value) { + return isRunningActivityValue((Value) o); + } else if (o instanceof String) { + return controller.isRunningActivity(o.toString()); + } else if (o instanceof Map) { + return controller.isRunningActivity((Map) o); + } else { + throw new RuntimeException("unknown type:" + o.getClass().getCanonicalName()); + } + } + + private synchronized boolean isRunningActivityValue(Value spec) { if (spec.isHostObject()) { return controller.isRunningActivity((ActivityDef) spec.asHostObject()); } else if (spec.isString()) {