Skip to content

Commit fe4f319

Browse files
author
Sylvain Lebresne
committed
Reject BatchStatement with more than 64K elements
JAVA-229 #fixes
1 parent 1bf247f commit fe4f319

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

driver-core/src/main/java/com/datastax/driver/core/BatchStatement.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ IdAndValues getIdAndValues() {
104104
*
105105
* @param statement the new statement to add.
106106
* @return this batch statement.
107+
*
108+
* @throws IllegalStateException if adding the new statement means that this
109+
* {@code BatchStatement} has more than 65536 statements (since this is the maximum number
110+
* of statements for a BatchStatement allowed by the underlying protocol).
107111
*/
108112
public BatchStatement add(Statement statement) {
109113

@@ -115,6 +119,8 @@ public BatchStatement add(Statement statement) {
115119
add(subStatements);
116120
}
117121
} else {
122+
if (statements.size() >= 0xFFFF)
123+
throw new IllegalStateException("Batch statement cannot contain more than " + 0xFFFF + " statements.");
118124
statements.add(statement);
119125
}
120126
return this;

driver-core/src/main/java/com/datastax/driver/core/Requests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ public static class Batch extends Message.Request {
256256
public static final Message.Coder<Batch> coder = new Message.Coder<Batch>() {
257257
public void encode(Batch msg, ChannelBuffer dest) {
258258
int queries = msg.queryOrIdList.size();
259+
assert queries <= 0xFFFF;
259260

260261
dest.writeByte(fromType(msg.type));
261262
dest.writeShort(queries);

0 commit comments

Comments
 (0)