From 7dbf0f4aa49414938b3a32d6bf244af23074822f Mon Sep 17 00:00:00 2001 From: Dave Fisher Date: Fri, 22 Nov 2024 16:08:48 -0800 Subject: [PATCH] Refactor to ResolverChain with enums --- .../api/activityconfig/OpsLoader.java | 6 +-- .../java/io/nosqlbench/nb/api/nbio/NBIO.java | 41 +++++++++---------- .../io/nosqlbench/nb/api/nbio/NBPathsAPI.java | 2 +- ...{ParseProtocol.java => ResolverChain.java} | 38 +++++++++-------- .../io/nosqlbench/nb/api/nbio/NBIOTest.java | 4 +- 5 files changed, 47 insertions(+), 44 deletions(-) rename nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/{ParseProtocol.java => ResolverChain.java} (57%) diff --git a/nb-apis/adapters-api/src/main/java/io/nosqlbench/adapters/api/activityconfig/OpsLoader.java b/nb-apis/adapters-api/src/main/java/io/nosqlbench/adapters/api/activityconfig/OpsLoader.java index 5dcdf50e6..7a7e2b3ad 100644 --- a/nb-apis/adapters-api/src/main/java/io/nosqlbench/adapters/api/activityconfig/OpsLoader.java +++ b/nb-apis/adapters-api/src/main/java/io/nosqlbench/adapters/api/activityconfig/OpsLoader.java @@ -20,7 +20,7 @@ import com.amazonaws.util.StringInputStream; import com.google.gson.GsonBuilder; import io.nosqlbench.nb.api.nbio.Content; import io.nosqlbench.nb.api.nbio.NBIO; -import io.nosqlbench.nb.api.nbio.ParseProtocol; +import io.nosqlbench.nb.api.nbio.ResolverChain; import io.nosqlbench.nb.api.advisor.NBAdvisorException; import io.nosqlbench.nb.api.errors.BasicError; import io.nosqlbench.adapters.api.activityconfig.rawyaml.RawOpsDocList; @@ -59,8 +59,8 @@ public class OpsLoader { public static OpsDocList loadPath(String path, Map params, String... searchPaths) { String[] extensions = path.indexOf('.')>-1 ? new String[]{} : YAML_EXTENSIONS; - ParseProtocol proto = new ParseProtocol(path); - Content foundPath = NBIO.protocol(proto.getProtocols()).searchPrefixes(searchPaths).pathname(proto.getPath()).extensionSet(extensions).first() + ResolverChain chain = new ResolverChain(path); + Content foundPath = NBIO.chain(chain.getChain()).searchPrefixes(searchPaths).pathname(chain.getPath()).extensionSet(extensions).first() .orElseThrow(() -> new RuntimeException("Unable to load path '" + path + "'")); OpTemplateFormat fmt = OpTemplateFormat.valueOfURI(foundPath.getURI()); return loadString(foundPath.asString(), fmt, params, foundPath.getURI()); diff --git a/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/NBIO.java b/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/NBIO.java index 50f981834..010023114 100644 --- a/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/NBIO.java +++ b/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/NBIO.java @@ -70,8 +70,8 @@ public class NBIO implements NBPathsAPI.Facets { } public static List readLines(String filename) { - ParseProtocol proto = new ParseProtocol(filename); - Content data = NBIO.protocol(proto.getProtocols()).searchPrefixes("data").pathname(proto.getPath()).first().orElseThrow( + ResolverChain chain = new ResolverChain(filename); + Content data = NBIO.chain(chain.getChain()).searchPrefixes("data").pathname(chain.getPath()).first().orElseThrow( () -> new BasicError("Unable to read lines from " + filename) ); String[] split = data.getCharBuffer().toString().split("\n"); @@ -96,18 +96,18 @@ public class NBIO implements NBPathsAPI.Facets { private static InputStream readInputStream(String filename, String... searchPaths) { - ParseProtocol proto = new ParseProtocol(filename); - return NBIO.protocol(proto.getProtocols()).searchPrefixes(searchPaths).pathname(proto.getPath()).one().getInputStream(); + ResolverChain chain = new ResolverChain(filename); + return NBIO.chain(chain.getChain()).searchPrefixes(searchPaths).pathname(chain.getPath()).one().getInputStream(); } private static Reader readReader(String filename, String... searchPaths) { - ParseProtocol proto = new ParseProtocol(filename); - return NBIO.protocol(proto.getProtocols()).searchPrefixes(searchPaths).pathname(proto.getPath()).one().getReader(); + ResolverChain chain = new ResolverChain(filename); + return NBIO.chain(chain.getChain()).searchPrefixes(searchPaths).pathname(chain.getPath()).one().getReader(); } public static CharBuffer readCharBuffer(String filename, String... searchPaths) { - ParseProtocol proto = new ParseProtocol(filename); - return NBIO.protocol(proto.getProtocols()).searchPrefixes(searchPaths).pathname(proto.getPath()).one().getCharBuffer(); + ResolverChain chain = new ResolverChain(filename); + return NBIO.chain(chain.getChain()).searchPrefixes(searchPaths).pathname(chain.getPath()).one().getCharBuffer(); } public static Path getFirstLocalPath(String... potentials) { @@ -190,29 +190,26 @@ public class NBIO implements NBPathsAPI.Facets { * {@inheritDoc} */ @Override - public NBPathsAPI.GetPrefixes protocolContent(List protocols) { + public NBPathsAPI.GetPrefixes chainContent(List chains) { this.resolver = null; - for (String protocol : protocols) { - switch (protocol) { - case "cp": - case "classpath": + for (ResolverChain.Chain chain : chains) { + switch (chain) { + case ResolverChain.Chain.CP: this.resolver = this.resolver != null ? this.resolver.inCP() : URIResolvers.inClasspath(); break; - case "file": + case ResolverChain.Chain.FILE: this.resolver = this.resolver != null ? this.resolver.inFS() : URIResolvers.inFS(); break; - case "local": + case ResolverChain.Chain.LOCAL: this.resolver = this.resolver != null ? this.resolver.inFS().inCP() : URIResolvers.inFS().inCP(); break; - case "cache": + case ResolverChain.Chain.CACHE: if (useNBIOCache) { this.resolver = this.resolver != null ? this.resolver.inNBIOCache() : URIResolvers.inNBIOCache(); } break; - case "all": + case ResolverChain.Chain.ALL: return allContent(); - default: - throw new BasicError("NBIO Error: the '"+protocol+"' protocol is not available."); } } if ( this.resolver == null ) { @@ -389,12 +386,12 @@ public class NBIO implements NBPathsAPI.Facets { } /** - * Search for ordered protocols + * Search for ordered chains * * @return a builder */ - public static NBPathsAPI.GetPrefixes protocol(List protocols) { - return new NBIO().protocolContent(protocols); + public static NBPathsAPI.GetPrefixes chain(List chains) { + return new NBIO().chainContent(chains); } /** diff --git a/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/NBPathsAPI.java b/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/NBPathsAPI.java index f70b98181..c475abae8 100644 --- a/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/NBPathsAPI.java +++ b/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/NBPathsAPI.java @@ -89,7 +89,7 @@ public interface NBPathsAPI { * @param protocols A list of protocols to include in the search * @return this builder */ - GetPrefixes protocolContent(List protocols); + GetPrefixes chainContent(List chains); } interface GetPrefixes extends GetPathname { diff --git a/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/ParseProtocol.java b/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/ResolverChain.java similarity index 57% rename from nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/ParseProtocol.java rename to nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/ResolverChain.java index 3b9348b2f..c2663fbd0 100644 --- a/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/ParseProtocol.java +++ b/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/nbio/ResolverChain.java @@ -22,26 +22,32 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -public class ParseProtocol { +public class ResolverChain { - private List protocols; + public enum Chain { + ALL, LOCAL, CP, FILE, CACHE + } + + private List chains; private String path; - public ParseProtocol(String filepath) { - protocols = new ArrayList<>(); + public ResolverChain(String filepath) { + chains = new ArrayList<>(); - if (filepath.startsWith("http")) { + String[] parts = filepath.split(":",2); + + if (parts.length < 2) { path = filepath; - protocols.add("all"); - } else { - String[] parts = filepath.split(":"); - - if (parts.length < 2) { + chains.add(Chain.ALL); + } + for (String chain : parts[0].split("\\+")) { + try { + chains.add(Chain.valueOf(chain.toUpperCase())); + path = filepath.substring(parts[0].length()+1); + } catch (IllegalArgumentException e) { path = filepath; - protocols.add("all"); - } else { - path = parts[1]; - protocols = Arrays.asList(parts[0].split(",")); + chains.add(Chain.ALL); + break; } } } @@ -50,7 +56,7 @@ public class ParseProtocol { return path; } - public List getProtocols() { - return protocols; + public List getChain() { + return chains; } } diff --git a/nb-apis/nb-api/src/test/java/io/nosqlbench/nb/api/nbio/NBIOTest.java b/nb-apis/nb-api/src/test/java/io/nosqlbench/nb/api/nbio/NBIOTest.java index 138609d0f..caee85d48 100644 --- a/nb-apis/nb-api/src/test/java/io/nosqlbench/nb/api/nbio/NBIOTest.java +++ b/nb-apis/nb-api/src/test/java/io/nosqlbench/nb/api/nbio/NBIOTest.java @@ -73,8 +73,8 @@ public class NBIOTest { @Test public void testExpandPrefixesAndFullName() { - ParseProtocol proto = new ParseProtocol("local:foo.bar"); - NBIO extensions = (NBIO) NBIO.protocol(proto.getProtocols()).searchPrefixes("act1","act2").pathname(proto.getPath()); + ResolverChain chain = new ResolverChain("local:foo.bar"); + NBIO extensions = (NBIO) NBIO.chain(chain.getChain()).searchPrefixes("act1","act2").pathname(chain.getPath()); LinkedHashSet searches = extensions.expandNamesAndSuffixes(); assertThat(searches).containsExactly("foo.bar","act1/foo.bar","act2/foo.bar"); }