validation: add is_cql_key_invalid()

This commit is contained in:
Botond Dénes
2020-04-23 18:26:52 +03:00
parent 95bf3a75de
commit dd76e8c8de
2 changed files with 15 additions and 5 deletions

View File

@@ -49,24 +49,32 @@ namespace validation {
/**
* Based on org.apache.cassandra.thrift.ThriftValidation#validate_key()
*/
void
validate_cql_key(const schema& schema, partition_key_view key) {
std::optional<sstring> is_cql_key_invalid(const schema& schema, partition_key_view key) {
// C* validates here that the thrift key is not empty.
// It can only be empty if it is not composite and its only component in CQL form is empty.
if (schema.partition_key_size() == 1 && key.begin(schema)->empty()) {
throw exceptions::invalid_request_exception("Key may not be empty");
return sstring("Key may not be empty");
}
// check that key can be handled by FBUtilities.writeShortByteArray
auto b = key.representation();
if (b.size() > max_key_size) {
throw exceptions::invalid_request_exception(format("Key length of {:d} is longer than maximum of {:d}", b.size(), max_key_size));
return format("Key length of {:d} is longer than maximum of {:d}", b.size(), max_key_size);
}
try {
key.validate(schema);
} catch (const marshal_exception& e) {
throw exceptions::invalid_request_exception(e.what());
return sstring(e.what());
}
return std::nullopt;
}
void
validate_cql_key(const schema& schema, partition_key_view key) {
if (const auto err = is_cql_key_invalid(schema, key); err) {
throw exceptions::invalid_request_exception(std::move(*err));
}
}

View File

@@ -51,6 +51,8 @@ namespace validation {
constexpr size_t max_key_size = std::numeric_limits<uint16_t>::max();
// Returns an error string if key is invalid, a disengaged optional otherwise.
std::optional<sstring> is_cql_key_invalid(const schema& schema, partition_key_view key);
void validate_cql_key(const schema& schema, partition_key_view key);
schema_ptr validate_column_family(database& db, const sstring& keyspace_name, const sstring& cf_name);
schema_ptr validate_column_family(const sstring& keyspace_name, const sstring& cf_name);