use of com.datastax.oss.driver.api.core.type.SetType in project java-driver by datastax.
the class DataTypeDetachableTest method set_should_be_attached_if_its_element_is.
@Test
public void set_should_be_attached_if_its_element_is() {
TupleType tuple = DataTypes.tupleOf(DataTypes.INT);
SetType set = DataTypes.setOf(tuple);
assertThat(tuple.isDetached()).isTrue();
assertThat(set.isDetached()).isTrue();
tuple.attach(attachmentPoint);
assertThat(set.isDetached()).isFalse();
}
use of com.datastax.oss.driver.api.core.type.SetType in project jnosql-diana-driver by eclipse.
the class QueryUtils method getUdtValue.
private static Object getUdtValue(UserDefinedType userType, Iterable elements, DataType type) {
Collection<Object> udtValues = getCollectionUdt(type);
UdtValue udtValue = userType.newValue();
final List<String> udtNames = userType.getFieldNames().stream().map(CqlIdentifier::asInternal).collect(Collectors.toList());
for (Object object : elements) {
if (Column.class.isInstance(object)) {
Column column = Column.class.cast(object);
Object convert = ValueUtil.convert(column.getValue());
final int index = udtNames.indexOf(column.getName());
if (index < 0) {
throw new CommunicationException("This field has not been found: " + column.getName() + " the fields available are " + udtNames + " in the UDT type " + userType.getName().asCql(true) + " at the keyspace " + userType.getKeyspace());
}
DataType fieldType = userType.getFieldTypes().get(index);
TypeCodec<Object> objectTypeCodec = CodecRegistry.DEFAULT.codecFor(fieldType);
if (fieldType instanceof SetType) {
udtValue.set(getName(column), new HashSet<Object>((Collection<?>) convert), objectTypeCodec);
} else {
udtValue.set(getName(column), convert, objectTypeCodec);
}
} else if (Iterable.class.isInstance(object)) {
udtValues.add(getUdtValue(userType, Iterable.class.cast(Iterable.class.cast(object)), type));
}
}
if (udtValues.isEmpty()) {
return udtValue;
}
return udtValues;
}
use of com.datastax.oss.driver.api.core.type.SetType in project java-driver by datastax.
the class TableParser method buildLegacyIndexTarget.
private static String buildLegacyIndexTarget(ColumnMetadata column, Map<String, String> options) {
String columnName = column.getName().asCql(true);
DataType columnType = column.getType();
if (options.containsKey("index_keys")) {
return String.format("keys(%s)", columnName);
}
if (options.containsKey("index_keys_and_values")) {
return String.format("entries(%s)", columnName);
}
if ((columnType instanceof ListType && ((ListType) columnType).isFrozen()) || (columnType instanceof SetType && ((SetType) columnType).isFrozen()) || (columnType instanceof MapType && ((MapType) columnType).isFrozen())) {
return String.format("full(%s)", columnName);
}
// Note: the keyword 'values' is not accepted as a valid index target function until 3.0
return columnName;
}
use of com.datastax.oss.driver.api.core.type.SetType 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.SetType in project java-driver by datastax.
the class DataTypeClassNameParserTest method should_parse_user_type_when_definition_not_already_available.
@Test
@UseDataProvider(location = TestDataProviders.class, value = "locales")
public void should_parse_user_type_when_definition_not_already_available(Locale locale) {
Locale def = Locale.getDefault();
try {
Locale.setDefault(locale);
UserDefinedType addressType = (UserDefinedType) parse("org.apache.cassandra.db.marshal.UserType(" + "foo,61646472657373," + ("737472656574:org.apache.cassandra.db.marshal.UTF8Type," + "7a6970636f6465:org.apache.cassandra.db.marshal.Int32Type," + ("70686f6e6573:org.apache.cassandra.db.marshal.SetType(" + "org.apache.cassandra.db.marshal.UserType(foo,70686f6e65," + "6e616d65:org.apache.cassandra.db.marshal.UTF8Type," + "6e756d626572:org.apache.cassandra.db.marshal.UTF8Type)") + "))"));
assertThat(addressType.getKeyspace().asInternal()).isEqualTo("foo");
assertThat(addressType.getName().asInternal()).isEqualTo("address");
assertThat(addressType.isFrozen()).isTrue();
assertThat(addressType.getFieldNames().size()).isEqualTo(3);
assertThat(addressType.getFieldNames().get(0).asInternal()).isEqualTo("street");
assertThat(addressType.getFieldTypes().get(0)).isEqualTo(DataTypes.TEXT);
assertThat(addressType.getFieldNames().get(1).asInternal()).isEqualTo("zipcode");
assertThat(addressType.getFieldTypes().get(1)).isEqualTo(DataTypes.INT);
assertThat(addressType.getFieldNames().get(2).asInternal()).isEqualTo("phones");
DataType phonesType = addressType.getFieldTypes().get(2);
assertThat(phonesType).isInstanceOf(SetType.class);
UserDefinedType phoneType = ((UserDefinedType) ((SetType) phonesType).getElementType());
assertThat(phoneType.getKeyspace().asInternal()).isEqualTo("foo");
assertThat(phoneType.getName().asInternal()).isEqualTo("phone");
assertThat(phoneType.isFrozen()).isTrue();
assertThat(phoneType.getFieldNames().size()).isEqualTo(2);
assertThat(phoneType.getFieldNames().get(0).asInternal()).isEqualTo("name");
assertThat(phoneType.getFieldTypes().get(0)).isEqualTo(DataTypes.TEXT);
assertThat(phoneType.getFieldNames().get(1).asInternal()).isEqualTo("number");
assertThat(phoneType.getFieldTypes().get(1)).isEqualTo(DataTypes.TEXT);
} finally {
Locale.setDefault(def);
}
}
Aggregations