Java Code Examples for org.apache.cassandra.utils.ByteBufferUtil#clone()

The following examples show how to use org.apache.cassandra.utils.ByteBufferUtil#clone() . 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: ColumnDefinition.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static ColumnDefinition fromThrift(String ksName, String cfName, AbstractType<?> thriftComparator, AbstractType<?> thriftSubcomparator, ColumnDef thriftColumnDef) throws SyntaxException, ConfigurationException
{
    // For super columns, the componentIndex is 1 because the ColumnDefinition applies to the column component.
    Integer componentIndex = thriftSubcomparator != null ? 1 : null;
    AbstractType<?> comparator = thriftSubcomparator == null ? thriftComparator : thriftSubcomparator;
    try
    {
        comparator.validate(thriftColumnDef.name);
    }
    catch (MarshalException e)
    {
        throw new ConfigurationException(String.format("Column name %s is not valid for comparator %s", ByteBufferUtil.bytesToHex(thriftColumnDef.name), comparator));
    }

    return new ColumnDefinition(ksName,
                                cfName,
                                new ColumnIdentifier(ByteBufferUtil.clone(thriftColumnDef.name), comparator),
                                TypeParser.parse(thriftColumnDef.validation_class),
                                thriftColumnDef.index_type == null ? null : IndexType.valueOf(thriftColumnDef.index_type.name()),
                                thriftColumnDef.index_options,
                                thriftColumnDef.index_name,
                                componentIndex,
                                Kind.REGULAR);
}
 
Example 2
Source File: ByteBufferUtils.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the specified {@link java.nio.ByteBuffer} as a byte array.
 *
 * @param byteBuffer a {@link java.nio.ByteBuffer} to be converted to a byte array.
 * @return the byte array representation of the {@code byteBuffer}.
 */
public static byte[] asArray(ByteBuffer byteBuffer) {
    ByteBuffer bb = ByteBufferUtil.clone(byteBuffer);
    byte[] bytes = new byte[bb.remaining()];
    bb.get(bytes);
    return bytes;
}
 
Example 3
Source File: Base256Serializer.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@code String} representation of the specified {@code ByteBuffer}.
 *
 * @param byteBuffer The {@code ByteBuffer} to be converted.
 * @return The {@code String} representation of the specified {@code ByteBuffer}.
 */
public static String string(ByteBuffer byteBuffer) {
    ByteBuffer bb = ByteBufferUtil.clone(byteBuffer);
    byte[] bytes = new byte[bb.remaining()];
    bb.get(bytes);
    return new String(chars(bytes));
}
 
Example 4
Source File: CFMetaDataTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testThriftConversion() throws Exception
{
    CfDef cfDef = new CfDef().setDefault_validation_class(AsciiType.class.getCanonicalName())
                             .setComment("Test comment")
                             .setColumn_metadata(columnDefs)
                             .setKeyspace(KEYSPACE)
                             .setName(COLUMN_FAMILY);

    // convert Thrift to CFMetaData
    CFMetaData cfMetaData = CFMetaData.fromThrift(cfDef);

    CfDef thriftCfDef = new CfDef();
    thriftCfDef.keyspace = KEYSPACE;
    thriftCfDef.name = COLUMN_FAMILY;
    thriftCfDef.default_validation_class = cfDef.default_validation_class;
    thriftCfDef.comment = cfDef.comment;
    thriftCfDef.column_metadata = new ArrayList<ColumnDef>();
    for (ColumnDef columnDef : columnDefs)
    {
        ColumnDef c = new ColumnDef();
        c.name = ByteBufferUtil.clone(columnDef.name);
        c.validation_class = columnDef.getValidation_class();
        c.index_name = columnDef.getIndex_name();
        c.index_type = IndexType.KEYS;
        thriftCfDef.column_metadata.add(c);
    }

    CfDef converted = cfMetaData.toThrift();

    assertEquals(thriftCfDef.keyspace, converted.keyspace);
    assertEquals(thriftCfDef.name, converted.name);
    assertEquals(thriftCfDef.default_validation_class, converted.default_validation_class);
    assertEquals(thriftCfDef.comment, converted.comment);
    assertEquals(new HashSet<>(thriftCfDef.column_metadata), new HashSet<>(converted.column_metadata));
}
 
Example 5
Source File: CassandraRecordUtils.java    From hdfs2cass with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer toByteBuffer(final Object value) {
  if (value == null) {
    return ByteBufferUtil.EMPTY_BYTE_BUFFER;
  } else if (value instanceof CharSequence) {
    return ByteBufferUtil.bytes(value.toString());
  } else if (value instanceof Double) {
    return ByteBufferUtil.bytes((Double) value);
  } else if (value instanceof Float) {
    return ByteBufferUtil.bytes((Float) value);
  } else if (value instanceof Integer) {
    return ByteBufferUtil.bytes((Integer) value);
  } else if (value instanceof Long) {
    return ByteBufferUtil.bytes((Long) value);
  } else if (value instanceof ByteBuffer) {
    return ByteBufferUtil.clone((ByteBuffer) value);
  } else if (value instanceof GenericData.Array) {
    return serializeList((GenericData.Array)value);
  } else if (value instanceof SpecificRecord) {
    List<ByteBuffer> buffers = Lists.newArrayList();
    SpecificRecord record = (SpecificRecord) value;
    for (Schema.Field field : record.getSchema().getFields()) {
      buffers.add(toByteBuffer(record.get(field.pos())));
    }
    return CompositeType.build(buffers.toArray(new ByteBuffer[0]));
  } else if (value instanceof Map) {
    return serializeMap((Map<?, ?>) value);
  } else if (value instanceof Set) {
    return serializeSet((Set<?>) value);
  } else if (value instanceof List) {
    return serializeList((List<?>) value);
  } else if (value instanceof UUID) {
    return ByteBufferUtil.bytes((UUID) value);
  }


  throw new CrunchRuntimeException("Can not transform field (class: " + value.getClass() + ") to ByteBuffer");
}