mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-20 11:38:28 -06:00
naming: use directapi instead of direct
This commit is contained in:
parent
9cd8892b69
commit
9763dd0be7
@ -1,29 +0,0 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<artifactId>mvn-defaults</artifactId>
|
||||
<groupId>io.nosqlbench</groupId>
|
||||
<version>4.15.52-SNAPSHOT</version>
|
||||
<relativePath>../mvn-defaults</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>driver-direct</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>${project.artifactId}</name>
|
||||
|
||||
<description>
|
||||
A direct API nosqlbench ActivityType (AT) driver module
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.nosqlbench</groupId>
|
||||
<artifactId>drivers-api</artifactId>
|
||||
<version>4.15.52-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,11 +0,0 @@
|
||||
package io.nosqlbench.driver.direct;
|
||||
|
||||
import io.nosqlbench.engine.api.activityimpl.OpDispenser;
|
||||
|
||||
public class CallMapper implements OpDispenser<DirectCall> {
|
||||
|
||||
@Override
|
||||
public DirectCall apply(long value) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package io.nosqlbench.driver.direct;
|
||||
|
||||
import io.nosqlbench.engine.api.activityimpl.uniform.flowtypes.Op;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class DirectCall implements Op,Runnable {
|
||||
private final Method method;
|
||||
private final Object[] args;
|
||||
private final Object instance;
|
||||
|
||||
public DirectCall(Method method, Object instance, Object[] args) {
|
||||
this.method = method;
|
||||
this.instance = instance;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
method.invoke(instance,args);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package io.nosqlbench.driver.direct;
|
||||
|
||||
import io.nosqlbench.engine.api.activityimpl.OpMapper;
|
||||
import io.nosqlbench.engine.api.activityimpl.uniform.BaseDriverAdapter;
|
||||
import io.nosqlbench.engine.api.activityimpl.uniform.DriverAdapter;
|
||||
import io.nosqlbench.nb.annotations.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* This activity type driver allows you to dynamically map any available
|
||||
* Java API which is exposed to the NoSQLBench runtime, executing methods
|
||||
* on this API by name, (optionally) storing named results, and re-using
|
||||
* these named results as arguments to subsequent calls.
|
||||
*
|
||||
* It supports static method dispatch, instance methods, and per-thread
|
||||
* object scoping.
|
||||
*/
|
||||
@Service(value = DriverAdapter.class, selector = "directapi")
|
||||
public class DirectCallAdapter extends BaseDriverAdapter<DirectCall,Void> {
|
||||
|
||||
@Override
|
||||
public List<Function<String, Optional<Map<String, Object>>>> getOpStmtRemappers() {
|
||||
return List.of(new DirectCallStmtParser());
|
||||
}
|
||||
|
||||
@Override
|
||||
public OpMapper<DirectCall> getOpMapper() {
|
||||
return new DirectOpMapper();
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package io.nosqlbench.driver.direct;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class DirectCallStmtParser implements Function<String, Optional<Map<String, Object>>> {
|
||||
|
||||
@Override
|
||||
public Optional<Map<String, Object>> apply(String s) {
|
||||
Pattern stmtPattern = Pattern.compile(
|
||||
"(?<package>[a-z](\\.[a-z]+)?)?(?<class>[A-Z]\\w+)(\\.(?<staticfield>\\w+))?(\\.(?<method>\\w+))\\((?<args>.+)\\)"
|
||||
);
|
||||
Matcher matcher = stmtPattern.matcher(s);
|
||||
if (matcher.matches()) {
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
||||
List.of("package","class","staticfield","method").forEach(n -> {
|
||||
if (matcher.group(n)!=null) {
|
||||
map.put(n,matcher.group(n));
|
||||
}
|
||||
});
|
||||
if (matcher.group("args")!=null) {
|
||||
String args = matcher.group("args");
|
||||
String[] argsplit = args.split(",");
|
||||
for (int i = 0; i < argsplit.length; i++) {
|
||||
String val = argsplit[i];
|
||||
if (val.startsWith("\\") && val.endsWith("\\")) {
|
||||
val = val.substring(1,val.length()-2);
|
||||
} else if (val.startsWith("'") && val.endsWith("'")) {
|
||||
val = val.substring(1,val.length()-2);
|
||||
}
|
||||
map.put("_arg"+i,argsplit[i]);
|
||||
}
|
||||
}
|
||||
return Optional.of(map);
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
package io.nosqlbench.driver.direct;
|
||||
|
||||
import io.nosqlbench.engine.api.activityimpl.OpDispenser;
|
||||
import io.nosqlbench.engine.api.activityimpl.OpMapper;
|
||||
import io.nosqlbench.engine.api.templating.ParsedCommand;
|
||||
import io.nosqlbench.nb.api.errors.OpConfigError;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.LongFunction;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DirectOpMapper implements OpMapper<DirectCall> {
|
||||
|
||||
@Override
|
||||
public OpDispenser<DirectCall> apply(ParsedCommand cmd) {
|
||||
|
||||
String pkg = cmd.getStaticValueOptionally("package", String.class).orElse("java.lang");
|
||||
String cls = cmd.getStaticValue("class");
|
||||
String fq = pkg + "." + cls;
|
||||
Class<?> clazz = null;
|
||||
Object instance = null;
|
||||
try {
|
||||
clazz = Class.forName(fq);
|
||||
|
||||
Class<?> finalClazz = clazz;
|
||||
Optional<Field> staticfield =
|
||||
cmd.getStaticValueOptionally("staticfield", String.class)
|
||||
.map(name -> {
|
||||
try {
|
||||
return finalClazz.getDeclaredField(name);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
});
|
||||
if (staticfield.isPresent()) {
|
||||
Field sfield = staticfield.get();
|
||||
if ((sfield.getModifiers() | Modifier.STATIC) > 0) {
|
||||
instance = sfield.get(null);
|
||||
clazz = instance.getClass();
|
||||
|
||||
} else {
|
||||
throw new OpConfigError("staticfield '" + cmd.getStaticValue("staticfield", String.class) + "' is not static");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
String methodName = cmd.getStaticValue("method");
|
||||
|
||||
Map<String, Object> protomap = cmd.getMap(0L);
|
||||
List<Class<?>> protoargs = new ArrayList<>();
|
||||
List<String> argnames = protomap.keySet().stream()
|
||||
.filter(n -> n.startsWith("_"))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
LongFunction<List<Object>> argsbinder = cmd.newListBinder(argnames);
|
||||
List<Object> args = argsbinder.apply(0L);
|
||||
List<Class<?>> types = args.stream().map(Object::getClass).collect(Collectors.toList());
|
||||
|
||||
Class<?>[] argTypes = types.toArray(new Class<?>[0]);
|
||||
|
||||
Method method = null;
|
||||
try {
|
||||
method = clazz.getMethod(methodName, argTypes);
|
||||
return new StaticMethodOpDispenser(method, instance, cmd.newArrayBinder(argnames));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package io.nosqlbench.driver.direct;
|
||||
|
||||
import io.nosqlbench.engine.api.activityconfig.yaml.OpTemplate;
|
||||
|
||||
import java.util.function.LongFunction;
|
||||
|
||||
public class DynamicCallDispenser implements LongFunction<DirectCall> {
|
||||
|
||||
public DynamicCallDispenser(OpTemplate opTemplate) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DirectCall apply(long value) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package io.nosqlbench.driver.direct;
|
||||
|
||||
import io.nosqlbench.engine.api.activityimpl.OpDispenser;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.LongFunction;
|
||||
|
||||
public class StaticMethodOpDispenser implements OpDispenser<DirectCall> {
|
||||
private final LongFunction<Object[]> argsfunc;
|
||||
private final Method method;
|
||||
private final Object instance;
|
||||
|
||||
public StaticMethodOpDispenser(Method method, Object instance, LongFunction<Object[]> argsfunc) {
|
||||
this.method = method;
|
||||
this.instance = instance;
|
||||
this.argsfunc = argsfunc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DirectCall apply(long value) {
|
||||
Object[] args = argsfunc.apply(value);
|
||||
return new DirectCall(method, instance, args);
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
package io.nosqlbench.driver.direct.optypes;
|
||||
|
||||
import io.nosqlbench.virtdata.library.basics.core.threadstate.SharedState;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.Map;
|
||||
|
||||
public class DynamicMethodCall implements Runnable {
|
||||
|
||||
private final static Logger logger = LogManager.getLogger(DynamicMethodCall.class);
|
||||
|
||||
private final Map<String, Object> callinfo;
|
||||
|
||||
public DynamicMethodCall(Map<String,Object> callinfo) {
|
||||
this.callinfo = callinfo;
|
||||
}
|
||||
|
||||
// At this point, class and method should have been set, and args optionally
|
||||
private void callMethod() {
|
||||
String className = callinfo.get("class").toString();
|
||||
String methodName = callinfo.get("method").toString();
|
||||
|
||||
Class<?> clazz;
|
||||
Method method;
|
||||
try {
|
||||
clazz = Class.forName(className);
|
||||
method = clazz.getMethod(methodName);
|
||||
|
||||
Object instance = null;
|
||||
if (!Modifier.isStatic(method.getModifiers())) {
|
||||
if (callinfo.containsKey("instance")) {
|
||||
String instanceName = callinfo.get("instance").toString();
|
||||
instance = SharedState.tl_ObjectMap.get().get(instanceName);
|
||||
}
|
||||
}
|
||||
|
||||
Parameter[] parameters = method.getParameters();
|
||||
Object[] args = new Object[parameters.length];
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
String posname = "arg" + i;
|
||||
if (callinfo.containsKey(posname)) {
|
||||
args[i] = callinfo.get(posname);
|
||||
} else if (parameters[i].isNamePresent()) {
|
||||
String argname = parameters[i].getName();
|
||||
if (callinfo.containsKey(argname)) {
|
||||
args[i]=callinfo.get(argname);
|
||||
} else {
|
||||
throw new RuntimeException("could not find arg named '" + posname + "', nor '" + argname + "' in op template for method " + method.toGenericString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Object result = method.invoke(instance, args);
|
||||
|
||||
if (callinfo.containsKey("save")) {
|
||||
String saveAs = callinfo.get("save").toString();
|
||||
SharedState.tl_ObjectMap.get().put(saveAs,result);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
callMethod();
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package io.nosqlbench.driver.direct.optypes;
|
||||
|
||||
public class ObjectCall {
|
||||
|
||||
}
|
@ -1 +0,0 @@
|
||||
io.nosqlbench.nb.annotations.ServiceProcessor
|
@ -1,26 +0,0 @@
|
||||
# Direct Driver
|
||||
|
||||
This is a unique type of NoSQLBench driver which assumes no particular
|
||||
runtime API, instead relying on runtime reflection to find and invoke
|
||||
the methods as specified in the op template.
|
||||
|
||||
Presently, therea re two
|
||||
```yaml
|
||||
statements:
|
||||
- "java.lang.System.out.println(\"Testing\");"
|
||||
- "System.out.println(\"Testing\");"
|
||||
- op: "System.out.println(\"Testing\");"
|
||||
- op:
|
||||
class: java.lang.System
|
||||
field: out
|
||||
method: println
|
||||
_arg0: Testing
|
||||
- op:
|
||||
class: java.lang.System
|
||||
staticfield: out
|
||||
method: println
|
||||
_x: Testing
|
||||
- op:
|
||||
object: myobj
|
||||
- myobj=System.out.println("testing");
|
||||
```
|
@ -101,7 +101,7 @@
|
||||
|
||||
<dependency>
|
||||
<groupId>io.nosqlbench</groupId>
|
||||
<artifactId>driver-direct</artifactId>
|
||||
<artifactId>driver-directapi</artifactId>
|
||||
<version>4.15.52-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user