Search in sources :

Example 16 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class DseSchemaParser method parseKeyspace.

private DseKeyspaceMetadata parseKeyspace(AdminRow keyspaceRow) {
    // Cassandra <= 2.2
    // CREATE TABLE system.schema_keyspaces (
    // keyspace_name text PRIMARY KEY,
    // durable_writes boolean,
    // strategy_class text,
    // strategy_options text
    // )
    // 
    // Cassandra >= 3.0:
    // CREATE TABLE system_schema.keyspaces (
    // keyspace_name text PRIMARY KEY,
    // durable_writes boolean,
    // replication frozen<map<text, text>>
    // )
    // 
    // DSE >= 6.8: same as Cassandra 3 + graph_engine text
    CqlIdentifier keyspaceId = CqlIdentifier.fromInternal(keyspaceRow.getString("keyspace_name"));
    boolean durableWrites = MoreObjects.firstNonNull(keyspaceRow.getBoolean("durable_writes"), false);
    String graphEngine = keyspaceRow.getString("graph_engine");
    Map<String, String> replicationOptions;
    if (keyspaceRow.contains("strategy_class")) {
        String strategyClass = keyspaceRow.getString("strategy_class");
        Map<String, String> strategyOptions = SimpleJsonParser.parseStringMap(keyspaceRow.getString("strategy_options"));
        replicationOptions = ImmutableMap.<String, String>builder().putAll(strategyOptions).put("class", strategyClass).build();
    } else {
        replicationOptions = keyspaceRow.getMapOfStringToString("replication");
    }
    Map<CqlIdentifier, UserDefinedType> types = parseTypes(keyspaceId);
    return new DefaultDseKeyspaceMetadata(keyspaceId, durableWrites, false, graphEngine, replicationOptions, types, parseTables(keyspaceId, types), parseViews(keyspaceId, types), parseFunctions(keyspaceId, types), parseAggregates(keyspaceId, types));
}
Also used : DefaultDseKeyspaceMetadata(com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseKeyspaceMetadata) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier)

Example 17 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class ComplexTypeSerializerUtil method toProtocolSpec.

private static RawType toProtocolSpec(DataType dataType) {
    int id = dataType.getProtocolCode();
    RawType type = RawType.PRIMITIVES.get(id);
    if (type != null) {
        return type;
    }
    switch(id) {
        case ProtocolConstants.DataType.CUSTOM:
            CustomType customType = ((CustomType) dataType);
            type = new RawType.RawCustom(customType.getClassName());
            break;
        case ProtocolConstants.DataType.LIST:
            ListType listType = ((ListType) dataType);
            type = new RawType.RawList(toProtocolSpec(listType.getElementType()));
            break;
        case ProtocolConstants.DataType.SET:
            SetType setType = ((SetType) dataType);
            type = new RawType.RawSet(toProtocolSpec(setType.getElementType()));
            break;
        case ProtocolConstants.DataType.MAP:
            MapType mapType = ((MapType) dataType);
            type = new RawType.RawMap(toProtocolSpec(mapType.getKeyType()), toProtocolSpec(mapType.getValueType()));
            break;
        case ProtocolConstants.DataType.TUPLE:
            TupleType tupleType = ((TupleType) dataType);
            ImmutableList.Builder<RawType> subTypesList = ImmutableList.builderWithExpectedSize(tupleType.getComponentTypes().size());
            for (int i = 0; i < tupleType.getComponentTypes().size(); i++) {
                subTypesList.add(toProtocolSpec(tupleType.getComponentTypes().get(i)));
            }
            type = new RawType.RawTuple(subTypesList.build());
            break;
        case ProtocolConstants.DataType.UDT:
            UserDefinedType userDefinedType = ((UserDefinedType) dataType);
            ImmutableMap.Builder<String, RawType> subTypesMap = ImmutableMap.builderWithExpectedSize(userDefinedType.getFieldNames().size());
            for (int i = 0; i < userDefinedType.getFieldTypes().size(); i++) {
                subTypesMap.put(userDefinedType.getFieldNames().get(i).asInternal(), toProtocolSpec(userDefinedType.getFieldTypes().get(i)));
            }
            type = new RawType.RawUdt(Objects.requireNonNull(userDefinedType.getKeyspace()).asInternal(), userDefinedType.getName().asInternal(), subTypesMap.build());
            break;
        default:
            throw new IllegalArgumentException("Unsupported type: " + dataType.asCql(true, true));
    }
    return type;
}
Also used : CustomType(com.datastax.oss.driver.api.core.type.CustomType) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) MapType(com.datastax.oss.driver.api.core.type.MapType) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) RawType(com.datastax.oss.protocol.internal.response.result.RawType)

Example 18 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class UdtValueSerializer method readDynamicCustomValue.

@Override
public UdtValue readDynamicCustomValue(Buffer buffer, GraphBinaryReader context) throws IOException {
    // read type definition first
    DataType driverType = ComplexTypeSerializerUtil.decodeTypeDefinition(buffer, driverContext);
    assert driverType instanceof UserDefinedType : "GraphBinary UdtValue deserializer was called on a value that is not encoded as a UdtValue.";
    UserDefinedType userDefinedType = (UserDefinedType) driverType;
    UdtValue value = userDefinedType.newValue();
    // then read values
    return ComplexTypeSerializerUtil.decodeValue(buffer, value, userDefinedType.getFieldTypes().size());
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType)

Example 19 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class SchemaRefreshTest method should_detect_top_level_change_and_children_changes.

@Test
public void should_detect_top_level_change_and_children_changes() {
    // Drop one type, modify the other and add a third one
    UserDefinedType newT2 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks1"), CqlIdentifier.fromInternal("t2")).withField(CqlIdentifier.fromInternal("i"), DataTypes.TEXT).build();
    UserDefinedType t3 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks1"), CqlIdentifier.fromInternal("t3")).withField(CqlIdentifier.fromInternal("i"), DataTypes.INT).build();
    // Also disable durable writes
    DefaultKeyspaceMetadata newKs1 = newKeyspace("ks1", false, newT2, t3);
    SchemaRefresh refresh = new SchemaRefresh(ImmutableMap.of(OLD_KS1.getName(), newKs1));
    MetadataRefresh.Result result = refresh.compute(oldMetadata, false, context);
    assertThat(result.newMetadata.getKeyspaces().get(OLD_KS1.getName())).isEqualTo(newKs1);
    assertThat(result.events).containsExactly(KeyspaceChangeEvent.updated(OLD_KS1, newKs1), TypeChangeEvent.dropped(OLD_T1), TypeChangeEvent.updated(OLD_T2, newT2), TypeChangeEvent.created(t3));
}
Also used : DefaultKeyspaceMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultKeyspaceMetadata) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) MetadataRefresh(com.datastax.oss.driver.internal.core.metadata.MetadataRefresh) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Example 20 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class DataTypeSerializationTest method should_serialize_and_deserialize.

@Test
public void should_serialize_and_deserialize() {
    TupleType tuple = DataTypes.tupleOf(DataTypes.INT, DataTypes.TEXT);
    UserDefinedType udt = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
    // Because primitive and custom types never use the codec registry, we consider them always
    // attached
    should_serialize_and_deserialize(DataTypes.INT, false);
    should_serialize_and_deserialize(DataTypes.custom("some.class.name"), false);
    should_serialize_and_deserialize(tuple, true);
    should_serialize_and_deserialize(udt, true);
    should_serialize_and_deserialize(DataTypes.listOf(DataTypes.INT), false);
    should_serialize_and_deserialize(DataTypes.listOf(tuple), true);
    should_serialize_and_deserialize(DataTypes.setOf(udt), true);
    should_serialize_and_deserialize(DataTypes.mapOf(tuple, udt), true);
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) Test(org.junit.Test)

Aggregations

UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)53 Test (org.junit.Test)33 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)21 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)20 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)15 TupleType (com.datastax.oss.driver.api.core.type.TupleType)13 CqlSession (com.datastax.oss.driver.api.core.CqlSession)9 DataType (com.datastax.oss.driver.api.core.type.DataType)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 ListType (com.datastax.oss.driver.api.core.type.ListType)6 SetType (com.datastax.oss.driver.api.core.type.SetType)6 SessionRule (com.datastax.oss.driver.api.testinfra.session.SessionRule)6 ParallelizableTests (com.datastax.oss.driver.categories.ParallelizableTests)6 Category (org.junit.experimental.categories.Category)6 Row (com.datastax.oss.driver.api.core.cql.Row)5 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)5 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)5 CcmRule (com.datastax.oss.driver.api.testinfra.ccm.CcmRule)5 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)4