Skip to content

Commit

Permalink
Use correct PLP threshold in BinaryCodec of 8000
Browse files Browse the repository at this point in the history
Binary values (byte[]/ByteBuffer) are now correctly encoded using PLP when the data size exceeds 8000 bytes. Previously, the threshold was set to 65535 which rendered value sizes between 8001 and 65534 not usable.

[closes #151]
  • Loading branch information
mp911de committed May 4, 2020
1 parent b6585f3 commit 4ed9988
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/main/java/io/r2dbc/mssql/codec/BinaryCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Encoded encode(ByteBufAllocator allocator, RpcParameterContext context, O

byte[] bytes = (byte[]) value;

if (bytes.length >= Encode.U_SHORT_MAX_VALUE) {
if (exceedsBigVarbinary(bytes.length)) {
return BlobCodec.INSTANCE.encode(allocator, context, Blob.from(Mono.just(ByteBuffer.wrap(bytes))));
}

Expand All @@ -100,7 +100,7 @@ public Encoded encode(ByteBufAllocator allocator, RpcParameterContext context, O

ByteBuffer bytes = (ByteBuffer) value;

if (bytes.remaining() >= Encode.U_SHORT_MAX_VALUE) {
if (exceedsBigVarbinary(bytes.remaining())) {
return BlobCodec.INSTANCE.encode(allocator, context, Blob.from(Mono.just(bytes)));
}

Expand Down Expand Up @@ -206,4 +206,8 @@ public String getFormalType() {
return FORMAL_TYPE;
}
}

private static boolean exceedsBigVarbinary(int length) {
return length > TypeUtils.SHORT_VARTYPE_MAX_BYTES;
}
}
6 changes: 5 additions & 1 deletion src/test/java/io/r2dbc/mssql/CodecIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ void shouldEncodeDoubleAsDecimal() {
testType(connection, "DECIMAL(38,5)", new BigDecimal("12345.12345"));
}


@Test
void shouldEncodeDoubleAsDecimal1() {
testType(connection, "DECIMAL(38,0)", new BigDecimal("12345"));
Expand Down Expand Up @@ -254,6 +253,11 @@ void shouldEncodeByteBufferAsVarBinary() {
@Test
void shouldEncodeByteArrayAsVarBinaryMax() {
testType(connection, "VARBINARY(MAX)", "foobarbaz".getBytes(), byte[].class, actual -> assertThat(actual).isEqualTo(ByteBuffer.wrap("foobarbaz".getBytes())));
testType(connection, "VARBINARY(MAX)", new byte[8000], byte[].class, actual -> assertThat(actual).isEqualTo(ByteBuffer.wrap(new byte[8000])));
testType(connection, "VARBINARY(MAX)", new byte[8001], byte[].class, actual -> assertThat(actual).isEqualTo(ByteBuffer.wrap(new byte[8001])));
testType(connection, "VARBINARY(MAX)", new byte[65534], byte[].class, actual -> assertThat(actual).isEqualTo(ByteBuffer.wrap(new byte[65534])));
testType(connection, "VARBINARY(MAX)", new byte[65535], byte[].class, actual -> assertThat(actual).isEqualTo(ByteBuffer.wrap(new byte[65535])));
testType(connection, "VARBINARY(MAX)", new byte[65536], byte[].class, actual -> assertThat(actual).isEqualTo(ByteBuffer.wrap(new byte[65536])));
}

@Test
Expand Down

0 comments on commit 4ed9988

Please sign in to comment.