Search in sources :

Example 11 with Query

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

the class GraphRequestHandlerTest method should_create_query_message_from_script_statement.

@Test
@UseDataProvider(location = DseTestDataProviders.class, value = "supportedGraphProtocols")
public void should_create_query_message_from_script_statement(GraphProtocol graphProtocol) throws IOException {
    // initialization
    GraphRequestHandlerTestHarness harness = GraphRequestHandlerTestHarness.builder().build();
    ScriptGraphStatement graphStatement = ScriptGraphStatement.newInstance("mockQuery").setQueryParam("p1", 1L).setQueryParam("p2", Uuids.random());
    GraphBinaryModule module = createGraphBinaryModule(harness.getContext());
    // when
    DriverExecutionProfile executionProfile = Conversions.resolveExecutionProfile(graphStatement, harness.getContext());
    Message m = GraphConversions.createMessageFromGraphStatement(graphStatement, graphProtocol, executionProfile, harness.getContext(), module);
    // checks
    assertThat(m).isInstanceOf(Query.class);
    Query q = ((Query) m);
    assertThat(q.query).isEqualTo("mockQuery");
    assertThat(q.options.positionalValues).containsExactly(serialize(graphStatement.getQueryParams(), graphProtocol, module));
    assertThat(q.options.namedValues).isEmpty();
}
Also used : Message(com.datastax.oss.protocol.internal.Message) Query(com.datastax.oss.protocol.internal.request.Query) RawBytesQuery(com.datastax.dse.protocol.internal.request.RawBytesQuery) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) ScriptGraphStatement(com.datastax.dse.driver.api.core.graph.ScriptGraphStatement) GraphTestUtils.createGraphBinaryModule(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule) GraphBinaryModule(com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 12 with Query

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

the class DseConversions method toContinuousPagingMessage.

public static Message toContinuousPagingMessage(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 = config.getInt(DseDriverOption.CONTINUOUS_PAGING_PAGE_SIZE);
    boolean pageSizeInBytes = config.getBoolean(DseDriverOption.CONTINUOUS_PAGING_PAGE_SIZE_BYTES);
    int maxPages = config.getInt(DseDriverOption.CONTINUOUS_PAGING_MAX_PAGES);
    int maxPagesPerSecond = config.getInt(DseDriverOption.CONTINUOUS_PAGING_MAX_PAGES_PER_SECOND);
    int maxEnqueuedPages = config.getInt(DseDriverOption.CONTINUOUS_PAGING_MAX_ENQUEUED_PAGES);
    ContinuousPagingOptions options = new ContinuousPagingOptions(maxPages, maxPagesPerSecond, maxEnqueuedPages);
    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();
    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);
        }
        DseQueryOptions queryOptions = new DseQueryOptions(consistencyCode, Conversions.encode(positionalValues, codecRegistry, protocolVersion), Conversions.encode(namedValues, codecRegistry, protocolVersion), false, pageSize, statement.getPagingState(), serialConsistencyCode, timestamp, (keyspace == null) ? null : keyspace.asInternal(), pageSizeInBytes, options);
        return new Query(simpleStatement.getQuery(), queryOptions);
    } else if (statement instanceof BoundStatement) {
        BoundStatement boundStatement = (BoundStatement) statement;
        if (!protocolVersionRegistry.supports(protocolVersion, DefaultProtocolFeature.UNSET_BOUND_VALUES)) {
            Conversions.ensureAllSet(boundStatement);
        }
        boolean skipMetadata = boundStatement.getPreparedStatement().getResultSetDefinitions().size() > 0;
        DseQueryOptions queryOptions = new DseQueryOptions(consistencyCode, boundStatement.getValues(), Collections.emptyMap(), skipMetadata, pageSize, statement.getPagingState(), serialConsistencyCode, timestamp, null, pageSizeInBytes, options);
        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 {
        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) ContinuousPagingOptions(com.datastax.dse.protocol.internal.request.query.ContinuousPagingOptions) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) ProtocolVersionRegistry(com.datastax.oss.driver.internal.core.ProtocolVersionRegistry) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ByteBuffer(java.nio.ByteBuffer) ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) ConsistencyLevelRegistry(com.datastax.oss.driver.internal.core.ConsistencyLevelRegistry) DseQueryOptions(com.datastax.dse.protocol.internal.request.query.DseQueryOptions) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 13 with Query

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

the class GraphConversions method createMessageFromGraphStatement.

static Message createMessageFromGraphStatement(GraphStatement<?> statement, GraphProtocol subProtocol, DriverExecutionProfile config, InternalDriverContext context, GraphBinaryModule graphBinaryModule) {
    final List<ByteBuffer> encodedQueryParams;
    if (!(statement instanceof ScriptGraphStatement) || ((ScriptGraphStatement) statement).getQueryParams().isEmpty()) {
        encodedQueryParams = Collections.emptyList();
    } else {
        try {
            Map<String, Object> queryParams = ((ScriptGraphStatement) statement).getQueryParams();
            if (subProtocol.isGraphBinary()) {
                Buffer graphBinaryParams = graphBinaryModule.serialize(queryParams);
                encodedQueryParams = Collections.singletonList(graphBinaryParams.nioBuffer());
                graphBinaryParams.release();
            } else {
                encodedQueryParams = Collections.singletonList(GraphSONUtils.serializeToByteBuffer(queryParams, subProtocol));
            }
        } catch (IOException e) {
            throw new UncheckedIOException("Couldn't serialize parameters for GraphStatement: " + statement, e);
        }
    }
    ConsistencyLevel consistency = statement.getConsistencyLevel();
    int consistencyLevel = (consistency == null) ? context.getConsistencyLevelRegistry().nameToCode(config.getString(DefaultDriverOption.REQUEST_CONSISTENCY)) : consistency.getProtocolCode();
    long timestamp = statement.getTimestamp();
    if (timestamp == Statement.NO_DEFAULT_TIMESTAMP) {
        timestamp = context.getTimestampGenerator().next();
    }
    DseQueryOptions queryOptions = new DseQueryOptions(consistencyLevel, encodedQueryParams, // ignored by the DSE Graph server
    Collections.emptyMap(), // also ignored
    true, // also ignored
    50, // also ignored
    null, // also ignored
    ProtocolConstants.ConsistencyLevel.LOCAL_SERIAL, timestamp, // also ignored
    null, // also ignored
    false, // also ignored
    null);
    if (statement instanceof ScriptGraphStatement) {
        return new Query(((ScriptGraphStatement) statement).getScript(), queryOptions);
    } else {
        return new RawBytesQuery(getQueryBytes(statement, subProtocol), queryOptions);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(org.apache.tinkerpop.gremlin.structure.io.Buffer) RawBytesQuery(com.datastax.dse.protocol.internal.request.RawBytesQuery) RawBytesQuery(com.datastax.dse.protocol.internal.request.RawBytesQuery) Query(com.datastax.oss.protocol.internal.request.Query) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ByteBuffer(java.nio.ByteBuffer) DefaultConsistencyLevel(com.datastax.oss.driver.api.core.DefaultConsistencyLevel) ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) ScriptGraphStatement(com.datastax.dse.driver.api.core.graph.ScriptGraphStatement) DseQueryOptions(com.datastax.dse.protocol.internal.request.query.DseQueryOptions)

Example 14 with Query

use of com.datastax.oss.protocol.internal.request.Query 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)

Example 15 with Query

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

the class AdminRequestHandler method query.

public static AdminRequestHandler<AdminResult> query(DriverChannel channel, String query, Map<String, Object> parameters, Duration timeout, int pageSize, String logPrefix) {
    Query message = new Query(query, buildQueryOptions(pageSize, serialize(parameters, channel.protocolVersion()), null));
    String debugString = "query '" + message.query + "'";
    if (!parameters.isEmpty()) {
        debugString += " with parameters " + parameters;
    }
    return new AdminRequestHandler<>(channel, true, message, Frame.NO_PAYLOAD, timeout, logPrefix, debugString, Rows.class);
}
Also used : Query(com.datastax.oss.protocol.internal.request.Query)

Aggregations

Query (com.datastax.oss.protocol.internal.request.Query)24 Test (org.junit.Test)17 Frame (com.datastax.oss.protocol.internal.Frame)7 Prepare (com.datastax.oss.protocol.internal.request.Prepare)6 RawBytesQuery (com.datastax.dse.protocol.internal.request.RawBytesQuery)5 ScriptGraphStatement (com.datastax.dse.driver.api.core.graph.ScriptGraphStatement)4 DseQueryOptions (com.datastax.dse.protocol.internal.request.query.DseQueryOptions)4 AdminResult (com.datastax.oss.driver.internal.core.adminrequest.AdminResult)4 Message (com.datastax.oss.protocol.internal.Message)4 Ready (com.datastax.oss.protocol.internal.response.Ready)4 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)4 ChannelFuture (io.netty.channel.ChannelFuture)4 InetSocketAddress (java.net.InetSocketAddress)4 ByteBuffer (java.nio.ByteBuffer)4 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)3 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)3 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)3 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)3 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)3 QueryOptions (com.datastax.oss.protocol.internal.request.query.QueryOptions)3