forked from datastax/gatling-dse-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DseCqlStatements.scala
270 lines (237 loc) · 10.3 KB
/
DseCqlStatements.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright (c) 2018 Datastax Inc.
*
* This software can be used solely with DataStax products. Please consult the file LICENSE.md.
*/
package com.datastax.gatling.plugin.model
import java.nio.ByteBuffer
import com.datastax.oss.driver.api.core.cql._
import com.datastax.gatling.plugin.exceptions.DseCqlStatementException
import com.datastax.gatling.plugin.utils.CqlPreparedStatementUtil
import com.datastax.oss.driver.api.core.`type`.DataType
import io.gatling.commons.validation._
import io.gatling.core.session._
import scala.collection.JavaConverters._
import scala.util.{Try, Failure => TryFailure, Success => TrySuccess}
trait DseCqlStatement[T <: Statement[T], B <: StatementBuilder[B,T]] extends DseStatement[B]
/**
* Simple CQL Statement from the java driver
*
* @param statement the statement to execute
*/
case class DseCqlSimpleStatement(statement: SimpleStatement)
extends DseCqlStatement[SimpleStatement, SimpleStatementBuilder] {
def buildFromSession(gatlingSession: Session): Validation[SimpleStatementBuilder] = {
SimpleStatement.builder(statement).success
}
}
/**
* Bound CQL Prepared Statement from named parameters
*
* @param preparedStatement the prepared statement on which to bind parameters
*/
case class DseCqlBoundStatementNamed(cqlTypes: CqlPreparedStatementUtil,
preparedStatement: PreparedStatement,
builderFn: (BoundStatement) => BoundStatementBuilder)
extends DseCqlStatement[BoundStatement, BoundStatementBuilder] {
def buildFromSession(gatlingSession: Session): Validation[BoundStatementBuilder] = {
val template:BoundStatement = bindParams(
gatlingSession,
preparedStatement.bind(),
cqlTypes.getParamsMap(preparedStatement))
builderFn(template).success
}
/**
* Bind Gatling Session Params to CQL Statement by Name and Type
*
* @param gatlingSession Gatling Session
* @param template CQL Prepared Statement
* @param queryParams CQL Query Named Params and Types
* @return
*/
protected def bindParams(gatlingSession: Session, template: BoundStatement,
queryParams: Map[String, DataType]): BoundStatement = {
val completedBuilder =
queryParams.foldLeft(builderFn(template)) {
(builder, kv) =>
kv match {
case (gatlingSessionKey, valType) =>
cqlTypes.bindParamByName(gatlingSession, builder, valType, gatlingSessionKey)
}
}
completedBuilder.build()
}
}
object DseCqlBoundStatementNamed {
val defaultBuilderFn = (s:BoundStatement) => new BoundStatementBuilder(s)
def apply(cqlTypes: CqlPreparedStatementUtil,
preparedStatement: PreparedStatement):DseCqlBoundStatementNamed =
new DseCqlBoundStatementNamed(cqlTypes, preparedStatement, defaultBuilderFn)
}
/**
* Bind Gatling session values to the CQL Prepared Statement
*
* @param preparedStatement CQL Prepared Statement
* @param params Gatling Session Params
*/
case class DseCqlBoundStatementWithPassedParams(cqlTypes: CqlPreparedStatementUtil,
preparedStatement: PreparedStatement,
builderFn: (BoundStatement) => BoundStatementBuilder,
params: Expression[AnyRef]*)
extends DseCqlStatement[BoundStatement, BoundStatementBuilder] {
def buildFromSession(gatlingSession: Session): Validation[BoundStatementBuilder] = {
val parsedParams: Seq[Validation[AnyRef]] = params.map(param => param(gatlingSession))
if (parsedParams.exists(_.isInstanceOf[Failure])) {
val firstError = StringBuilder.newBuilder
parsedParams
.filter(_.isInstanceOf[Failure])
.head
.onFailure(msg => firstError.append(msg))
firstError.toString().failure
} else {
val template:BoundStatement = preparedStatement.bind(parsedParams.map(_.get): _*)
builderFn(template).success
}
}
}
object DseCqlBoundStatementWithPassedParams {
val defaultBuilderFn = (s:BoundStatement) => new BoundStatementBuilder(s)
def apply(cqlTypes: CqlPreparedStatementUtil,
preparedStatement: PreparedStatement,
params: Expression[AnyRef]*):DseCqlBoundStatementWithPassedParams =
new DseCqlBoundStatementWithPassedParams(cqlTypes, preparedStatement, defaultBuilderFn, params:_*)
}
/**
* Bind Gatling session params to the CQL Prepared Statement
*
* @param sessionKeys List of session params to bind to the prepared statement
*/
case class DseCqlBoundStatementWithParamList(cqlTypes: CqlPreparedStatementUtil,
preparedStatement: PreparedStatement,
sessionKeys: Seq[String],
builderFn: (BoundStatement) => BoundStatementBuilder)
extends DseCqlStatement[BoundStatement, BoundStatementBuilder] {
/**
* Apply the Gatling session params to the Prepared statement
*
* @param gatlingSession current Gatling session
* @return
*/
def buildFromSession(gatlingSession: Session): Validation[BoundStatementBuilder] = {
val template:BoundStatement = bindParams(gatlingSession, preparedStatement.bind(), sessionKeys)
builderFn(template).success
}
/**
* Bind the Gatling session params to the CQL Prepared Statement
*
* @param gatlingSession Gatling Session
* @param template CQL Prepared Statement
* @param sessionKeys List of session params to apply, put in order of query ?'s
* @return
*/
protected def bindParams(gatlingSession: Session, template: BoundStatement,
sessionKeys: Seq[String]): BoundStatement = {
val params = cqlTypes.getParamsList(preparedStatement)
val completedBuilder =
sessionKeys.zip(Iterable.range(0,sessionKeys.size)).foldLeft(builderFn(template)) {
(builder, kv) =>
kv match {
case (sessionKey, cnt) => cqlTypes.bindParamByOrder(gatlingSession, builder, params(cnt), sessionKey, cnt)
}
}
completedBuilder.build()
}
}
object DseCqlBoundStatementWithParamList {
val defaultBuilderFn = (s:BoundStatement) => new BoundStatementBuilder(s)
def apply(cqlTypes: CqlPreparedStatementUtil,
preparedStatement: PreparedStatement,
sessionKeys: Seq[String]):DseCqlBoundStatementWithParamList =
new DseCqlBoundStatementWithParamList(cqlTypes, preparedStatement, sessionKeys, defaultBuilderFn)
}
/**
* Bound CQL Prepared Statement from Named Params
*
* @param statements CQL Prepared Statements
*/
case class DseCqlBoundBatchStatement(cqlTypes: CqlPreparedStatementUtil,
statements: Seq[PreparedStatement],
builderFn: (BoundStatement) => BoundStatementBuilder)
extends DseCqlStatement[BatchStatement, BatchStatementBuilder] {
def buildFromSession(gatlingSession: Session): Validation[BatchStatementBuilder] = {
val builder:BatchStatementBuilder = BatchStatement.builder(DefaultBatchType.LOGGED)
val batchables = statements.map(bindParams(gatlingSession))
builder.addStatements(batchables:_*).success
}
/**
* Bind Gatling Session Params to CQL Statement by Name and Type
*
* @param gatlingSession Gatling Session
* @param statement CQL Prepared Statement
* @return
*/
def bindParams(gatlingSession: Session)(statement: PreparedStatement): BoundStatement = {
val queryParams: Map[String, DataType] = cqlTypes.getParamsMap(statement)
val initBuilder:BoundStatementBuilder = builderFn(statement.bind())
val completedBuilder =
queryParams.foldLeft(initBuilder) {
(builder, kv) =>
kv match {
case (gatlingSessionKey, valType) =>
cqlTypes.bindParamByName(gatlingSession, builder, valType, gatlingSessionKey)
}
}
completedBuilder.build()
}
}
object DseCqlBoundBatchStatement {
val defaultBuilderFn = (s:BoundStatement) => new BoundStatementBuilder(s)
def apply(cqlTypes: CqlPreparedStatementUtil,
statements: Seq[PreparedStatement]):DseCqlBoundBatchStatement =
new DseCqlBoundBatchStatement(cqlTypes, statements, defaultBuilderFn)
}
/**
* Set a custom payload on the statement
*
* @param statement SimpleStaten
* @param payloadRef session variable for custom payload
*/
case class DseCqlCustomPayloadStatement(statement: SimpleStatement, payloadRef: String)
extends DseCqlStatement[SimpleStatement, SimpleStatementBuilder] {
def buildFromSession(gatlingSession: Session): Validation[SimpleStatementBuilder] = {
if (!gatlingSession.contains(payloadRef)) {
throw new DseCqlStatementException(s"Passed sessionKey: {$payloadRef} does not exist in Session.")
}
Try {
val payload = gatlingSession(payloadRef).as[Map[String, ByteBuffer]].asJava
statement.setCustomPayload(payload)
} match {
case TrySuccess(stmt) => SimpleStatement.builder(stmt).success
case TryFailure(error) => error.getMessage.failure
}
}
}
/**
* Fetch a prepared statement from the Gatling session, given its key and binds all its parameters.
* The prepared statement MUST have at least one variable.
*
* @param sessionKey the session key which is associated to a PreparedStatement
*/
case class DseCqlBoundStatementNamedFromSession(cqlTypes: CqlPreparedStatementUtil,
sessionKey: String,
builderFn: (BoundStatement) => BoundStatementBuilder)
extends DseCqlStatement[BoundStatement, BoundStatementBuilder] {
def buildFromSession(gatlingSession: Session): Validation[BoundStatementBuilder] = {
if (!gatlingSession.contains(sessionKey)) {
throw new DseCqlStatementException(s"Passed sessionKey: {$sessionKey} does not exist in Session.")
}
val preparedStatement = gatlingSession(sessionKey).as[PreparedStatement]
DseCqlBoundStatementNamed(cqlTypes, preparedStatement, builderFn).buildFromSession(gatlingSession)
}
}
object DseCqlBoundStatementNamedFromSession {
val defaultBuilderFn = (s:BoundStatement) => new BoundStatementBuilder(s)
def apply(cqlTypes: CqlPreparedStatementUtil,
sessionKey: String):DseCqlBoundStatementNamedFromSession =
new DseCqlBoundStatementNamedFromSession(cqlTypes, sessionKey, defaultBuilderFn)
}