improve NBEnvironment and S3 interaction

This commit is contained in:
Jonathan Shook
2021-10-15 15:32:05 -05:00
parent cb47799652
commit e53af8bf5f
5 changed files with 129 additions and 21 deletions

View File

@@ -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) {

View File

@@ -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 {

View File

@@ -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");
}
}