use of com.datastax.oss.driver.api.core.type.TupleType 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.TupleType in project java-driver by datastax.
the class GraphDataTypesTest method complexUdtTests.
@Test
public void complexUdtTests() throws IOException {
UserDefinedType type1 = new UserDefinedTypeBuilder("ks", "udt1").withField("a", INT).withField("b", TEXT).build();
verifySerDeBinary(type1.newValue(1, "2"));
TupleType secondNested = tupleOf(BIGINT, listOf(BIGINT));
TupleType firstNested = tupleOf(TEXT, secondNested);
UserDefinedType type2 = new UserDefinedTypeBuilder("ks", "udt2").withField("a", INT).withField("b", TEXT).withField("c", type1).withField("mylist", listOf(BIGINT)).withField("mytuple_withlist", firstNested).build();
verifySerDeBinary(type2.newValue(1, "2", type1.newValue(3, "4"), ImmutableList.of(5L), firstNested.newValue("6", secondNested.newValue(7L, ImmutableList.of(8L)))));
UserDefinedType type3 = new UserDefinedTypeBuilder("ks", "udt3").withField("a", listOf(INT)).withField("b", setOf(FLOAT)).withField("c", mapOf(TEXT, BIGINT)).withField("d", listOf(listOf(DOUBLE))).withField("e", setOf(setOf(FLOAT))).withField("f", listOf(tupleOf(INT, TEXT))).build();
verifySerDeBinary(type3.newValue(ImmutableList.of(1), ImmutableSet.of(2.1f), ImmutableMap.of("3", 4L), ImmutableList.of(ImmutableList.of(5.1d, 6.1d), ImmutableList.of(7.1d)), ImmutableSet.of(ImmutableSet.of(8.1f), ImmutableSet.of(9.1f)), ImmutableList.of(tupleOf(INT, TEXT).newValue(10, "11"))));
}
use of com.datastax.oss.driver.api.core.type.TupleType in project java-driver by datastax.
the class TuplesMapped method registerCoordinatesCodec.
private static void registerCoordinatesCodec(CqlSession session) {
// retrieve the codec registry
MutableCodecRegistry codecRegistry = (MutableCodecRegistry) session.getContext().getCodecRegistry();
// create the tuple metadata
TupleType coordinatesType = DataTypes.tupleOf(DataTypes.INT, DataTypes.INT);
// retrieve the driver built-in codec for the tuple "coordinates"
TypeCodec<TupleValue> innerCodec = codecRegistry.codecFor(coordinatesType);
// create a custom codec to map the "coordinates" tuple to the Coordinates class
CoordinatesCodec coordinatesCodec = new CoordinatesCodec(innerCodec);
// register the new codec
codecRegistry.register(coordinatesCodec);
}
use of com.datastax.oss.driver.api.core.type.TupleType in project java-driver by datastax.
the class TuplesSimple method insertData.
private static void insertData(CqlSession session) {
// prepare the INSERT statement
PreparedStatement prepared = session.prepare("INSERT INTO examples.tuples (k, c) VALUES (?, ?)");
// create the tuple metadata
TupleType coordinatesType = DataTypes.tupleOf(DataTypes.INT, DataTypes.INT);
// bind the parameters in one pass
TupleValue coordinates1 = coordinatesType.newValue(12, 34);
BoundStatement boundStatement1 = prepared.bind(1, coordinates1);
// execute the insertion
session.execute(boundStatement1);
// alternate method: bind the parameters one by one
TupleValue coordinates2 = coordinatesType.newValue(56, 78);
BoundStatement boundStatement2 = prepared.bind().setInt("k", 2).setTupleValue("c", coordinates2);
// execute the insertion
session.execute(boundStatement2);
}
use of com.datastax.oss.driver.api.core.type.TupleType in project java-driver by datastax.
the class UserDefinedTypeListParserTest method should_resolve_nested_dependency.
@Test
public void should_resolve_nested_dependency() {
UserDefinedTypeParser parser = new UserDefinedTypeParser(new DataTypeCqlNameParser(), context);
Map<CqlIdentifier, UserDefinedType> types = parser.parse(KEYSPACE_ID, mockTypeRow("ks", "a", ImmutableList.of("bs"), ImmutableList.of("frozen<tuple<int, frozen<list<frozen<b>>>>>")), mockTypeRow("ks", "b", ImmutableList.of("i"), ImmutableList.of("int")));
assertThat(types).hasSize(2);
UserDefinedType aType = types.get(CqlIdentifier.fromInternal("a"));
UserDefinedType bType = types.get(CqlIdentifier.fromInternal("b"));
TupleType tupleType = (TupleType) aType.getFieldTypes().get(0);
ListType listType = (ListType) tupleType.getComponentTypes().get(1);
assertThat(listType.getElementType()).isEqualTo(bType);
}
Aggregations