Files
scylladb/index/index_option_utils.cc
Dawid Pawlik a396129e5c index: extract option validation helpers
Move `validate_enumerated_option`, `validate_positive_option`,
and `validate_factor_option` into shared index option utilities
under the `secondary_index::util` namespace. These functions were
previously defined as file-local statics in `vector_index.cc` with
hardcoded index names in error messages.

The shared versions take `index_type_name` as a parameter, allowing
each `custom_index` subclass to pass its own name via the virtual
`index_type_name()` method at the call site. The options maps use
`std::bind_front` to bind config params (supported values, limits),
leaving `index_name` as the first unbound argument passed by
`check_index_options()`.

Add `index_type_name()` as a pure virtual method on `custom_index`.
Move the shared utility implementations into `index_option_utils.cc`
and update `vector_index.cc` to use them.
2026-05-08 11:28:39 +02:00

71 lines
2.8 KiB
C++

/*
* Copyright 2026-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.1
*/
#include "index/index_option_utils.hh"
#include "exceptions/exceptions.hh"
#include <boost/algorithm/string.hpp>
#include <fmt/ranges.h>
#include <seastar/core/format.hh>
namespace secondary_index::util {
void validate_enumerated_option(
const std::vector<sstring>& supported_values, std::string_view index_type_name, const sstring& value_name, const sstring& value) {
bool is_valid = std::any_of(supported_values.begin(), supported_values.end(), [&](const std::string& v) {
return boost::iequals(value, v);
});
if (!is_valid) {
throw exceptions::invalid_request_exception(seastar::format("Invalid value in option '{}' for {} index: '{}'."
" Supported are case-insensitive: {}",
value_name, index_type_name, value, fmt::join(supported_values, ", ")));
}
}
void validate_positive_option(int max, std::string_view index_type_name, const sstring& value_name, const sstring& value) {
int num_value;
size_t len;
try {
num_value = std::stoi(value, &len);
} catch (...) {
throw exceptions::invalid_request_exception(
seastar::format("Invalid value in option '{}' for {} index: '{}' is not an integer", value_name, index_type_name, value));
}
if (len != value.size()) {
throw exceptions::invalid_request_exception(
seastar::format("Invalid value in option '{}' for {} index: '{}' is not an integer", value_name, index_type_name, value));
}
if (num_value <= 0 || num_value > max) {
throw exceptions::invalid_request_exception(
seastar::format("Invalid value in option '{}' for {} index: '{}' is out of valid range [1 - {}]", value_name, index_type_name, value, max));
}
}
void validate_factor_option(float min, float max, std::string_view index_type_name, const sstring& value_name, const sstring& value) {
float num_value;
size_t len;
try {
num_value = std::stof(value, &len);
} catch (...) {
throw exceptions::invalid_request_exception(
seastar::format("Invalid value in option '{}' for {} index: '{}' is not a float", value_name, index_type_name, value));
}
if (len != value.size()) {
throw exceptions::invalid_request_exception(
seastar::format("Invalid value in option '{}' for {} index: '{}' is not a float", value_name, index_type_name, value));
}
if (!(num_value >= min && num_value <= max)) {
throw exceptions::invalid_request_exception(seastar::format(
"Invalid value in option '{}' for {} index: '{}' is out of valid range [{} - {}]", value_name, index_type_name, value, min, max));
}
}
} // namespace secondary_index::util