insert raw/parametrized in SimpleStatementValuesBinder

This commit is contained in:
Pierre Sauvage 2020-04-17 15:58:57 +02:00
parent 4c586c6335
commit 97a820391f
4 changed files with 43 additions and 80 deletions

View File

@ -155,7 +155,7 @@ public class CqlActivity extends SimpleActivity implements Activity, ActivityDef
ParsedStmt parsed = stmtDef.getParsed().orError();
boolean prepared = Boolean.valueOf(stmtDef.getParams().getOrDefault("prepared", "true"));
boolean raw = Boolean.valueOf(stmtDef.getParams().getOrDefault("raw", "false"));
boolean parametrized = Boolean.valueOf(stmtDef.getParams().getOrDefault("parametrized", "false"));
long ratio = Long.valueOf(stmtDef.getParams().getOrDefault("ratio", "1"));
Optional<ConsistencyLevel> cl = Optional.ofNullable(
@ -205,17 +205,6 @@ public class CqlActivity extends SimpleActivity implements Activity, ActivityDef
.getOrDefault("binder", CqlBinderTypes.DEFAULT.toString()));
template = new ReadyCQLStatementTemplate(binderType, getSession(), prepare, ratio, parsed.getName());
} else if (raw) {
cl.ifPresent((conlvl) -> {
psummary.append(" consistency_level=>").append(conlvl);
});
serial_cl.ifPresent((scl) -> {
psummary.append(" serial_consistency_level=>").append(scl);
});
idempotent.ifPresent((i) -> {
psummary.append(" idempotent=>").append(i);
});
template = new ReadyCQLStatementTemplate(getSession(), stmtForDriver, ratio, parsed.getName(), cl, serial_cl, idempotent);
} else {
SimpleStatement simpleStatement = new SimpleStatement(stmtForDriver);
cl.ifPresent((conlvl) -> {
@ -230,7 +219,7 @@ public class CqlActivity extends SimpleActivity implements Activity, ActivityDef
psummary.append(" idempotent=>").append(i);
simpleStatement.setIdempotent(i);
});
template = new ReadyCQLStatementTemplate(getSession(), simpleStatement, ratio, parsed.getName());
template = new ReadyCQLStatementTemplate(getSession(), simpleStatement, ratio, parsed.getName(), parametrized);
}
Optional.ofNullable(stmtDef.getParams().getOrDefault("save", null))

View File

@ -1,48 +0,0 @@
package io.nosqlbench.activitytype.cql.statements.binders;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Statement;
import io.nosqlbench.virtdata.core.bindings.ValuesArrayBinder;
import java.util.Optional;
/**
* This binder is only meant to be used to directly generate final SimpleStatement query
* without use of preparedStatement. This should be avoided any time possible since it's far more
* less optimized than use of PreparedStatement. Only Use it when PreparedStatement is not possible
*/
public class RawValueBinder
implements ValuesArrayBinder<String[], Statement> {
private Optional<ConsistencyLevel> cl;
private Optional<ConsistencyLevel> serial_cl;
private Optional<Boolean> idempotent;
public RawValueBinder(Optional<ConsistencyLevel> cl, Optional<ConsistencyLevel> serial_cl, Optional<Boolean> idempotent) {
this.cl = cl;
this.serial_cl = serial_cl;
this.idempotent = idempotent;
}
@Override
public Statement bindValues(String[] context, Object[] values) {
StringBuilder sb = new StringBuilder();
sb.append(context[0]);
for(int i=1; i<context.length; i++){
sb.append(values[i-1]);
sb.append(context[i]);
}
SimpleStatement simpleStatement = new SimpleStatement(sb.toString());
cl.ifPresent((conlvl) -> {
simpleStatement.setConsistencyLevel(conlvl);
});
serial_cl.ifPresent((scl) -> {
simpleStatement.setSerialConsistencyLevel(scl);
});
idempotent.ifPresent((i) -> {
simpleStatement.setIdempotent(i);
});
return simpleStatement;
}
}

View File

@ -1,19 +1,55 @@
package io.nosqlbench.activitytype.cql.statements.binders;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Statement;
import io.nosqlbench.virtdata.core.bindings.ValuesArrayBinder;
/**
* This binder is not meant to be used with anything but DDL or statements
* which should not be trying to parameterize values in general. If this changes,
* support will be added for parameterized values here.
* which should not be trying to parameterize values in general.
* Parametrized values are still possible through parametrized constructor parameter.
* This binder should be avoided in favor of binders returning PreparedStatement
*/
public class SimpleStatementValuesBinder
implements ValuesArrayBinder<SimpleStatement, Statement> {
private final boolean parametrized;
public SimpleStatementValuesBinder(boolean parametrized){
this.parametrized = parametrized;
}
@Override
public Statement bindValues(SimpleStatement context, Object[] values) {
return new SimpleStatement(context.getQueryString(), values);
String query = context.getQueryString();
if(parametrized) {
String[] splits = query.split("\\?");
assert splits.length == values.length+1;
StringBuilder sb = new StringBuilder();
sb.append(splits[0]);
for(int i = 1; i < splits.length; i++) {
sb.append(values[i - 1]);
sb.append(splits[i]);
}
query = sb.toString();
System.out.println(query);
}
SimpleStatement simpleStatement = new SimpleStatement(query);
ConsistencyLevel cl = context.getConsistencyLevel();
if(cl != null){
simpleStatement.setConsistencyLevel(context.getConsistencyLevel());
}
//Does it really makes senses?
ConsistencyLevel serial_cl = context.getSerialConsistencyLevel();
if(serial_cl != null){
simpleStatement.setSerialConsistencyLevel(context.getSerialConsistencyLevel());
}
Boolean idempotent = context.isIdempotent();
if(idempotent != null){
simpleStatement.setIdempotent(idempotent);
}
return simpleStatement;
}
}

View File

@ -2,7 +2,6 @@ package io.nosqlbench.activitytype.cql.statements.core;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Timer;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
@ -11,7 +10,6 @@ import io.nosqlbench.activitytype.cql.api.ResultSetCycleOperator;
import io.nosqlbench.activitytype.cql.api.RowCycleOperator;
import io.nosqlbench.activitytype.cql.core.CqlActivity;
import io.nosqlbench.activitytype.cql.statements.binders.CqlBinderTypes;
import io.nosqlbench.activitytype.cql.statements.binders.RawValueBinder;
import io.nosqlbench.activitytype.cql.statements.binders.SimpleStatementValuesBinder;
import io.nosqlbench.engine.api.metrics.ActivityMetrics;
import io.nosqlbench.virtdata.core.bindings.BindingsTemplate;
@ -21,7 +19,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Writer;
import java.util.Optional;
public class ReadyCQLStatementTemplate {
@ -53,24 +50,13 @@ public class ReadyCQLStatementTemplate {
this.ratio = ratio;
}
public ReadyCQLStatementTemplate(Session session, SimpleStatement simpleStatement, long ratio, String name) {
public ReadyCQLStatementTemplate(Session session, SimpleStatement simpleStatement, long ratio, String name, boolean parametrized) {
this.session = session;
this.name = name;
template = new ContextualBindingsArrayTemplate<>(
simpleStatement,
new BindingsTemplate(),
new SimpleStatementValuesBinder()
);
this.ratio = ratio;
}
public ReadyCQLStatementTemplate(Session session, String stmtForDriver, long ratio, String name, Optional<ConsistencyLevel> cl, Optional<ConsistencyLevel> serial_cl, Optional<Boolean> idempotent) {
this.session = session;
this.name = name;
template = new ContextualBindingsArrayTemplate<>(
stmtForDriver.split("\\?"),
new BindingsTemplate(),
new RawValueBinder(cl, serial_cl, idempotent)
new SimpleStatementValuesBinder(parametrized)
);
this.ratio = ratio;
}