use of com.datastax.oss.driver.api.core.ConsistencyLevel 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);
}
}
use of com.datastax.oss.driver.api.core.ConsistencyLevel 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());
}
}
use of com.datastax.oss.driver.api.core.ConsistencyLevel in project java-driver by datastax.
the class BoundStatementCcmIT method should_propagate_attributes_when_preparing_a_simple_statement.
@Test
public void should_propagate_attributes_when_preparing_a_simple_statement() {
CqlSession session = sessionRule.session();
DriverExecutionProfile mockProfile = session.getContext().getConfig().getDefaultProfile().withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(10));
ByteBuffer mockPagingState = Bytes.fromHexString("0xaaaa");
CqlIdentifier mockKeyspace = supportsPerRequestKeyspace(session) ? CqlIdentifier.fromCql("system") : null;
CqlIdentifier mockRoutingKeyspace = CqlIdentifier.fromCql("mockRoutingKeyspace");
ByteBuffer mockRoutingKey = Bytes.fromHexString("0xbbbb");
Token mockRoutingToken = session.getMetadata().getTokenMap().get().newToken(mockRoutingKey);
Map<String, ByteBuffer> mockCustomPayload = NullAllowingImmutableMap.of("key1", Bytes.fromHexString("0xcccc"));
Duration mockTimeout = Duration.ofSeconds(1);
ConsistencyLevel mockCl = DefaultConsistencyLevel.LOCAL_QUORUM;
ConsistencyLevel mockSerialCl = DefaultConsistencyLevel.LOCAL_SERIAL;
int mockPageSize = 2000;
SimpleStatementBuilder simpleStatementBuilder = SimpleStatement.builder("SELECT release_version FROM system.local").setExecutionProfile(mockProfile).setPagingState(mockPagingState).setKeyspace(mockKeyspace).setRoutingKeyspace(mockRoutingKeyspace).setRoutingKey(mockRoutingKey).setRoutingToken(mockRoutingToken).setQueryTimestamp(42).setIdempotence(true).setTracing().setTimeout(mockTimeout).setConsistencyLevel(mockCl).setSerialConsistencyLevel(mockSerialCl).setPageSize(mockPageSize);
if (atLeastV4) {
simpleStatementBuilder = simpleStatementBuilder.addCustomPayload("key1", mockCustomPayload.get("key1"));
}
PreparedStatement preparedStatement = session.prepare(simpleStatementBuilder.build());
// Cover all the ways to create bound statements:
ImmutableList<Function<PreparedStatement, BoundStatement>> createMethods = ImmutableList.of(PreparedStatement::bind, p -> p.boundStatementBuilder().build());
for (Function<PreparedStatement, BoundStatement> createMethod : createMethods) {
BoundStatement boundStatement = createMethod.apply(preparedStatement);
assertThat(boundStatement.getExecutionProfile()).isEqualTo(mockProfile);
assertThat(boundStatement.getPagingState()).isEqualTo(mockPagingState);
assertThat(boundStatement.getRoutingKeyspace()).isEqualTo(mockKeyspace != null ? mockKeyspace : mockRoutingKeyspace);
assertThat(boundStatement.getRoutingKey()).isEqualTo(mockRoutingKey);
assertThat(boundStatement.getRoutingToken()).isEqualTo(mockRoutingToken);
if (atLeastV4) {
assertThat(boundStatement.getCustomPayload()).isEqualTo(mockCustomPayload);
}
assertThat(boundStatement.isIdempotent()).isTrue();
assertThat(boundStatement.isTracing()).isTrue();
assertThat(boundStatement.getTimeout()).isEqualTo(mockTimeout);
assertThat(boundStatement.getConsistencyLevel()).isEqualTo(mockCl);
assertThat(boundStatement.getSerialConsistencyLevel()).isEqualTo(mockSerialCl);
assertThat(boundStatement.getPageSize()).isEqualTo(mockPageSize);
// Bound statements do not support per-query keyspaces, so this is not set
assertThat(boundStatement.getKeyspace()).isNull();
// Should not be propagated
assertThat(boundStatement.getQueryTimestamp()).isEqualTo(Statement.NO_DEFAULT_TIMESTAMP);
}
}
Aggregations