Skip to content

Commit c086174

Browse files
committed
CDM-55 consolidate Codec protocol version to a single place
1 parent e940045 commit c086174

13 files changed

+76
-75
lines changed

src/main/java/com/datastax/cdm/data/CqlConversion.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.datastax.oss.driver.api.core.type.*;
77
import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
88
import com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry;
9+
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

@@ -15,6 +16,7 @@
1516

1617
public class CqlConversion {
1718
public static final Logger logger = LoggerFactory.getLogger(CqlConversion.class);
19+
public static final ProtocolVersion PROTOCOL_VERSION = ProtocolVersion.DEFAULT;
1820

1921
enum Type {
2022
NONE,
@@ -234,8 +236,8 @@ protected static Object convert_CODEC(Object value, DataType fromDataType, DataT
234236
if (toCodec == null) {
235237
throw new IllegalArgumentException("No codec found in codecRegistry for Java type " + toClass.getName() + " to CQL type " + toDataType);
236238
}
237-
ByteBuffer encoded = fromCodec.encode(value, ProtocolVersion.DEFAULT);
238-
return toCodec.decode(encoded, ProtocolVersion.DEFAULT);
239+
ByteBuffer encoded = fromCodec.encode(value, PROTOCOL_VERSION);
240+
return toCodec.decode(encoded, PROTOCOL_VERSION);
239241
}
240242

241243
protected static UdtValue convert_UDT(UdtValue fromUDTValue, UserDefinedType fromUDT, UserDefinedType toUDT) {

src/test/java/com/datastax/cdm/cql/codec/BIGINT_StringCodecTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.datastax.cdm.cql.codec;
22

3+
import com.datastax.cdm.data.CqlConversion;
34
import com.datastax.cdm.properties.PropertyHelper;
4-
import com.datastax.oss.driver.api.core.ProtocolVersion;
55
import com.datastax.oss.driver.api.core.type.DataTypes;
66
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
77
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
@@ -38,27 +38,27 @@ void getCqlType_ShouldReturnIntType() {
3838

3939
@Test
4040
void encode_ShouldReturnNull_WhenValueIsNull() {
41-
ByteBuffer result = codec.encode(null, ProtocolVersion.DEFAULT);
41+
ByteBuffer result = codec.encode(null, CqlConversion.PROTOCOL_VERSION);
4242
Assertions.assertNull(result);
4343
}
4444

4545
@Test
4646
void encode_ShouldEncodeStringValueToByteBuffer_WhenValueIsNotNull() {
4747
String valueAsString = "9223372036854775807";
4848
Long value = Long.parseLong(valueAsString);
49-
ByteBuffer expected = TypeCodecs.BIGINT.encode(value, ProtocolVersion.DEFAULT);
49+
ByteBuffer expected = TypeCodecs.BIGINT.encode(value, CqlConversion.PROTOCOL_VERSION);
5050

51-
ByteBuffer result = codec.encode(valueAsString, ProtocolVersion.DEFAULT);
51+
ByteBuffer result = codec.encode(valueAsString, CqlConversion.PROTOCOL_VERSION);
5252
CodecTestHelper.assertByteBufferEquals(expected, result);
5353
}
5454

5555
@Test
5656
void decode_ShouldDecodeByteBufferToValueAndReturnAsString() {
5757
String valueAsString = "9223372036854775807";
5858
Long value = Long.parseLong(valueAsString);
59-
ByteBuffer byteBuffer = TypeCodecs.BIGINT.encode(value, ProtocolVersion.DEFAULT);
59+
ByteBuffer byteBuffer = TypeCodecs.BIGINT.encode(value, CqlConversion.PROTOCOL_VERSION);
6060

61-
String result = codec.decode(byteBuffer, ProtocolVersion.DEFAULT);
61+
String result = codec.decode(byteBuffer, CqlConversion.PROTOCOL_VERSION);
6262
Assertions.assertEquals(valueAsString, result);
6363
}
6464

src/test/java/com/datastax/cdm/cql/codec/DECIMAL_StringCodecTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.datastax.cdm.cql.codec;
22

3-
import com.datastax.oss.driver.api.core.ProtocolVersion;
3+
import com.datastax.cdm.data.CqlConversion;
44
import com.datastax.oss.driver.api.core.type.DataTypes;
55
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
66
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
@@ -32,27 +32,27 @@ void getCqlType_ShouldReturnDecimalType() {
3232

3333
@Test
3434
void encode_ShouldReturnNull_WhenValueIsNull() {
35-
ByteBuffer result = codec.encode(null, ProtocolVersion.DEFAULT);
35+
ByteBuffer result = codec.encode(null, CqlConversion.PROTOCOL_VERSION);
3636
Assertions.assertNull(result);
3737
}
3838

3939
@Test
4040
void encode_ShouldEncodeStringValueToByteBuffer_WhenValueIsNotNull() {
4141
String valueAsString = "123.456";
4242
BigDecimal value = new BigDecimal(valueAsString);
43-
ByteBuffer expected = TypeCodecs.DECIMAL.encode(value, ProtocolVersion.DEFAULT);
43+
ByteBuffer expected = TypeCodecs.DECIMAL.encode(value, CqlConversion.PROTOCOL_VERSION);
4444

45-
ByteBuffer result = codec.encode(valueAsString, ProtocolVersion.DEFAULT);
45+
ByteBuffer result = codec.encode(valueAsString, CqlConversion.PROTOCOL_VERSION);
4646
CodecTestHelper.assertByteBufferEquals(expected, result);
4747
}
4848

4949
@Test
5050
void decode_ShouldDecodeByteBufferToValueAndReturnAsString() {
5151
String valueAsString = "123.456";
5252
BigDecimal value = new BigDecimal(valueAsString);
53-
ByteBuffer byteBuffer = TypeCodecs.DECIMAL.encode(value, ProtocolVersion.DEFAULT);
53+
ByteBuffer byteBuffer = TypeCodecs.DECIMAL.encode(value, CqlConversion.PROTOCOL_VERSION);
5454

55-
String result = codec.decode(byteBuffer, ProtocolVersion.DEFAULT);
55+
String result = codec.decode(byteBuffer, CqlConversion.PROTOCOL_VERSION);
5656
Assertions.assertEquals(valueAsString, result);
5757
}
5858

src/test/java/com/datastax/cdm/cql/codec/DOUBLE_StringCodecTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.datastax.cdm.cql.codec;
22

3-
import com.datastax.oss.driver.api.core.ProtocolVersion;
3+
import com.datastax.cdm.data.CqlConversion;
44
import com.datastax.oss.driver.api.core.type.DataTypes;
55
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
66
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
@@ -32,27 +32,27 @@ void getCqlType_ShouldReturnIntType() {
3232

3333
@Test
3434
void encode_ShouldReturnNull_WhenValueIsNull() {
35-
ByteBuffer result = codec.encode(null, ProtocolVersion.DEFAULT);
35+
ByteBuffer result = codec.encode(null, CqlConversion.PROTOCOL_VERSION);
3636
Assertions.assertNull(result);
3737
}
3838

3939
@Test
4040
void encode_ShouldEncodeStringValueToByteBuffer_WhenValueIsNotNull() {
4141
String stringValue = "21474836470.7";
4242
Double value = Double.parseDouble(stringValue);
43-
ByteBuffer expected = TypeCodecs.DOUBLE.encode(value, ProtocolVersion.DEFAULT);
43+
ByteBuffer expected = TypeCodecs.DOUBLE.encode(value, CqlConversion.PROTOCOL_VERSION);
4444

45-
ByteBuffer result = codec.encode(stringValue, ProtocolVersion.DEFAULT);
45+
ByteBuffer result = codec.encode(stringValue, CqlConversion.PROTOCOL_VERSION);
4646
CodecTestHelper.assertByteBufferEquals(expected, result);
4747
}
4848

4949
@Test
5050
void decode_ShouldDecodeByteBufferToValueAndReturnAsString() {
5151
String valueAsString = "21474836470.7";
5252
Double value = Double.parseDouble(valueAsString);
53-
ByteBuffer byteBuffer = TypeCodecs.DOUBLE.encode(value, ProtocolVersion.DEFAULT);
53+
ByteBuffer byteBuffer = TypeCodecs.DOUBLE.encode(value, CqlConversion.PROTOCOL_VERSION);
5454

55-
String result = codec.decode(byteBuffer, ProtocolVersion.DEFAULT);
55+
String result = codec.decode(byteBuffer, CqlConversion.PROTOCOL_VERSION);
5656
Assertions.assertEquals(valueAsString, result);
5757
}
5858

src/test/java/com/datastax/cdm/cql/codec/INT_StringCodecTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.datastax.cdm.cql.codec;
22

3-
import com.datastax.oss.driver.api.core.ProtocolVersion;
3+
import com.datastax.cdm.data.CqlConversion;
44
import com.datastax.oss.driver.api.core.type.DataTypes;
55
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
66
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
@@ -32,27 +32,27 @@ void getCqlType_ShouldReturnIntType() {
3232

3333
@Test
3434
void encode_ShouldReturnNull_WhenValueIsNull() {
35-
ByteBuffer result = codec.encode(null, ProtocolVersion.DEFAULT);
35+
ByteBuffer result = codec.encode(null, CqlConversion.PROTOCOL_VERSION);
3636
Assertions.assertNull(result);
3737
}
3838

3939
@Test
4040
void encode_ShouldEncodeStringValueToByteBuffer_WhenValueIsNotNull() {
4141
String stringValue = "10";
4242
Integer value = Integer.valueOf(stringValue);
43-
ByteBuffer expected = TypeCodecs.INT.encode(value, ProtocolVersion.DEFAULT);
43+
ByteBuffer expected = TypeCodecs.INT.encode(value, CqlConversion.PROTOCOL_VERSION);
4444

45-
ByteBuffer result = codec.encode(stringValue, ProtocolVersion.DEFAULT);
45+
ByteBuffer result = codec.encode(stringValue, CqlConversion.PROTOCOL_VERSION);
4646
CodecTestHelper.assertByteBufferEquals(expected, result);
4747
}
4848

4949
@Test
5050
void decode_ShouldDecodeByteBufferToValueAndReturnAsString() {
5151
String valueAsString = "10";
5252
Integer value = Integer.valueOf(valueAsString);
53-
ByteBuffer byteBuffer = TypeCodecs.INT.encode(value, ProtocolVersion.DEFAULT);
53+
ByteBuffer byteBuffer = TypeCodecs.INT.encode(value, CqlConversion.PROTOCOL_VERSION);
5454

55-
String result = codec.decode(byteBuffer, ProtocolVersion.DEFAULT);
55+
String result = codec.decode(byteBuffer, CqlConversion.PROTOCOL_VERSION);
5656
Assertions.assertEquals(valueAsString, result);
5757
}
5858

src/test/java/com/datastax/cdm/cql/codec/TEXTFormat_InstantCodecTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.datastax.cdm.cql.codec;
22

3-
import com.datastax.cdm.properties.IPropertyHelper;
3+
import com.datastax.cdm.data.CqlConversion;
44
import com.datastax.cdm.properties.KnownProperties;
55
import com.datastax.cdm.properties.PropertyHelper;
6-
import com.datastax.oss.driver.api.core.ProtocolVersion;
76
import com.datastax.oss.driver.api.core.type.DataTypes;
87
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
98
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
@@ -60,27 +59,27 @@ void getCqlType_ShouldReturnTimestampType() {
6059

6160
@Test
6261
void encode_ShouldReturnNull_WhenValueIsNull() {
63-
ByteBuffer result = codec.encode(null, ProtocolVersion.DEFAULT);
62+
ByteBuffer result = codec.encode(null, CqlConversion.PROTOCOL_VERSION);
6463
Assertions.assertNull(result);
6564
}
6665

6766
@Test
6867
void encode_ShouldEncodeInstantToTextByteBuffer_WhenValueIsNotNull() {
6968
String valueAsString = "220412215715";
7069
Instant value = LocalDateTime.parse(valueAsString, formatter).toInstant(zoneOffset);
71-
ByteBuffer expected = TypeCodecs.TEXT.encode(valueAsString, ProtocolVersion.DEFAULT);
70+
ByteBuffer expected = TypeCodecs.TEXT.encode(valueAsString, CqlConversion.PROTOCOL_VERSION);
7271

73-
ByteBuffer result = codec.encode(value, ProtocolVersion.DEFAULT);
72+
ByteBuffer result = codec.encode(value, CqlConversion.PROTOCOL_VERSION);
7473
CodecTestHelper.assertByteBufferEquals(expected, result);
7574
}
7675

7776
@Test
7877
void decode_ShouldDecodeTextByteBufferAndReturnAsInstant() {
7978
String valueAsString = "220412215715";
8079
Instant value = LocalDateTime.parse(valueAsString, formatter).toInstant(zoneOffset);
81-
ByteBuffer byteBuffer = TypeCodecs.TEXT.encode(valueAsString, ProtocolVersion.DEFAULT);
80+
ByteBuffer byteBuffer = TypeCodecs.TEXT.encode(valueAsString, CqlConversion.PROTOCOL_VERSION);
8281

83-
Instant result = codec.decode(byteBuffer, ProtocolVersion.DEFAULT);
82+
Instant result = codec.decode(byteBuffer, CqlConversion.PROTOCOL_VERSION);
8483
Assertions.assertEquals(value, result);
8584
}
8685

src/test/java/com/datastax/cdm/cql/codec/TEXTMillis_InstantCodecTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.datastax.cdm.cql.codec;
22

3+
import com.datastax.cdm.data.CqlConversion;
34
import com.datastax.cdm.properties.PropertyHelper;
4-
import com.datastax.oss.driver.api.core.ProtocolVersion;
55
import com.datastax.oss.driver.api.core.type.DataTypes;
66
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
77
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
@@ -39,27 +39,27 @@ void getCqlType_ShouldReturnTimestampType() {
3939

4040
@Test
4141
void encode_ShouldReturnNull_WhenValueIsNull() {
42-
ByteBuffer result = codec.encode(null, ProtocolVersion.DEFAULT);
42+
ByteBuffer result = codec.encode(null, CqlConversion.PROTOCOL_VERSION);
4343
Assertions.assertNull(result);
4444
}
4545

4646
@Test
4747
void encode_ShouldEncodeInstantToTextByteBuffer_WhenValueIsNotNull() {
4848
String valueAsString = "1681333035000";
4949
Instant value = Instant.ofEpochMilli(Long.parseLong(valueAsString));
50-
ByteBuffer expected = TypeCodecs.TEXT.encode(valueAsString, ProtocolVersion.DEFAULT);
50+
ByteBuffer expected = TypeCodecs.TEXT.encode(valueAsString, CqlConversion.PROTOCOL_VERSION);
5151

52-
ByteBuffer result = codec.encode(value, ProtocolVersion.DEFAULT);
52+
ByteBuffer result = codec.encode(value, CqlConversion.PROTOCOL_VERSION);
5353
CodecTestHelper.assertByteBufferEquals(expected, result);
5454
}
5555

5656
@Test
5757
void decode_ShouldDecodeTextByteBufferAndReturnAsInstant() {
5858
String valueAsString = "1681333035000";
5959
Instant value = Instant.ofEpochMilli(Long.parseLong(valueAsString));
60-
ByteBuffer byteBuffer = TypeCodecs.TEXT.encode(valueAsString, ProtocolVersion.DEFAULT);
60+
ByteBuffer byteBuffer = TypeCodecs.TEXT.encode(valueAsString, CqlConversion.PROTOCOL_VERSION);
6161

62-
Instant result = codec.decode(byteBuffer, ProtocolVersion.DEFAULT);
62+
Instant result = codec.decode(byteBuffer, CqlConversion.PROTOCOL_VERSION);
6363
Assertions.assertEquals(value, result);
6464
}
6565

src/test/java/com/datastax/cdm/cql/codec/TEXT_BigDecimalCodecTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.datastax.cdm.cql.codec;
22

3-
import com.datastax.oss.driver.api.core.ProtocolVersion;
3+
import com.datastax.cdm.data.CqlConversion;
44
import com.datastax.oss.driver.api.core.type.DataTypes;
55
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
66
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
@@ -32,27 +32,27 @@ void getCqlType_ShouldReturnTextType() {
3232

3333
@Test
3434
void encode_ShouldReturnNull_WhenValueIsNull() {
35-
ByteBuffer result = codec.encode(null, ProtocolVersion.DEFAULT);
35+
ByteBuffer result = codec.encode(null, CqlConversion.PROTOCOL_VERSION);
3636
Assertions.assertNull(result);
3737
}
3838

3939
@Test
4040
void encode_ShouldEncodeNumberToTextByteBuffer_WhenValueIsNotNull() {
4141
String valueAsString = "12345.6789";
4242
BigDecimal value = new BigDecimal(valueAsString);
43-
ByteBuffer expected = TypeCodecs.TEXT.encode(valueAsString, ProtocolVersion.DEFAULT);
43+
ByteBuffer expected = TypeCodecs.TEXT.encode(valueAsString, CqlConversion.PROTOCOL_VERSION);
4444

45-
ByteBuffer result = codec.encode(value, ProtocolVersion.DEFAULT);
45+
ByteBuffer result = codec.encode(value, CqlConversion.PROTOCOL_VERSION);
4646
CodecTestHelper.assertByteBufferEquals(expected, result);
4747
}
4848

4949
@Test
5050
void decode_ShouldDecodeTextByteBufferAndReturnAsNumber() {
5151
String valueAsString = "12345.6789";
5252
BigDecimal value = new BigDecimal(valueAsString);
53-
ByteBuffer byteBuffer = TypeCodecs.TEXT.encode(valueAsString, ProtocolVersion.DEFAULT);
53+
ByteBuffer byteBuffer = TypeCodecs.TEXT.encode(valueAsString, CqlConversion.PROTOCOL_VERSION);
5454

55-
BigDecimal result = codec.decode(byteBuffer, ProtocolVersion.DEFAULT);
55+
BigDecimal result = codec.decode(byteBuffer, CqlConversion.PROTOCOL_VERSION);
5656
Assertions.assertEquals(value, result);
5757
}
5858

src/test/java/com/datastax/cdm/cql/codec/TEXT_DoubleCodecTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.datastax.cdm.cql.codec;
22

3-
import com.datastax.oss.driver.api.core.ProtocolVersion;
3+
import com.datastax.cdm.data.CqlConversion;
44
import com.datastax.oss.driver.api.core.type.DataTypes;
55
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
66
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
@@ -32,7 +32,7 @@ void getCqlType_ShouldReturnDoubleType() {
3232

3333
@Test
3434
void encode_ShouldReturnNull_WhenValueIsNull() {
35-
ByteBuffer result = codec.encode(null, ProtocolVersion.DEFAULT);
35+
ByteBuffer result = codec.encode(null, CqlConversion.PROTOCOL_VERSION);
3636
Assertions.assertNull(result);
3737
}
3838

@@ -42,9 +42,9 @@ void encode_ShouldEncodeNumberToTextByteBuffer_WhenValueIsNotNull() {
4242
Double value = Double.valueOf(valueAsString);
4343
// Because the valueAsString could be a Double with a decimal point or with an E notation
4444
// but we expect the encoded value to be from a proper CQL type, we need to encode the String.valueOf(value)
45-
ByteBuffer expected = TypeCodecs.TEXT.encode(valueAsString, ProtocolVersion.DEFAULT);
45+
ByteBuffer expected = TypeCodecs.TEXT.encode(valueAsString, CqlConversion.PROTOCOL_VERSION);
4646

47-
ByteBuffer result = codec.encode(value, ProtocolVersion.DEFAULT);
47+
ByteBuffer result = codec.encode(value, CqlConversion.PROTOCOL_VERSION);
4848
CodecTestHelper.assertByteBufferEquals(expected, result);
4949
}
5050

@@ -53,9 +53,9 @@ void decode_ShouldDecodeTextByteBufferAndReturnAsNumber() {
5353
String valueAsString = "21474836470.7";
5454
Double value = Double.valueOf(valueAsString);
5555
// encoding could be from user input, so may not be in Java Double.toString() format
56-
ByteBuffer byteBuffer = TypeCodecs.TEXT.encode(valueAsString, ProtocolVersion.DEFAULT);
56+
ByteBuffer byteBuffer = TypeCodecs.TEXT.encode(valueAsString, CqlConversion.PROTOCOL_VERSION);
5757

58-
Double result = codec.decode(byteBuffer, ProtocolVersion.DEFAULT);
58+
Double result = codec.decode(byteBuffer, CqlConversion.PROTOCOL_VERSION);
5959
Assertions.assertEquals(value, result);
6060
}
6161

0 commit comments

Comments
 (0)