Search in sources :

Example 1 with RandomToken

use of com.datastax.oss.driver.internal.core.metadata.token.RandomToken in project java-driver by datastax.

the class ValuesHelper method encodeValues.

public static ByteBuffer[] encodeValues(Object[] values, List<DataType> fieldTypes, CodecRegistry codecRegistry, ProtocolVersion protocolVersion) {
    Preconditions.checkArgument(values.length <= fieldTypes.size(), "Too many values (expected %s, got %s)", fieldTypes.size(), values.length);
    ByteBuffer[] encodedValues = new ByteBuffer[fieldTypes.size()];
    for (int i = 0; i < values.length; i++) {
        Object value = values[i];
        ByteBuffer encodedValue;
        if (value instanceof Token) {
            if (value instanceof Murmur3Token) {
                encodedValue = TypeCodecs.BIGINT.encode(((Murmur3Token) value).getValue(), protocolVersion);
            } else if (value instanceof ByteOrderedToken) {
                encodedValue = TypeCodecs.BLOB.encode(((ByteOrderedToken) value).getValue(), protocolVersion);
            } else if (value instanceof RandomToken) {
                encodedValue = TypeCodecs.VARINT.encode(((RandomToken) value).getValue(), protocolVersion);
            } else {
                throw new IllegalArgumentException("Unsupported token type " + value.getClass());
            }
        } else {
            TypeCodec<Object> codec = (value == null) ? codecRegistry.codecFor(fieldTypes.get(i)) : codecRegistry.codecFor(fieldTypes.get(i), value);
            encodedValue = codec.encode(value, protocolVersion);
        }
        encodedValues[i] = encodedValue;
    }
    return encodedValues;
}
Also used : Murmur3Token(com.datastax.oss.driver.internal.core.metadata.token.Murmur3Token) Token(com.datastax.oss.driver.api.core.metadata.token.Token) ByteOrderedToken(com.datastax.oss.driver.internal.core.metadata.token.ByteOrderedToken) Murmur3Token(com.datastax.oss.driver.internal.core.metadata.token.Murmur3Token) RandomToken(com.datastax.oss.driver.internal.core.metadata.token.RandomToken) ByteOrderedToken(com.datastax.oss.driver.internal.core.metadata.token.ByteOrderedToken) RandomToken(com.datastax.oss.driver.internal.core.metadata.token.RandomToken) ByteBuffer(java.nio.ByteBuffer)

Example 2 with RandomToken

use of com.datastax.oss.driver.internal.core.metadata.token.RandomToken in project java-driver by datastax.

the class ValuesHelper method encodePreparedValues.

public static ByteBuffer[] encodePreparedValues(Object[] values, ColumnDefinitions variableDefinitions, CodecRegistry codecRegistry, ProtocolVersion protocolVersion) {
    // Almost same as encodeValues, but we can't reuse because of variableDefinitions. Rebuilding a
    // list of datatypes is not worth it, so duplicate the code.
    Preconditions.checkArgument(values.length <= variableDefinitions.size(), "Too many variables (expected %s, got %s)", variableDefinitions.size(), values.length);
    ByteBuffer[] encodedValues = new ByteBuffer[variableDefinitions.size()];
    int i;
    for (i = 0; i < values.length; i++) {
        Object value = values[i];
        ByteBuffer encodedValue;
        if (value instanceof Token) {
            if (value instanceof Murmur3Token) {
                encodedValue = TypeCodecs.BIGINT.encode(((Murmur3Token) value).getValue(), protocolVersion);
            } else if (value instanceof ByteOrderedToken) {
                encodedValue = TypeCodecs.BLOB.encode(((ByteOrderedToken) value).getValue(), protocolVersion);
            } else if (value instanceof RandomToken) {
                encodedValue = TypeCodecs.VARINT.encode(((RandomToken) value).getValue(), protocolVersion);
            } else {
                throw new IllegalArgumentException("Unsupported token type " + value.getClass());
            }
        } else {
            TypeCodec<Object> codec = (value == null) ? codecRegistry.codecFor(variableDefinitions.get(i).getType()) : codecRegistry.codecFor(variableDefinitions.get(i).getType(), value);
            encodedValue = codec.encode(value, protocolVersion);
        }
        encodedValues[i] = encodedValue;
    }
    for (; i < encodedValues.length; i++) {
        encodedValues[i] = ProtocolConstants.UNSET_VALUE;
    }
    return encodedValues;
}
Also used : Murmur3Token(com.datastax.oss.driver.internal.core.metadata.token.Murmur3Token) Token(com.datastax.oss.driver.api.core.metadata.token.Token) ByteOrderedToken(com.datastax.oss.driver.internal.core.metadata.token.ByteOrderedToken) Murmur3Token(com.datastax.oss.driver.internal.core.metadata.token.Murmur3Token) RandomToken(com.datastax.oss.driver.internal.core.metadata.token.RandomToken) ByteOrderedToken(com.datastax.oss.driver.internal.core.metadata.token.ByteOrderedToken) RandomToken(com.datastax.oss.driver.internal.core.metadata.token.RandomToken) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Token (com.datastax.oss.driver.api.core.metadata.token.Token)2 ByteOrderedToken (com.datastax.oss.driver.internal.core.metadata.token.ByteOrderedToken)2 Murmur3Token (com.datastax.oss.driver.internal.core.metadata.token.Murmur3Token)2 RandomToken (com.datastax.oss.driver.internal.core.metadata.token.RandomToken)2 ByteBuffer (java.nio.ByteBuffer)2