mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
canonicalize cql anchor rewrites for parameterized no prepared
This commit is contained in:
parent
ef8ecdffc2
commit
8f7be5cfff
@ -4,6 +4,13 @@ import com.datastax.driver.core.ConsistencyLevel;
|
|||||||
import com.datastax.driver.core.SimpleStatement;
|
import com.datastax.driver.core.SimpleStatement;
|
||||||
import com.datastax.driver.core.Statement;
|
import com.datastax.driver.core.Statement;
|
||||||
import io.nosqlbench.virtdata.core.bindings.ValuesArrayBinder;
|
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
|
* 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
|
public class SimpleStatementValuesBinder
|
||||||
implements ValuesArrayBinder<SimpleStatement, Statement> {
|
implements ValuesArrayBinder<SimpleStatement, Statement> {
|
||||||
|
|
||||||
|
private final static Logger logger = LogManager.getLogger(SimpleStatementValuesBinder.class);
|
||||||
private final boolean parameterized;
|
private final boolean parameterized;
|
||||||
|
|
||||||
public SimpleStatementValuesBinder(boolean parameterized) {
|
public SimpleStatementValuesBinder(boolean parameterized) {
|
||||||
this.parameterized = 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
|
@Override
|
||||||
public Statement bindValues(SimpleStatement context, Object[] values) {
|
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();
|
ConsistencyLevel cl = context.getConsistencyLevel();
|
||||||
if(cl != null){
|
if (cl != null) {
|
||||||
simpleStatement.setConsistencyLevel(context.getConsistencyLevel());
|
simpleStatement.setConsistencyLevel(context.getConsistencyLevel());
|
||||||
}
|
}
|
||||||
//Does it really makes senses?
|
//Does it really makes senses?
|
||||||
ConsistencyLevel serial_cl = context.getSerialConsistencyLevel();
|
ConsistencyLevel serial_cl = context.getSerialConsistencyLevel();
|
||||||
if(serial_cl != null){
|
if (serial_cl != null) {
|
||||||
simpleStatement.setSerialConsistencyLevel(context.getSerialConsistencyLevel());
|
simpleStatement.setSerialConsistencyLevel(context.getSerialConsistencyLevel());
|
||||||
}
|
}
|
||||||
Boolean idempotent = context.isIdempotent();
|
Boolean idempotent = context.isIdempotent();
|
||||||
if(idempotent != null){
|
if (idempotent != null) {
|
||||||
simpleStatement.setIdempotent(idempotent);
|
simpleStatement.setIdempotent(idempotent);
|
||||||
}
|
}
|
||||||
return simpleStatement;
|
return simpleStatement;
|
||||||
|
@ -41,12 +41,17 @@ public class ReadyCQLStatementTemplate {
|
|||||||
private List<String> startTimers;
|
private List<String> startTimers;
|
||||||
private List<String> stopTimers;
|
private List<String> stopTimers;
|
||||||
|
|
||||||
public ReadyCQLStatementTemplate(Map<String, Object> fconfig, CqlBinderTypes binderType, Session session,
|
public ReadyCQLStatementTemplate(
|
||||||
PreparedStatement preparedStmt, long ratio, String name) {
|
Map<String, Object> fconfig,
|
||||||
|
CqlBinderTypes binderType,
|
||||||
|
Session session,
|
||||||
|
PreparedStatement preparedStmt,
|
||||||
|
long ratio, String name
|
||||||
|
) {
|
||||||
this.session = session;
|
this.session = session;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
ValuesArrayBinder<PreparedStatement, Statement> binder = binderType.get(session);
|
ValuesArrayBinder<PreparedStatement, Statement> binder = binderType.get(session);
|
||||||
logger.trace("Using binder_type=>" + binder.toString());
|
logger.trace("Using binder_type=>" + binder);
|
||||||
|
|
||||||
template = new ContextualBindingsArrayTemplate<>(
|
template = new ContextualBindingsArrayTemplate<>(
|
||||||
preparedStmt,
|
preparedStmt,
|
||||||
@ -56,6 +61,22 @@ public class ReadyCQLStatementTemplate {
|
|||||||
this.ratio = ratio;
|
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) {
|
public void addTimerStart(String name) {
|
||||||
if (startTimers == null) {
|
if (startTimers == null) {
|
||||||
startTimers = new ArrayList<>();
|
startTimers = new ArrayList<>();
|
||||||
@ -70,24 +91,6 @@ public class ReadyCQLStatementTemplate {
|
|||||||
stopTimers.add(name);
|
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() {
|
public ReadyCQLStatement resolve() {
|
||||||
return new ReadyCQLStatement(template.resolveBindings(), ratio, name)
|
return new ReadyCQLStatement(template.resolveBindings(), ratio, name)
|
||||||
.withMetrics(this.successTimer, this.errorTimer, this.rowsFetchedHisto)
|
.withMetrics(this.successTimer, this.errorTimer, this.rowsFetchedHisto)
|
||||||
@ -118,19 +121,19 @@ public class ReadyCQLStatementTemplate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addResultSetOperators(ResultSetCycleOperator... addingOperators) {
|
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];
|
ResultSetCycleOperator[] newOperators = new ResultSetCycleOperator[resultSetCycleOperators.length + addingOperators.length];
|
||||||
System.arraycopy(resultSetCycleOperators,0,newOperators,0,resultSetCycleOperators.length);
|
System.arraycopy(resultSetCycleOperators, 0, newOperators, 0, resultSetCycleOperators.length);
|
||||||
System.arraycopy(addingOperators,0,newOperators,resultSetCycleOperators.length,addingOperators.length);
|
System.arraycopy(addingOperators, 0, newOperators, resultSetCycleOperators.length, addingOperators.length);
|
||||||
this.resultSetCycleOperators=newOperators;
|
this.resultSetCycleOperators = newOperators;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addRowCycleOperators(RowCycleOperator... addingOperators) {
|
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];
|
RowCycleOperator[] newOperators = new RowCycleOperator[rowCycleOperators.length + addingOperators.length];
|
||||||
System.arraycopy(rowCycleOperators,0,newOperators,0,rowCycleOperators.length);
|
System.arraycopy(rowCycleOperators, 0, newOperators, 0, rowCycleOperators.length);
|
||||||
System.arraycopy(addingOperators, 0, newOperators,rowCycleOperators.length,addingOperators.length);
|
System.arraycopy(addingOperators, 0, newOperators, rowCycleOperators.length, addingOperators.length);
|
||||||
this.rowCycleOperators = newOperators;
|
this.rowCycleOperators = newOperators;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,13 @@ import com.datastax.driver.core.ConsistencyLevel;
|
|||||||
import com.datastax.driver.core.SimpleStatement;
|
import com.datastax.driver.core.SimpleStatement;
|
||||||
import com.datastax.driver.core.Statement;
|
import com.datastax.driver.core.Statement;
|
||||||
import io.nosqlbench.virtdata.core.bindings.ValuesArrayBinder;
|
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
|
* 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
|
public class SimpleStatementValuesBinder
|
||||||
implements ValuesArrayBinder<SimpleStatement, Statement> {
|
implements ValuesArrayBinder<SimpleStatement, Statement> {
|
||||||
|
|
||||||
|
private final static Logger logger = LogManager.getLogger(SimpleStatementValuesBinder.class);
|
||||||
|
|
||||||
private final boolean parameterized;
|
private final boolean parameterized;
|
||||||
|
|
||||||
public SimpleStatementValuesBinder(boolean parameterized) {
|
public SimpleStatementValuesBinder(boolean parameterized) {
|
||||||
this.parameterized = parameterized;
|
this.parameterized = parameterized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final static Pattern anchorPattern = Pattern.compile("(?<anchor>\\{[a-zA-Z_][-a-zA-Z_.]}|\\?[a-z]*)");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement bindValues(SimpleStatement context, Object[] values) {
|
public Statement bindValues(SimpleStatement context, Object[] values) {
|
||||||
String query = context.getQueryString();
|
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();
|
ConsistencyLevel cl = context.getConsistencyLevel();
|
||||||
if(cl != null){
|
if(cl != null){
|
||||||
simpleStatement.setConsistencyLevel(context.getConsistencyLevel());
|
simpleStatement.setConsistencyLevel(context.getConsistencyLevel());
|
||||||
|
Loading…
Reference in New Issue
Block a user