canonicalize cql anchor rewrites for parameterized no prepared

This commit is contained in:
Jonathan Shook 2021-07-20 18:26:41 -05:00
parent ef8ecdffc2
commit 8f7be5cfff
3 changed files with 91 additions and 56 deletions

View File

@ -4,6 +4,13 @@ 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 org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This binder is not meant to be used with anything but DDL or statements
@ -14,40 +21,49 @@ import io.nosqlbench.virtdata.core.bindings.ValuesArrayBinder;
public class SimpleStatementValuesBinder
implements ValuesArrayBinder<SimpleStatement, Statement> {
private final static Logger logger = LogManager.getLogger(SimpleStatementValuesBinder.class);
private final boolean parameterized;
public SimpleStatementValuesBinder(boolean parameterized) {
this.parameterized = parameterized;
}
private final static Pattern anchorPattern = Pattern.compile("(?<name>\\?([a-zA-Z_][-a-zA-Z_.-])*|\\{[a-zA-Z_][-a-zA-Z_.-]*})");
@Override
public Statement bindValues(SimpleStatement context, Object[] values) {
String query = context.getQueryString();
if (parameterized) {
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);
String query = context.getQueryString();
SimpleStatement simpleStatement = null;
if (parameterized) {
Matcher matcher = anchorPattern.matcher(query);
StringBuilder sb = new StringBuilder();
List<String> anchors = new ArrayList<>();
while (matcher.find()) {
String name = matcher.group("name");
anchors.add(name == null ? "_unnamed_" : name);
matcher.appendReplacement(sb, "?");
}
matcher.appendTail(sb);
assert (anchors.size() == values.length);
query = sb.toString();
simpleStatement = new SimpleStatement(query, values);
} else {
simpleStatement = new SimpleStatement(query);
}
SimpleStatement simpleStatement = new SimpleStatement(query);
ConsistencyLevel cl = context.getConsistencyLevel();
if(cl != null){
if (cl != null) {
simpleStatement.setConsistencyLevel(context.getConsistencyLevel());
}
//Does it really makes senses?
ConsistencyLevel serial_cl = context.getSerialConsistencyLevel();
if(serial_cl != null){
if (serial_cl != null) {
simpleStatement.setSerialConsistencyLevel(context.getSerialConsistencyLevel());
}
Boolean idempotent = context.isIdempotent();
if(idempotent != null){
if (idempotent != null) {
simpleStatement.setIdempotent(idempotent);
}
return simpleStatement;

View File

@ -41,12 +41,17 @@ public class ReadyCQLStatementTemplate {
private List<String> startTimers;
private List<String> stopTimers;
public ReadyCQLStatementTemplate(Map<String, Object> fconfig, CqlBinderTypes binderType, Session session,
PreparedStatement preparedStmt, long ratio, String name) {
public ReadyCQLStatementTemplate(
Map<String, Object> fconfig,
CqlBinderTypes binderType,
Session session,
PreparedStatement preparedStmt,
long ratio, String name
) {
this.session = session;
this.name = name;
ValuesArrayBinder<PreparedStatement, Statement> binder = binderType.get(session);
logger.trace("Using binder_type=>" + binder.toString());
logger.trace("Using binder_type=>" + binder);
template = new ContextualBindingsArrayTemplate<>(
preparedStmt,
@ -56,6 +61,22 @@ public class ReadyCQLStatementTemplate {
this.ratio = ratio;
}
public ReadyCQLStatementTemplate(
Map<String, Object> fconfig,
Session session,
SimpleStatement simpleStatement,
long ratio, String name,
boolean parameterized) {
this.session = session;
this.name = name;
template = new ContextualBindingsArrayTemplate<>(
simpleStatement,
new BindingsTemplate(fconfig),
new SimpleStatementValuesBinder(parameterized)
);
this.ratio = ratio;
}
public void addTimerStart(String name) {
if (startTimers == null) {
startTimers = new ArrayList<>();
@ -70,24 +91,6 @@ public class ReadyCQLStatementTemplate {
stopTimers.add(name);
}
public ReadyCQLStatementTemplate(
Map<String, Object> fconfig,
Session session,
SimpleStatement simpleStatement,
long ratio, String name,
boolean parameterized,
List<String> startTimers,
List<String> stopTimers) {
this.session = session;
this.name = name;
template = new ContextualBindingsArrayTemplate<>(
simpleStatement,
new BindingsTemplate(fconfig),
new SimpleStatementValuesBinder(parameterized)
);
this.ratio = ratio;
}
public ReadyCQLStatement resolve() {
return new ReadyCQLStatement(template.resolveBindings(), ratio, name)
.withMetrics(this.successTimer, this.errorTimer, this.rowsFetchedHisto)
@ -118,19 +121,19 @@ public class ReadyCQLStatementTemplate {
}
public void addResultSetOperators(ResultSetCycleOperator... addingOperators) {
resultSetCycleOperators = (resultSetCycleOperators==null) ? new ResultSetCycleOperator[0]: resultSetCycleOperators;
resultSetCycleOperators = (resultSetCycleOperators == null) ? new ResultSetCycleOperator[0] : resultSetCycleOperators;
ResultSetCycleOperator[] newOperators = new ResultSetCycleOperator[resultSetCycleOperators.length + addingOperators.length];
System.arraycopy(resultSetCycleOperators,0,newOperators,0,resultSetCycleOperators.length);
System.arraycopy(addingOperators,0,newOperators,resultSetCycleOperators.length,addingOperators.length);
this.resultSetCycleOperators=newOperators;
System.arraycopy(resultSetCycleOperators, 0, newOperators, 0, resultSetCycleOperators.length);
System.arraycopy(addingOperators, 0, newOperators, resultSetCycleOperators.length, addingOperators.length);
this.resultSetCycleOperators = newOperators;
}
public void addRowCycleOperators(RowCycleOperator... addingOperators) {
rowCycleOperators = (rowCycleOperators==null) ? new RowCycleOperator[0]: rowCycleOperators;
rowCycleOperators = (rowCycleOperators == null) ? new RowCycleOperator[0] : rowCycleOperators;
RowCycleOperator[] newOperators = new RowCycleOperator[rowCycleOperators.length + addingOperators.length];
System.arraycopy(rowCycleOperators,0,newOperators,0,rowCycleOperators.length);
System.arraycopy(addingOperators, 0, newOperators,rowCycleOperators.length,addingOperators.length);
System.arraycopy(rowCycleOperators, 0, newOperators, 0, rowCycleOperators.length);
System.arraycopy(addingOperators, 0, newOperators, rowCycleOperators.length, addingOperators.length);
this.rowCycleOperators = newOperators;
}

View File

@ -4,6 +4,13 @@ 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 org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This binder is not meant to be used with anything but DDL or statements
@ -14,29 +21,38 @@ import io.nosqlbench.virtdata.core.bindings.ValuesArrayBinder;
public class SimpleStatementValuesBinder
implements ValuesArrayBinder<SimpleStatement, Statement> {
private final static Logger logger = LogManager.getLogger(SimpleStatementValuesBinder.class);
private final boolean parameterized;
public SimpleStatementValuesBinder(boolean parameterized) {
this.parameterized = parameterized;
}
private final static Pattern anchorPattern = Pattern.compile("(?<anchor>\\{[a-zA-Z_][-a-zA-Z_.]}|\\?[a-z]*)");
@Override
public Statement bindValues(SimpleStatement context, Object[] values) {
String query = context.getQueryString();
if (parameterized) {
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 = null;
if (parameterized) {
Matcher matcher = anchorPattern.matcher(query);
StringBuilder sb = new StringBuilder();
List<String> anchors = new ArrayList<>();
while (matcher.find()) {
String name = matcher.group("name");
anchors.add(name == null ? "_unnamed_" : name);
matcher.appendReplacement(sb, "?");
}
matcher.appendTail(sb);
assert (anchors.size() == values.length);
query = sb.toString();
simpleStatement = new SimpleStatement(query, values);
} else {
simpleStatement = new SimpleStatement(query);
}
SimpleStatement simpleStatement = new SimpleStatement(query);
ConsistencyLevel cl = context.getConsistencyLevel();
if(cl != null){
simpleStatement.setConsistencyLevel(context.getConsistencyLevel());