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);
}
}
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();
}
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();
}
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"));
}
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);
}
Aggregations