mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
Add Protocol Resolver Chain Selection to NBIO (#2098)
* Add Protocol Selection to NBIO * Workloads can use a protocol * protocol errors and defaults * Test new protocol methods * Refactor to ResolverChain with enums * Rename the enum to ResolverChain.Link * OF - typo
This commit is contained in:
parent
86b2495aed
commit
59d930a054
@ -20,6 +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.ResolverChain;
|
||||
import io.nosqlbench.nb.api.advisor.NBAdvisorException;
|
||||
import io.nosqlbench.nb.api.errors.BasicError;
|
||||
import io.nosqlbench.adapters.api.activityconfig.rawyaml.RawOpsDocList;
|
||||
@ -58,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;
|
||||
|
||||
Content<?> foundPath = NBIO.all().searchPrefixes(searchPaths).pathname(path).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());
|
||||
|
@ -70,7 +70,8 @@ public class NBIO implements NBPathsAPI.Facets {
|
||||
}
|
||||
|
||||
public static List<String> readLines(String filename) {
|
||||
Content<?> data = NBIO.all().searchPrefixes("data").pathname(filename).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");
|
||||
@ -95,15 +96,18 @@ public class NBIO implements NBPathsAPI.Facets {
|
||||
|
||||
|
||||
private static InputStream readInputStream(String filename, String... searchPaths) {
|
||||
return NBIO.all().searchPrefixes(searchPaths).pathname(filename).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) {
|
||||
return NBIO.all().searchPrefixes(searchPaths).pathname(filename).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) {
|
||||
return NBIO.all().searchPrefixes(searchPaths).pathname(fileName).one().getCharBuffer();
|
||||
public static CharBuffer readCharBuffer(String filename, String... searchPaths) {
|
||||
ResolverChain chain = new ResolverChain(filename);
|
||||
return NBIO.chain(chain.getChain()).searchPrefixes(searchPaths).pathname(chain.getPath()).one().getCharBuffer();
|
||||
}
|
||||
|
||||
public static Path getFirstLocalPath(String... potentials) {
|
||||
@ -182,6 +186,38 @@ public class NBIO implements NBPathsAPI.Facets {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public NBPathsAPI.GetPrefixes chainContent(List<ResolverChain.Link> chains) {
|
||||
this.resolver = null;
|
||||
for (ResolverChain.Link chain : chains) {
|
||||
switch (chain) {
|
||||
case ResolverChain.Link.CP:
|
||||
this.resolver = this.resolver != null ? this.resolver.inCP() : URIResolvers.inClasspath();
|
||||
break;
|
||||
case ResolverChain.Link.FILE:
|
||||
this.resolver = this.resolver != null ? this.resolver.inFS() : URIResolvers.inFS();
|
||||
break;
|
||||
case ResolverChain.Link.LOCAL:
|
||||
this.resolver = this.resolver != null ? this.resolver.inFS().inCP() : URIResolvers.inFS().inCP();
|
||||
break;
|
||||
case ResolverChain.Link.CACHE:
|
||||
if (useNBIOCache) {
|
||||
this.resolver = this.resolver != null ? this.resolver.inNBIOCache() : URIResolvers.inNBIOCache();
|
||||
}
|
||||
break;
|
||||
case ResolverChain.Link.ALL:
|
||||
return allContent();
|
||||
}
|
||||
}
|
||||
if ( this.resolver == null ) {
|
||||
return allContent();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@ -349,6 +385,15 @@ public class NBIO implements NBPathsAPI.Facets {
|
||||
return new NBIO().localContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for ordered chains
|
||||
*
|
||||
* @return a builder
|
||||
*/
|
||||
public static NBPathsAPI.GetPrefixes chain(List<ResolverChain.Link> chains) {
|
||||
return new NBIO().chainContent(chains);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for named resources only in URLs
|
||||
*
|
||||
|
@ -82,6 +82,14 @@ public interface NBPathsAPI {
|
||||
* @return this builder
|
||||
*/
|
||||
GetPrefixes allContent();
|
||||
|
||||
/**
|
||||
* Return content from protocols that you choose in the order you specify
|
||||
*
|
||||
* @param protocols A list of protocols to include in the search
|
||||
* @return this builder
|
||||
*/
|
||||
GetPrefixes chainContent(List<ResolverChain.Link> chains);
|
||||
}
|
||||
|
||||
interface GetPrefixes extends GetPathname {
|
||||
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) nosqlbench
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.nosqlbench.nb.api.nbio;
|
||||
|
||||
import io.nosqlbench.nb.api.nbio.NBIO;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ResolverChain {
|
||||
|
||||
public enum Link {
|
||||
ALL, LOCAL, CP, FILE, CACHE
|
||||
}
|
||||
|
||||
private List<Link> chains;
|
||||
private String path;
|
||||
|
||||
public ResolverChain(String filepath) {
|
||||
chains = new ArrayList<>();
|
||||
|
||||
String[] parts = filepath.split(":",2);
|
||||
|
||||
if (parts.length < 2) {
|
||||
path = filepath;
|
||||
chains.add(Link.ALL);
|
||||
}
|
||||
for (String chain : parts[0].split("\\+")) {
|
||||
try {
|
||||
chains.add(Link.valueOf(chain.toUpperCase()));
|
||||
path = filepath.substring(parts[0].length()+1);
|
||||
} catch (IllegalArgumentException e) {
|
||||
path = filepath;
|
||||
chains.add(Link.ALL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public List<Link> getChain() {
|
||||
return chains;
|
||||
}
|
||||
}
|
@ -73,7 +73,8 @@ public class NBIOTest {
|
||||
|
||||
@Test
|
||||
public void testExpandPrefixesAndFullName() {
|
||||
NBIO extensions = (NBIO) NBIO.all().searchPrefixes("act1","act2").pathname("foo.bar");
|
||||
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");
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ public enum NBAtFileFormats {
|
||||
String[] parts = strings[0].split("[=:]", 2);
|
||||
return parts[0]+"="+parts[1];
|
||||
} else {
|
||||
throw new RuntimeException("Unable to match data for namedd value form: " + String.join("|",Arrays.asList(strings)));
|
||||
throw new RuntimeException("Unable to match data for named value form: " + String.join("|",Arrays.asList(strings)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ public enum NBAtFileFormats {
|
||||
String[] parts = strings[0].split("[=:]", 2);
|
||||
return parts[0]+":"+parts[1];
|
||||
} else {
|
||||
throw new RuntimeException("Unable to match data for namedd value form: " + String.join("|",Arrays.asList(strings)));
|
||||
throw new RuntimeException("Unable to match data for named value form: " + String.join("|",Arrays.asList(strings)));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user