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 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<String, ?> 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());

View File

@ -70,8 +70,8 @@ public class NBIO implements NBPathsAPI.Facets {
}
public static List<String> 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<String> protocols) {
public NBPathsAPI.GetPrefixes chainContent(List<ResolverChain.Chain> 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<String> protocols) {
return new NBIO().protocolContent(protocols);
public static NBPathsAPI.GetPrefixes chain(List<ResolverChain.Chain> chains) {
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
* @return this builder
*/
GetPrefixes protocolContent(List<String> protocols);
GetPrefixes chainContent(List<ResolverChain.Chain> chains);
}
interface GetPrefixes extends GetPathname {

View File

@ -22,26 +22,32 @@ import java.util.ArrayList;
import java.util.Arrays;
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;
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<String> getProtocols() {
return protocols;
public List<Chain> getChain() {
return chains;
}
}

View File

@ -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<String> searches = extensions.expandNamesAndSuffixes();
assertThat(searches).containsExactly("foo.bar","act1/foo.bar","act2/foo.bar");
}