centralize synonym canonicalization

This commit is contained in:
Jonathan Shook
2020-03-23 19:18:00 -05:00
parent 670954f9b4
commit 7bb13226fa
3 changed files with 77 additions and 6 deletions

View File

@@ -360,13 +360,8 @@ public class NBCLIOptions {
while(arglist.size() > 0 && arglist.peekFirst().contains("=")){
String arg = arglist.removeFirst();
String oldArg = arg;
arg = arg
.replaceAll("\\stype=", " driver=")
.replaceAll("\\syaml=", " workload=");
arg = Synonyms.canonicalize(arg, logger);
if (!arg.equals(oldArg)){
logger.warn("Identified deprecated usage of parameter name yaml or type. The new parameters are now workload and driver respectively");
}
for(int i =0 ; i< cmds.size(); i++){
String yamlCmd = cmds.get(i);
String[] argArray = arg.split("=");

View File

@@ -0,0 +1,55 @@
package io.nosqlbench.engine.cli;
import org.slf4j.Logger;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.regex.Pattern;
/**
* This class is just a central reference point for the names of parameters
* or other configuration-level primitives which have been given better names.
* For the sake of backwards compatibility, the old names are retained, but
* deprecated and warned against.
*/
public class Synonyms {
/**
* Each entry in this list is a list of synonyms in configuration.
*/
public final static Map<String, Set<String>> PARAM_SYNONYMS = new HashMap<>() {{
put("hosts",Set.of("host"));
put("workload",Set.of("yaml"));
put("driver",Set.of("type"));
put("cyclerate",Set.of("targetrate"));
}};
/**
* use this method to convert deprecated
* @param input A configuration string from a user or file
* @param synonyms A list of known synonym lists with the preferred values first, like {@link #PARAM_SYNONYMS}
* @param warnings An BiConsumer which can handle (deprecated, preferred) for subsitutions.
* @return The configuration string in canonicalized form, with the preferred names used where possible
*/
public static String canonicalize(String input, Map<String, Set<String>> synonyms, BiConsumer<String,String> warnings) {
String replaced = input;
for (Map.Entry<String, Set<String>> syns : synonyms.entrySet()) {
String preferred = syns.getKey();
for (String deprecated : syns.getValue()) {
Pattern p = Pattern.compile("\\b" + deprecated + "\\b");
String prior = replaced;
replaced = replaced.replaceAll(p.pattern(),preferred);
if (!prior.equals(replaced) && warnings!=null) {
warnings.accept(deprecated,preferred);
}
}
}
return replaced;
}
public static String canonicalize(String arg, Logger logger) {
return canonicalize(arg, PARAM_SYNONYMS, (d, p) -> logger.warn(
"Identified deprecated use of '" + d + ", please use '" + p + "' as the preferred form to remove this warning."
));
}
}

View File

@@ -0,0 +1,21 @@
package io.nosqlbench.engine.cli;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class SynonymsTest {
@Test
public void testCanonicalizeText() {
String before = "test yaml ayamlfoo type btype typea targetrate";
StringBuilder sb = new StringBuilder();
String result = Synonyms.canonicalize(before, Synonyms.PARAM_SYNONYMS,
(s, s2) -> sb.append("replaced ").append(s).append(" with ").append(s2).append("\n"));
assertThat(result).isEqualTo("test workload ayamlfoo driver btype typea cyclerate");
assertThat(sb.toString()).isEqualTo("replaced type with driver\n" +
"replaced targetrate with cyclerate\n" +
"replaced yaml with workload\n");
}
}