use of io.nosqlbench.nb.api.errors.BasicError in project nosqlbench by nosqlbench.
the class NBCLIScenarioParser method parseStep.
private static LinkedHashMap<String, CmdArg> parseStep(String cmd) {
LinkedHashMap<String, CmdArg> parsedStep = new LinkedHashMap<>();
String[] namedStepPieces = cmd.split(" ");
for (String commandFragment : namedStepPieces) {
Matcher matcher = WordAndMaybeAssignment.matcher(commandFragment);
if (!matcher.matches()) {
throw new BasicError("Unable to recognize scenario cmd spec in '" + commandFragment + "'");
}
String commandName = matcher.group("name");
commandName = Synonyms.canonicalize(commandName, logger);
String assignmentOp = matcher.group("oper");
String assignedValue = matcher.group("val");
parsedStep.put(commandName, new CmdArg(commandName, assignmentOp, assignedValue));
}
return parsedStep;
}
use of io.nosqlbench.nb.api.errors.BasicError in project nosqlbench by nosqlbench.
the class OpDef method getOp.
@Override
public Optional<Map<String, Object>> getOp() {
Object op = rawStmtDef.getOp();
if (op == null) {
return Optional.empty();
}
HashMap<String, Object> newmap = new LinkedHashMap<>();
if (op instanceof Map) {
((Map<?, ?>) op).forEach((k, v) -> {
newmap.put(k.toString(), v);
});
} else if (op instanceof CharSequence) {
newmap.put("stmt", op.toString());
} else {
throw new BasicError("Unable to coerce a '" + op.getClass().getCanonicalName() + "' into an op template");
}
return Optional.of(newmap);
}
use of io.nosqlbench.nb.api.errors.BasicError in project nosqlbench by nosqlbench.
the class VirtDataConversions method adaptFunction.
/**
* Adapt a functional object into a different type of functional object.
*
* @param func The original function object.
* @param functionType The type of the function you need
* @param <F> The generic type of function being converted from
* @param <T> The generic type of function being converted to
* @param resultSignature The signature of all output types, linearized for use after type-erasure.
* @return An instance of T
*/
public static <F, T> T adaptFunction(F func, Class<T> functionType, Class<?>... resultSignature) {
FuncType funcType = FuncType.valueOf(func.getClass());
List<Class<?>> signature = new ArrayList<>();
List<Class<?>> fromSignature = linearizeObjectSignature(func);
List<Class<?>> resultTypes = new ArrayList<>();
resultTypes.add(functionType);
for (Class<?> aClass : resultSignature) {
resultTypes.add(aClass);
}
List<Class<?>> toSignature = linearizeSignature(resultTypes);
signature.addAll(fromSignature);
signature.addAll(toSignature);
if (fromSignature.equals(toSignature)) {
return (T) func;
}
if (isAssignableFromTo(fromSignature, toSignature)) {
return (T) func;
}
Class<?>[] methodSignature = signature.toArray(new Class<?>[0]);
Method adapter = null;
Class<?> hostclass = NBFunctionConverter.class;
try {
adapter = NBFunctionConverter.class.getMethod("adapt", methodSignature);
} catch (NoSuchMethodException e) {
StringBuilder example = new StringBuilder();
example.append(" // Ignore the place holders in your implementation, but ensure the return type is accurate\n");
String toTypeSyntax = canonicalSyntaxFor(toSignature);
example.append(" public static ").append(toTypeSyntax);
example.append(" adapt(");
String fromTypeSyntax = canonicalSyntaxFor(fromSignature);
example.append(fromTypeSyntax).append(" f");
int idx = 1;
for (int i = 1; i < signature.size(); i++) {
Class<?> sigpart = signature.get(i);
example.append(", ").append(sigpart.getSimpleName()).append(" i").append(idx++);
}
example.append(") {\n }\n");
String forInstance = example.toString();
throw new BasicError("adapter method is not implemented on class " + hostclass.getCanonicalName() + ":\n" + forInstance);
}
FuncType fromType = FuncType.valueOf(func.getClass());
if (fromType.functionClazz.getTypeParameters().length > 0) {
TypeVariable<? extends Class<?>>[] funcParms = func.getClass().getTypeParameters();
}
Object[] args = new Object[signature.size()];
args[0] = func;
for (int i = 1; i < args.length; i++) {
args[i] = null;
}
T result = null;
try {
result = (T) adapter.invoke(null, args);
return result;
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
use of io.nosqlbench.nb.api.errors.BasicError in project nosqlbench by nosqlbench.
the class VirtDataConversions method findMethod.
private static Method findMethod(Class<?> hostclass, Class<?> fromClass, Class<?> toClass, Class<?>... generics) {
Class<?>[] argTypes = new Class[generics.length + 2];
argTypes[0] = fromClass;
argTypes[1] = toClass;
for (int i = 0; i < generics.length; i++) {
argTypes[i + 2] = generics[i];
}
try {
return hostclass.getMethod("adapt", argTypes);
} catch (NoSuchMethodException e) {
StringBuilder example = new StringBuilder();
StringBuilder genericsBuffer = new StringBuilder();
TypeVariable<? extends Class<?>>[] typeParameters = toClass.getTypeParameters();
if (typeParameters.length > 0) {
genericsBuffer.append("<");
for (int i = 0; i < typeParameters.length; i++) {
if (generics.length < typeParameters.length) {
throw new RuntimeException("You must provide " + typeParameters.length + " generic parameter types for " + toClass.getCanonicalName());
}
// if (generics[i].isPrimitive()) {
// throw new RuntimeException("You must declare non-primitive types in generic parameter placeholders, not " + generics[i].getSimpleName());
// }
genericsBuffer.append(generics[i].getSimpleName());
genericsBuffer.append(",");
}
genericsBuffer.setLength(genericsBuffer.length() - 1);
genericsBuffer.append(">");
}
String genericSignature = genericsBuffer.toString();
example.append(" // Ignore the place holders in your implementation, but ensure the return type is accurate\n");
example.append(" public static ").append(toClass.getSimpleName());
example.append(genericSignature);
example.append(" adapt(");
example.append(fromClass.getSimpleName()).append(" f, ");
example.append(toClass.getSimpleName()).append(genericSignature).append(" ignore0");
int idx = 1;
for (Class<?> generic : generics) {
example.append(", ").append(generic.getSimpleName()).append(" ignore").append(+idx);
}
example.append(") {\n }\n");
String forInstance = example.toString();
throw new BasicError("adapter method is not implemented on class " + hostclass.getCanonicalName() + ":\n" + forInstance);
}
}
use of io.nosqlbench.nb.api.errors.BasicError in project nosqlbench by nosqlbench.
the class BindPointParser method apply.
@Override
public Result apply(String template, Map<String, String> bindings) {
Matcher m = BINDPOINT_ANCHOR.matcher(template);
int lastMatch = 0;
List<String> spans = new ArrayList<>();
List<BindPoint> bindpoints = new ArrayList<>();
int genid = 0;
while (m.find()) {
String pre = template.substring(lastMatch, m.start());
spans.add(pre);
lastMatch = m.end();
String anchor = m.group("anchor");
String extendedAnchor = m.group("extended");
if (anchor != null) {
bindpoints.add(BindPoint.of(anchor, bindings.getOrDefault(anchor, null), BindPoint.Type.reference));
spans.add(anchor);
} else if (extendedAnchor != null) {
bindpoints.add(BindPoint.of(DEFINITION, extendedAnchor, BindPoint.Type.definition));
spans.add(extendedAnchor);
} else {
throw new BasicError("Unable to parse: " + template);
}
}
spans.add(lastMatch >= 0 ? template.substring(lastMatch) : template);
return new Result(spans, bindpoints);
}
Aggregations