public List<ByteBuffer> getRoutingKeys(ITuple tuple) { List<ByteBuffer> keys = new ArrayList<>(routingKeys.size()); for (String s : routingKeys) { Object value = tuple.getValueByField(s); ByteBuffer serialized = CodecRegistry.DEFAULT_INSTANCE.codecFor(value).serialize(value, ProtocolVersion.NEWEST_SUPPORTED); keys.add(serialized); } return keys; } }
DataType mainType = getPrimitiveDataTypeFromString(mainTypeString); if (mainType != null) { TypeCodec typeCodec = codecRegistry.codecFor(mainType); DataType secondParamType = getPrimitiveDataTypeFromString(secondParamTypeName); DataType mapType = DataType.map(firstParamType, secondParamType); statement.setMap(paramIndex, (Map) codecRegistry.codecFor(mapType).parse(paramValue)); return; statement.setSet(paramIndex, (Set) codecRegistry.codecFor(setType).parse(paramValue)); return; } else if (DataType.Name.LIST.toString().equalsIgnoreCase(mainTypeString)) { DataType listType = DataType.list(firstParamType); statement.setList(paramIndex, (List) codecRegistry.codecFor(listType).parse(paramValue)); return;
TypeCodec firstCodec = codecRegistry.codecFor(firstArg); if (dataType.equals(DataType.set(firstArg))) { return row.getSet(i, firstCodec.getJavaType()); TypeCodec secondCodec = codecRegistry.codecFor(secondArg); if (dataType.equals(DataType.map(firstArg, secondArg))) { return row.getMap(i, firstCodec.getJavaType(), secondCodec.getJavaType());
/** * {@inheritDoc} */ @Override public BoundStatement apply(PreparedStatement statement, List<Column> columns) { Object[] values = Column.getVals(columns); BoundStatement boundStatement = statement.bind(); for (Column col : columns) { // For native protocol V3 or below, all variables must be bound. // With native protocol V4 or above, variables can be left unset, // in which case they will be ignored server side (no tombstones will be generated). if (col.isNull()) { boundStatement.setToNull(col.getColumnName()); } else { boundStatement.set(col.getColumnName(), col.getVal(), CodecRegistry.DEFAULT_INSTANCE.codecFor(col.getVal())); } } return statement.bind(values); } }
@Override public String toString() { StringBuilder sb = new StringBuilder(); TypeCodec<Object> codec = getCodecRegistry().codecFor(definition); sb.append(codec.format(this)); return sb.toString(); } }
@Override public String toString() { StringBuilder sb = new StringBuilder(); TypeCodec<Object> codec = getCodecRegistry().codecFor(type); sb.append(codec.format(this)); return sb.toString(); } }
@Override public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { if (routingKeyValues == null) return null; ByteBuffer[] routingKeyParts = new ByteBuffer[partitionKey.size()]; for (int i = 0; i < partitionKey.size(); i++) { Object value = routingKeyValues.get(i); if (value == null) return null; TypeCodec<Object> codec = codecRegistry.codecFor(partitionKey.get(i).getType(), value); routingKeyParts[i] = codec.serialize(value, protocolVersion); } return routingKeyParts.length == 1 ? routingKeyParts[0] : Utils.compose(routingKeyParts); }
@Override protected String formatField(TupleValue value, int index) { DataType elementType = definition.getComponentTypes().get(index); TypeCodec<Object> codec = definition.getCodecRegistry().codecFor(elementType); return codec.format(value.get(index, codec.getJavaType())); }
@Test( groups = "unit", expectedExceptions = {IllegalArgumentException.class}) public void collectionElementTooLargeTest() throws Exception { DataType cqlType = DataType.list(DataType.text()); List<String> list = newArrayList(Strings.repeat("a", 65536)); TypeCodec<List<?>> codec = codecRegistry.codecFor(cqlType); codec.serialize(list, ProtocolVersion.V2); }
public void checkGetValuesReturnsSerializedValue( ProtocolVersion protocolVersion, SimpleStatement statement, TestTable table) { CodecRegistry codecRegistry = cluster().getConfiguration().getCodecRegistry(); ByteBuffer[] values = statement.getValues(protocolVersion, codecRegistry); assertThat(values.length).isEqualTo(1); assertThat(values[0]) .as("Value not serialized as expected for " + table.sampleValue) .isEqualTo( codecRegistry .codecFor(table.testColumnType) .serialize(table.sampleValue, protocolVersion)); }
@Test(groups = "unit") public void testCustomList() throws Exception { DataType cqlType = list(CUSTOM_FOO); TypeCodec<List<?>> codec = codecRegistry.codecFor(cqlType); assertThat(codec).isNotNull().accepts(cqlType); }
@Test(groups = "unit") public void testCustomSet() throws Exception { DataType cqlType = set(CUSTOM_FOO); TypeCodec<Set<?>> codec = codecRegistry.codecFor(cqlType); assertThat(codec).isNotNull().accepts(cqlType); }
@Test(groups = "unit") public void formatNativeTest() { for (DataType dt : DataType.allPrimitiveTypes()) { if (exclude(dt)) continue; for (TestValue value : primitiveTestValues(dt)) assertThat(codecRegistry.codecFor(dt).format(value.javaObject)) .as("Formatting a %s expecting %s", dt, value.cqlOutputString) .isEqualTo(value.cqlOutputString); } }
@Test(groups = "unit") public void testCustomValueMap() throws Exception { DataType cqlType = map(text(), CUSTOM_FOO); TypeCodec<Map<?, ?>> codec = codecRegistry.codecFor(cqlType); assertThat(codec).isNotNull().accepts(cqlType); }
@Test(groups = "unit", dataProvider = "cql") public void should_find_codec_by_cql_type(DataType cqlType, TypeCodec<?> expected) { // given CodecRegistry registry = new CodecRegistry(); // when TypeCodec<?> actual = registry.codecFor(cqlType); // then assertThat(actual).isNotNull().accepts(cqlType).isSameAs(expected); }
@Test(groups = "unit") public void testCustomKeyMap() throws Exception { DataType cqlType = map(CUSTOM_FOO, text()); TypeCodec<Map<?, ?>> codec = codecRegistry.codecFor(cqlType); assertThat(codec).isNotNull().accepts(cqlType); }
@Test(groups = "unit", dataProvider = "cqlAndValue") public void should_find_codec_by_cql_type_and_value( DataType cqlType, Object value, TypeCodec<?> expected) { // given CodecRegistry registry = new CodecRegistry(); // when TypeCodec<?> actual = registry.codecFor(cqlType, value); // then assertThat(actual).isNotNull().accepts(cqlType).accepts(value).isSameAs(expected); }
@Test(groups = "unit") public void should_ignore_codec_colliding_with_already_registered_codec() { MemoryAppender logs = startCapturingLogs(); CodecRegistry registry = new CodecRegistry(); TypeCodec<?> newCodec = mockCodec(cint(), of(Integer.class)); registry.register(newCodec); assertThat(logs.getNext()).contains("Ignoring codec MockCodec"); assertThat(registry.codecFor(cint(), Integer.class)).isNotSameAs(newCodec); stopCapturingLogs(logs); }
@Test(groups = "unit") public void should_ignore_codec_colliding_with_already_generated_codec() { MemoryAppender logs = startCapturingLogs(); CodecRegistry registry = new CodecRegistry(); // Force generation of a list token from the default token registry.codecFor(list(cint()), listOf(Integer.class)); TypeCodec<?> newCodec = mockCodec(list(cint()), listOf(Integer.class)); registry.register(newCodec); assertThat(logs.getNext()).contains("Ignoring codec MockCodec"); assertThat(registry.codecFor(list(cint()), listOf(Integer.class))).isNotSameAs(newCodec); stopCapturingLogs(logs); }
@Test(groups = "unit") public void should_find_newly_registered_codec_by_cql_type_and_java_type() { // given CodecRegistry registry = new CodecRegistry(); TypeCodec<?> expected = mockCodec(list(text()), listOf(String.class)); registry.register(expected); // when TypeCodec<?> actual = registry.codecFor(list(text()), listOf(String.class)); // then assertThat(actual).isNotNull().isSameAs(expected); }