org.apache.cassandra.db.marshal.UserType Java Examples

The following examples show how to use org.apache.cassandra.db.marshal.UserType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserTypeDeserializer.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
public Object deserialize(AbstractType<?> abstractType, ByteBuffer bb) {
    ByteBuffer userTypeByteBuffer = (ByteBuffer) super.deserialize(abstractType, bb);
    UserType userType = (UserType) abstractType;

    UserTypes.Value value = UserTypes.Value.fromSerialized(userTypeByteBuffer, userType);
    List<ByteBuffer> elements = value.getElements();

    Struct struct = new Struct(getSchemaBuilder(abstractType).build());

    for (int i = 0; i < userType.fieldNames().size(); i++) {
        String fieldName = userType.fieldNameAsString(i);
        AbstractType<?> fieldType = userType.type(i);
        struct.put(fieldName, CassandraTypeDeserializer.deserialize(fieldType, elements.get(i)));
    }

    return struct;
}
 
Example #2
Source File: UserTypeConverter.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
@Override
public UserType convert(DataType dataType) {
    com.datastax.driver.core.UserType userType = (com.datastax.driver.core.UserType) dataType;

    String typeNameString = userType.getTypeName();
    Collection<String> fieldNames = userType.getFieldNames();

    List<AbstractType<?>> innerAbstractTypes = new ArrayList<>(fieldNames.size());

    ByteBuffer typeNameBuffer = UTF8Type.instance.fromString(typeNameString);

    List<FieldIdentifier> fieldIdentifiers = new ArrayList<>(fieldNames.size());
    for (String fieldName : fieldNames) {
        fieldIdentifiers.add(FieldIdentifier.forInternalString(fieldName));
        innerAbstractTypes.add((CassandraTypeConverter.convert(userType.getFieldType(fieldName))));
    }

    return new UserType(userType.getKeyspace(),
            typeNameBuffer,
            fieldIdentifiers,
            innerAbstractTypes,
            !userType.isFrozen());
}
 
Example #3
Source File: UserTypes.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private void validateAssignableTo(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
    if (!(receiver.type instanceof UserType))
        throw new InvalidRequestException(String.format("Invalid user type literal for %s of type %s", receiver, receiver.type.asCQL3Type()));

    UserType ut = (UserType)receiver.type;
    for (int i = 0; i < ut.size(); i++)
    {
        ColumnIdentifier field = new ColumnIdentifier(ut.fieldName(i), UTF8Type.instance);
        Term.Raw value = entries.get(field);
        if (value == null)
            continue;

        ColumnSpecification fieldSpec = fieldSpecOf(receiver, i);
        if (!value.isAssignableTo(keyspace, fieldSpec))
            throw new InvalidRequestException(String.format("Invalid user type literal for %s: field %s is not of type %s", receiver, field, fieldSpec.type.asCQL3Type()));
    }
}
 
Example #4
Source File: DefsTables.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static void dropType(UserType ut)
{
    KSMetaData ksm = Schema.instance.getKSMetaData(ut.keyspace);
    assert ksm != null;

    ksm.userTypes.removeType(ut);

    if (!StorageService.instance.isClientMode())
        MigrationManager.instance.notifyDropUserType(ut);
}
 
Example #5
Source File: DefsTables.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static void updateType(UserType ut)
{
    KSMetaData ksm = Schema.instance.getKSMetaData(ut.keyspace);
    assert ksm != null;

    logger.info("Updating {}", ut);

    ksm.userTypes.addType(ut);

    if (!StorageService.instance.isClientMode())
        MigrationManager.instance.notifyUpdateUserType(ut);
}
 
Example #6
Source File: DefsTables.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static void addType(UserType ut)
{
    KSMetaData ksm = Schema.instance.getKSMetaData(ut.keyspace);
    assert ksm != null;

    logger.info("Loading {}", ut);

    ksm.userTypes.addType(ut);

    if (!StorageService.instance.isClientMode())
        MigrationManager.instance.notifyCreateUserType(ut);
}
 
Example #7
Source File: CreateTypeStatement.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private UserType createType() throws InvalidRequestException
{
    List<ByteBuffer> names = new ArrayList<>(columnNames.size());
    for (ColumnIdentifier name : columnNames)
        names.add(name.bytes);

    List<AbstractType<?>> types = new ArrayList<>(columnTypes.size());
    for (CQL3Type.Raw type : columnTypes)
        types.add(type.prepare(keyspace()).getType());

    return new UserType(name.getKeyspace(), name.getUserTypeName(), names, types);
}
 
Example #8
Source File: CreateTypeStatement.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static void checkForDuplicateNames(UserType type) throws InvalidRequestException
{
    for (int i = 0; i < type.size() - 1; i++)
    {
        ByteBuffer fieldName = type.fieldName(i);
        for (int j = i+1; j < type.size(); j++)
        {
            if (fieldName.equals(type.fieldName(j)))
                throw new InvalidRequestException(String.format("Duplicate field name %s in type %s",
                                                                UTF8Type.instance.getString(fieldName),
                                                                UTF8Type.instance.getString(type.name)));
        }
    }
}
 
Example #9
Source File: UserTypes.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static ColumnSpecification fieldSpecOf(ColumnSpecification column, int field)
{
    UserType ut = (UserType)column.type;
    return new ColumnSpecification(column.ksName,
                                   column.cfName,
                                   new ColumnIdentifier(column.name + "." + UTF8Type.instance.compose(ut.fieldName(field)), true),
                                   ut.fieldType(field));
}
 
Example #10
Source File: CassandraUtils.java    From sstable-tools with Apache License 2.0 5 votes vote down vote up
private static Types getTypes() {
    if (knownTypes.isEmpty()) {
        return Types.none();
    } else {
        return Types.of(knownTypes.values().toArray(new UserType[0]));
    }
}
 
Example #11
Source File: UserTypeDeserializer.java    From debezium-incubator with Apache License 2.0 5 votes vote down vote up
@Override
public SchemaBuilder getSchemaBuilder(AbstractType<?> abstractType) {
    UserType userType = (UserType) abstractType;
    SchemaBuilder schemaBuilder = SchemaBuilder.struct().name(userType.keyspace + "." + userType.getNameAsString());
    List<org.apache.cassandra.cql3.FieldIdentifier> fieldIdentifiers = userType.fieldNames();
    List<AbstractType<?>> fieldTypes = userType.fieldTypes();
    for (int i = 0; i < fieldIdentifiers.size(); i++) {
        Schema fieldSchema = CassandraTypeDeserializer.getSchemaBuilder(fieldTypes.get(i)).build();
        schemaBuilder.field(fieldIdentifiers.get(i).toString(), fieldSchema);
    }
    return schemaBuilder;
}
 
Example #12
Source File: UserTypes.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuffer bindAndGet(QueryOptions options) throws InvalidRequestException
{
    return UserType.buildValue(bindInternal(options));
}
 
Example #13
Source File: Selection.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public FieldSelector(UserType type, int field, Selector selected)
{
    this.type = type;
    this.field = field;
    this.selected = selected;
}
 
Example #14
Source File: UserTypes.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public DelayedValue(UserType type, List<Term> values)
{
    this.type = type;
    this.values = values;
}
 
Example #15
Source File: MigrationManager.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public void notifyCreateUserType(UserType ut)
{
    for (MigrationListener listener : listeners)
        listener.onCreateUserType(ut.keyspace, ut.getNameAsString());
}
 
Example #16
Source File: MigrationManager.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public void notifyUpdateUserType(UserType ut)
{
    for (MigrationListener listener : listeners)
        listener.onUpdateUserType(ut.keyspace, ut.getNameAsString());
}
 
Example #17
Source File: MigrationManager.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public void notifyDropUserType(UserType ut)
{
    for (MigrationListener listener : listeners)
        listener.onDropUserType(ut.keyspace, ut.getNameAsString());
}
 
Example #18
Source File: MigrationManager.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static void announceNewType(UserType newType)
{
    announceNewType(newType, false);
}
 
Example #19
Source File: MigrationManager.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static void announceNewType(UserType newType, boolean announceLocally)
{
    announce(addSerializedKeyspace(UTMetaData.toSchema(newType, FBUtilities.timestampMicros()), newType.keyspace), announceLocally);
}
 
Example #20
Source File: MigrationManager.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static void announceTypeUpdate(UserType updatedType)
{
    announceTypeUpdate(updatedType, false);
}
 
Example #21
Source File: MigrationManager.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static void announceTypeUpdate(UserType updatedType, boolean announceLocally)
{
    announceNewType(updatedType, announceLocally);
}
 
Example #22
Source File: MigrationManager.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static void announceTypeDrop(UserType droppedType)
{
    announceTypeDrop(droppedType, false);
}
 
Example #23
Source File: MigrationManager.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static void announceTypeDrop(UserType droppedType, boolean announceLocally)
{
    announce(addSerializedKeyspace(UTMetaData.dropFromSchema(droppedType, FBUtilities.timestampMicros()), droppedType.keyspace), announceLocally);
}
 
Example #24
Source File: CassandraTypeDeserializerTest.java    From debezium-incubator with Apache License 2.0 4 votes vote down vote up
@Test
public void testUserType() {
    // this is slightly complicated, so we're testing in two parts:
    // first, explicitly test for schema correctness
    ByteBuffer expectedTypeName = ByteBuffer.wrap("FooType".getBytes(Charset.defaultCharset()));
    List<FieldIdentifier> expectedFieldIdentifiers = new ArrayList<>();
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("asciiField".getBytes(Charset.defaultCharset()))));
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("doubleField".getBytes(Charset.defaultCharset()))));
    expectedFieldIdentifiers.add(new FieldIdentifier(ByteBuffer.wrap("durationField".getBytes(Charset.defaultCharset()))));
    // testing duration to make sure that recursive deserialization works correctly
    List<AbstractType<?>> expectedFieldTypes = new ArrayList<>();
    expectedFieldTypes.add(AsciiType.instance);
    expectedFieldTypes.add(DoubleType.instance);
    expectedFieldTypes.add(DurationType.instance);
    UserType userType = new UserType("barspace",
            expectedTypeName,
            expectedFieldIdentifiers,
            expectedFieldTypes,
            true);

    Schema userSchema = CassandraTypeDeserializer.getSchemaBuilder(userType).build();

    long expectedNanoDuration = (30 + 2) * ChronoUnit.DAYS.getDuration().toNanos() + 3;

    Struct expectedUserTypeData = new Struct(userSchema)
            .put("asciiField", "foobar")
            .put("doubleField", 1.5d)
            .put("durationField", expectedNanoDuration);

    Map<String, Object> jsonObject = new HashMap<>(3);
    jsonObject.put("\"asciiField\"", "foobar");
    jsonObject.put("\"doubleField\"", 1.5d);
    jsonObject.put("\"durationField\"", DurationType.instance.getSerializer().toString(Duration.newInstance(1, 2, 3)));
    Term userTypeObject = userType.fromJSONObject(jsonObject);

    ByteBuffer buffer = userTypeObject.bindAndGet(QueryOptions.DEFAULT);

    ByteBuffer serializedUserTypeObject = userType.decompose(buffer);

    Object deserializedUserTypeObject = CassandraTypeDeserializer.deserialize(userType, serializedUserTypeObject);

    Assert.assertEquals(expectedUserTypeData, deserializedUserTypeObject);
}