io.nosqlbench.engine.api.activityconfig.rawyaml.RawStmtsLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of engine-api Show documentation
Show all versions of engine-api Show documentation
The engine API for nosqlbench;
Provides the interfaces needed to build internal modules for the
nosqlbench core engine
package io.nosqlbench.engine.api.activityconfig.rawyaml;
import io.nosqlbench.engine.api.activityimpl.ActivityInitializationError;
import io.nosqlbench.engine.api.templating.StrInterpolator;
import io.nosqlbench.nb.api.content.Content;
import io.nosqlbench.nb.api.content.NBIO;
import io.nosqlbench.nb.api.errors.BasicError;
import org.apache.logging.log4j.Logger;
import org.yaml.snakeyaml.Yaml;
import java.util.*;
import java.util.function.Function;
public class RawStmtsLoader {
public static String[] YAML_EXTENSIONS = new String[]{"yaml","yml"};
List> stringTransformers = new ArrayList<>();
private final ArrayList> transformers = new ArrayList<>();
public RawStmtsLoader(Function transformer) {
addTransformer(transformer);
}
public RawStmtsLoader() {
addTransformer(new StrInterpolator());
}
private void addTransformer(Function newTransformer) {
Collections.addAll(this.transformers, newTransformer);
}
public RawStmtsDocList loadString(Logger logger, String data) {
try {
if (logger != null) logger.trace("Applying string transformer to yaml data:" + data);
for (Function transformer : transformers) {
data = transformer.apply(data);
}
} catch (Exception e) {
RuntimeException t = new ActivityInitializationError("Error applying string transforms to input", e);
throw t;
}
return parseYaml(logger, data);
}
public RawStmtsDocList loadPath(
Logger logger,
String path,
String... searchPaths) {
String data = null;
try {
Optional> oyaml = NBIO.all().prefix(searchPaths).name(path).extension(YAML_EXTENSIONS).first();
data = oyaml.map(Content::asString).orElseThrow(() -> new BasicError("Unable to load " + path));
return loadString(logger, data);
} catch (Exception e) {
throw new RuntimeException("error while reading file " + path, e);
}
}
private RawStmtsDocList parseYaml(Logger logger, String data) {
Yaml yaml = new Yaml();
Iterable
© 2015 - 2024 Weber Informatics LLC | Privacy Policy