From 8f7be5cfff9aae386e8f0595e77266c48c511eef Mon Sep 17 00:00:00 2001 From: Jonathan Shook Date: Tue, 20 Jul 2021 18:26:41 -0500 Subject: [PATCH] canonicalize cql anchor rewrites for parameterized no prepared --- .../binders/SimpleStatementValuesBinder.java | 48 ++++++++++----- .../core/ReadyCQLStatementTemplate.java | 59 ++++++++++--------- .../binders/SimpleStatementValuesBinder.java | 40 +++++++++---- 3 files changed, 91 insertions(+), 56 deletions(-) diff --git a/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/binders/SimpleStatementValuesBinder.java b/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/binders/SimpleStatementValuesBinder.java index 770ba8752..944205363 100644 --- a/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/binders/SimpleStatementValuesBinder.java +++ b/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/binders/SimpleStatementValuesBinder.java @@ -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 { + 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("(?\\?([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 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; diff --git a/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/core/ReadyCQLStatementTemplate.java b/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/core/ReadyCQLStatementTemplate.java index 7fab53158..2dda0eec1 100644 --- a/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/core/ReadyCQLStatementTemplate.java +++ b/driver-cql-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/core/ReadyCQLStatementTemplate.java @@ -41,12 +41,17 @@ public class ReadyCQLStatementTemplate { private List startTimers; private List stopTimers; - public ReadyCQLStatementTemplate(Map fconfig, CqlBinderTypes binderType, Session session, - PreparedStatement preparedStmt, long ratio, String name) { + public ReadyCQLStatementTemplate( + Map fconfig, + CqlBinderTypes binderType, + Session session, + PreparedStatement preparedStmt, + long ratio, String name + ) { this.session = session; this.name = name; ValuesArrayBinder 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 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 fconfig, - Session session, - SimpleStatement simpleStatement, - long ratio, String name, - boolean parameterized, - List startTimers, - List 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; } diff --git a/driver-cqld3-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/binders/SimpleStatementValuesBinder.java b/driver-cqld3-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/binders/SimpleStatementValuesBinder.java index 770ba8752..6426b54cc 100644 --- a/driver-cqld3-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/binders/SimpleStatementValuesBinder.java +++ b/driver-cqld3-shaded/src/main/java/io/nosqlbench/activitytype/cql/statements/binders/SimpleStatementValuesBinder.java @@ -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 { + 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("(?\\{[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 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());