Search in sources :

Example 1 with Query

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

the class GraphConversions method createContinuousMessageFromGraphStatement.

public static Message createContinuousMessageFromGraphStatement(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);
        }
    }
    int consistencyLevel = DefaultConsistencyLevel.valueOf(config.getString(DefaultDriverOption.REQUEST_CONSISTENCY)).getProtocolCode();
    long timestamp = statement.getTimestamp();
    if (timestamp == Statement.NO_DEFAULT_TIMESTAMP) {
        timestamp = context.getTimestampGenerator().next();
    }
    int pageSize = config.getInt(DseDriverOption.GRAPH_CONTINUOUS_PAGING_PAGE_SIZE);
    int maxPages = config.getInt(DseDriverOption.GRAPH_CONTINUOUS_PAGING_MAX_PAGES);
    int maxPagesPerSecond = config.getInt(DseDriverOption.GRAPH_CONTINUOUS_PAGING_MAX_PAGES_PER_SECOND);
    int maxEnqueuedPages = config.getInt(DseDriverOption.GRAPH_CONTINUOUS_PAGING_MAX_ENQUEUED_PAGES);
    ContinuousPagingOptions options = new ContinuousPagingOptions(maxPages, maxPagesPerSecond, maxEnqueuedPages);
    DseQueryOptions queryOptions = new DseQueryOptions(consistencyLevel, encodedQueryParams, // ignored by the DSE Graph server
    Collections.emptyMap(), // also ignored
    true, pageSize, null, // also ignored
    ProtocolConstants.ConsistencyLevel.LOCAL_SERIAL, timestamp, // also ignored
    null, // graph CP does not support sizeInBytes
    false, options);
    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) ContinuousPagingOptions(com.datastax.dse.protocol.internal.request.query.ContinuousPagingOptions) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ByteBuffer(java.nio.ByteBuffer) ScriptGraphStatement(com.datastax.dse.driver.api.core.graph.ScriptGraphStatement) DseQueryOptions(com.datastax.dse.protocol.internal.request.query.DseQueryOptions)

Example 2 with Query

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

the class ProtocolInitHandlerTest method should_initialize_with_keyspace.

@Test
public void should_initialize_with_keyspace() {
    DriverChannelOptions options = DriverChannelOptions.builder().withKeyspace(CqlIdentifier.fromCql("ks")).build();
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, options, heartbeatHandler, false));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    writeInboundFrame(readOutboundFrame(), new Ready());
    writeInboundFrame(readOutboundFrame(), TestResponses.clusterNameResponse("someClusterName"));
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Query.class);
    assertThat(((Query) requestFrame.message).query).isEqualTo("USE \"ks\"");
    writeInboundFrame(requestFrame, new SetKeyspace("ks"));
    assertThat(connectFuture).isSuccess();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame) Query(com.datastax.oss.protocol.internal.request.Query) InetSocketAddress(java.net.InetSocketAddress) SetKeyspace(com.datastax.oss.protocol.internal.response.result.SetKeyspace) Test(org.junit.Test)

Example 3 with Query

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

the class DriverChannelTest method should_wait_for_coalesced_writes_when_closing_gracefully.

/**
 * Ensures that the potential delay introduced by the write coalescer does not mess with the
 * graceful shutdown sequence: any write submitted before {@link DriverChannel#close()} is
 * guaranteed to complete.
 */
@Test
public void should_wait_for_coalesced_writes_when_closing_gracefully() {
    // Given
    MockResponseCallback responseCallback = new MockResponseCallback();
    driverChannel.write(new Query("test"), false, Frame.NO_PAYLOAD, responseCallback);
    // nothing written yet because the coalescer hasn't flushed
    assertNoOutboundFrame();
    // When
    Future<java.lang.Void> closeFuture = driverChannel.close();
    // Then
    // not closed yet because there is still a pending write
    assertThat(closeFuture).isNotDone();
    assertNoOutboundFrame();
    // When
    // the coalescer finally runs
    writeCoalescer.triggerFlush();
    // Then
    // the pending write goes through
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame).isNotNull();
    // not closed yet because there is now a pending response
    assertThat(closeFuture).isNotDone();
    // When
    // the pending response arrives
    writeInboundFrame(requestFrame, Void.INSTANCE);
    assertThat(responseCallback.getLastResponse().message).isEqualTo(Void.INSTANCE);
    // Then
    assertThat(closeFuture).isSuccess();
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Query(com.datastax.oss.protocol.internal.request.Query) Void(com.datastax.oss.protocol.internal.response.result.Void) Test(org.junit.Test)

Example 4 with Query

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

the class ProtocolInitHandlerTest method should_fail_to_initialize_if_keyspace_is_invalid.

@Test
public void should_fail_to_initialize_if_keyspace_is_invalid() {
    DriverChannelOptions driverChannelOptions = DriverChannelOptions.builder().withKeyspace(CqlIdentifier.fromCql("ks")).build();
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, driverChannelOptions, heartbeatHandler, false));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    writeInboundFrame(readOutboundFrame(), new Ready());
    writeInboundFrame(readOutboundFrame(), TestResponses.clusterNameResponse("someClusterName"));
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Query.class);
    assertThat(((Query) requestFrame.message).query).isEqualTo("USE \"ks\"");
    writeInboundFrame(requestFrame, new Error(ProtocolConstants.ErrorCode.INVALID, "invalid keyspace"));
    assertThat(connectFuture).isFailed(error -> assertThat(error).isInstanceOf(InvalidKeyspaceException.class).hasMessage("invalid keyspace"));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame) Query(com.datastax.oss.protocol.internal.request.Query) InetSocketAddress(java.net.InetSocketAddress) Error(com.datastax.oss.protocol.internal.response.Error) Test(org.junit.Test)

Example 5 with Query

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

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