Search in sources :

Example 1 with QueryOptions

use of com.datastax.oss.protocol.internal.request.query.QueryOptions in project java-driver by datastax.

the class AdminRequestHandler method copy.

private AdminRequestHandler<ResultT> copy(ByteBuffer pagingState) {
    assert message instanceof Query;
    Query current = (Query) this.message;
    QueryOptions currentOptions = current.options;
    QueryOptions newOptions = buildQueryOptions(currentOptions.pageSize, currentOptions.namedValues, pagingState);
    return new AdminRequestHandler<>(channel, // This is called for next page queries, so we always need to reacquire an id:
    true, new Query(current.query, newOptions), customPayload, timeout, logPrefix, debugString, expectedResponseType);
}
Also used : Query(com.datastax.oss.protocol.internal.request.Query) QueryOptions(com.datastax.oss.protocol.internal.request.query.QueryOptions)

Example 2 with QueryOptions

use of com.datastax.oss.protocol.internal.request.query.QueryOptions in project java-driver by datastax.

the class InsightsClient method sendJsonMessage.

private CompletionStage<Void> sendJsonMessage(String jsonMessage) {
    QueryOptions queryOptions = createQueryOptionsWithJson(jsonMessage);
    String logPrefix = driverContext.getSessionName();
    Duration timeout = driverContext.getConfig().getDefaultProfile().getDuration(DefaultDriverOption.CONTROL_CONNECTION_TIMEOUT);
    LOGGER.debug("sending JSON message: {}", jsonMessage);
    Query query = new Query(REPORT_INSIGHT_RPC, queryOptions);
    return AdminRequestHandler.call(controlConnection.channel(), query, timeout, logPrefix).start();
}
Also used : Query(com.datastax.oss.protocol.internal.request.Query) Duration(java.time.Duration) QueryOptions(com.datastax.oss.protocol.internal.request.query.QueryOptions)

Example 3 with QueryOptions

use of com.datastax.oss.protocol.internal.request.query.QueryOptions in project java-driver by datastax.

the class InsightsClient method createQueryOptionsWithJson.

private QueryOptions createQueryOptionsWithJson(String json) {
    TypeCodec<String> codec = driverContext.getCodecRegistry().codecFor(DataTypes.TEXT, String.class);
    ByteBuffer startupMessageSerialized = codec.encode(json, DseProtocolVersion.DSE_V2);
    return new QueryOptions(QueryOptions.DEFAULT.consistency, Collections.singletonList(startupMessageSerialized), QueryOptions.DEFAULT.namedValues, QueryOptions.DEFAULT.skipMetadata, QueryOptions.DEFAULT.pageSize, QueryOptions.DEFAULT.pagingState, QueryOptions.DEFAULT.serialConsistency, QueryOptions.DEFAULT.defaultTimestamp, QueryOptions.DEFAULT.keyspace, QueryOptions.DEFAULT.nowInSeconds);
}
Also used : ByteBuffer(java.nio.ByteBuffer) QueryOptions(com.datastax.oss.protocol.internal.request.query.QueryOptions)

Example 4 with QueryOptions

use of com.datastax.oss.protocol.internal.request.query.QueryOptions in project java-driver by datastax.

the class Conversions method toMessage.

public static Message toMessage(Statement<?> statement, DriverExecutionProfile config, InternalDriverContext context) {
    ConsistencyLevelRegistry consistencyLevelRegistry = context.getConsistencyLevelRegistry();
    ConsistencyLevel consistency = statement.getConsistencyLevel();
    int consistencyCode = (consistency == null) ? consistencyLevelRegistry.nameToCode(config.getString(DefaultDriverOption.REQUEST_CONSISTENCY)) : consistency.getProtocolCode();
    int pageSize = statement.getPageSize();
    if (pageSize <= 0) {
        pageSize = config.getInt(DefaultDriverOption.REQUEST_PAGE_SIZE);
    }
    ConsistencyLevel serialConsistency = statement.getSerialConsistencyLevel();
    int serialConsistencyCode = (serialConsistency == null) ? consistencyLevelRegistry.nameToCode(config.getString(DefaultDriverOption.REQUEST_SERIAL_CONSISTENCY)) : serialConsistency.getProtocolCode();
    long timestamp = statement.getQueryTimestamp();
    if (timestamp == Statement.NO_DEFAULT_TIMESTAMP) {
        timestamp = context.getTimestampGenerator().next();
    }
    CodecRegistry codecRegistry = context.getCodecRegistry();
    ProtocolVersion protocolVersion = context.getProtocolVersion();
    ProtocolVersionRegistry protocolVersionRegistry = context.getProtocolVersionRegistry();
    CqlIdentifier keyspace = statement.getKeyspace();
    int nowInSeconds = statement.getNowInSeconds();
    if (nowInSeconds != Statement.NO_NOW_IN_SECONDS && !protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.NOW_IN_SECONDS)) {
        throw new IllegalArgumentException("Can't use nowInSeconds with protocol " + protocolVersion);
    }
    if (statement instanceof SimpleStatement) {
        SimpleStatement simpleStatement = (SimpleStatement) statement;
        List<Object> positionalValues = simpleStatement.getPositionalValues();
        Map<CqlIdentifier, Object> namedValues = simpleStatement.getNamedValues();
        if (!positionalValues.isEmpty() && !namedValues.isEmpty()) {
            throw new IllegalArgumentException("Can't have both positional and named values in a statement.");
        }
        if (keyspace != null && !protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.PER_REQUEST_KEYSPACE)) {
            throw new IllegalArgumentException("Can't use per-request keyspace with protocol " + protocolVersion);
        }
        QueryOptions queryOptions = new QueryOptions(consistencyCode, encode(positionalValues, codecRegistry, protocolVersion), encode(namedValues, codecRegistry, protocolVersion), false, pageSize, statement.getPagingState(), serialConsistencyCode, timestamp, (keyspace == null) ? null : keyspace.asInternal(), nowInSeconds);
        return new Query(simpleStatement.getQuery(), queryOptions);
    } else if (statement instanceof BoundStatement) {
        BoundStatement boundStatement = (BoundStatement) statement;
        if (!protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.UNSET_BOUND_VALUES)) {
            ensureAllSet(boundStatement);
        }
        boolean skipMetadata = boundStatement.getPreparedStatement().getResultSetDefinitions().size() > 0;
        QueryOptions queryOptions = new QueryOptions(consistencyCode, boundStatement.getValues(), Collections.emptyMap(), skipMetadata, pageSize, statement.getPagingState(), serialConsistencyCode, timestamp, null, nowInSeconds);
        PreparedStatement preparedStatement = boundStatement.getPreparedStatement();
        ByteBuffer id = preparedStatement.getId();
        ByteBuffer resultMetadataId = preparedStatement.getResultMetadataId();
        return new Execute(Bytes.getArray(id), (resultMetadataId == null) ? null : Bytes.getArray(resultMetadataId), queryOptions);
    } else if (statement instanceof BatchStatement) {
        BatchStatement batchStatement = (BatchStatement) statement;
        if (!protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.UNSET_BOUND_VALUES)) {
            ensureAllSet(batchStatement);
        }
        if (keyspace != null && !protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.PER_REQUEST_KEYSPACE)) {
            throw new IllegalArgumentException("Can't use per-request keyspace with protocol " + protocolVersion);
        }
        List<Object> queriesOrIds = new ArrayList<>(batchStatement.size());
        List<List<ByteBuffer>> values = new ArrayList<>(batchStatement.size());
        for (BatchableStatement<?> child : batchStatement) {
            if (child instanceof SimpleStatement) {
                SimpleStatement simpleStatement = (SimpleStatement) child;
                if (simpleStatement.getNamedValues().size() > 0) {
                    throw new IllegalArgumentException(String.format("Batch statements cannot contain simple statements with named values " + "(offending statement: %s)", simpleStatement.getQuery()));
                }
                queriesOrIds.add(simpleStatement.getQuery());
                values.add(encode(simpleStatement.getPositionalValues(), codecRegistry, protocolVersion));
            } else if (child instanceof BoundStatement) {
                BoundStatement boundStatement = (BoundStatement) child;
                queriesOrIds.add(Bytes.getArray(boundStatement.getPreparedStatement().getId()));
                values.add(boundStatement.getValues());
            } else {
                throw new IllegalArgumentException("Unsupported child statement: " + child.getClass().getName());
            }
        }
        return new Batch(batchStatement.getBatchType().getProtocolCode(), queriesOrIds, values, consistencyCode, serialConsistencyCode, timestamp, (keyspace == null) ? null : keyspace.asInternal(), nowInSeconds);
    } else {
        throw new IllegalArgumentException("Unsupported statement type: " + statement.getClass().getName());
    }
}
Also used : Query(com.datastax.oss.protocol.internal.request.Query) Execute(com.datastax.oss.protocol.internal.request.Execute) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ArrayList(java.util.ArrayList) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) ProtocolVersionRegistry(com.datastax.oss.driver.internal.core.ProtocolVersionRegistry) QueryOptions(com.datastax.oss.protocol.internal.request.query.QueryOptions) ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) Batch(com.datastax.oss.protocol.internal.request.Batch) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) NullAllowingImmutableList(com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableList) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ByteBuffer(java.nio.ByteBuffer) BatchStatement(com.datastax.oss.driver.api.core.cql.BatchStatement) ConsistencyLevelRegistry(com.datastax.oss.driver.internal.core.ConsistencyLevelRegistry) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Aggregations

QueryOptions (com.datastax.oss.protocol.internal.request.query.QueryOptions)4 Query (com.datastax.oss.protocol.internal.request.Query)3 ByteBuffer (java.nio.ByteBuffer)2 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)1 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)1 BatchStatement (com.datastax.oss.driver.api.core.cql.BatchStatement)1 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)1 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)1 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)1 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)1 ConsistencyLevelRegistry (com.datastax.oss.driver.internal.core.ConsistencyLevelRegistry)1 ProtocolVersionRegistry (com.datastax.oss.driver.internal.core.ProtocolVersionRegistry)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1 Batch (com.datastax.oss.protocol.internal.request.Batch)1 Execute (com.datastax.oss.protocol.internal.request.Execute)1 NullAllowingImmutableList (com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableList)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1