Example usage for org.apache.cassandra.db.marshal AbstractType compose

List of usage examples for org.apache.cassandra.db.marshal AbstractType compose

Introduction

In this page you can find the example usage for org.apache.cassandra.db.marshal AbstractType compose.

Prototype

public T compose(ByteBuffer bytes) 

Source Link

Usage

From source file:com.datastax.driver.core.DataType.java

License:Apache License

/**
 * Deserialize a value of this type from the provided bytes.
 * <p>//from w ww  .j  a  v a  2s  . c  o  m
 * The format of {@code bytes} must correspond to the Cassandra
 * encoding for this type.
 *
 * @param bytes bytes holding the value to deserialize.
 * @return the deserialized value (of class {@code this.asJavaClass()}).
 * Will return {@code null} if either {@code bytes} is {@code null} or if
 * {@code bytes.remaining() == 0} and this type has no value corresponding
 * to an empty byte buffer (the latter somewhat strange behavior is due to
 * the fact that for historical/technical reason, Cassandra types always
 * accept empty byte buffer as valid value of those type, and so we avoid
 * throwing an exception in that case. It is however highly discouraged to
 * store empty byte buffers for types for which it doesn't make sense, so
 * this implementation can generally be ignored).
 *
 * @throws InvalidTypeException if {@code bytes} is not a valid
 * encoding of an object of this {@code DataType}.
 */
public Object deserialize(ByteBuffer bytes) {
    AbstractType<?> codec = Codec.getCodec(this);
    try {
        codec.validate(bytes);
    } catch (MarshalException e) {
        throw new InvalidTypeException(
                String.format("Invalid serialized value for type %s (%s)", toString(), e.getMessage()));
    }

    try {
        return codec.compose(bytes);
    } catch (IndexOutOfBoundsException e) {
        // As it happens, types like Int32Type will accept empty byte buffers
        // in their validate method, but their compose method will throw. We
        // should probably fix that Cassandra side, but in the meantime ...
        return null;
    }
}

From source file:com.dse.pig.udfs.AbstractCassandraStorage.java

License:Apache License

protected Object cassandraToObj(AbstractType validator, ByteBuffer value) {
    if (validator instanceof DecimalType || validator instanceof InetAddressType)
        return validator.getString(value);
    else/*w  ww . jav  a  2  s . c  o  m*/
        return validator.compose(value);
}

From source file:com.impetus.client.cassandra.CassandraClientBase.java

License:Apache License

/**
 * Compose column value./*from   www . ja v a  2s .  c o  m*/
 * 
 * @param cqlMetadata
 *            the cql metadata
 * @param thriftColumnValue
 *            the thrift column value
 * @param thriftColumnName
 *            the thrift column name
 * @return the object
 */
private Object composeColumnValue(CqlMetadata cqlMetadata, byte[] thriftColumnValue, byte[] thriftColumnName) {
    Map<ByteBuffer, String> schemaTypes = cqlMetadata.getValue_types();
    AbstractType<?> type = null;
    try {
        type = TypeParser.parse(schemaTypes.get(ByteBuffer.wrap(thriftColumnName)));
    } catch (SyntaxException | ConfigurationException ex) {
        log.error(ex.getMessage());
        throw new KunderaException("Error while deserializing column value " + ex);
    }
    if (type.isCollection()) {
        return ((CollectionSerializer) type.getSerializer())
                .deserializeForNativeProtocol(ByteBuffer.wrap(thriftColumnValue), 2);
    }
    return type.compose(ByteBuffer.wrap(thriftColumnValue));
}

From source file:com.impetus.client.cassandra.datahandler.CassandraDataHandlerBase.java

License:Apache License

/**
 * Compose and add.//from w w w .  j  av a2  s.  c  o  m
 * 
 * @param entity
 *            the entity
 * @param cqlMetadata
 *            the cql metadata
 * @param thriftColumnValue
 *            the thrift column value
 * @param thriftColumnName
 *            the thrift column name
 */
private void composeAndAdd(HashMap entity, CqlMetadata cqlMetadata, Object thriftColumnValue,
        String thriftColumnName) {
    byte[] columnName = thriftColumnName.getBytes();

    Map<ByteBuffer, String> schemaTypes = this.clientBase.getCqlMetadata().getValue_types();
    AbstractType<?> type = null;
    try {
        type = TypeParser.parse(schemaTypes.get(ByteBuffer.wrap((byte[]) columnName)));
    } catch (SyntaxException | ConfigurationException e) {
        log.error(e.getMessage());
        throw new KunderaException("Error while parsing CQL Type " + e);
    }

    entity.put(thriftColumnName, type.compose(ByteBuffer.wrap((byte[]) thriftColumnValue)));
}

From source file:com.stratio.cassandra.index.RegularCellsMapper.java

License:Apache License

@SuppressWarnings("rawtypes")
public Columns columns(Row row) {
    ColumnFamily columnFamily = row.cf;//  w  ww .  j  a va  2  s .  c o  m
    Columns columns = new Columns();

    // Get row's columns iterator skipping clustering column
    Iterator<Cell> cellIterator = columnFamily.iterator();
    cellIterator.next();

    // Stuff for grouping collection columns (sets, lists and maps)
    String name;
    CollectionType collectionType;

    while (cellIterator.hasNext()) {
        Cell cell = cellIterator.next();
        CellName cellName = cell.name();
        ColumnDefinition columnDefinition = metadata.getColumnDefinition(cellName);
        if (columnDefinition == null) {
            continue;
        }

        AbstractType<?> valueType = columnDefinition.type;

        ByteBuffer cellValue = cell.value();

        name = cellName.cql3ColumnName(metadata).toString();

        if (valueType.isCollection()) {
            collectionType = (CollectionType<?>) valueType;
            switch (collectionType.kind) {
            case SET: {
                AbstractType<?> type = collectionType.nameComparator();
                ByteBuffer value = cellName.collectionElement();
                columns.add(new Column(name, value, type));
                break;
            }
            case LIST: {
                AbstractType<?> type = collectionType.valueComparator();
                columns.add(new Column(name, cellValue, type));
                break;
            }
            case MAP: {
                AbstractType<?> type = collectionType.valueComparator();
                ByteBuffer keyValue = cellName.collectionElement();
                AbstractType<?> keyType = collectionType.nameComparator();
                String nameSufix = keyType.compose(keyValue).toString();
                columns.add(new Column(name, nameSufix, cellValue, type));
                break;
            }
            }
        } else {
            columns.add(new Column(name, cellValue, valueType));
        }
    }

    return columns;
}

From source file:com.stratio.cassandra.index.util.ByteBufferUtils.java

License:Apache License

/**
 * Returns a {@code String} representation of {@code byteBuffer} validated by {@code type}.
 *
 * @param byteBuffer the {@link java.nio.ByteBuffer} to be converted to {@code String}.
 * @param type       {@link AbstractType} of {@code byteBuffer}.
 * @return a {@code String} representation of {@code byteBuffer} validated by {@code type}.
 *//* www.  j  a  va2 s .  c om*/
public static String toString(ByteBuffer byteBuffer, AbstractType<?> type) {
    if (type instanceof CompositeType) {
        CompositeType composite = (CompositeType) type;
        List<AbstractType<?>> types = composite.types;
        ByteBuffer[] components = composite.split(byteBuffer);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < components.length; i++) {
            AbstractType<?> componentType = types.get(i);
            ByteBuffer component = components[i];
            sb.append(componentType.compose(component));
            if (i < types.size() - 1) {
                sb.append(':');
            }
        }
        return sb.toString();
    } else {
        return type.compose(byteBuffer).toString();
    }
}

From source file:com.stratio.cassandra.lucene.column.ColumnBuilder.java

License:Apache License

/**
 * Returns a new {@link Column} using the specified decomposed value and its type.
 *
 * @param decomposedValue the decomposed value
 * @param type the value type/*from  w ww.  j  a v  a  2s.  c o m*/
 * @param <T> the marshaller's base type
 * @return the built column
 */
public <T> Column<T> buildWithDecomposed(ByteBuffer decomposedValue, AbstractType<T> type) {
    T composedValue = type.compose(decomposedValue);
    return new Column<>(cellName, udtNames, mapNames, decomposedValue, composedValue, type);
}

From source file:com.stratio.cassandra.lucene.column.ColumnsMapper.java

License:Apache License

private void addColumns(Columns columns, Cell cell) {
    if (cell != null) {
        ColumnDefinition columnDefinition = cell.column();
        String name = columnDefinition.name.toString();
        AbstractType<?> type = cell.column().type;
        ByteBuffer value = cell.value();
        ColumnBuilder builder = Column.builder(name);
        if (type.isCollection() && !type.isFrozenCollection()) {
            CollectionType<?> collectionType = (CollectionType<?>) type;
            switch (collectionType.kind) {
            case SET: {
                type = collectionType.nameComparator();
                value = cell.path().get(0);
                addColumns(columns, builder, type, value);
                break;
            }//from ww w  .jav  a2 s .co  m
            case LIST: {
                type = collectionType.valueComparator();
                addColumns(columns, builder, type, value);
                break;
            }
            case MAP: {
                type = collectionType.valueComparator();
                ByteBuffer keyValue = cell.path().get(0);
                AbstractType<?> keyType = collectionType.nameComparator();
                String nameSuffix = keyType.compose(keyValue).toString();
                addColumns(columns, builder.withMapName(nameSuffix), type, value);
                break;
            }
            default: {
                throw new IndexException("Unknown collection type %s", collectionType.kind);
            }
            }
        } else {
            addColumns(columns, Column.builder(name), type, value);
        }
    }
}

From source file:com.stratio.cassandra.lucene.column.ColumnsMapper.java

License:Apache License

private void addColumns(Columns columns, ColumnBuilder builder, AbstractType type, ByteBuffer value) {
    if (type.isCollection()) {
        value = ByteBufferUtil.clone(value);
        CollectionType<?> collectionType = (CollectionType<?>) type;
        switch (collectionType.kind) {
        case SET: {
            AbstractType<?> nameType = collectionType.nameComparator();
            int colSize = CollectionSerializer.readCollectionSize(value, Server.CURRENT_VERSION);
            for (int j = 0; j < colSize; j++) {
                ByteBuffer itemValue = CollectionSerializer.readValue(value, Server.CURRENT_VERSION);
                addColumns(columns, builder, nameType, itemValue);
            }//from w  w w  .  j  av  a2  s .com
            break;
        }
        case LIST: {
            AbstractType<?> valueType = collectionType.valueComparator();
            int colSize = CollectionSerializer.readCollectionSize(value, Server.CURRENT_VERSION);
            for (int j = 0; j < colSize; j++) {
                ByteBuffer itemValue = CollectionSerializer.readValue(value, Server.CURRENT_VERSION);
                addColumns(columns, builder, valueType, itemValue);
            }
            break;
        }
        case MAP: {
            AbstractType<?> keyType = collectionType.nameComparator();
            AbstractType<?> valueType = collectionType.valueComparator();
            int colSize = MapSerializer.readCollectionSize(value, Server.CURRENT_VERSION);
            for (int j = 0; j < colSize; j++) {
                ByteBuffer mapKey = MapSerializer.readValue(value, Server.CURRENT_VERSION);
                ByteBuffer mapValue = MapSerializer.readValue(value, Server.CURRENT_VERSION);
                String itemName = keyType.compose(mapKey).toString();
                collectionType.nameComparator();
                addColumns(columns, builder.withMapName(itemName), valueType, mapValue);
            }
            break;
        }
        default: {
            throw new IndexException("Unknown collection type %s", collectionType.kind);
        }
        }
    } else if (type instanceof UserType) {
        UserType userType = (UserType) type;
        ByteBuffer[] values = userType.split(value);
        for (int i = 0; i < userType.fieldNames().size(); i++) {
            String itemName = userType.fieldNameAsString(i);
            AbstractType<?> itemType = userType.fieldType(i);
            // This only occurs in UDT not fully composed
            if (values[i] != null) {
                addColumns(columns, builder.withUDTName(itemName), itemType, values[i]);
            }
        }
    } else if (type instanceof TupleType) {
        TupleType tupleType = (TupleType) type;
        ByteBuffer[] values = tupleType.split(value);
        for (Integer i = 0; i < tupleType.size(); i++) {
            String itemName = i.toString();
            AbstractType<?> itemType = tupleType.type(i);
            addColumns(columns, builder.withUDTName(itemName), itemType, values[i]);
        }
    } else {
        if (value != null) {
            columns.add(builder.buildWithDecomposed(value, type));
        }
    }
}

From source file:com.stratio.cassandra.lucene.schema.column.Column.java

License:Apache License

/**
 * Returns the {@link Column} defined by the specified name, raw value and type.
 *
 * @param name            The column name.
 * @param decomposedValue The column raw value.
 * @param type            The column type/marshaller.
 * @param isCollection    If the {@link Column} belongs to a collection.
 * @param <T>             The base type.
 * @return A {@link Column}./* w ww.  java  2 s.c  om*/
 */
public static <T> Column<T> fromDecomposed(String name, ByteBuffer decomposedValue, AbstractType<T> type,
        boolean isCollection) {
    T composedValue = type.compose(decomposedValue);
    return new Column<>(name, null, decomposedValue, composedValue, type, isCollection);
}