Refactor to ResolverChain with enums

This commit is contained in:
Dave Fisher 2024-11-22 16:08:48 -08:00
parent b1dd6ffd1e
commit 7dbf0f4aa4
5 changed files with 47 additions and 44 deletions

View File

@ -20,7 +20,7 @@ import com.amazonaws.util.StringInputStream;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import io.nosqlbench.nb.api.nbio.Content; import io.nosqlbench.nb.api.nbio.Content;
import io.nosqlbench.nb.api.nbio.NBIO; 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.advisor.NBAdvisorException;
import io.nosqlbench.nb.api.errors.BasicError; import io.nosqlbench.nb.api.errors.BasicError;
import io.nosqlbench.adapters.api.activityconfig.rawyaml.RawOpsDocList; import io.nosqlbench.adapters.api.activityconfig.rawyaml.RawOpsDocList;
@ -59,8 +59,8 @@ public class OpsLoader {
public static OpsDocList loadPath(String path, Map<String, ?> params, String... searchPaths) { public static OpsDocList loadPath(String path, Map<String, ?> params, String... searchPaths) {
String[] extensions = path.indexOf('.')>-1 ? new String[]{} : YAML_EXTENSIONS; String[] extensions = path.indexOf('.')>-1 ? new String[]{} : YAML_EXTENSIONS;
ParseProtocol proto = new ParseProtocol(path); ResolverChain chain = new ResolverChain(path);
Content<?> foundPath = NBIO.protocol(proto.getProtocols()).searchPrefixes(searchPaths).pathname(proto.getPath()).extensionSet(extensions).first() Content<?> foundPath = NBIO.chain(chain.getChain()).searchPrefixes(searchPaths).pathname(chain.getPath()).extensionSet(extensions).first()
.orElseThrow(() -> new RuntimeException("Unable to load path '" + path + "'")); .orElseThrow(() -> new RuntimeException("Unable to load path '" + path + "'"));
OpTemplateFormat fmt = OpTemplateFormat.valueOfURI(foundPath.getURI()); OpTemplateFormat fmt = OpTemplateFormat.valueOfURI(foundPath.getURI());
return loadString(foundPath.asString(), fmt, params, foundPath.getURI()); return loadString(foundPath.asString(), fmt, params, foundPath.getURI());

View File

@ -70,8 +70,8 @@ public class NBIO implements NBPathsAPI.Facets {
} }
public static List<String> readLines(String filename) { public static List<String> readLines(String filename) {
ParseProtocol proto = new ParseProtocol(filename); ResolverChain chain = new ResolverChain(filename);
Content<?> data = NBIO.protocol(proto.getProtocols()).searchPrefixes("data").pathname(proto.getPath()).first().orElseThrow( Content<?> data = NBIO.chain(chain.getChain()).searchPrefixes("data").pathname(chain.getPath()).first().orElseThrow(
() -> new BasicError("Unable to read lines from " + filename) () -> new BasicError("Unable to read lines from " + filename)
); );
String[] split = data.getCharBuffer().toString().split("\n"); 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) { private static InputStream readInputStream(String filename, String... searchPaths) {
ParseProtocol proto = new ParseProtocol(filename); ResolverChain chain = new ResolverChain(filename);
return NBIO.protocol(proto.getProtocols()).searchPrefixes(searchPaths).pathname(proto.getPath()).one().getInputStream(); return NBIO.chain(chain.getChain()).searchPrefixes(searchPaths).pathname(chain.getPath()).one().getInputStream();
} }
private static Reader readReader(String filename, String... searchPaths) { private static Reader readReader(String filename, String... searchPaths) {
ParseProtocol proto = new ParseProtocol(filename); ResolverChain chain = new ResolverChain(filename);
return NBIO.protocol(proto.getProtocols()).searchPrefixes(searchPaths).pathname(proto.getPath()).one().getReader(); return NBIO.chain(chain.getChain()).searchPrefixes(searchPaths).pathname(chain.getPath()).one().getReader();
} }
public static CharBuffer readCharBuffer(String filename, String... searchPaths) { public static CharBuffer readCharBuffer(String filename, String... searchPaths) {
ParseProtocol proto = new ParseProtocol(filename); ResolverChain chain = new ResolverChain(filename);
return NBIO.protocol(proto.getProtocols()).searchPrefixes(searchPaths).pathname(proto.getPath()).one().getCharBuffer(); return NBIO.chain(chain.getChain()).searchPrefixes(searchPaths).pathname(chain.getPath()).one().getCharBuffer();
} }
public static Path getFirstLocalPath(String... potentials) { public static Path getFirstLocalPath(String... potentials) {
@ -190,29 +190,26 @@ public class NBIO implements NBPathsAPI.Facets {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public NBPathsAPI.GetPrefixes protocolContent(List<String> protocols) { public NBPathsAPI.GetPrefixes chainContent(List<ResolverChain.Chain> chains) {
this.resolver = null; this.resolver = null;
for (String protocol : protocols) { for (ResolverChain.Chain chain : chains) {
switch (protocol) { switch (chain) {
case "cp": case ResolverChain.Chain.CP:
case "classpath":
this.resolver = this.resolver != null ? this.resolver.inCP() : URIResolvers.inClasspath(); this.resolver = this.resolver != null ? this.resolver.inCP() : URIResolvers.inClasspath();
break; break;
case "file": case ResolverChain.Chain.FILE:
this.resolver = this.resolver != null ? this.resolver.inFS() : URIResolvers.inFS(); this.resolver = this.resolver != null ? this.resolver.inFS() : URIResolvers.inFS();
break; break;
case "local": case ResolverChain.Chain.LOCAL:
this.resolver = this.resolver != null ? this.resolver.inFS().inCP() : URIResolvers.inFS().inCP(); this.resolver = this.resolver != null ? this.resolver.inFS().inCP() : URIResolvers.inFS().inCP();
break; break;
case "cache": case ResolverChain.Chain.CACHE:
if (useNBIOCache) { if (useNBIOCache) {
this.resolver = this.resolver != null ? this.resolver.inNBIOCache() : URIResolvers.inNBIOCache(); this.resolver = this.resolver != null ? this.resolver.inNBIOCache() : URIResolvers.inNBIOCache();
} }
break; break;
case "all": case ResolverChain.Chain.ALL:
return allContent(); return allContent();
default:
throw new BasicError("NBIO Error: the '"+protocol+"' protocol is not available.");
} }
} }
if ( this.resolver == null ) { 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 * @return a builder
*/ */
public static NBPathsAPI.GetPrefixes protocol(List<String> protocols) { public static NBPathsAPI.GetPrefixes chain(List<ResolverChain.Chain> chains) {
return new NBIO().protocolContent(protocols); return new NBIO().chainContent(chains);
} }
/** /**

View File

@ -89,7 +89,7 @@ public interface NBPathsAPI {
* @param protocols A list of protocols to include in the search * @param protocols A list of protocols to include in the search
* @return this builder * @return this builder
*/ */
GetPrefixes protocolContent(List<String> protocols); GetPrefixes chainContent(List<ResolverChain.Chain> chains);
} }
interface GetPrefixes extends GetPathname { interface GetPrefixes extends GetPathname {

View File

@ -22,26 +22,32 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
public class ParseProtocol { public class ResolverChain {
private List<String> protocols; public enum Chain {
ALL, LOCAL, CP, FILE, CACHE
}
private List<Chain> chains;
private String path; private String path;
public ParseProtocol(String filepath) { public ResolverChain(String filepath) {
protocols = new ArrayList<>(); chains = new ArrayList<>();
if (filepath.startsWith("http")) { String[] parts = filepath.split(":",2);
if (parts.length < 2) {
path = filepath; path = filepath;
protocols.add("all"); chains.add(Chain.ALL);
} else { }
String[] parts = filepath.split(":"); for (String chain : parts[0].split("\\+")) {
try {
if (parts.length < 2) { chains.add(Chain.valueOf(chain.toUpperCase()));
path = filepath.substring(parts[0].length()+1);
} catch (IllegalArgumentException e) {
path = filepath; path = filepath;
protocols.add("all"); chains.add(Chain.ALL);
} else { break;
path = parts[1];
protocols = Arrays.asList(parts[0].split(","));
} }
} }
} }
@ -50,7 +56,7 @@ public class ParseProtocol {
return path; return path;
} }
public List<String> getProtocols() { public List<Chain> getChain() {
return protocols; return chains;
} }
} }

View File

@ -73,8 +73,8 @@ public class NBIOTest {
@Test @Test
public void testExpandPrefixesAndFullName() { public void testExpandPrefixesAndFullName() {
ParseProtocol proto = new ParseProtocol("local:foo.bar"); ResolverChain chain = new ResolverChain("local:foo.bar");
NBIO extensions = (NBIO) NBIO.protocol(proto.getProtocols()).searchPrefixes("act1","act2").pathname(proto.getPath()); NBIO extensions = (NBIO) NBIO.chain(chain.getChain()).searchPrefixes("act1","act2").pathname(chain.getPath());
LinkedHashSet<String> searches = extensions.expandNamesAndSuffixes(); LinkedHashSet<String> searches = extensions.expandNamesAndSuffixes();
assertThat(searches).containsExactly("foo.bar","act1/foo.bar","act2/foo.bar"); assertThat(searches).containsExactly("foo.bar","act1/foo.bar","act2/foo.bar");
} }