use of com.datastax.oss.driver.api.core.type.codec.TypeCodec in project java-driver by datastax.
the class CachingCodecRegistry method createCodec.
// Try to create a codec when we haven't found it in the cache.
// Variant where the Java type is unknown.
@NonNull
protected TypeCodec<?> createCodec(@NonNull DataType cqlType) {
if (cqlType instanceof ListType) {
DataType elementType = ((ListType) cqlType).getElementType();
TypeCodec<Object> elementCodec = codecFor(elementType);
return TypeCodecs.listOf(elementCodec);
} else if (cqlType instanceof SetType) {
DataType elementType = ((SetType) cqlType).getElementType();
TypeCodec<Object> elementCodec = codecFor(elementType);
return TypeCodecs.setOf(elementCodec);
} else if (cqlType instanceof MapType) {
DataType keyType = ((MapType) cqlType).getKeyType();
DataType valueType = ((MapType) cqlType).getValueType();
TypeCodec<Object> keyCodec = codecFor(keyType);
TypeCodec<Object> valueCodec = codecFor(valueType);
return TypeCodecs.mapOf(keyCodec, valueCodec);
} else if (cqlType instanceof TupleType) {
return TypeCodecs.tupleOf((TupleType) cqlType);
} else if (cqlType instanceof UserDefinedType) {
return TypeCodecs.udtOf((UserDefinedType) cqlType);
} else if (cqlType instanceof CustomType) {
return TypeCodecs.custom(cqlType);
}
throw new CodecNotFoundException(cqlType, null);
}
use of com.datastax.oss.driver.api.core.type.codec.TypeCodec in project java-driver by datastax.
the class RelationParser method parseOptions.
protected Map<CqlIdentifier, Object> parseOptions(AdminRow row) {
ImmutableMap.Builder<CqlIdentifier, Object> builder = ImmutableMap.builder();
for (Map.Entry<String, TypeCodec<?>> entry : OPTION_CODECS.entrySet()) {
String name = entry.getKey();
CqlIdentifier id = CqlIdentifier.fromInternal(name);
TypeCodec<?> codec = entry.getValue();
if (name.equals("caching") && row.isString("caching")) {
// C* <=2.2, caching is stored as a string, and also appears as a string in the WITH clause.
builder.put(id, row.getString(name));
} else if (name.equals("compaction_strategy_class")) {
// C* <=2.2, compaction options split in two columns
String strategyClass = row.getString(name);
if (strategyClass != null) {
builder.put(CqlIdentifier.fromInternal("compaction"), ImmutableMap.<String, String>builder().put("class", strategyClass).putAll(SimpleJsonParser.parseStringMap(row.getString("compaction_strategy_options"))).build());
}
} else if (name.equals("compression_parameters")) {
// C* <=2.2, compression stored as a string
String compressionParameters = row.getString(name);
if (compressionParameters != null) {
builder.put(CqlIdentifier.fromInternal("compression"), ImmutableMap.copyOf(SimpleJsonParser.parseStringMap(row.getString(name))));
}
} else if (!isDeprecatedInCassandra4(name)) {
// Default case, read the value in a generic fashion
Object value = row.get(name, codec);
if (value != null) {
builder.put(id, value);
}
}
}
return builder.build();
}
use of com.datastax.oss.driver.api.core.type.codec.TypeCodec in project java-driver by datastax.
the class RelationParser method appendOptions.
public static void appendOptions(Map<CqlIdentifier, Object> options, ScriptBuilder builder) {
for (Map.Entry<CqlIdentifier, Object> entry : options.entrySet()) {
CqlIdentifier name = entry.getKey();
Object value = entry.getValue();
String formattedValue;
if (name.asInternal().equals("caching") && value instanceof String) {
formattedValue = TypeCodecs.TEXT.format((String) value);
} else {
@SuppressWarnings("unchecked") TypeCodec<Object> codec = (TypeCodec<Object>) RelationParser.OPTION_CODECS.get(name.asInternal());
formattedValue = codec.format(value);
}
String optionName = name.asCql(true);
if ("local_read_repair_chance".equals(optionName)) {
// Another small quirk in C* <= 2.2
optionName = "dclocal_read_repair_chance";
}
builder.andWith().append(optionName).append(" = ").append(formattedValue);
}
}
use of com.datastax.oss.driver.api.core.type.codec.TypeCodec in project java-driver by datastax.
the class CachingCodecRegistry method createCodec.
// Try to create a codec when we haven't found it in the cache
@NonNull
protected TypeCodec<?> createCodec(@Nullable DataType cqlType, @Nullable GenericType<?> javaType, boolean isJavaCovariant) {
LOG.trace("[{}] Cache miss, creating codec", logPrefix);
// Either type can be null, but not both.
if (javaType == null) {
assert cqlType != null;
return createCodec(cqlType);
} else if (cqlType == null) {
return createCodec(javaType, isJavaCovariant);
} else {
// Both non-null
TypeToken<?> token = javaType.__getToken();
if (cqlType instanceof ListType && List.class.isAssignableFrom(token.getRawType())) {
DataType elementCqlType = ((ListType) cqlType).getElementType();
TypeCodec<Object> elementCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
} else {
elementCodec = codecFor(elementCqlType);
}
return TypeCodecs.listOf(elementCodec);
} else if (cqlType instanceof SetType && Set.class.isAssignableFrom(token.getRawType())) {
DataType elementCqlType = ((SetType) cqlType).getElementType();
TypeCodec<Object> elementCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
} else {
elementCodec = codecFor(elementCqlType);
}
return TypeCodecs.setOf(elementCodec);
} else if (cqlType instanceof MapType && Map.class.isAssignableFrom(token.getRawType())) {
DataType keyCqlType = ((MapType) cqlType).getKeyType();
DataType valueCqlType = ((MapType) cqlType).getValueType();
TypeCodec<Object> keyCodec;
TypeCodec<Object> valueCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> keyJavaType = GenericType.of(typeArguments[0]);
GenericType<?> valueJavaType = GenericType.of(typeArguments[1]);
keyCodec = uncheckedCast(codecFor(keyCqlType, keyJavaType, isJavaCovariant));
valueCodec = uncheckedCast(codecFor(valueCqlType, valueJavaType, isJavaCovariant));
} else {
keyCodec = codecFor(keyCqlType);
valueCodec = codecFor(valueCqlType);
}
return TypeCodecs.mapOf(keyCodec, valueCodec);
} else if (cqlType instanceof TupleType && TupleValue.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.tupleOf((TupleType) cqlType);
} else if (cqlType instanceof UserDefinedType && UdtValue.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.udtOf((UserDefinedType) cqlType);
} else if (cqlType instanceof CustomType && ByteBuffer.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.custom(cqlType);
}
throw new CodecNotFoundException(cqlType, javaType);
}
}
use of com.datastax.oss.driver.api.core.type.codec.TypeCodec in project java-driver by datastax.
the class CodecRegistryIT method should_register_and_use_custom_codec_for_user_defined_type.
@Test
public void should_register_and_use_custom_codec_for_user_defined_type() {
Map<String, Coordinates> coordinatesMap = ImmutableMap.of("home", new Coordinates(12, 34));
GenericType<Map<String, Coordinates>> coordinatesMapType = GenericType.mapOf(String.class, Coordinates.class);
// Still create a separate session because we don't want to interfere with other tests
try (CqlSession session = SessionUtils.newSession(CCM_RULE, SESSION_RULE.keyspace())) {
// register the mapping codec for UDT coordinates
UserDefinedType coordinatesUdt = session.getMetadata().getKeyspace(SESSION_RULE.keyspace()).flatMap(ks -> ks.getUserDefinedType("coordinates")).orElseThrow(IllegalStateException::new);
MutableCodecRegistry codecRegistry = (MutableCodecRegistry) session.getContext().getCodecRegistry();
// Retrieve the inner codec
TypeCodec<UdtValue> innerCodec = codecRegistry.codecFor(coordinatesUdt);
assertThat(innerCodec).isInstanceOf(UdtCodec.class);
// Create the "outer" codec and register it
CoordinatesCodec coordinatesCodec = new CoordinatesCodec(innerCodec);
codecRegistry.register(coordinatesCodec);
// Test that the codec will be used to create on-the-fly codecs
assertThat(codecRegistry.codecFor(Coordinates.class)).isSameAs(coordinatesCodec);
assertThat(codecRegistry.codecFor(coordinatesMapType).accepts(coordinatesMap)).isTrue();
// test insertion
PreparedStatement prepared = session.prepare("INSERT INTO test3 (k0, k1, v) values (?, ?, ?)");
BoundStatement insert = prepared.boundStatementBuilder().setString(0, name.getMethodName()).setInt(1, 0).set(2, coordinatesMap, // use java type so has to be looked up in registry.
coordinatesMapType).build();
session.execute(insert);
// test retrieval
ResultSet result = session.execute(SimpleStatement.builder("SELECT v from test3 where k0 = ? AND k1 = ?").addPositionalValues(name.getMethodName(), 0).build());
List<Row> rows = result.all();
assertThat(rows).hasSize(1);
Row row = rows.get(0);
assertThat(row.get(0, coordinatesMapType)).isEqualTo(coordinatesMap);
assertThat(row.getMap(0, String.class, Coordinates.class)).isEqualTo(coordinatesMap);
}
}
Aggregations