Example usage for org.apache.cassandra.utils UUIDGen getTimeUUIDBytes

List of usage examples for org.apache.cassandra.utils UUIDGen getTimeUUIDBytes

Introduction

In this page you can find the example usage for org.apache.cassandra.utils UUIDGen getTimeUUIDBytes.

Prototype

public static byte[] getTimeUUIDBytes() 

Source Link

Document

Returns a 16 byte representation of a type 1 UUID (a time-based UUID), based on the current system time.

Usage

From source file:com.perpetumobile.bit.orm.cassandra.CliMain.java

License:Apache License

/**
 * Used to convert value (function argument, string) into byte[]
 * @param functionCall - tree representing function call ^(FUNCTION_CALL function_name value)
 * @param columnFamily - column family definition (CfDef)
 * @param columnName   - column name as byte[] (used to update CfDef)
 * @param withUpdate   - also updates column family metadata for given column
 * @return byte[] - string value as byte[]
 *//*from w w  w.  j  a  va 2s . c o  m*/
private ByteBuffer convertValueByFunction(Tree functionCall, CfDef columnFamily, ByteBuffer columnName,
        boolean withUpdate) {
    String functionName = functionCall.getChild(0).getText();
    Tree argumentTree = functionCall.getChild(1);
    String functionArg = (argumentTree == null) ? "" : CliUtils.unescapeSQLString(argumentTree.getText());
    AbstractType<?> validator = getTypeByFunction(functionName);

    try {

        ByteBuffer value;

        if (functionArg.isEmpty()) {
            if (validator instanceof TimeUUIDType) {
                value = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes());
            } else if (validator instanceof LexicalUUIDType) {
                value = ByteBuffer.wrap(UUIDGen.decompose(UUID.randomUUID()));
            } else if (validator instanceof BytesType) {
                value = ByteBuffer.wrap(new byte[0]);
            } else {
                throw new RuntimeException(
                        String.format("Argument for '%s' could not be empty.", functionName));
            }
        } else {
            value = getBytesAccordingToType(functionArg, validator);
        }

        // performing ColumnDef local validator update
        if (withUpdate) {
            updateColumnMetaData(columnFamily, columnName, validator.toString());
        }

        return value;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}