relativize within workspaces

This commit is contained in:
Jonathan Shook 2020-08-25 15:34:12 -05:00
parent 8ee27c15fc
commit 866b606e83
8 changed files with 162 additions and 35 deletions

View File

@ -13,7 +13,7 @@ import java.util.Arrays;
public class DocServerApp {
public final static String APPNAME_DOCSERVER = "docserver";
private static Logger logger = LogManager.getLogger(DocServerApp.class);
private static final Logger logger = LogManager.getLogger(DocServerApp.class);
// static {
// // defer to an extant logger context if it is there, otherwise
@ -82,9 +82,11 @@ public class DocServerApp {
Path relativePath = dirpath.resolve(Path.of("services/docs/markdown", markdownFile));
logger.info("Creating " + relativePath.toString());
String markdown = dds.getFileByPath(markdownFile);
Path path = dds.findPath(markdownFile);
// String markdown = dds.getFileByPath(markdownFile);
// Files.writeString(relativePath, markdown, OVERWRITE);
Files.createDirectories(relativePath.getParent());
Files.writeString(relativePath, markdown, OVERWRITE);
Files.write(relativePath,Files.readAllBytes(path),OVERWRITE);
}
}

View File

@ -3,10 +3,11 @@ package io.nosqlbench.engine.api.scenarios;
import io.nosqlbench.engine.api.activityconfig.StatementsLoader;
import io.nosqlbench.engine.api.activityconfig.yaml.Scenarios;
import io.nosqlbench.engine.api.activityconfig.yaml.StmtsDocList;
import io.nosqlbench.nb.api.config.Synonyms;
import io.nosqlbench.engine.api.templating.StrInterpolator;
import io.nosqlbench.nb.api.config.Synonyms;
import io.nosqlbench.nb.api.content.Content;
import io.nosqlbench.nb.api.content.NBIO;
import io.nosqlbench.nb.api.content.NBPathsAPI;
import io.nosqlbench.nb.api.errors.BasicError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -15,6 +16,7 @@ import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -30,8 +32,7 @@ public class NBCLIScenarioParser {
private static final String SEARCH_IN = "activities";
public static final String WORKLOAD_SCENARIO_STEP = "WORKLOAD_SCENARIO_STEP";
public static boolean isFoundWorkload(String workload,
String... includes) {
public static boolean isFoundWorkload(String workload, String... includes) {
Optional<Content<?>> found = NBIO.all()
.prefix("activities")
.prefix(includes)
@ -252,30 +253,39 @@ public class NBCLIScenarioParser {
}
}
private static Pattern templatePattern = Pattern.compile("TEMPLATE\\((.+?)\\)");
private static Pattern innerTemplatePattern = Pattern.compile("TEMPLATE\\((.+?)$");
private static Pattern templatePattern2 = Pattern.compile("<<(.+?)>>");
private static final Pattern templatePattern = Pattern.compile("TEMPLATE\\((.+?)\\)");
private static final Pattern innerTemplatePattern = Pattern.compile("TEMPLATE\\((.+?)$");
private static final Pattern templatePattern2 = Pattern.compile("<<(.+?)>>");
public static List<WorkloadDesc> getWorkloadsWithScenarioScripts(String... includes) {
public static List<WorkloadDesc> filterForScenarios(List<Content<?>> candidates) {
List<Content<?>> activities = NBIO.all()
.prefix(SEARCH_IN)
.prefix(includes)
.extension("yaml")
.list();
List<Path> yamlPathList = activities.stream().map(Content::asPath).collect(Collectors.toList());
List<Path> yamlPathList = candidates.stream().map(Content::asPath).collect(Collectors.toList());
List<WorkloadDesc> workloadDescriptions = new ArrayList<>();
for (Path yamlPath : yamlPathList) {
String referenced = yamlPath.toString();
referenced = referenced.startsWith("/") ? referenced.substring(1) :
referenced;
if (referenced.startsWith("/")) {
if (yamlPath.getFileSystem()==FileSystems.getDefault()) {
Path relative = Paths.get(System.getProperty("user.dir")).toAbsolutePath().relativize(yamlPath);
if (!relative.toString().contains("..")) {
referenced=relative.toString();
}
}
// String alternate = referenced.startsWith("/") ? referenced.substring(1) : referenced;
// Optional<Content<?>> checkLoad = NBIO.all().prefix(SEARCH_IN)
// .name(alternate).extension("yaml")
// .first();
// if (checkLoad.isPresent()) {
// referenced = alternate;
// }
}
Content<?> content = NBIO.all().prefix(SEARCH_IN)
.name(referenced).extension("yaml")
.one();
.name(referenced).extension("yaml")
.one();
StmtsDocList stmts = StatementsLoader.loadContent(logger, content);
@ -295,8 +305,8 @@ public class NBCLIScenarioParser {
List<String> scenarioNames = scenarios.getScenarioNames();
if (scenarioNames != null && scenarioNames.size() > 0) {
String path = yamlPath.toString();
path = path.startsWith(FileSystems.getDefault().getSeparator()) ? path.substring(1) : path;
// String path = yamlPath.toString();
// path = path.startsWith(FileSystems.getDefault().getSeparator()) ? path.substring(1) : path;
LinkedHashMap<String, String> sortedTemplates = new LinkedHashMap<>();
ArrayList<String> keyNames = new ArrayList<>(templates.keySet());
Collections.sort(keyNames);
@ -305,12 +315,29 @@ public class NBCLIScenarioParser {
}
String description = stmts.getDescription();
workloadDescriptions.add(new WorkloadDesc(path, scenarioNames, sortedTemplates, description));
workloadDescriptions.add(new WorkloadDesc(referenced, scenarioNames, sortedTemplates, description,""));
}
}
Collections.sort(workloadDescriptions);
return workloadDescriptions;
}
public static List<WorkloadDesc> getWorkloadsWithScenarioScripts(boolean defaultIncludes, String... includes) {
NBPathsAPI.GetPrefix searchin = NBIO.all();
if (defaultIncludes) {
searchin= searchin.prefix(SEARCH_IN);
}
List<Content<?>> activities = searchin
.prefix(includes)
.extension("yaml")
.list();
return filterForScenarios(activities);
}
public static Map<String, String> matchTemplates(String line, Map<String, String> templates) {

View File

@ -2,10 +2,13 @@ package io.nosqlbench.engine.api.scenarios;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
public class WorkloadDesc implements Comparable<WorkloadDesc> {
private final String workspace;
private final String yamlPath;
private final List<String> scenarioNames;
private final Map<String, String> templates;
@ -14,11 +17,13 @@ public class WorkloadDesc implements Comparable<WorkloadDesc> {
public WorkloadDesc(String yamlPath,
List<String> scenarioNames,
Map<String, String> templates,
String description) {
String description,
String workspace) {
this.yamlPath = yamlPath;
this.scenarioNames = scenarioNames;
this.templates = templates;
this.description = description;
this.workspace = workspace;
}
public String getYamlPath() {
@ -101,4 +106,22 @@ public class WorkloadDesc implements Comparable<WorkloadDesc> {
public int compareTo(@NotNull WorkloadDesc o) {
return this.yamlPath.compareTo(o.yamlPath);
}
public WorkloadDesc relativize(Path wsPath) {
Path yPath = Paths.get(this.yamlPath).toAbsolutePath();
Path relativePath = wsPath.relativize(yPath);
String wsName = wsPath.getFileName().toString();
return new WorkloadDesc(
relativePath.toString(),
this.scenarioNames,
this.templates,
description,
wsName
);
}
public String getWorkspace() {
return workspace;
}
}

View File

@ -4,13 +4,12 @@ import io.nosqlbench.engine.api.scenarios.NBCLIScenarioParser;
import io.nosqlbench.engine.api.scenarios.WorkloadDesc;
import java.util.List;
import java.util.Map;
public class NBCLIScenarios {
public static void printWorkloads(boolean includeScenarios,
String... includes) {
List<WorkloadDesc> workloads =
NBCLIScenarioParser.getWorkloadsWithScenarioScripts(includes);
NBCLIScenarioParser.getWorkloadsWithScenarioScripts(true, includes);
for (WorkloadDesc workload : workloads) {
System.out.println(workload.toString(includeScenarios));

View File

@ -358,6 +358,26 @@ public class NBIO implements NBPathsAPI.Facets {
return new ArrayList<>(foundFiles);
}
@Override
public List<Path> relativeTo(String... base) {
String base1 = base[0];
String[] rest = new String[base.length-1];
System.arraycopy(base,1,rest,0,rest.length);
List<Path> paths = new ArrayList<>();
List<Content<?>> list = list();
for (Content<?> c : list) {
Path path = c.asPath();
Path fsBase = path.getFileSystem().getPath(base1,rest);
Path relative = fsBase.relativize(path);
paths.add(relative);
}
return paths;
}
private static String tailmatch(String name) {
if (!name.startsWith("^") && !name.startsWith(".")) {
name = ".*" + name;

View File

@ -0,0 +1,25 @@
package io.nosqlbench.nb.api.content;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class NBIORelativizer {
public static List<Path> relativizePaths(Path base, List<Path> contained) {
List<Path> relativized = new ArrayList<>();
for (Path path : contained) {
Path relative = base.relativize(path);
relativized.add(relative);
}
return relativized;
}
public static List<Path> relativizeContent(Path base, List<Content<?>> contained) {
return relativizePaths(
base,
contained.stream().map(Content::asPath).collect(Collectors.toList()));
}
}

View File

@ -1,16 +1,15 @@
package io.nosqlbench.nb.api.content;
import io.nosqlbench.nb.api.content.Content;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
public interface NBPathsAPI {
public static interface Facets extends
interface Facets extends
GetSource, GetPrefix, GetName, GetExtension, DoSearch {}
public static interface GetSource {
interface GetSource {
/**
* Only provide content from the class path and the local filesystem.
* @return this builder
@ -45,7 +44,7 @@ public interface NBPathsAPI {
GetPrefix allContent();
}
public static interface GetPrefix extends GetName {
interface GetPrefix extends GetName {
/**
* Each of the prefix paths will be searched if the resource is not found with the exact
* path given.
@ -55,7 +54,7 @@ public interface NBPathsAPI {
GetPrefix prefix(String... prefixPaths);
}
public static interface GetName extends GetExtension {
interface GetName extends GetExtension {
/**
* Provide the names of the resources to be resolved. More than one resource may be provided.
* @param name The name of the resource to load
@ -64,7 +63,7 @@ public interface NBPathsAPI {
GetExtension name(String... name);
}
public static interface GetExtension extends DoSearch {
interface GetExtension extends DoSearch {
/**
* provide a list of optional file extensions which should be considered. If the content is
* not found under the provided name, then each of the extensios is tried in order.
@ -75,7 +74,7 @@ public interface NBPathsAPI {
}
public static interface DoSearch {
interface DoSearch {
/**
* Return the result of resolving the resource.
* @return an optional {@code Content<?>} element.
@ -89,8 +88,21 @@ public interface NBPathsAPI {
*/
List<List<Content<?>>> resolveEach();
/**
* Provide a list of all matching content that was matched by the search qualifers
* @return a list of content
*/
List<Content<?>> list();
/**
* Return a list of paths which are comprised of the relative part
* once the provided base has been removed from the front. This is done
* per content item within the direct filesystem the path belongs to.
* @param base The root path elements to remove
* @return Relative paths
*/
List<Path> relativeTo(String... base);
/**
* Find exactly one source of content under the search parameters given.
* It is an error if you find none, or more than one.

View File

@ -2,9 +2,12 @@ package io.nosqlbench.nb.api.content;
import org.junit.Test;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
@ -137,4 +140,20 @@ public class NBIOTest {
assertThat(list).hasSize(1);
}
@Test
public void testPathSearchInDifferentVantagePoints() {
List<Path> list = NBIO.fs()
.prefix("target/test-classes/nesteddir1")
.extension("csv")
.list().stream().map(Content::asPath)
.collect(Collectors.toList());
// assertThat(list).containsExactly(Paths.get("."));
List<Path> relatives = NBIORelativizer.relativizePaths(Paths.get("target/test-classes/"), list);
assertThat(relatives).hasSize(2);
}
}