From f082cc82731fd21092fc2e72cf873cb151acd6f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Botond=20D=C3=A9nes?= Date: Fri, 3 Nov 2023 10:34:44 -0400 Subject: [PATCH] tools/scylla-nodetool: extract keyspace/table parsing Having to extract 1 keyspace and N tables from the command-line is proving to be a common pattern among commands. Extract this into a method, so the boiler-plate can be shared. Add a forward-looking overload as well, which will be used in the next patch. --- tools/scylla-nodetool.cc | 52 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/tools/scylla-nodetool.cc b/tools/scylla-nodetool.cc index 1701f91ada..8d456dd674 100644 --- a/tools/scylla-nodetool.cc +++ b/tools/scylla-nodetool.cc @@ -108,23 +108,40 @@ std::vector get_keyspaces(scylla_rest_client& client, std::optional tables; +}; + +keyspace_and_tables parse_keyspace_and_tables(scylla_rest_client& client, const bpo::variables_map& vm, const char* common_keyspace_table_arg_name) { + keyspace_and_tables ret; + + const auto args = vm[common_keyspace_table_arg_name].as>(); + + ret.keyspace = args.at(0); + + const auto all_keyspaces = get_keyspaces(client); + if (std::ranges::find(all_keyspaces, ret.keyspace) == all_keyspaces.end()) { + throw std::invalid_argument(fmt::format("keyspace {} does not exist", ret.keyspace)); + } + + if (args.size() > 1) { + ret.tables.insert(ret.tables.end(), args.begin() + 1, args.end()); + } + + return ret; +} + using operation_func = void(*)(scylla_rest_client&, const bpo::variables_map&); std::map get_operations_with_func(); void cleanup_operation(scylla_rest_client& client, const bpo::variables_map& vm) { if (vm.count("cleanup_arg")) { - const auto all_keyspaces = get_keyspaces(client); - - auto args = vm["cleanup_arg"].as>(); + const auto [keyspace, tables] = parse_keyspace_and_tables(client, vm, "cleanup_arg"); std::unordered_map params; - const auto keyspace = args[0]; - if (std::ranges::find(all_keyspaces, keyspace) == all_keyspaces.end()) { - throw std::invalid_argument(fmt::format("keyspace {} does not exist", keyspace)); - } - - if (args.size() > 1) { - params["cf"] = fmt::to_string(fmt::join(args.begin() + 1, args.end(), ",")); + if (!tables.empty()) { + params["cf"] = fmt::to_string(fmt::join(tables.begin(), tables.end(), ",")); } client.post(format("/storage_service/keyspace_cleanup/{}", keyspace), std::move(params)); } else { @@ -164,22 +181,15 @@ void compact_operation(scylla_rest_client& client, const bpo::variables_map& vm) throw std::invalid_argument("--user-defined flag is unsupported"); } - const auto all_keyspaces = get_keyspaces(client); - if (vm.count("compaction_arg")) { - auto args = vm["compaction_arg"].as>(); + const auto [keyspace, tables] = parse_keyspace_and_tables(client, vm, "compaction_arg"); std::unordered_map params; - const auto keyspace = args[0]; - if (std::ranges::find(all_keyspaces, keyspace) == all_keyspaces.end()) { - throw std::invalid_argument(fmt::format("keyspace {} does not exist", keyspace)); - } - - if (args.size() > 1) { - params["cf"] = fmt::to_string(fmt::join(args.begin() + 1, args.end(), ",")); + if (!tables.empty()) { + params["cf"] = fmt::to_string(fmt::join(tables.begin(), tables.end(), ",")); } client.post(format("/storage_service/keyspace_compaction/{}", keyspace), std::move(params)); } else { - for (const auto& keyspace : all_keyspaces) { + for (const auto& keyspace : get_keyspaces(client)) { client.post(format("/storage_service/keyspace_compaction/{}", keyspace)); } }