NBPaths is the main path utility

This commit is contained in:
Jonathan Shook 2020-04-01 19:03:17 -05:00
parent 7afb19daca
commit c66352afac
11 changed files with 21 additions and 176 deletions

View File

@ -1,7 +1,7 @@
package io.nosqlbench.activitytype.cql.statements.core;
import io.nosqlbench.engine.api.activityimpl.ActivityInitializationError;
import io.nosqlbench.nb.api.pathutil.NBFiles;
import io.nosqlbench.nb.api.pathutil.NBPaths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.TypeDescription;
@ -32,7 +32,7 @@ public class YamlCQLStatementLoader {
public AvailableCQLStatements load(String fromPath, String... searchPaths) {
InputStream stream = NBFiles.findRequiredStreamOrFile(fromPath,
InputStream stream = NBPaths.findRequiredStreamOrFile(fromPath,
"yaml", searchPaths);
String data = "";
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(stream))) {

View File

@ -19,7 +19,7 @@ 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.pathutil.NBFiles;
import io.nosqlbench.nb.api.pathutil.NBPaths;
import org.slf4j.Logger;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
@ -55,7 +55,7 @@ public class RawYamlStatementLoader {
}
protected String loadRawFile(Logger logger, String fromPath, String... searchPaths) {
InputStream stream = NBFiles.findRequiredStreamOrFile(fromPath, "yaml", searchPaths);
InputStream stream = NBPaths.findRequiredStreamOrFile(fromPath, "yaml", searchPaths);
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(stream))) {
return buffer.lines().collect(Collectors.joining("\n"));
} catch (Exception e) {

View File

@ -17,7 +17,7 @@
package io.nosqlbench.engine.api.util;
import io.nosqlbench.nb.api.pathutil.NBFiles;
import io.nosqlbench.nb.api.pathutil.NBPaths;
import org.junit.Test;
import java.io.InputStream;
@ -25,11 +25,11 @@ import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
public class NBFilesTest {
public class NBPathsTest {
@Test
public void testNestedClasspathLoading() {
Optional<InputStream> optionalStreamOrFile = NBFiles.findOptionalStreamOrFile("nested/testfile", "txt", "activities");
Optional<InputStream> optionalStreamOrFile = NBPaths.findOptionalStreamOrFile("nested/testfile", "txt", "activities");
assertThat(optionalStreamOrFile).isPresent();
}

View File

@ -6,7 +6,7 @@ import io.nosqlbench.engine.api.activityapi.cyclelog.outputs.cyclelog.CycleLogIm
import io.nosqlbench.engine.api.activityapi.input.InputType;
import io.nosqlbench.engine.api.activityapi.output.OutputType;
import io.nosqlbench.engine.api.exceptions.BasicError;
import io.nosqlbench.nb.api.pathutil.NBFiles;
import io.nosqlbench.nb.api.pathutil.NBPaths;
import io.nosqlbench.engine.core.MarkdownDocInfo;
import io.nosqlbench.engine.core.ScenarioLogger;
import io.nosqlbench.engine.core.ScenariosResults;

View File

@ -2,7 +2,7 @@ package io.nosqlbench.engine.cli;
import ch.qos.logback.classic.Level;
import io.nosqlbench.engine.api.metrics.IndicatorMode;
import io.nosqlbench.nb.api.pathutil.NBFiles;
import io.nosqlbench.nb.api.pathutil.NBPaths;
import io.nosqlbench.engine.api.util.Unit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -303,7 +303,7 @@ public class NBCLIOptions {
break;
default:
Optional<InputStream> optionalScript =
NBFiles.findOptionalStreamOrFile(word, "js", "scripts/auto");
NBPaths.findOptionalStreamOrFile(word, "js", "scripts/auto");
//Script
if (optionalScript.isPresent()) {
arglist.removeFirst();

View File

@ -6,7 +6,7 @@ import io.nosqlbench.engine.api.activityconfig.yaml.Scenarios;
import io.nosqlbench.engine.api.activityconfig.yaml.StmtsDocList;
import io.nosqlbench.engine.api.exceptions.BasicError;
import io.nosqlbench.nb.api.pathutil.NBPaths;
import io.nosqlbench.nb.api.pathutil.NBFiles;
import io.nosqlbench.nb.api.pathutil.NBPaths;
import io.nosqlbench.engine.api.util.StrInterpolator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -28,14 +28,14 @@ public class NBCLIScenarioParser {
private final static Logger logger = LoggerFactory.getLogger(NBCLIScenarioParser.class);
public static boolean isFoundWorkload(String word) {
Optional<Path> workloadPath = NBFiles.findOptionalPath(word, "yaml", false, "activities");
Optional<Path> workloadPath = NBPaths.findOptionalPath(word, "yaml", false, "activities");
return workloadPath.isPresent();
}
public static void parseScenarioCommand(LinkedList<String> arglist) {
String workloadName = arglist.removeFirst();
Optional<Path> workloadPathSearch = NBFiles.findOptionalPath(workloadName, "yaml", false, "activities");
Optional<Path> workloadPathSearch = NBPaths.findOptionalPath(workloadName, "yaml", false, "activities");
Path workloadPath = workloadPathSearch.orElseThrow();
List<String> scenarioNames = new ArrayList<>();

View File

@ -1,7 +1,7 @@
package io.nosqlbench.engine.cli;
import io.nosqlbench.engine.api.activityimpl.ActivityDef;
import io.nosqlbench.nb.api.pathutil.NBFiles;
import io.nosqlbench.nb.api.pathutil.NBPaths;
import io.nosqlbench.engine.api.util.StrInterpolator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -81,7 +81,7 @@ public class NBCLIScriptAssembly {
} catch (IOException ignored) {
}
InputStream resourceAsStream = NBFiles.findRequiredStreamOrFile(cmd.getCmdSpec(), "js", "scripts");
InputStream resourceAsStream = NBPaths.findRequiredStreamOrFile(cmd.getCmdSpec(), "js", "scripts");
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(resourceAsStream))) {
scriptData = buffer.lines().collect(Collectors.joining("\n"));

View File

@ -12,7 +12,7 @@ import com.github.dockerjava.api.model.ContainerNetworkSettings;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.core.async.ResultCallbackTemplate;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import io.nosqlbench.nb.api.pathutil.NBFiles;
import io.nosqlbench.nb.api.pathutil.NBPaths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -147,7 +147,7 @@ public class DockerMetricsManager {
}
private void setupPromFiles(String ip) {
String datasource = NBFiles.readFile("docker/prometheus/prometheus.yml");
String datasource = NBPaths.readFile("docker/prometheus/prometheus.yml");
if (ip == null) {
logger.error("IP for graphite container not found");

View File

@ -1,7 +1,7 @@
package io.nosqlbench.engine.docker;
import io.nosqlbench.engine.api.exceptions.BasicError;
import io.nosqlbench.nb.api.pathutil.NBFiles;
import io.nosqlbench.nb.api.pathutil.NBPaths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -36,7 +36,7 @@ public class RestHelper {
if (path !=null) {
logger.debug("POSTing " + path + " to " + url);
String dashboard = NBFiles.readFile(path);
String dashboard = NBPaths.readFile(path);
logger.debug("length of content for " + path + " is " + dashboard.length());
builder = builder.POST(HttpRequest.BodyPublishers.ofString(dashboard));
builder.setHeader("Content-Type", "application/json");

View File

@ -17,12 +17,12 @@
package io.nosqlbench.engine.extensions.files;
import io.nosqlbench.nb.api.pathutil.NBFiles;
import io.nosqlbench.nb.api.pathutil.NBPaths;
public class FileAccess extends FileAccessPluginData {
public String read(String filepath) {
String filedata = NBFiles.readFile(filepath);
String filedata = NBPaths.readFile(filepath);
return filedata;
}

View File

@ -1,155 +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.nb.api.pathutil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URL;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
public class NBFiles {
private final static Logger logger = LoggerFactory.getLogger(NBFiles.class);
public static InputStream findRequiredStreamOrFile(String basename, String extension, String... searchPaths) {
Optional<InputStream> optionalStreamOrFile = findOptionalStreamOrFile(basename, extension, searchPaths);
return optionalStreamOrFile.orElseThrow(() -> new RuntimeException(
"Unable to find " + basename + " with extension " + extension + " in file system or in classpath, with"
+ " search paths: " + Arrays.stream(searchPaths).collect(Collectors.joining(","))
));
}
public static Optional<InputStream> findOptionalStreamOrFile(String basename, String extension, String... searchPaths) {
boolean needsExtension = (extension != null && !extension.isEmpty() && !basename.endsWith("." + extension));
String filename = basename + (needsExtension ? "." + extension : "");
ArrayList<String> paths = new ArrayList<String>() {{
add(filename);
if (!isRemote(basename)) {
addAll(Arrays.stream(searchPaths).map(s -> s + File.separator + filename)
.collect(Collectors.toCollection(ArrayList::new)));
}
}};
for (String path : paths) {
Optional<InputStream> stream = getInputStream(path);
if (stream.isPresent()) {
return stream;
}
}
return Optional.empty();
}
/**
* Search for the path
* @param basename Basename of path, with or without extension
* @param extension The extension of the filename
* @param searchWithin If enabled, all searchPaths are traversed, looking for a matching suffix pattern.
* @param searchPaths Additional places to look for the path suffix
* @return An optional path
*/
public static Optional<Path> findOptionalPath(String basename, String extension, boolean searchWithin, String... searchPaths) {
boolean needsExtension = (extension != null && !extension.isEmpty() && !basename.endsWith("." + extension));
String filename = basename + (needsExtension ? "." + extension : "");
ArrayList<String> paths = new ArrayList<String>() {{
add(filename);
if (!isRemote(basename)) {
addAll(Arrays.stream(searchPaths).map(s -> s + File.separator + filename)
.collect(Collectors.toCollection(ArrayList::new)));
}
}};
for (String path : paths) {
Optional<InputStream> stream = getInputStream(path);
if (stream.isPresent()) {
return Optional.of(Path.of(path));
}
}
if (searchWithin) {
throw new RuntimeException("not implemented");
// for (String searchPath : searchPaths) {
// NBPathWalker.findEndMatching(Path.of(searchPath), Path.of(filename));
// }
}
return Optional.empty();
}
private static boolean isRemote(String path) {
return (path.toLowerCase().startsWith("http:")
|| path.toLowerCase().startsWith("https:"));
}
public static Optional<InputStream> getInputStream(String path) {
// URLs, if http: or https:
if (isRemote(path)) {
URL url;
try {
url = new URL(path);
InputStream inputStream = url.openStream();
if (inputStream!=null) {
return Optional.of(inputStream);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// Files
try {
InputStream stream = new FileInputStream(path);
return Optional.of(stream);
} catch (FileNotFoundException ignored) {
}
// Classpath
ClassLoader classLoader = NBFiles.class.getClassLoader();
InputStream stream = classLoader.getResourceAsStream(path);
if (stream != null) {
return Optional.of(stream);
}
return Optional.empty();
}
public static String readFile(String basename) {
InputStream requiredStreamOrFile = findRequiredStreamOrFile(basename, "");
try (BufferedReader buffer = new BufferedReader((new InputStreamReader(requiredStreamOrFile)))) {
String filedata = buffer.lines().collect(Collectors.joining("\n"));
return filedata;
} catch (IOException ioe) {
throw new RuntimeException("Error while reading required file to string", ioe);
}
}
}