allow for 'UNSET' on overrides

This commit is contained in:
Jonathan Shook 2020-07-20 22:38:23 -05:00
parent 2091c5ad5a
commit 6c1b92e667
3 changed files with 53 additions and 5 deletions

View File

@ -18,16 +18,23 @@
package io.nosqlbench.engine.core.script;
import ch.qos.logback.classic.Logger;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.nosqlbench.nb.api.errors.BasicError;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Wrap the script parameters in a type which allows for easy manipulation
*/
public class ScriptParams extends HashMap<String, String> {
private static final Logger logger = (Logger) LoggerFactory.getLogger(ScriptParams.class);
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
public Map<String,String> withOverrides(Map<String, String> overrides) {
if (overrides == null) {
@ -36,9 +43,22 @@ public class ScriptParams extends HashMap<String, String> {
}
checkForNulls("params", "calling withOverrides", this);
checkForNulls("overrides", "calling withOverrides", overrides);
HashMap<String, String> result = new HashMap<>();
ScriptParams result = new ScriptParams();
result.putAll(this);
result.putAll(overrides);
for (Entry<String, String> overrideEntry : overrides.entrySet()) {
String oKey = overrideEntry.getKey();
String oVal = overrideEntry.getValue();
if (oVal.toUpperCase().endsWith("UNDEF")) {
String removed = result.remove(oKey);
logger.trace("Removed key '" + oKey + "': '" + removed + "' from script params because it was "+ oVal + " in overrides");
} else {
String was = result.get(oKey);
was = (was==null ? "NULL" : was);
result.put(oKey,oVal);
logger.trace("Overrode key '" + oKey + "': from '" + was + " to " + oVal);
}
}
return result;
}
@ -47,7 +67,7 @@ public class ScriptParams extends HashMap<String, String> {
logger.warn("A null map was provided to withDefaults. This could be a bug in your script.");
defaults=Map.of();
}
HashMap<String, String> result = new HashMap<>();
ScriptParams result = new ScriptParams();
checkForNulls("params", "calling withDefaults", this);
checkForNulls("defaults", "calling withDefaults", defaults);
result.putAll(defaults);
@ -78,6 +98,11 @@ public class ScriptParams extends HashMap<String, String> {
logger.info(mapdetail);
}
@Override
public String toString() {
return gson.toJson(this,Map.class).toString();
}
private static String valueOf(Object o) {
if (o == null) {
return "NULL";

View File

@ -161,6 +161,13 @@ public class AsyncScriptIntegrationTests {
assertThat(scenarioResult.getIOLog()).contains("params.get(\"four\") [defaulted-four-niner]='niner'");
}
@Test
public void testScriptParamsUndefVariableWithOverride() {
ScenarioResult scenarioResult = runScenario("undef_param", "one", "two", "three", "four");
assertThat(scenarioResult.getIOLog()).contains("before: params.get(\"three\"):four");
assertThat(scenarioResult.getIOLog()).contains("after: params.get(\"three\"):null");
}
@Test
public void testExtensionHistoStatsLogger() throws IOException {
ScenarioResult scenarioResult = runScenario("extension_histostatslogger");

View File

@ -0,0 +1,16 @@
print("params from command line:");
print(params);
print('before: params.get("three"):' + params.get("three"));
print('before: params.three:' + params.get("three"));
var overrides = {
'three': "undef",
};
print("params.three after overriding with three:UNDEF");
params = params.withOverrides({'three':'UNDEF'});
print(params);
print('after: params.get("three"):' + params.get("three"));
print('after: params.three:' + params.get("three"));