mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
improve NBEnvironment and S3 interaction
This commit is contained in:
@@ -92,16 +92,26 @@ public class NBEnvironment {
|
||||
* @param defaultValue The value to return if the name is not found
|
||||
* @return the system property or environment variable's value, or the default value
|
||||
*/
|
||||
public String getOr(String name, String defaultValue) {
|
||||
String value = peek(name);
|
||||
public String getOr(String name, String defaultValue, Map<String,String> supplemental) {
|
||||
String value = peek(name, supplemental);
|
||||
if (value == null) {
|
||||
value = defaultValue;
|
||||
}
|
||||
return reference(name, value);
|
||||
}
|
||||
|
||||
private String peek(String name) {
|
||||
public String getOr(String name, String defaultValue) {
|
||||
return getOr(name, defaultValue, Map.of());
|
||||
}
|
||||
|
||||
private String peek(String name, Map<String,String> supplemental) {
|
||||
String value = null;
|
||||
if (supplemental.containsKey(name)) {
|
||||
value = supplemental.get(name);
|
||||
if (value!=null) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
if (name.contains(".")) {
|
||||
value = System.getProperty(name.toLowerCase());
|
||||
if (value != null) {
|
||||
@@ -145,7 +155,11 @@ public class NBEnvironment {
|
||||
}
|
||||
|
||||
public boolean containsKey(String name) {
|
||||
String value = peek(name);
|
||||
return containsKey(name, Map.of());
|
||||
}
|
||||
|
||||
public boolean containsKey(String name, Map<String,String> supplemental) {
|
||||
String value = peek(name, supplemental);
|
||||
return (value != null);
|
||||
}
|
||||
|
||||
@@ -162,7 +176,7 @@ public class NBEnvironment {
|
||||
* @param word The word to interpolate the environment values into
|
||||
* @return The interpolated value, after substitutions, or null if any lookup failed
|
||||
*/
|
||||
public Optional<String> interpolate(String word) {
|
||||
public Optional<String> interpolate(String word, Map<String,String> supplemental) {
|
||||
Pattern envpattern = Pattern.compile("(\\$(?<env1>[a-zA-Z_][A-Za-z0-9_.]+)|\\$\\{(?<env2>[^}]+)\\})");
|
||||
Matcher matcher = envpattern.matcher(word);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@@ -171,7 +185,7 @@ public class NBEnvironment {
|
||||
if (envvar == null) {
|
||||
envvar = matcher.group("env2");
|
||||
}
|
||||
String value = peek(envvar);
|
||||
String value = peek(envvar,supplemental);
|
||||
if (value == null) {
|
||||
if (logger != null) {
|
||||
logger.debug("no value found for '" + envvar + "', returning Optional.empty() for '" + word + "'");
|
||||
@@ -186,6 +200,10 @@ public class NBEnvironment {
|
||||
return Optional.of(sb.toString());
|
||||
}
|
||||
|
||||
public Optional<String> interpolate(String word) {
|
||||
return interpolate(word,Map.of());
|
||||
}
|
||||
|
||||
public List<String> interpolateEach(CharSequence delim, String toBeRecombined) {
|
||||
String[] split = toBeRecombined.split(delim.toString());
|
||||
List<String> mapped = new ArrayList<>();
|
||||
@@ -224,12 +242,8 @@ public class NBEnvironment {
|
||||
*/
|
||||
public final Optional<String> interpolateWithTimestamp(String rawtext, long millis, Map<String, String> map) {
|
||||
String result = rawtext;
|
||||
for (String key : map.keySet()) {
|
||||
String value = map.get(key);
|
||||
result = result.replaceAll(Pattern.quote(key), value);
|
||||
}
|
||||
result = SessionNamer.format(result, millis);
|
||||
return interpolate(result);
|
||||
return interpolate(result,map);
|
||||
}
|
||||
|
||||
public final Optional<String> interpolateWithTimestamp(String rawText, long millis) {
|
||||
|
||||
@@ -26,9 +26,11 @@ public class S3UploaderDemo {
|
||||
if (!FileSystems.getDefault().equals(sourcePath.getFileSystem())) {
|
||||
throw new RuntimeException("The file must reside on the default filesystem to be uploaded by S3.");
|
||||
}
|
||||
|
||||
if (!Files.isDirectory(sourcePath, LinkOption.NOFOLLOW_LINKS)) {
|
||||
throw new RuntimeException("path '" + sourcePath + "' is not a directory.");
|
||||
}
|
||||
|
||||
TransferManager tm = TransferManagerBuilder.defaultTransferManager();
|
||||
MultipleFileUpload mfu = tm.uploadDirectory(bucket, prefix, sourcePath.toFile(), true);
|
||||
try {
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.nosqlbench.nb.api;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -23,7 +24,16 @@ public class NBEnvironmentTest {
|
||||
long millis = 1633964892320L;
|
||||
String time1 = env.interpolateWithTimestamp("word WOO %td %% end", millis, Map.of("WOO","WOW")).orElse(null);
|
||||
assertThat(time1).isEqualTo("word WOW 11 % end");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterpolationPrecedence() {
|
||||
NBEnvironment env = new NBEnvironment();
|
||||
Optional<String> superseded = env.interpolate("$TEST_KEY, $USER", Map.of("TEST_KEY", "supersedes1", "USER", "supersedes2"));
|
||||
assertThat(superseded).contains("supersedes1, supersedes2");
|
||||
superseded = env.interpolate("$USER", Map.of("TEST_KEY", "supersedes1"));
|
||||
assertThat(superseded).isPresent();
|
||||
assertThat(superseded.get()).isNotEqualTo("supersedes2");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user