extract ratio from optemplate before construction

This commit is contained in:
Jonathan Shook 2020-10-23 02:35:45 -05:00
parent dcf458c4de
commit 453b1b2c19
3 changed files with 38 additions and 15 deletions

View File

@ -12,11 +12,13 @@ public interface OpTemplate extends Tagged {
String getStmt();
Map<String,String> getBindings();
Map<String, String> getBindings();
Map<String, Object> getParams();
<T> Map<String,T> getParamsAsValueType(Class<? extends T> type);
<T> Map<String, T> getParamsAsValueType(Class<? extends T> type);
<V> V removeParamOrDefault(String name, V defaultValue);
@SuppressWarnings("unchecked")
<V> V getParamOrDefault(String name, V defaultValue);

View File

@ -29,7 +29,7 @@ import java.util.Optional;
public class StmtDef implements OpTemplate {
private final RawStmtDef rawStmtDef;
private StmtsBlock block;
private final StmtsBlock block;
public StmtDef(StmtsBlock block, RawStmtDef rawStmtDef) {
this.block = block;
@ -47,7 +47,7 @@ public class StmtDef implements OpTemplate {
}
@Override
public Map<String,String> getBindings() {
public Map<String, String> getBindings() {
return new MultiMapLookup<>(rawStmtDef.getBindings(), block.getBindings());
}
@ -64,11 +64,28 @@ public class StmtDef implements OpTemplate {
return map;
}
@Override
public <V> V removeParamOrDefault(String name, V defaultValue) {
Objects.requireNonNull(defaultValue);
MultiMapLookup<Object> lookup = new MultiMapLookup<>(rawStmtDef.getParams(), block.getParams());
if (!lookup.containsKey(name)) {
return defaultValue;
}
Object value = lookup.remove(name);
try {
return (V) defaultValue.getClass().cast(value);
} catch (Exception e) {
throw new RuntimeException("Unable to cast type " + value.getClass().getCanonicalName() + " to " + defaultValue.getClass().getCanonicalName(), e);
}
}
@Override
@SuppressWarnings("unchecked")
public <V> V getParamOrDefault(String name, V defaultValue) {
Objects.requireNonNull(defaultValue);
MultiMapLookup<Object> lookup = new MultiMapLookup<>(rawStmtDef.getParams(), block.getParams());
MultiMapLookup<Object> lookup = new MultiMapLookup<>(rawStmtDef.getParams(), block.getParams());
if (!lookup.containsKey(name)) {
return defaultValue;
@ -83,7 +100,7 @@ public class StmtDef implements OpTemplate {
@Override
public <V> V getParam(String name, Class<? extends V> type) {
MultiMapLookup<Object> lookup = new MultiMapLookup<>(rawStmtDef.getParams(), block.getParams());
MultiMapLookup<Object> lookup = new MultiMapLookup<>(rawStmtDef.getParams(), block.getParams());
Object object = lookup.get(name);
V value = type.cast(object);
return value;
@ -96,10 +113,10 @@ public class StmtDef implements OpTemplate {
throw new RuntimeException("Do not use primitive types for the target class here. For example, Boolean.class is accepted, but boolean.class is not.");
}
// TODO: add warning here if primitive types are not allowed
MultiMapLookup<Object> lookup = new MultiMapLookup<>(rawStmtDef.getParams(), block.getParams());
MultiMapLookup<Object> lookup = new MultiMapLookup<>(rawStmtDef.getParams(), block.getParams());
if (lookup.containsKey(name)) {
Object object = lookup.get(name);
if (object==null) {
if (object == null) {
return Optional.empty();
}
try {
@ -114,18 +131,18 @@ public class StmtDef implements OpTemplate {
@Override
public Optional<String> getOptionalStringParam(String name) {
return getOptionalStringParam(name,String.class);
return getOptionalStringParam(name, String.class);
}
@Override
public Map<String,String> getTags() {
public Map<String, String> getTags() {
return new MultiMapLookup<>(rawStmtDef.getTags(), block.getTags());
}
@Override
public String toString() {
return "stmt(name:" + getName() + ", stmt:" + getStmt() + ", tags:(" + getTags() + "), params:(" + getParams() +"), bindings:(" + getBindings()+"))";
return "stmt(name:" + getName() + ", stmt:" + getStmt() + ", tags:(" + getTags() + "), params:(" + getParams() + "), bindings:(" + getBindings() + "))";
}
@Override

View File

@ -432,15 +432,19 @@ public class SimpleActivity implements Activity, ProgressCapable {
}
List<OpTemplate> stmts = stmtsDocList.getStmts(tagfilter);
List<Long> ratios = new ArrayList<>(stmts.size());
for (int i = 0; i < stmts.size(); i++) {
long ratio = stmts.get(i).removeParamOrDefault("ratio", 1);
ratios.add(ratio);
}
if (stmts.size() == 0) {
throw new BasicError("There were no active statements with tag filter '" + tagfilter + "'");
}
for (OpTemplate optemplate : stmts) {
long ratio = optemplate.getParamOrDefault("ratio", 1);
// CommandTemplate cmd = new CommandTemplate(optemplate);
for (int i = 0; i < stmts.size(); i++) {
long ratio = ratios.get(i);
OpTemplate optemplate = stmts.get(i);
O driverSpecificOp = opinit.apply(optemplate);
planner.addOp(driverSpecificOp, ratio);
}