mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
#158 Simplify YamlStatement Loader part 2, remove previous loader
This commit is contained in:
parent
46b2447d81
commit
e27b39b0c2
@ -301,13 +301,13 @@ public class CqlActivity extends SimpleActivity implements Activity, ActivityDef
|
||||
"http://docs.engineblock.io/user-guide/standard_yaml/ for more details.");
|
||||
break;
|
||||
case "2":
|
||||
doclist = StatementsLoader.load(logger, yaml_loc, interp, "activities");
|
||||
doclist = StatementsLoader.loadPath(logger, yaml_loc, interp, "activities");
|
||||
break;
|
||||
case "unset":
|
||||
try {
|
||||
logger.debug("You can suffix your yaml filename or url with the " +
|
||||
"format version, such as :1 or :2. Assuming version 2.");
|
||||
doclist = StatementsLoader.load(null, yaml_loc, interp, "activities");
|
||||
doclist = StatementsLoader.loadPath(null, yaml_loc, interp, "activities");
|
||||
} catch (Exception ignored) {
|
||||
try {
|
||||
doclist = getVersion1StmtsDoc(interp, yaml_loc);
|
||||
@ -323,7 +323,7 @@ public class CqlActivity extends SimpleActivity implements Activity, ActivityDef
|
||||
"for the standard format. To force loading version 1 with detailed logging, add" +
|
||||
" a version qualifier to your yaml filename or url like ':1'");
|
||||
// retrigger the error again, this time with logging enabled.
|
||||
doclist = StatementsLoader.load(logger, yaml_loc, interp, "activities");
|
||||
doclist = StatementsLoader.loadPath(logger, yaml_loc, interp, "activities");
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -59,7 +59,7 @@ public class HttpActivity extends SimpleActivity implements Activity, ActivityDe
|
||||
.getOptionalString("yaml", "workload")
|
||||
.orElse("default");
|
||||
|
||||
stmtsDocList = StatementsLoader.load(logger,yaml_loc, "activities");
|
||||
stmtsDocList = StatementsLoader.loadPath(logger,yaml_loc, "activities");
|
||||
}
|
||||
|
||||
|
||||
|
@ -101,7 +101,8 @@ public class MongoActivity extends SimpleActivity implements ActivityDefObserver
|
||||
);
|
||||
SequencePlanner<ReadyMongoStatement> sequencer = new SequencePlanner<>(sequencerType);
|
||||
|
||||
StmtsDocList stmtsDocList = StatementsLoader.load(logger, yamlLoc, new StrInterpolator(activityDef), "activities");
|
||||
StmtsDocList stmtsDocList = StatementsLoader.loadPath(logger, yamlLoc, new StrInterpolator(activityDef),
|
||||
"activities");
|
||||
|
||||
String tagfilter = activityDef.getParams().getOptionalString("tags").orElse("");
|
||||
|
||||
|
@ -34,7 +34,7 @@ public class ReadyMongoStatementTest {
|
||||
};
|
||||
activityDef = ActivityDef.parseActivityDef(String.join(";", params));
|
||||
String yaml_loc = activityDef.getParams().getOptionalString("yaml", "workload").orElse("default");
|
||||
stmtsDocList = StatementsLoader.load(logger, yaml_loc, new StrInterpolator(activityDef), "activities");
|
||||
stmtsDocList = StatementsLoader.loadPath(logger, yaml_loc, new StrInterpolator(activityDef), "activities");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -76,7 +76,7 @@ public class StdoutActivity extends SimpleActivity implements ActivityDefObserve
|
||||
|
||||
this.showstmts = activityDef.getParams().getOptionalBoolean("showstatements").orElse(false);
|
||||
this.fileName = activityDef.getParams().getOptionalString("filename").orElse("stdout");
|
||||
this.stmtsDocList = StatementsLoader.load(logger, yaml_loc, interp, "activities");
|
||||
this.stmtsDocList = StatementsLoader.loadPath(logger, yaml_loc, interp, "activities");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -168,7 +168,7 @@ public class WebDriverActivity extends SimpleActivity {
|
||||
private OpSequence<CommandTemplate> initOpSequenceFromYaml() {
|
||||
StrInterpolator interp = new StrInterpolator(activityDef);
|
||||
String yaml_loc = activityDef.getParams().getOptionalString("yaml", "workload").orElse("default");
|
||||
StmtsDocList stmtsDocList = StatementsLoader.load(logger, yaml_loc, interp, "activities");
|
||||
StmtsDocList stmtsDocList = StatementsLoader.loadPath(logger, yaml_loc, interp, "activities");
|
||||
|
||||
SequencerType sequencerType = getParams()
|
||||
.getOptionalString("seq")
|
||||
|
@ -17,13 +17,10 @@
|
||||
|
||||
package io.nosqlbench.engine.api.activityconfig;
|
||||
|
||||
import io.nosqlbench.engine.api.activityconfig.rawyaml.RawStmtsDoc;
|
||||
import io.nosqlbench.engine.api.activityconfig.rawyaml.RawStmtsDocList;
|
||||
import io.nosqlbench.engine.api.activityconfig.rawyaml.RawYamlStatementLoader;
|
||||
import io.nosqlbench.engine.api.activityconfig.stmtloader.GenericStmtLoader;
|
||||
import io.nosqlbench.engine.api.activityconfig.rawyaml.RawStmtsLoader;
|
||||
import io.nosqlbench.engine.api.activityconfig.yaml.StmtsDocList;
|
||||
import io.nosqlbench.nb.api.content.Content;
|
||||
import io.nosqlbench.nb.api.content.NBIO;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.util.function.Function;
|
||||
@ -35,72 +32,60 @@ public class StatementsLoader {
|
||||
generified
|
||||
}
|
||||
|
||||
public static StmtsDocList load(Logger logger, Content<?> content) {
|
||||
RawYamlStatementLoader loader = new RawYamlStatementLoader();
|
||||
RawStmtsDocList rawDocList = loader.loadString(logger, content.get());
|
||||
public static StmtsDocList loadContent(
|
||||
Logger logger,
|
||||
Content<?> content) {
|
||||
RawStmtsLoader loader = new RawStmtsLoader();
|
||||
RawStmtsDocList rawDocList = loader.loadString(logger, content.get().toString());
|
||||
StmtsDocList layered = new StmtsDocList(rawDocList);
|
||||
return layered;
|
||||
}
|
||||
|
||||
// public static StmtsDocList load(Logger logger, Path path) {
|
||||
// RawYamlStatementLoader loader = new RawYamlStatementLoader();
|
||||
// RawStmtsDocList rawDocList = loader.load(logger, path);
|
||||
public static StmtsDocList loadPath(
|
||||
Logger logger,
|
||||
String path,
|
||||
String... searchPaths) {
|
||||
RawStmtsDocList list = null;
|
||||
|
||||
RawStmtsLoader gloaderImpl = new RawStmtsLoader();
|
||||
list = gloaderImpl.loadPath(logger, path, searchPaths);
|
||||
return new StmtsDocList(list);
|
||||
}
|
||||
|
||||
public static StmtsDocList loadPath(
|
||||
Logger logger,
|
||||
String path,
|
||||
Function<String, String> transformer,
|
||||
String... searchPaths) {
|
||||
RawStmtsDocList list = null;
|
||||
|
||||
RawStmtsLoader gloaderImpl = new RawStmtsLoader();
|
||||
gloaderImpl.addTransformer(transformer);
|
||||
list = gloaderImpl.loadPath(logger, path, searchPaths);
|
||||
return new StmtsDocList(list);
|
||||
}
|
||||
|
||||
// public static StmtsDocList load(Logger logger, String path, String... searchPaths) {
|
||||
// Content<?> content = NBIO.all()
|
||||
// .prefix(searchPaths)
|
||||
// .name(path)
|
||||
// .one();
|
||||
// try {
|
||||
// RawYamlStatementLoader loader = new RawYamlStatementLoader();
|
||||
// RawStmtsDocList rawDocList = loader.loadString(logger, content.get());
|
||||
// StmtsDocList layered = new StmtsDocList(rawDocList);
|
||||
// return layered;
|
||||
// } catch (Exception e) {
|
||||
// throw new RuntimeException("error while reading file " + path, e);
|
||||
// }
|
||||
// }
|
||||
|
||||
// public static StmtsDocList load(Logger logger, String path, Function<String, String> transformer, String... searchPaths) {
|
||||
// RawYamlStatementLoader loader = new RawYamlStatementLoader(transformer);
|
||||
// RawStmtsDocList rawDocList = loader.load(logger, path, searchPaths);
|
||||
// StmtsDocList layered = new StmtsDocList(rawDocList);
|
||||
// return layered;
|
||||
// }
|
||||
|
||||
public static StmtsDocList load(Loader loader, Logger logger, String path, String... searchPaths) {
|
||||
RawStmtsDocList list= null;
|
||||
|
||||
switch (loader) {
|
||||
case generified:
|
||||
GenericStmtLoader gloaderImpl = new GenericStmtLoader();
|
||||
list = gloaderImpl.load(logger,path,searchPaths);
|
||||
case original:
|
||||
RawYamlStatementLoader yloaderImpl = new RawYamlStatementLoader();
|
||||
list = yloaderImpl.load(logger, path, searchPaths);
|
||||
}
|
||||
return new StmtsDocList(list);
|
||||
}
|
||||
|
||||
public static StmtsDocList load(Logger logger, String path, String... searchPaths) {
|
||||
Content<?> content = NBIO.all()
|
||||
.prefix(searchPaths)
|
||||
.name(path)
|
||||
.one();
|
||||
try {
|
||||
RawYamlStatementLoader loader = new RawYamlStatementLoader();
|
||||
RawStmtsDocList rawDocList = loader.loadString(logger, content.get());
|
||||
StmtsDocList layered = new StmtsDocList(rawDocList);
|
||||
return layered;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("error while reading file " + path, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static StmtsDocList load(Logger logger, String path, Function<String, String> transformer, String... searchPaths) {
|
||||
RawYamlStatementLoader loader = new RawYamlStatementLoader(transformer);
|
||||
RawStmtsDocList rawDocList = loader.load(logger, path, searchPaths);
|
||||
StmtsDocList layered = new StmtsDocList(rawDocList);
|
||||
return layered;
|
||||
}
|
||||
|
||||
public static StmtsDocList load(Loader loader, Logger logger, String path,
|
||||
Function<String,String> transformer, String... searchPaths) {
|
||||
RawStmtsDocList list= null;
|
||||
|
||||
switch (loader) {
|
||||
case generified:
|
||||
GenericStmtLoader gloaderImpl = new GenericStmtLoader();
|
||||
gloaderImpl.addTransformers(transformer);
|
||||
list = gloaderImpl.load(logger,path,searchPaths);
|
||||
case original:
|
||||
RawYamlStatementLoader yloaderImpl = new RawYamlStatementLoader();
|
||||
yloaderImpl.addTransformer(transformer);
|
||||
list = yloaderImpl.load(logger, path, searchPaths);
|
||||
}
|
||||
return new StmtsDocList(list);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -2,38 +2,43 @@ package io.nosqlbench.engine.api.activityconfig.rawyaml;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class RawScenarios extends LinkedHashMap<String, Object> {
|
||||
public class RawScenarios {
|
||||
|
||||
public static String STEPNAME = "%03d";
|
||||
private Map<String, Map<String, String>> scenarios = new LinkedHashMap<>();
|
||||
|
||||
public List<String> getScenarioNames() {
|
||||
return new LinkedList<>(this.keySet());
|
||||
return new ArrayList<>(scenarios.keySet());
|
||||
}
|
||||
|
||||
public void setPropertiesByReflection(Map<String,Object> scenarioInfo) {
|
||||
scenarioInfo.forEach(this::put);
|
||||
}
|
||||
public void setPropertiesByReflection(Object scenariosObject) {
|
||||
scenarios.clear();
|
||||
|
||||
public Map<String,String> getNamedScenario(String scenarioName) {
|
||||
|
||||
Object v = this.get(scenarioName);
|
||||
|
||||
if (v==null) { return null; }
|
||||
|
||||
// Yes this looks strange. Yes it will work. SnakeYaml and generics are a bad combo.
|
||||
if (v instanceof List) {
|
||||
List<String> list = (List<String>) v;
|
||||
Map<String,String> map = new LinkedHashMap<>();
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
map.put(String.format(STEPNAME,i),list.get(i));
|
||||
Objects.requireNonNull(scenariosObject);
|
||||
if (scenariosObject instanceof Map) {
|
||||
Map<String, Object> rawNamedScenarios = (Map<String, Object>) scenariosObject;
|
||||
for (Map.Entry<String, Object> namedEntry : rawNamedScenarios.entrySet()) {
|
||||
String scenarioName = namedEntry.getKey();
|
||||
Object scenarioObj = namedEntry.getValue();
|
||||
if (scenarioObj instanceof CharSequence) {
|
||||
scenarios.put(scenarioName, Map.of(String.format(STEPNAME, 1), scenarioObj.toString()));
|
||||
} else if (scenarioObj instanceof List) {
|
||||
List<String> list = (List<String>) scenarioObj;
|
||||
Map<String, String> scenarioMap = new LinkedHashMap<>();
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
scenarioMap.put(String.format(STEPNAME, i), list.get(i));
|
||||
}
|
||||
scenarios.put(scenarioName, scenarioMap);
|
||||
} else if (scenarioObj instanceof Map) {
|
||||
scenarios.put(scenarioName, (Map<String,String>)scenarioObj);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
} else if (v instanceof CharSequence) {
|
||||
return Map.of(String.format(STEPNAME,1),v.toString());
|
||||
} else if (v instanceof Map) {
|
||||
return ((Map)v);
|
||||
} else {
|
||||
throw new RuntimeException("Unknown type while access raw named scenarios data: " + v.getClass().getCanonicalName());
|
||||
throw new RuntimeException("Named scenarios must be a map at the top level, instead found '" + scenariosObject.getClass().getCanonicalName() + "'");
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> getNamedScenario(String scenarioName) {
|
||||
return scenarios.get(scenarioName);
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ public class RawStmtsDoc extends StatementsOwner {
|
||||
List<Object> blockList = ((List<Object>) blocksObjects);
|
||||
for (Object blockData : blockList) {
|
||||
if (blockData instanceof Map) {
|
||||
Map<String,Object> blockDataMap = (Map<String,Object>)blockData;
|
||||
Map<String, Object> blockDataMap = (Map<String, Object>) blockData;
|
||||
RawStmtsBlock rawStmtsBlock = new RawStmtsBlock();
|
||||
rawStmtsBlock.setFieldsByReflection(blockDataMap);
|
||||
blocks.add(rawStmtsBlock);
|
||||
@ -57,14 +57,9 @@ public class RawStmtsDoc extends StatementsOwner {
|
||||
}
|
||||
|
||||
Object scenariosData = properties.remove("scenarios");
|
||||
if (scenariosData!=null) {
|
||||
if (scenariosData instanceof Map) {
|
||||
RawScenarios rawScenarios = new RawScenarios();
|
||||
Map<String,Object> scenariosObjMap = (Map<String,Object>)scenariosData;
|
||||
rawScenarios.setPropertiesByReflection(scenariosObjMap);
|
||||
} else {
|
||||
throw new RuntimeException("Invalid type for scenarios data: " + scenariosData.getClass().getCanonicalName());
|
||||
}
|
||||
|
||||
if (scenariosData != null) {
|
||||
scenarios.setPropertiesByReflection(scenariosData);
|
||||
}
|
||||
|
||||
super.setFieldsByReflection(properties);
|
||||
|
@ -1,41 +1,25 @@
|
||||
package io.nosqlbench.engine.api.activityconfig.stmtloader;
|
||||
package io.nosqlbench.engine.api.activityconfig.rawyaml;
|
||||
|
||||
import io.nosqlbench.engine.api.activityconfig.rawyaml.RawStmtsDoc;
|
||||
import io.nosqlbench.engine.api.activityconfig.rawyaml.RawStmtsDocList;
|
||||
import io.nosqlbench.engine.api.activityconfig.yaml.StmtsDocList;
|
||||
import io.nosqlbench.engine.api.activityimpl.ActivityInitializationError;
|
||||
import io.nosqlbench.nb.api.content.Content;
|
||||
import io.nosqlbench.nb.api.content.NBIO;
|
||||
import io.nosqlbench.nb.api.errors.BasicError;
|
||||
import io.nosqlbench.virtdata.library.basics.core.stathelpers.DiscreteProbabilityBuffer;
|
||||
import org.slf4j.Logger;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
public class GenericStmtLoader {
|
||||
public class RawStmtsLoader {
|
||||
|
||||
List<Function<String, String>> stringTransformers = new ArrayList<>();
|
||||
private final ArrayList<Function<String,String>> transformers = new ArrayList<>();
|
||||
|
||||
public void addTransformers(Function<String, String>... newTransformers) {
|
||||
Collections.addAll(this.transformers, newTransformers);
|
||||
public void addTransformer(Function<String, String> newTransformer) {
|
||||
Collections.addAll(this.transformers, newTransformer);
|
||||
}
|
||||
|
||||
public RawStmtsDocList load(
|
||||
Logger logger,
|
||||
String path,
|
||||
String... searchPaths) {
|
||||
|
||||
String data = null;
|
||||
try {
|
||||
Optional<Content<?>> oyaml = NBIO.all().prefix(searchPaths).name(path).extension("yaml").first();
|
||||
data = oyaml.map(Content::asString).orElseThrow(() -> new BasicError("Unable to load " + path));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("error while reading file " + path, e);
|
||||
}
|
||||
public RawStmtsDocList loadString(Logger logger, String data) {
|
||||
|
||||
try {
|
||||
if (logger != null) logger.debug("Applying string transformer to yaml data:" + data);
|
||||
@ -49,7 +33,22 @@ public class GenericStmtLoader {
|
||||
}
|
||||
|
||||
return parseYaml(logger, data);
|
||||
}
|
||||
|
||||
public RawStmtsDocList loadPath(
|
||||
Logger logger,
|
||||
String path,
|
||||
String... searchPaths) {
|
||||
|
||||
String data = null;
|
||||
try {
|
||||
Optional<Content<?>> oyaml = NBIO.all().prefix(searchPaths).name(path).extension("yaml").first();
|
||||
data = oyaml.map(Content::asString).orElseThrow(() -> new BasicError("Unable to load " + path));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("error while reading file " + path, e);
|
||||
}
|
||||
|
||||
return loadString(logger, data);
|
||||
}
|
||||
|
||||
private RawStmtsDocList parseYaml(Logger logger, String data) {
|
@ -1,144 +0,0 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2016 jshook
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* /
|
||||
*/
|
||||
|
||||
package io.nosqlbench.engine.api.activityconfig.rawyaml;
|
||||
|
||||
import io.nosqlbench.engine.api.activityconfig.snakecharmer.SnakeYamlCharmer;
|
||||
import io.nosqlbench.engine.api.activityimpl.ActivityInitializationError;
|
||||
import io.nosqlbench.nb.api.content.Content;
|
||||
import io.nosqlbench.nb.api.content.NBIO;
|
||||
import io.nosqlbench.nb.api.errors.BasicError;
|
||||
import org.slf4j.Logger;
|
||||
import org.yaml.snakeyaml.TypeDescription;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class RawYamlStatementLoader {
|
||||
|
||||
List<Function<String, String>> stringTransformers = new ArrayList<>();
|
||||
|
||||
public RawYamlStatementLoader() {
|
||||
}
|
||||
|
||||
public RawYamlStatementLoader(Function<String, String> stringTransformer) {
|
||||
this.addTransformer(stringTransformer);
|
||||
}
|
||||
|
||||
public RawStmtsDocList load(Logger logger, String fromPath, String... searchPaths) {
|
||||
try {
|
||||
|
||||
Optional<Content<?>> oyaml = NBIO.all().prefix(searchPaths).name(fromPath).extension("yaml").first();
|
||||
String data = oyaml.map(Content::asString).orElseThrow(() -> new BasicError("Unable to load " + fromPath));
|
||||
data = applyTransforms(logger, data);
|
||||
return parseYaml(logger, data);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("error while reading file " + fromPath,e);
|
||||
}
|
||||
}
|
||||
|
||||
public void addTransformer(Function<String, String> transformer) {
|
||||
stringTransformers.add(transformer);
|
||||
}
|
||||
|
||||
public RawStmtsDocList load(Logger logger, Path path) {
|
||||
try {
|
||||
String yamlImg = Files.readString(path);
|
||||
return parseYaml(logger, yamlImg);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Error while reading YAML from search paths: " + e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
protected String applyTransforms(Logger logger, String data) {
|
||||
for (Function<String, String> xform : stringTransformers) {
|
||||
try {
|
||||
if (logger != null) logger.debug("Applying string transformer to yaml data:" + xform);
|
||||
data = xform.apply(data);
|
||||
} catch (Exception e) {
|
||||
RuntimeException t = new ActivityInitializationError("Error applying string applyTransforms to input", e);
|
||||
if (logger != null) logger.error(t.getMessage(), t);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
protected RawStmtsDocList parseYaml(Logger logger, String data) {
|
||||
Yaml yaml = getCustomYaml();
|
||||
|
||||
try {
|
||||
Iterable<Object> objects = yaml.loadAll(data);
|
||||
|
||||
List<RawStmtsDoc> stmtListList = new ArrayList<>();
|
||||
for (Object object : objects) {
|
||||
RawStmtsDoc tgsd = (RawStmtsDoc) object;
|
||||
stmtListList.add(tgsd);
|
||||
}
|
||||
return new RawStmtsDocList(stmtListList);
|
||||
} catch (Exception e) {
|
||||
if (logger != null) logger.error("yaml-construction-error: Error building configuration:"
|
||||
+ e.getMessage() + "" +
|
||||
" For more details on this error see the " +
|
||||
"troubleshooting section of the YAML format docs " +
|
||||
"for yaml-construction-error.", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
protected Yaml getCustomYaml() {
|
||||
|
||||
SnakeYamlCharmer charmer = new SnakeYamlCharmer(RawStmtsDoc.class);
|
||||
charmer.addHandler(StatementsOwner.class, "statements", new StatementsReader());
|
||||
charmer.addHandler(StatementsOwner.class, "statement", new StatementsReader());
|
||||
|
||||
TypeDescription tds = new TypeDescription(RawStmtsDoc.class);
|
||||
tds.addPropertyParameters("blocks", RawStmtsBlock.class);
|
||||
charmer.addTypeDescription(tds);
|
||||
|
||||
return new Yaml(charmer);
|
||||
}
|
||||
|
||||
|
||||
protected RawStmtsDocList loadString(Logger logger, String rawYaml) {
|
||||
String data = applyTransforms(logger, rawYaml);
|
||||
return parseYaml(logger, data);
|
||||
}
|
||||
|
||||
public RawStmtsDocList loadString(Logger logger, CharSequence rawYaml) {
|
||||
String data = applyTransforms(logger, rawYaml.toString());
|
||||
return parseYaml(logger, data);
|
||||
}
|
||||
|
||||
|
||||
private class StatementsReader implements SnakeYamlCharmer.FieldHandler {
|
||||
@Override
|
||||
public void handleMapping(Object object, Object nodeTuple) {
|
||||
//System.out.println("Handling mapping for" + object +", nodes:" + nodeTuple);
|
||||
if (object instanceof StatementsOwner) {
|
||||
((StatementsOwner) object).setByObject(nodeTuple);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -36,6 +36,11 @@ public class StatementsOwner extends BlockParams {
|
||||
|
||||
public void setFieldsByReflection(Map<String, Object> propsmap) {
|
||||
Object statementsObject = propsmap.remove("statements");
|
||||
|
||||
if (statementsObject==null) {
|
||||
statementsObject = propsmap.remove("statement");
|
||||
}
|
||||
|
||||
if (statementsObject!=null) {
|
||||
setByObject(statementsObject);
|
||||
}
|
||||
|
@ -1,110 +0,0 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2016 jshook
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* /
|
||||
*/
|
||||
|
||||
package io.nosqlbench.engine.api.activityconfig.snakecharmer;
|
||||
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
||||
import org.yaml.snakeyaml.nodes.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This is as arcane and strange looking as it sounds. SnakeYaml has a design that does not
|
||||
* make it easy to mix and match bean-style constructions with generic collections or variant
|
||||
* YAML structure (as allowed by the YAML spec), so this is a set of surgical APIs that
|
||||
* help to do such.
|
||||
*
|
||||
* It would be a nice improvement to be able to drop these classes and rely instead directly on
|
||||
* SnakeYaml APIs of similar flavor if/when it can do so, or if/when the documentation explains
|
||||
* more cleanly how to do so.
|
||||
*/
|
||||
public class SnakeYamlCharmer extends Constructor {
|
||||
private final Map<String,HandlerDef> handlerDefs = new HashMap<>();
|
||||
private final ReSafeConstructor ctor = new ReSafeConstructor();
|
||||
|
||||
public SnakeYamlCharmer(Class<?> targetClass) {
|
||||
super(targetClass);
|
||||
this.yamlClassConstructors.put(NodeId.mapping, new DelegatorConstructor());
|
||||
}
|
||||
|
||||
public <T> void addHandler(Class<T> typeName, String fieldName, FieldHandler fieldHandler) {
|
||||
this.handlerDefs.put(fieldName, new HandlerDef(fieldName, typeName, fieldHandler));
|
||||
}
|
||||
|
||||
public interface FieldHandler {
|
||||
void handleMapping(Object object, Object subObject);
|
||||
}
|
||||
|
||||
private static class HandlerDef {
|
||||
final String fieldName;
|
||||
final Class<?> handlerClass;
|
||||
final FieldHandler handler;
|
||||
|
||||
HandlerDef(String fieldName, Class<?> handlerClass, FieldHandler handler) {
|
||||
this.fieldName = fieldName;
|
||||
this.handlerClass = handlerClass;
|
||||
this.handler = handler;
|
||||
}
|
||||
}
|
||||
|
||||
private class DelegatorConstructor extends Constructor.ConstructMapping {
|
||||
|
||||
@Override
|
||||
protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
|
||||
|
||||
if (node.getNodeId()==NodeId.mapping) {
|
||||
List<NodeTuple> toExtract = new ArrayList<>();
|
||||
|
||||
// Find all matching field names and remember them
|
||||
for (NodeTuple nodeTuple : node.getValue()) {
|
||||
Node prospectNode = nodeTuple.getKeyNode();
|
||||
if (nodeTuple.getKeyNode() instanceof ScalarNode) {
|
||||
ScalarNode nameNode = (ScalarNode) prospectNode;
|
||||
if (SnakeYamlCharmer.this.handlerDefs.keySet().contains(nameNode.getValue())) {
|
||||
toExtract.add(nodeTuple);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove each matching field name by node and owning object
|
||||
* Construct a safe collection-based Java object
|
||||
* Call the delegated handler for the owning object and the sub-object
|
||||
*/
|
||||
for (NodeTuple nodeTuple : toExtract) {
|
||||
node.getValue().remove(nodeTuple);
|
||||
String nodeName = ((ScalarNode) nodeTuple.getKeyNode()).getValue();
|
||||
Object subObject = ctor.constructObject(nodeTuple.getValueNode());
|
||||
HandlerDef handlerDef = SnakeYamlCharmer.this.handlerDefs.get(nodeName);
|
||||
handlerDef.handler.handleMapping(object,subObject);
|
||||
}
|
||||
}
|
||||
return super.constructJavaBean2ndStep(node,object);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ReSafeConstructor extends SafeConstructor {
|
||||
@Override
|
||||
public Object constructObject(Node node) {
|
||||
return super.constructObject(node);
|
||||
}
|
||||
}
|
||||
}
|
@ -98,7 +98,7 @@ public class NBCLIScenarioParser {
|
||||
.extension("yaml")
|
||||
.one();
|
||||
|
||||
StmtsDocList stmts = StatementsLoader.load(logger, yamlWithNamedScenarios);
|
||||
StmtsDocList stmts = StatementsLoader.loadContent(logger, yamlWithNamedScenarios);
|
||||
|
||||
Scenarios scenarios = stmts.getDocScenarios();
|
||||
|
||||
@ -277,7 +277,7 @@ public class NBCLIScenarioParser {
|
||||
.name(referenced).extension("yaml")
|
||||
.one();
|
||||
|
||||
StmtsDocList stmts = StatementsLoader.load(logger, content);
|
||||
StmtsDocList stmts = StatementsLoader.loadContent(logger, content);
|
||||
|
||||
Map<String, String> templates = new LinkedHashMap<>();
|
||||
try {
|
||||
|
@ -37,7 +37,7 @@ public class RawStmtDefDefsTest {
|
||||
@Test
|
||||
public void testLayering() {
|
||||
|
||||
StmtsDocList all = StatementsLoader.load(logger, "testdocs/docs_blocks_stmts.yaml");
|
||||
StmtsDocList all = StatementsLoader.loadPath(logger, "testdocs/docs_blocks_stmts.yaml");
|
||||
assertThat(all).isNotNull();
|
||||
assertThat(all.getStmtDocs()).hasSize(2);
|
||||
StmtsDoc doc1 = all.getStmtDocs().get(0);
|
||||
@ -62,42 +62,9 @@ public class RawStmtDefDefsTest {
|
||||
assertThat(block22.getTags().get("root1")).isEqualTo("value23");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLayeringWithGenericLoader() {
|
||||
|
||||
StmtsDocList all = StatementsLoader.load(
|
||||
StatementsLoader.Loader.generified,
|
||||
logger,
|
||||
"testdocs" + "/docs_blocks_stmts.yaml"
|
||||
);
|
||||
assertThat(all).isNotNull();
|
||||
assertThat(all.getStmtDocs()).hasSize(2);
|
||||
StmtsDoc doc1 = all.getStmtDocs().get(0);
|
||||
assertThat(doc1.getName()).isEqualTo("doc1");
|
||||
assertThat(doc1.getBlocks()).hasSize(1);
|
||||
StmtsDoc doc2 = all.getStmtDocs().get(1);
|
||||
assertThat(doc2.getBlocks()).hasSize(2);
|
||||
|
||||
StmtsBlock block1 = doc1.getBlocks().get(0);
|
||||
assertThat(block1.getBindings()).hasSize(2);
|
||||
assertThat(block1.getName()).isEqualTo("doc1--block0");
|
||||
assertThat(block1.getTags()).hasSize(1);
|
||||
|
||||
StmtsBlock block21 = doc2.getBlocks().get(0);
|
||||
StmtsBlock block22 = doc2.getBlocks().get(1);
|
||||
|
||||
assertThat(block21.getName()).isEqualTo("doc2--block1");
|
||||
assertThat(block21.getTags()).hasSize(3);
|
||||
|
||||
assertThat(block22.getName()).isEqualTo("doc2--block2");
|
||||
assertThat(block22.getTags()).hasSize(2);
|
||||
assertThat(block22.getTags().get("root1")).isEqualTo("value23");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testStatementRendering() {
|
||||
StmtsDocList all = StatementsLoader.load(logger, "testdocs/docs_blocks_stmts.yaml");
|
||||
StmtsDocList all = StatementsLoader.loadPath(logger, "testdocs/docs_blocks_stmts.yaml");
|
||||
assertThat(all).isNotNull();
|
||||
assertThat(all.getStmtDocs()).hasSize(2);
|
||||
StmtsDoc doc1 = all.getStmtDocs().get(0);
|
||||
@ -108,29 +75,6 @@ public class RawStmtDefDefsTest {
|
||||
StmtDef sdef1 = assys.get(0);
|
||||
assertThat(sdef1.getName()).isEqualTo("doc1--block0--stmt1");
|
||||
assertThat(assys.get(0).getStmt()).isEqualTo("s1");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStatementRenderingGenerified() {
|
||||
StmtsDocList all = StatementsLoader.load(
|
||||
StatementsLoader.Loader.generified,
|
||||
logger,
|
||||
"testdocs" +
|
||||
"/docs_blocks_stmts.yaml"
|
||||
);
|
||||
|
||||
assertThat(all).isNotNull();
|
||||
assertThat(all.getStmtDocs()).hasSize(2);
|
||||
StmtsDoc doc1 = all.getStmtDocs().get(0);
|
||||
StmtsBlock block1 = doc1.getBlocks().get(0);
|
||||
assertThat(block1.getName()).isEqualTo("doc1--block0");
|
||||
List<StmtDef> assys = block1.getStmts();
|
||||
assertThat(assys).hasSize(2);
|
||||
StmtDef sdef1 = assys.get(0);
|
||||
assertThat(sdef1.getName()).isEqualTo("doc1--block0--stmt1");
|
||||
assertThat(assys.get(0).getStmt()).isEqualTo("s1");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -31,8 +31,8 @@ public class RawYamlStatementLoaderTest {
|
||||
|
||||
@Test
|
||||
public void testLoadPropertiesBlock() {
|
||||
RawYamlStatementLoader ysl = new RawYamlStatementLoader();
|
||||
RawStmtsDocList rawBlockDocs = ysl.load(logger, "testdocs/rawblock.yaml");
|
||||
RawStmtsLoader ysl = new RawStmtsLoader();
|
||||
RawStmtsDocList rawBlockDocs = ysl.loadPath(logger, "testdocs/rawblock.yaml");
|
||||
assertThat(rawBlockDocs.getStmtsDocs()).hasSize(1);
|
||||
RawStmtsDoc rawBlockDoc = rawBlockDocs.getStmtsDocs().get(0);
|
||||
assertThat(rawBlockDoc.getRawStmtDefs()).hasSize(1);
|
||||
@ -44,8 +44,8 @@ public class RawYamlStatementLoaderTest {
|
||||
|
||||
@Test
|
||||
public void testLoadFullFormat() {
|
||||
RawYamlStatementLoader ysl = new RawYamlStatementLoader();
|
||||
RawStmtsDocList erthing = ysl.load(logger, "testdocs/docs_blocks_stmts.yaml");
|
||||
RawStmtsLoader ysl = new RawStmtsLoader();
|
||||
RawStmtsDocList erthing = ysl.loadPath(logger, "testdocs/docs_blocks_stmts.yaml");
|
||||
List<RawStmtsDoc> rawStmtsDocs = erthing.getStmtsDocs();
|
||||
assertThat(rawStmtsDocs).hasSize(2);
|
||||
RawStmtsDoc rawStmtsDoc = rawStmtsDocs.get(0);
|
||||
@ -58,8 +58,8 @@ public class RawYamlStatementLoaderTest {
|
||||
|
||||
@Test
|
||||
public void testLoadScenarios() {
|
||||
RawYamlStatementLoader ysl = new RawYamlStatementLoader();
|
||||
RawStmtsDocList erthing = ysl.load(logger, "testdocs/docs_blocks_stmts.yaml");
|
||||
RawStmtsLoader ysl = new RawStmtsLoader();
|
||||
RawStmtsDocList erthing = ysl.loadPath(logger, "testdocs/docs_blocks_stmts.yaml");
|
||||
List<RawStmtsDoc> rawStmtsDocs = erthing.getStmtsDocs();
|
||||
assertThat(rawStmtsDocs).hasSize(2);
|
||||
RawStmtsDoc rawStmtsDoc = rawStmtsDocs.get(0);
|
||||
|
@ -39,7 +39,7 @@ public class StmtEscapingTest {
|
||||
@BeforeClass
|
||||
public static void testLayering() {
|
||||
|
||||
StmtsDocList all = StatementsLoader.load(logger, "testdocs/escaped_stmts.yaml");
|
||||
StmtsDocList all = StatementsLoader.loadPath(logger, "testdocs/escaped_stmts.yaml");
|
||||
assertThat(all).isNotNull();
|
||||
assertThat(all.getStmtDocs()).hasSize(1);
|
||||
StmtsDoc doc1 = all.getStmtDocs().get(0);
|
||||
|
@ -17,7 +17,6 @@
|
||||
|
||||
package io.nosqlbench.engine.api.activityconfig.rawyaml;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.junit.Test;
|
||||
@ -32,7 +31,7 @@ public class StmtVariationTests {
|
||||
|
||||
@Test
|
||||
public void testListStmtsOnly() {
|
||||
RawYamlStatementLoader ysl = new RawYamlStatementLoader();
|
||||
RawStmtsLoader ysl = new RawStmtsLoader();
|
||||
RawStmtsDocList docs = ysl.loadString(logger,
|
||||
"statements:\n" +
|
||||
" - first statement\n" +
|
||||
@ -53,7 +52,7 @@ public class StmtVariationTests {
|
||||
|
||||
@Test
|
||||
public void testSingleEntryMapStmtsOnly() {
|
||||
RawYamlStatementLoader ysl = new RawYamlStatementLoader();
|
||||
RawStmtsLoader ysl = new RawStmtsLoader();
|
||||
RawStmtsDocList docs = ysl.loadString(logger,
|
||||
"statements:\n" +
|
||||
" - s1: statement one\n" +
|
||||
@ -73,7 +72,7 @@ public class StmtVariationTests {
|
||||
|
||||
@Test
|
||||
public void testMapStmtsOnly() {
|
||||
RawYamlStatementLoader ysl = new RawYamlStatementLoader();
|
||||
RawStmtsLoader ysl = new RawStmtsLoader();
|
||||
RawStmtsDocList docs = ysl.loadString(logger,
|
||||
"statements:\n" +
|
||||
" - name: s1\n" +
|
||||
@ -95,7 +94,7 @@ public class StmtVariationTests {
|
||||
|
||||
@Test
|
||||
public void testMixedForms() {
|
||||
RawYamlStatementLoader ysl = new RawYamlStatementLoader();
|
||||
RawStmtsLoader ysl = new RawStmtsLoader();
|
||||
RawStmtsDocList docs = ysl.loadString(logger,
|
||||
"statement:\n" +
|
||||
" - name: s1\n" +
|
||||
|
@ -32,7 +32,7 @@ public class ParsedStmtTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void testLoadYaml() {
|
||||
doclist = StatementsLoader.load(logger, "testdocs/bindings.yaml");
|
||||
doclist = StatementsLoader.loadPath(logger, "testdocs/bindings.yaml");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -33,7 +33,7 @@ public class StmtDetailOverrideTest {
|
||||
@Test
|
||||
public void testStmtOverrides() {
|
||||
|
||||
StmtsDocList doclist = StatementsLoader.load(logger, "testdocs/stmt_details.yaml");
|
||||
StmtsDocList doclist = StatementsLoader.loadPath(logger, "testdocs/stmt_details.yaml");
|
||||
|
||||
assertThat(doclist).isNotNull();
|
||||
|
||||
|
@ -51,7 +51,7 @@ public class StmtsDocListTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void testLoadYaml() {
|
||||
doclist = StatementsLoader.load(logger, "testdocs/docs_blocks_stmts.yaml");
|
||||
doclist = StatementsLoader.loadPath(logger, "testdocs/docs_blocks_stmts.yaml");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user